-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface_service_iface_test.go
More file actions
78 lines (69 loc) · 1.81 KB
/
interface_service_iface_test.go
File metadata and controls
78 lines (69 loc) · 1.81 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
package truenas
import (
"context"
"testing"
)
func TestMockInterfaceService_ImplementsInterface(t *testing.T) {
// Compile-time check
var _ InterfaceServiceAPI = (*InterfaceService)(nil)
var _ InterfaceServiceAPI = (*MockInterfaceService)(nil)
}
func TestMockInterfaceService_DefaultsToNil(t *testing.T) {
mock := &MockInterfaceService{}
ctx := context.Background()
ifaces, err := mock.List(ctx)
if err != nil {
t.Fatalf("expected nil error, got: %v", err)
}
if ifaces != nil {
t.Fatalf("expected nil result, got: %v", ifaces)
}
iface, err := mock.Get(ctx, "eno1")
if err != nil {
t.Fatalf("expected nil error, got: %v", err)
}
if iface != nil {
t.Fatalf("expected nil result, got: %v", iface)
}
}
func TestMockInterfaceService_CallsListFunc(t *testing.T) {
called := false
mock := &MockInterfaceService{
ListFunc: func(ctx context.Context) ([]NetworkInterface, error) {
called = true
return []NetworkInterface{{ID: "eno1", Name: "eno1"}}, nil
},
}
ifaces, err := mock.List(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !called {
t.Fatal("expected ListFunc to be called")
}
if len(ifaces) != 1 {
t.Fatalf("expected 1 interface, got %d", len(ifaces))
}
if ifaces[0].ID != "eno1" {
t.Fatalf("expected ID eno1, got %s", ifaces[0].ID)
}
}
func TestMockInterfaceService_CallsGetFunc(t *testing.T) {
called := false
mock := &MockInterfaceService{
GetFunc: func(ctx context.Context, id string) (*NetworkInterface, error) {
called = true
return &NetworkInterface{ID: id, Name: id}, nil
},
}
iface, err := mock.Get(context.Background(), "eno1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !called {
t.Fatal("expected GetFunc to be called")
}
if iface.ID != "eno1" {
t.Fatalf("expected ID eno1, got %s", iface.ID)
}
}