-
Notifications
You must be signed in to change notification settings - Fork 8
/
instance_test.go
72 lines (61 loc) · 1.61 KB
/
instance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package metadata
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
// mock client for testing purposes
type InstanceMockclient struct {
Resp *InstanceData
Err error
}
func (m *InstanceMockclient) GetInstance(ctx context.Context) (*InstanceData, error) {
if m.Err != nil {
return nil, m.Err
}
return m.Resp, nil
}
func TestGetInstance_Success(t *testing.T) {
// Create a mock client with a successful response
mockClient := &InstanceMockclient{
Resp: &InstanceData{
ID: 1,
Label: "test-instance",
Region: "us-west",
Type: "standard",
HostUUID: "abc123",
Tags: []string{"tag1", "tag2"},
Specs: InstanceSpecsData{
VCPUs: 2,
Memory: 4096,
GPUs: 0,
Transfer: 2000,
Disk: 50,
},
Backups: InstanceBackupsData{
Enabled: true,
Status: String("active"),
},
},
}
instance, err := mockClient.GetInstance(context.Background())
// Assert the result
assert.NoError(t, err, "Expected no error")
assert.NotNil(t, instance, "Expected non-nil instance")
assert.Equal(t, "test-instance", instance.Label, "Unexpected instance label")
}
func TestGetInstance_Error(t *testing.T) {
// Create a mock client with an error response
mockClient := &InstanceMockclient{
Err: errors.New("mock error"),
}
instance, err := mockClient.GetInstance(context.Background())
assert.Error(t, err, "Expected an error")
assert.Nil(t, instance, "Expected nil instance")
assert.EqualError(t, err, "mock error", "Unexpected error message")
}
// Helper function to create a string pointer
func String(s string) *string {
return &s
}