-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_test.go
More file actions
107 lines (97 loc) · 2.47 KB
/
Copy pathplugin_test.go
File metadata and controls
107 lines (97 loc) · 2.47 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
package main
import (
"testing"
)
func TestValidatePortal(t *testing.T) {
tests := []struct {
name string
portal string
want bool
}{
{"empty", "", false},
{"valid IPv4", "192.168.1.1:3260", true},
{"valid IPv6", "[::1]:3260", true},
{"no port", "192.168.1.1", false},
{"bad port", "192.168.1.1:abc", false},
{"extra colon", "192.168.1.1:3260:extra", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validatePortal(tt.portal)
got := err == nil
if got != tt.want {
t.Errorf("validatePortal(%q) = %v, want %v", tt.portal, err, tt.want)
}
})
}
}
func TestValidateIQN(t *testing.T) {
tests := []struct {
name string
iqn string
want bool
}{
{"empty", "", false},
{"valid", "iqn.1992-08.com.example:diskstation.target-1.abcdef", true},
{"missing date", "iqn.com.vendor:id", false},
{"wildcard", "iqn.*", false},
{"with path sep", "iqn.2020-01.com.test:foo/bar", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateIQN(tt.iqn)
got := err == nil
if got != tt.want {
t.Errorf("validateIQN(%q) = %v, want %v", tt.iqn, err, tt.want)
}
})
}
}
func TestSplitPortal(t *testing.T) {
tests := []struct {
portal string
wantHost string
wantPort string
}{
{"192.168.1.1:3260", "192.168.1.1", "3260"},
{"[::1]:3260", "::1", "3260"},
{"192.168.1.1", "192.168.1.1", "3260"},
{"[2001:db8::1]:3260", "2001:db8::1", "3260"},
}
for _, tt := range tests {
host, port := splitPortal(tt.portal)
if host != tt.wantHost || port != tt.wantPort {
t.Errorf("splitPortal(%q) = (%q, %q), want (%q, %q)", tt.portal, host, port, tt.wantHost, tt.wantPort)
}
}
}
func TestPluginState(t *testing.T) {
ps := NewPluginState("")
// Get non-existent LUN
_, ok := ps.GetLUN("1.2.3.4:3260", "iqn.test")
if ok {
t.Error("GetLUN on empty state should return false")
}
// Upsert and get
ps.UpsertLUN("1.2.3.4:3260", "iqn.test", func(lun *LUNState) {
lun.MountPoint = "/mnt/test"
})
lun, ok := ps.GetLUN("1.2.3.4:3260", "iqn.test")
if !ok {
t.Fatal("GetLUN after UpsertLUN should return true")
}
if lun.MountPoint != "/mnt/test" {
t.Errorf("unexpected LUN state: %+v", lun)
}
// ListLUNs
luns := ps.ListLUNs()
if len(luns) != 1 {
t.Errorf("ListLUNs len = %d, want 1", len(luns))
}
// DeleteLUN
ps.DeleteLUN("1.2.3.4:3260", "iqn.test")
_, ok = ps.GetLUN("1.2.3.4:3260", "iqn.test")
if ok {
t.Error("GetLUN after DeleteLUN should return false")
}
}