forked from netbox-community/go-netbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetbox_test.go
More file actions
97 lines (85 loc) · 2.21 KB
/
netbox_test.go
File metadata and controls
97 lines (85 loc) · 2.21 KB
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
// Copyright 2016 The go-netbox Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package netbox
import (
"encoding/json"
"fmt"
"math"
"net/http"
"net/http/httptest"
"testing"
)
func TestFamilyValid(t *testing.T) {
var tests = []struct {
f Family
ok bool
}{
{
f: math.MinInt64,
},
{
f: math.MaxInt64,
},
{
f: FamilyIPv4,
ok: true,
},
{
f: FamilyIPv6,
ok: true,
},
}
for _, tt := range tests {
if want, got := tt.ok, tt.f.Valid(); want != got {
t.Fatalf("unexpected Family(%d).Valid():\n- want: %v\n- got: %v",
tt.f, want, got)
}
}
}
// ExampleNewClient demonstrates usage of the Client type.
func ExampleNewClient() {
// Sets up a minimal, mocked NetBox server
addr, done := exampleServer()
defer done()
// Creates a client configured to use the test server
c, err := NewClient(addr, nil)
if err != nil {
panic(fmt.Sprintf("failed to create netbox.Client: %v", err))
}
// Retrieve an IPAddress with ID 1
ip, err := c.IPAM.GetIPAddress(1)
if err != nil {
panic(fmt.Sprintf("failed to retrieve IP address: %v", err))
}
fmt.Printf("IP #%03d: %s (%s)\n", ip.ID, ip.Address.String(), ip.Family)
// Output:
// IP #001: 192.168.1.1/32 (IPv4)
}
// exampleServer creates a test HTTP server which returns its address and
// can be closed using the returned closure.
func exampleServer() (string, func()) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip := struct {
ID int `json:"id"`
Family Family `json:"family"`
Address string `json:"address"`
}{
ID: 1,
Family: FamilyIPv4,
Address: "192.168.1.1/32",
}
_ = json.NewEncoder(w).Encode(ip)
}))
return s.URL, func() { s.Close() }
}