...
1
16
17 package cache
18
19 import (
20 "runtime"
21 "testing"
22
23 "github.com/stretchr/testify/require"
24 )
25
26
27
28
29 func Test_DSW_AddOrUpdatePlugin_Positive_NewPlugin(t *testing.T) {
30 dsw := NewDesiredStateOfWorld()
31 socketPath := "/var/lib/kubelet/device-plugins/test-plugin.sock"
32 err := dsw.AddOrUpdatePlugin(socketPath)
33
34 if err != nil {
35 t.Fatalf("AddOrUpdatePlugin failed. Expected: <no error> Actual: <%v>", err)
36 }
37
38
39 dswPlugins := dsw.GetPluginsToRegister()
40 if len(dswPlugins) != 1 {
41 t.Fatalf("Desired state of world length should be one but it's %d", len(dswPlugins))
42 }
43 if dswPlugins[0].SocketPath != socketPath {
44 t.Fatalf("Expected\n%s\nin desired state of world, but got\n%v\n", socketPath, dswPlugins[0])
45 }
46
47
48 if !dsw.PluginExists(socketPath) {
49 t.Fatalf("PluginExists returns false for the newly added plugin")
50 }
51 }
52
53
54
55
56 func Test_DSW_AddOrUpdatePlugin_Positive_ExistingPlugin(t *testing.T) {
57
58 if runtime.GOOS == "windows" {
59 t.Skip("Skipping test that fails on Windows")
60 }
61
62 dsw := NewDesiredStateOfWorld()
63 socketPath := "/var/lib/kubelet/device-plugins/test-plugin.sock"
64
65 err := dsw.AddOrUpdatePlugin(socketPath)
66 if err != nil {
67 t.Fatalf("AddOrUpdatePlugin failed. Expected: <no error> Actual: <%v>", err)
68 }
69
70
71 dswPlugins := dsw.GetPluginsToRegister()
72 if len(dswPlugins) != 1 {
73 t.Fatalf("Desired state of world length should be one but it's %d", len(dswPlugins))
74 }
75 if dswPlugins[0].SocketPath != socketPath {
76 t.Fatalf("Expected\n%s\nin desired state of world, but got\n%v\n", socketPath, dswPlugins[0])
77 }
78 oldTimestamp := dswPlugins[0].Timestamp
79
80
81 err = dsw.AddOrUpdatePlugin(socketPath)
82 if err != nil {
83 t.Fatalf("AddOrUpdatePlugin failed. Expected: <no error> Actual: <%v>", err)
84 }
85 newDswPlugins := dsw.GetPluginsToRegister()
86 if len(newDswPlugins) != 1 {
87 t.Fatalf("Desired state of world length should be one but it's %d", len(newDswPlugins))
88 }
89 if newDswPlugins[0].SocketPath != socketPath {
90 t.Fatalf("Expected\n%s\nin desired state of world, but got\n%v\n", socketPath, newDswPlugins[0])
91 }
92
93
94 if !newDswPlugins[0].Timestamp.After(oldTimestamp) {
95 t.Fatal("New timestamp is not newer than the old timestamp", newDswPlugins[0].Timestamp, oldTimestamp)
96 }
97
98 }
99
100
101
102
103 func Test_DSW_AddOrUpdatePlugin_Negative_PluginMissingInfo(t *testing.T) {
104 dsw := NewDesiredStateOfWorld()
105 socketPath := ""
106 err := dsw.AddOrUpdatePlugin(socketPath)
107 require.EqualError(t, err, "socket path is empty")
108
109
110 dswPlugins := dsw.GetPluginsToRegister()
111 if len(dswPlugins) != 0 {
112 t.Fatalf("Desired state of world length should be zero but it's %d", len(dswPlugins))
113 }
114
115
116 if dsw.PluginExists(socketPath) {
117 t.Fatalf("PluginExists returns true for the plugin that should not have been registered")
118 }
119 }
120
121
122
123
124 func Test_DSW_RemovePlugin_Positive(t *testing.T) {
125
126 dsw := NewDesiredStateOfWorld()
127 socketPath := "/var/lib/kubelet/device-plugins/test-plugin.sock"
128 err := dsw.AddOrUpdatePlugin(socketPath)
129
130 if err != nil {
131 t.Fatalf("AddOrUpdatePlugin failed. Expected: <no error> Actual: <%v>", err)
132 }
133
134
135 dsw.RemovePlugin(socketPath)
136
137
138 dswPlugins := dsw.GetPluginsToRegister()
139 if len(dswPlugins) != 0 {
140 t.Fatalf("Desired state of world length should be zero but it's %d", len(dswPlugins))
141 }
142
143
144 if dsw.PluginExists(socketPath) {
145 t.Fatalf("PluginExists returns true for the removed plugin")
146 }
147 }
148
View as plain text