Skip to content

Commit 8c70f3f

Browse files
committed
add tests per-issue
1 parent 34e9279 commit 8c70f3f

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

tests/000_defaults_test.go

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
"testing"
8+
9+
"math/rand"
10+
11+
"github.com/netbox-community/go-netbox/v4"
12+
)
13+
14+
const (
15+
manufacturerName = "test-manufacturer"
16+
deviceTypeName = "test-device-type"
17+
siteName = "test-site"
18+
deviceName = "test-device"
19+
deviceRoleName = "test-device-role"
20+
interfaceName = "test-interface"
21+
)
22+
23+
type Defaults struct {
24+
Site netbox.Site
25+
DeviceRole netbox.DeviceRole
26+
Manufacturer netbox.Manufacturer
27+
DeviceType netbox.DeviceType
28+
Device netbox.DeviceWithConfigContext
29+
}
30+
31+
func (d *Defaults) Cleanup(t *testing.T, client *netbox.APIClient) {
32+
33+
_, err := client.DcimAPI.DcimDevicesDestroy(context.Background(), d.Device.Id).Execute()
34+
if err != nil {
35+
t.Errorf("Failed to delete device: %v", err)
36+
}
37+
38+
_, err = client.DcimAPI.DcimDeviceTypesDestroy(context.Background(), d.DeviceType.Id).Execute()
39+
if err != nil {
40+
t.Errorf("Failed to delete device type: %v", err)
41+
}
42+
43+
_, err = client.DcimAPI.DcimManufacturersDestroy(context.Background(), d.Manufacturer.Id).Execute()
44+
if err != nil {
45+
t.Errorf("Failed to delete manufacturer: %v", err)
46+
}
47+
48+
_, err = client.DcimAPI.DcimDeviceRolesDestroy(context.Background(), d.DeviceRole.Id).Execute()
49+
if err != nil {
50+
t.Errorf("Failed to delete device role: %v", err)
51+
}
52+
53+
_, err = client.DcimAPI.DcimSitesDestroy(context.Background(), d.Site.Id).Execute()
54+
if err != nil {
55+
t.Errorf("Failed to delete site: %v", err)
56+
}
57+
}
58+
59+
func randString(n int) string {
60+
letters := []rune("abcdefghijklmnopqrstuvwxyz")
61+
s := make([]rune, n)
62+
for i := range s {
63+
s[i] = letters[rand.Intn(len(letters))]
64+
}
65+
return string(s)
66+
}
67+
68+
func fatalHttp(t *testing.T, msg string, err error, res *http.Response) {
69+
body, _ := io.ReadAll(res.Body)
70+
t.Fatalf("%s: %v\n response: %v", msg, err, string(body))
71+
}
72+
73+
func HGetDefaults(t *testing.T, client *netbox.APIClient) *Defaults {
74+
site := netbox.WritableSiteRequest{
75+
Name: siteName + randString(4),
76+
Slug: siteName + randString(4),
77+
}
78+
79+
newSite, res, err := client.DcimAPI.DcimSitesCreate(context.Background()).WritableSiteRequest(site).Execute()
80+
if err != nil {
81+
fatalHttp(t, "Failed to create site", err, res)
82+
}
83+
84+
deviceRole := netbox.DeviceRoleRequest{
85+
Name: deviceRoleName + randString(4),
86+
Slug: deviceRoleName + randString(4),
87+
}
88+
89+
newDeviceRole, res, err := client.DcimAPI.DcimDeviceRolesCreate(context.Background()).DeviceRoleRequest(deviceRole).Execute()
90+
if err != nil {
91+
fatalHttp(t, "Failed to create device role", err, res)
92+
}
93+
94+
manufacturer := netbox.ManufacturerRequest{
95+
Name: manufacturerName + randString(4),
96+
Slug: manufacturerName + randString(4),
97+
}
98+
99+
newManufacturer, res, err := client.DcimAPI.DcimManufacturersCreate(context.Background()).ManufacturerRequest(manufacturer).Execute()
100+
if err != nil {
101+
fatalHttp(t, "Failed to create manufacturer", err, res)
102+
}
103+
104+
deviceType := netbox.WritableDeviceTypeRequest{
105+
Model: deviceTypeName + randString(4),
106+
Slug: deviceTypeName + randString(4),
107+
Manufacturer: *netbox.NewBriefManufacturerRequest(newManufacturer.Name, newManufacturer.Slug),
108+
}
109+
110+
newDeviceType, res, err := client.DcimAPI.DcimDeviceTypesCreate(context.Background()).WritableDeviceTypeRequest(deviceType).Execute()
111+
if err != nil {
112+
fatalHttp(t, "Failed to create device type", err, res)
113+
}
114+
115+
name := deviceName + randString(4)
116+
117+
device := netbox.WritableDeviceWithConfigContextRequest{
118+
Name: *netbox.NewNullableString(&name),
119+
Role: *netbox.NewBriefDeviceRoleRequest(newDeviceRole.Name, newDeviceRole.Slug),
120+
Site: *netbox.NewBriefSiteRequest(newSite.Name, newSite.Slug),
121+
DeviceType: *netbox.NewBriefDeviceTypeRequest(*netbox.NewBriefManufacturerRequest(newManufacturer.Name, newManufacturer.Slug), newDeviceType.Model, newDeviceType.Slug),
122+
}
123+
124+
newDevice, res, err := client.DcimAPI.DcimDevicesCreate(context.Background()).WritableDeviceWithConfigContextRequest(device).Execute()
125+
if err != nil {
126+
fatalHttp(t, "Failed to create device", err, res)
127+
}
128+
129+
return &Defaults{
130+
Site: *newSite,
131+
DeviceRole: *newDeviceRole,
132+
Device: *newDevice,
133+
Manufacturer: *newManufacturer,
134+
DeviceType: *newDeviceType,
135+
}
136+
}
137+
138+
func TestDefaults(t *testing.T) {
139+
140+
client := HGetClient(t)
141+
142+
defaults := HGetDefaults(t, client)
143+
144+
defaults.Cleanup(t, client)
145+
}

tests/000_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/netbox-community/go-netbox/v4"
8+
)
9+
10+
func HGetClient(t *testing.T) *netbox.APIClient {
11+
hostname := ""
12+
token := ""
13+
14+
if os.Getenv("NETBOX_HOSTNAME") != "" {
15+
hostname = os.Getenv("NETBOX_HOSTNAME")
16+
}
17+
18+
if os.Getenv("NETBOX_TOKEN") != "" {
19+
token = os.Getenv("NETBOX_TOKEN")
20+
}
21+
22+
if hostname == "" || token == "" {
23+
t.Skip("NETBOX_HOSTNAME and NETBOX_TOKEN must be set")
24+
}
25+
26+
client := netbox.NewAPIClientFor(hostname, token)
27+
return client
28+
}

tests/198_test.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"io"
6+
"testing"
7+
8+
"github.com/netbox-community/go-netbox/v4"
9+
)
10+
11+
type Seed198 struct {
12+
Interfaces []*netbox.Interface
13+
Cable *netbox.Cable
14+
}
15+
16+
func (s *Seed198) Cleanup(t *testing.T, client *netbox.APIClient) {
17+
_, err := client.DcimAPI.DcimCablesDestroy(context.Background(), s.Cable.Id).Execute()
18+
if err != nil {
19+
t.Errorf("Failed to delete cable: %v", err)
20+
}
21+
for _, iface := range s.Interfaces {
22+
_, err := client.DcimAPI.DcimInterfacesDestroy(context.Background(), iface.Id).Execute()
23+
if err != nil {
24+
t.Errorf("Failed to delete interface: %v", err)
25+
}
26+
}
27+
}
28+
func HSeed198(t *testing.T, client *netbox.APIClient, defaults *Defaults) Seed198 {
29+
dr := netbox.BriefDeviceRequest{Name: defaults.Device.Name}
30+
31+
interfaceRequests := []netbox.WritableInterfaceRequest{
32+
{
33+
Name: randString(10),
34+
Device: dr,
35+
Type: netbox.INTERFACETYPEVALUE__10GBASE_T,
36+
},
37+
{
38+
Name: randString(10),
39+
Device: dr,
40+
Type: netbox.INTERFACETYPEVALUE__10GBASE_T,
41+
},
42+
}
43+
44+
interfaces := []*netbox.Interface{}
45+
for _, iface := range interfaceRequests {
46+
i, res, err := client.DcimAPI.DcimInterfacesCreate(context.Background()).WritableInterfaceRequest(iface).Execute()
47+
if err != nil {
48+
fatalHttp(t, "Failed to create interface", err, res)
49+
}
50+
body, _ := io.ReadAll(res.Body)
51+
t.Logf("Created interface: %s\n Theoretically: %d", string(body), i.Id)
52+
interfaces = append(interfaces, i)
53+
}
54+
55+
cr := netbox.WritableCableRequest{
56+
ATerminations: []netbox.GenericObjectRequest{
57+
{
58+
ObjectType: "dcim.interface",
59+
ObjectId: interfaces[0].Id,
60+
},
61+
},
62+
BTerminations: []netbox.GenericObjectRequest{
63+
{
64+
ObjectType: "dcim.interface",
65+
ObjectId: interfaces[1].Id,
66+
},
67+
},
68+
}
69+
70+
c, res, err := client.DcimAPI.DcimCablesCreate(context.Background()).WritableCableRequest(cr).Execute()
71+
if err != nil {
72+
fatalHttp(t, "Failed to created cable", err, res)
73+
}
74+
75+
return Seed198{
76+
Interfaces: interfaces,
77+
Cable: c,
78+
}
79+
}
80+
81+
func Test198(t *testing.T) {
82+
client := HGetClient(t)
83+
defaults := HGetDefaults(t, client)
84+
seed := HSeed198(t, client, defaults)
85+
86+
_, _, err := client.DcimAPI.DcimInterfacesTraceRetrieve(context.Background(), seed.Interfaces[0].Id).Execute()
87+
if err == nil {
88+
t.Errorf("Expected error, got nil")
89+
}
90+
91+
seed.Cleanup(t, client)
92+
}

0 commit comments

Comments
 (0)