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