Skip to content

Commit d8f67f2

Browse files
author
ethanholen-hpe
committed
started adding tests for nautobot export
1 parent 8f96de8 commit d8f67f2

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
*
3+
* MIT License
4+
*
5+
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a
8+
* copy of this software and associated documentation files (the "Software"),
9+
* to deal in the Software without restriction, including without limitation
10+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
* and/or sell copies of the Software, and to permit persons to whom the
12+
* Software is furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included
15+
* in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*
25+
*/
26+
package export
27+
28+
import (
29+
"strings"
30+
"testing"
31+
)
32+
33+
func TestNewNautobotClient(t *testing.T) {
34+
tests := []struct {
35+
name string
36+
url string
37+
token string
38+
expectErr bool
39+
errorMsg string
40+
}{
41+
{
42+
name: "valid url and token creates client",
43+
url: "http://localhost:8080/api",
44+
token: "abc123",
45+
expectErr: false,
46+
},
47+
{
48+
name: "empty url returns error",
49+
url: "",
50+
token: "abc123",
51+
expectErr: true,
52+
errorMsg: "nautobot URL is required",
53+
},
54+
{
55+
name: "empty token returns error",
56+
url: "http://localhost:8080/api",
57+
token: "",
58+
expectErr: true,
59+
errorMsg: "nautobot API token is required",
60+
},
61+
}
62+
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
client, err := NewNautobotClient(tt.url, tt.token)
66+
67+
if tt.expectErr {
68+
if err == nil {
69+
t.Error("expected error but got none")
70+
}
71+
if tt.errorMsg != "" && !strings.Contains(err.Error(), tt.errorMsg) {
72+
t.Errorf("expected error containing %q but got %q", tt.errorMsg, err.Error())
73+
}
74+
return
75+
}
76+
77+
if err != nil {
78+
t.Errorf("unexpected error: %v", err)
79+
}
80+
if client == nil {
81+
t.Error("expected client to be non-nil")
82+
}
83+
})
84+
}
85+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
*
3+
* MIT License
4+
*
5+
* (C) Copyright 2023-2024 Hewlett Packard Enterprise Development LP
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a
8+
* copy of this software and associated documentation files (the "Software"),
9+
* to deal in the Software without restriction, including without limitation
10+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
11+
* and/or sell copies of the Software, and to permit persons to whom the
12+
* Software is furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included
15+
* in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*
25+
*/
26+
package export
27+
28+
import (
29+
"testing"
30+
31+
"github.com/Cray-HPE/cani/pkg/devicetypes"
32+
nautobotapi "github.com/Cray-HPE/cani/pkg/nautobot"
33+
)
34+
35+
func TestComparePosition(t *testing.T) {
36+
pos42 := 42
37+
pos10 := 10
38+
39+
tests := []struct {
40+
name string
41+
device *devicetypes.CaniDeviceType
42+
remote *nautobotapi.Device
43+
wantDiff bool
44+
}{
45+
{
46+
name: "positions differ returns diff",
47+
device: &devicetypes.CaniDeviceType{RackPosition: 42},
48+
remote: &nautobotapi.Device{Position: &pos10},
49+
wantDiff: true,
50+
},
51+
{
52+
name: "positions match returns no diff",
53+
device: &devicetypes.CaniDeviceType{RackPosition: 42},
54+
remote: &nautobotapi.Device{Position: &pos42},
55+
wantDiff: false,
56+
},
57+
{
58+
name: "local position zero skips comparison",
59+
device: &devicetypes.CaniDeviceType{RackPosition: 0},
60+
remote: &nautobotapi.Device{Position: &pos10},
61+
wantDiff: false,
62+
},
63+
{
64+
name: "remote position nil returns diff",
65+
device: &devicetypes.CaniDeviceType{RackPosition: 5},
66+
remote: &nautobotapi.Device{Position: nil},
67+
wantDiff: true,
68+
},
69+
}
70+
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
diffs := comparePosition(tt.device, tt.remote)
74+
if tt.wantDiff && len(diffs) == 0 {
75+
t.Error("expected diff but got none")
76+
}
77+
if !tt.wantDiff && len(diffs) > 0 {
78+
t.Errorf("expected no diff but got %v", diffs)
79+
}
80+
})
81+
}
82+
}
83+
84+
func TestCompareFace(t *testing.T) {
85+
frontVal := nautobotapi.DeviceFaceValue("front")
86+
rearVal := nautobotapi.DeviceFaceValue("rear")
87+
88+
tests := []struct {
89+
name string
90+
device *devicetypes.CaniDeviceType
91+
remote *nautobotapi.Device
92+
wantDiff bool
93+
}{
94+
{
95+
name: "faces differ returns diff",
96+
device: &devicetypes.CaniDeviceType{Face: "rear"},
97+
remote: &nautobotapi.Device{
98+
Face: &nautobotapi.DeviceFace{Value: &frontVal},
99+
},
100+
wantDiff: true,
101+
},
102+
{
103+
name: "faces match returns no diff",
104+
device: &devicetypes.CaniDeviceType{Face: "rear"},
105+
remote: &nautobotapi.Device{
106+
Face: &nautobotapi.DeviceFace{Value: &rearVal},
107+
},
108+
wantDiff: false,
109+
},
110+
{
111+
name: "empty local face skips comparison",
112+
device: &devicetypes.CaniDeviceType{Face: ""},
113+
remote: &nautobotapi.Device{},
114+
wantDiff: false,
115+
},
116+
}
117+
118+
for _, tt := range tests {
119+
t.Run(tt.name, func(t *testing.T) {
120+
diffs := compareFace(tt.device, tt.remote)
121+
if tt.wantDiff && len(diffs) == 0 {
122+
t.Error("expected diff but got none")
123+
}
124+
if !tt.wantDiff && len(diffs) > 0 {
125+
t.Errorf("expected no diff but got %v", diffs)
126+
}
127+
})
128+
}
129+
}
130+
131+
func TestPtrStr(t *testing.T) {
132+
val := "hello"
133+
134+
tests := []struct {
135+
name string
136+
input *string
137+
expected string
138+
}{
139+
{
140+
name: "non-nil returns value",
141+
input: &val,
142+
expected: "hello",
143+
},
144+
{
145+
name: "nil returns empty string",
146+
input: nil,
147+
expected: "",
148+
},
149+
}
150+
151+
for _, tt := range tests {
152+
t.Run(tt.name, func(t *testing.T) {
153+
got := ptrStr(tt.input)
154+
if got != tt.expected {
155+
t.Errorf("ptrStr() = %q, want %q", got, tt.expected)
156+
}
157+
})
158+
}
159+
}
160+
161+
func TestOrNone(t *testing.T) {
162+
tests := []struct {
163+
name string
164+
input string
165+
expected string
166+
}{
167+
{
168+
name: "non-empty returns input",
169+
input: "hello",
170+
expected: "hello",
171+
},
172+
{
173+
name: "empty returns (none)",
174+
input: "",
175+
expected: "(none)",
176+
},
177+
}
178+
179+
for _, tt := range tests {
180+
t.Run(tt.name, func(t *testing.T) {
181+
got := orNone(tt.input)
182+
if got != tt.expected {
183+
t.Errorf("orNone() = %q, want %q", got, tt.expected)
184+
}
185+
})
186+
}
187+
}

0 commit comments

Comments
 (0)