-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_test.go
More file actions
96 lines (83 loc) · 2.33 KB
/
plugin_test.go
File metadata and controls
96 lines (83 loc) · 2.33 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
package network
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/msutara/config-manager-core/plugin"
)
var _ plugin.Plugin = (*NetworkPlugin)(nil)
func TestNewNetworkPlugin(t *testing.T) {
p := NewNetworkPlugin()
if p == nil {
t.Fatal("NewNetworkPlugin returned nil")
}
if p.svc == nil {
t.Fatal("NewNetworkPlugin().svc is nil")
}
}
func TestNetworkPlugin_Metadata(t *testing.T) {
p := NewNetworkPlugin()
if got := p.Name(); got != "network" {
t.Errorf("Name: got %q, want %q", got, "network")
}
if got := p.Version(); got == "" {
t.Error("Version: got empty string")
}
if got := p.Description(); got == "" {
t.Error("Description: got empty string")
}
}
func TestNetworkPlugin_Routes(t *testing.T) {
p := NewNetworkPlugin()
h := p.Routes()
if h == nil {
t.Fatal("Routes returned nil handler")
}
// Smoke test: /interfaces should respond (uses net.Interfaces, cross-platform)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/interfaces", nil)
h.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("/interfaces: got %d, want %d", w.Code, http.StatusOK)
}
// Unknown route should not be 200
w = httptest.NewRecorder()
r = httptest.NewRequest(http.MethodGet, "/nonexistent", nil)
h.ServeHTTP(w, r)
if w.Code == http.StatusOK {
t.Error("/nonexistent: expected non-200")
}
}
func TestNetworkPlugin_Endpoints(t *testing.T) {
p := NewNetworkPlugin()
eps := p.Endpoints()
if len(eps) != 9 {
t.Fatalf("Endpoints: got %d, want 9", len(eps))
}
want := []struct{ method, path string }{
{http.MethodGet, "/interfaces"},
{http.MethodGet, "/interfaces/{name}"},
{http.MethodPut, "/interfaces/{name}"},
{http.MethodDelete, "/interfaces/{name}"},
{http.MethodPost, "/interfaces/{name}/rollback"},
{http.MethodGet, "/dns"},
{http.MethodPut, "/dns"},
{http.MethodPost, "/dns/rollback"},
{http.MethodGet, "/status"},
}
for i, w := range want {
if eps[i].Method != w.method || eps[i].Path != w.path {
t.Errorf("endpoint[%d] = %s %s, want %s %s", i, eps[i].Method, eps[i].Path, w.method, w.path)
}
if eps[i].Description == "" {
t.Errorf("endpoint[%d] has empty description", i)
}
}
}
func TestNetworkPlugin_ScheduledJobs(t *testing.T) {
p := NewNetworkPlugin()
jobs := p.ScheduledJobs()
if jobs != nil {
t.Errorf("expected nil scheduled jobs, got %d", len(jobs))
}
}