forked from thecodeteam/libstorage
/
tests_std.go
146 lines (111 loc) · 3.56 KB
/
tests_std.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package tests
import (
"encoding/json"
"testing"
gofig "github.com/akutz/gofig/types"
"github.com/stretchr/testify/assert"
"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/types"
"github.com/codedellemc/libstorage/api/utils"
)
// TestRoot tests the GET / route.
var TestRoot = func(config gofig.Config, client types.Client, t *testing.T) {
reply, err := client.API().Root(nil)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(reply), 6)
}
// InstanceIDTest is the test harness for testing the instance ID.
type InstanceIDTest struct {
// Driver is the name of the driver/executor for which to get the instance
// ID.
Driver string
// Expected is the expected instance ID value.
Expected *types.InstanceID
}
// Test is the APITestFunc for the InstanceIDTest.
func (tt *InstanceIDTest) Test(
config gofig.Config,
client types.Client,
t *testing.T) {
expectedBuf, err := json.Marshal(tt.Expected)
assert.NoError(t, err)
expectedJSON := string(expectedBuf)
ctx := context.Background().WithValue(context.ServiceKey, tt.Driver)
iid, err := client.Executor().InstanceID(ctx, utils.NewStore())
assert.NoError(t, err)
iidBuf, err := json.Marshal(iid)
assert.NoError(t, err)
iidJSON := string(iidBuf)
assert.Equal(t, expectedJSON, iidJSON)
}
// InstanceTest is the test harness for testing instance inspection.
type InstanceTest struct {
// Driver is the name of the driver/executor for which to inspect the
// instance.
Driver string
// Expected is the expected instance.
Expected *types.Instance
}
// Test is the APITestFunc for the InstanceTest.
func (tt *InstanceTest) Test(
config gofig.Config,
client types.Client,
t *testing.T) {
expectedBuf, err := json.Marshal(tt.Expected)
assert.NoError(t, err)
expectedJSON := string(expectedBuf)
ctx := context.Background().WithValue(context.ServiceKey, tt.Driver)
iid, err := client.Storage().InstanceInspect(ctx, utils.NewStore())
assert.NoError(t, err)
iidBuf, err := json.Marshal(iid)
assert.NoError(t, err)
iidJSON := string(iidBuf)
assert.Equal(t, expectedJSON, iidJSON)
}
// NextDeviceTest is the test harness for testing getting the next device.
type NextDeviceTest struct {
// Driver is the name of the driver/executor for which to get the next
// device.
Driver string
// Expected is the expected next device.
Expected string
}
// Test is the APITestFunc for the NextDeviceTest.
func (tt *NextDeviceTest) Test(
config gofig.Config,
client types.Client,
t *testing.T) {
ctx := context.Background().WithValue(context.ServiceKey, tt.Driver)
val, err := client.Executor().NextDevice(ctx, utils.NewStore())
assert.NoError(t, err)
assert.Equal(t, tt.Expected, val)
}
// LocalDevicesTest is the test harness for testing getting the local devices.
type LocalDevicesTest struct {
// Driver is the name of the driver/executor for which to get the local
// devices.
Driver string
// Expected is the expected local devices.
Expected *types.LocalDevices
}
// Test is the APITestFunc for the LocalDevicesTest.
func (tt *LocalDevicesTest) Test(
config gofig.Config,
client types.Client,
t *testing.T) {
expectedBuf, err := tt.Expected.MarshalText()
assert.NoError(t, err)
expectedText := string(expectedBuf)
ctx := context.Background().WithValue(context.ServiceKey, tt.Driver)
val, err := client.Executor().LocalDevices(ctx, &types.LocalDevicesOpts{
ScanType: types.DeviceScanQuick,
Opts: utils.NewStore(),
})
assert.NoError(t, err)
buf, err := val.MarshalText()
assert.NoError(t, err)
actualText := string(buf)
assert.Equal(t, expectedText, actualText)
}