-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_service_test.go
More file actions
181 lines (163 loc) · 4.74 KB
/
interface_service_test.go
File metadata and controls
181 lines (163 loc) · 4.74 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package truenas
import (
"context"
"encoding/json"
"errors"
"testing"
)
func sampleInterfaceJSON() json.RawMessage {
return json.RawMessage(`[{
"id": "eno1",
"name": "eno1",
"type": "PHYSICAL",
"state": {
"name": "eno1",
"link_state": "LINK_STATE_UP",
"active_media_type": "Ethernet",
"active_media_subtype": "1000baseT"
},
"aliases": [
{"type": "INET", "address": "192.168.1.100", "netmask": 24},
{"type": "INET6", "address": "fe80::1", "netmask": 64}
],
"description": "Primary NIC",
"mtu": 1500
}]`)
}
func TestInterfaceService_List(t *testing.T) {
mock := &mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
if method != "interface.query" {
t.Errorf("expected method interface.query, got %s", method)
}
return sampleInterfaceJSON(), nil
},
}
svc := NewInterfaceService(mock, Version{Major: 25, Minor: 4})
ifaces, err := svc.List(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(ifaces) != 1 {
t.Fatalf("expected 1 interface, got %d", len(ifaces))
}
iface := ifaces[0]
if iface.ID != "eno1" {
t.Errorf("expected ID eno1, got %s", iface.ID)
}
if iface.Type != InterfaceTypePhysical {
t.Errorf("expected type PHYSICAL, got %s", iface.Type)
}
if iface.State.LinkState != LinkStateUp {
t.Errorf("expected LINK_STATE_UP, got %s", iface.State.LinkState)
}
if len(iface.Aliases) != 2 {
t.Fatalf("expected 2 aliases, got %d", len(iface.Aliases))
}
if iface.Aliases[0].Address != "192.168.1.100" {
t.Errorf("expected address 192.168.1.100, got %s", iface.Aliases[0].Address)
}
if iface.MTU != 1500 {
t.Errorf("expected MTU 1500, got %d", iface.MTU)
}
}
func TestInterfaceService_List_Empty(t *testing.T) {
mock := &mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`[]`), nil
},
}
svc := NewInterfaceService(mock, Version{Major: 25, Minor: 4})
ifaces, err := svc.List(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(ifaces) != 0 {
t.Errorf("expected 0 interfaces, got %d", len(ifaces))
}
}
func TestInterfaceService_List_Error(t *testing.T) {
mock := &mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return nil, errors.New("network error")
},
}
svc := NewInterfaceService(mock, Version{Major: 25, Minor: 4})
_, err := svc.List(context.Background())
if err == nil {
t.Fatal("expected error")
}
}
func TestInterfaceService_List_ParseError(t *testing.T) {
mock := &mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`not json`), nil
},
}
svc := NewInterfaceService(mock, Version{Major: 25, Minor: 4})
_, err := svc.List(context.Background())
if err == nil {
t.Fatal("expected parse error")
}
}
func TestInterfaceService_Get(t *testing.T) {
mock := &mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
filter := params.([][]any)
if filter[0][2] != "eno1" {
t.Errorf("expected filter for eno1, got %v", filter)
}
return sampleInterfaceJSON(), nil
},
}
svc := NewInterfaceService(mock, Version{Major: 25, Minor: 4})
iface, err := svc.Get(context.Background(), "eno1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if iface == nil {
t.Fatal("expected non-nil interface")
}
if iface.ID != "eno1" {
t.Errorf("expected ID eno1, got %s", iface.ID)
}
}
func TestInterfaceService_Get_NotFound(t *testing.T) {
mock := &mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`[]`), nil
},
}
svc := NewInterfaceService(mock, Version{Major: 25, Minor: 4})
iface, err := svc.Get(context.Background(), "nonexistent")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if iface != nil {
t.Error("expected nil interface for not found")
}
}
func TestInterfaceService_Get_Error(t *testing.T) {
mock := &mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return nil, errors.New("timeout")
},
}
svc := NewInterfaceService(mock, Version{Major: 25, Minor: 4})
_, err := svc.Get(context.Background(), "eno1")
if err == nil {
t.Fatal("expected error")
}
}
func TestInterfaceService_Get_ParseError(t *testing.T) {
mock := &mockCaller{
callFunc: func(ctx context.Context, method string, params any) (json.RawMessage, error) {
return json.RawMessage(`not json`), nil
},
}
svc := NewInterfaceService(mock, Version{Major: 25, Minor: 4})
_, err := svc.Get(context.Background(), "eno1")
if err == nil {
t.Fatal("expected parse error")
}
}