-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
94 lines (80 loc) · 2.52 KB
/
client_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"sort"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestClientGetDevicesDefaultNameField(t *testing.T) {
assert := assert.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
test_payload := `
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"name": "test-device-1",
"custom_fields": {}
},
{
"name": "test-device-1",
"custom_fields": {}
},
{
"name": "test-device-2",
"custom_fields": {}
}
]
}
`
mock_response := httpmock.NewStringResponder(200, test_payload)
httpmock.RegisterResponder("GET", "http://localhost/api/dcim/devices/", mock_response)
devices := queryDevices()
// since hash type is used during query, no guarentee it will be ordered
sort.Slice(devices, func(i, j int) bool { return devices[i] < devices[j] })
assert.Equal(2, len(devices), "2 devices are returned after de-dup")
assert.Equal("test-device-1", devices[0], "test-device-1 in return value")
assert.Equal("test-device-2", devices[1], "test-device-1 in return value")
}
func TestClientGetDevicesCustomNameField(t *testing.T) {
assert := assert.New(t)
*namefield = "fqdn"
httpmock.Activate()
defer httpmock.DeactivateAndReset()
test_payload := `
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"name": "test-device-node0",
"custom_fields": {
"fqdn": "test-device"
}
},
{
"name": "test-device-node1",
"custom_fields": {
"fqdn": "test-device"
}
},
{
"name": "test-device-2",
"custom_fields": {}
}
]
}
`
mock_response := httpmock.NewStringResponder(200, test_payload)
httpmock.RegisterResponder("GET", "http://localhost/api/dcim/devices/", mock_response)
devices := queryDevices()
// since hash type is used during query, no guarentee it will be ordered
sort.Slice(devices, func(i, j int) bool { return devices[i] < devices[j] })
assert.Equal(2, len(devices), "2 devices are returned after de-dup")
assert.Equal("test-device", devices[0], "correctly returned device using fqdn only once")
assert.Equal("test-device-2", devices[1], "test-device-2 in return value")
}