@@ -17,20 +17,23 @@ limitations under the License.
17
17
package cache
18
18
19
19
import (
20
- "runtime"
20
+ goruntime "runtime"
21
21
"testing"
22
22
"time"
23
23
24
24
"github.com/stretchr/testify/require"
25
+ "k8s.io/apimachinery/pkg/util/uuid"
25
26
)
26
27
27
28
// Calls AddPlugin() to add a plugin
28
29
// Verifies newly added plugin exists in GetRegisteredPlugins()
29
- // Verifies PluginExistsWithCorrectTimestamp returns true for the plugin
30
+ // Verifies PluginExistsWithCorrectUUID returns true for the plugin
31
+ // Verifies PluginExistsWithCorrectTimestamp returns true for the plugin (excluded on Windows)
30
32
func Test_ASW_AddPlugin_Positive_NewPlugin (t * testing.T ) {
31
33
pluginInfo := PluginInfo {
32
34
SocketPath : "/var/lib/kubelet/device-plugins/test-plugin.sock" ,
33
35
Timestamp : time .Now (),
36
+ UUID : uuid .NewUUID (),
34
37
Handler : nil ,
35
38
Name : "test" ,
36
39
}
@@ -50,20 +53,29 @@ func Test_ASW_AddPlugin_Positive_NewPlugin(t *testing.T) {
50
53
t .Fatalf ("Expected\n %v\n in actual state of world, but got\n %v\n " , pluginInfo , aswPlugins [0 ])
51
54
}
52
55
56
+ // Check PluginExistsWithCorrectUUID returns true
57
+ if ! asw .PluginExistsWithCorrectUUID (pluginInfo ) {
58
+ t .Fatalf ("PluginExistsWithCorrectUUID returns false for plugin that should be registered" )
59
+ }
60
+
53
61
// Check PluginExistsWithCorrectTimestamp returns true
54
- if ! asw .PluginExistsWithCorrectTimestamp (pluginInfo ) {
62
+ // Skipped on Windows. Time measurements are not as fine-grained on Windows and can lead to
63
+ // 2 consecutive time.Now() calls to be return identical timestamps.
64
+ if goruntime .GOOS != "windows" && ! asw .PluginExistsWithCorrectTimestamp (pluginInfo ) {
55
65
t .Fatalf ("PluginExistsWithCorrectTimestamp returns false for plugin that should be registered" )
56
66
}
57
67
}
58
68
59
69
// Calls AddPlugin() to add an empty string for socket path
60
70
// Verifies the plugin does not exist in GetRegisteredPlugins()
61
- // Verifies PluginExistsWithCorrectTimestamp returns false
71
+ // Verifies PluginExistsWithCorrectUUID returns false
72
+ // Verifies PluginExistsWithCorrectTimestamp returns false (excluded on Windows)
62
73
func Test_ASW_AddPlugin_Negative_EmptySocketPath (t * testing.T ) {
63
74
asw := NewActualStateOfWorld ()
64
75
pluginInfo := PluginInfo {
65
76
SocketPath : "" ,
66
77
Timestamp : time .Now (),
78
+ UUID : uuid .NewUUID (),
67
79
Handler : nil ,
68
80
Name : "test" ,
69
81
}
@@ -76,21 +88,30 @@ func Test_ASW_AddPlugin_Negative_EmptySocketPath(t *testing.T) {
76
88
t .Fatalf ("Actual state of world length should be zero but it's %d" , len (aswPlugins ))
77
89
}
78
90
91
+ // Check PluginExistsWithCorrectUUID returns false
92
+ if asw .PluginExistsWithCorrectUUID (pluginInfo ) {
93
+ t .Fatalf ("PluginExistsWithCorrectUUID returns true for plugin that's not registered" )
94
+ }
95
+
79
96
// Check PluginExistsWithCorrectTimestamp returns false
80
- if asw .PluginExistsWithCorrectTimestamp (pluginInfo ) {
97
+ // Skipped on Windows. Time measurements are not as fine-grained on Windows and can lead to
98
+ // 2 consecutive time.Now() calls to be return identical timestamps.
99
+ if goruntime .GOOS != "windows" && asw .PluginExistsWithCorrectTimestamp (pluginInfo ) {
81
100
t .Fatalf ("PluginExistsWithCorrectTimestamp returns true for plugin that's not registered" )
82
101
}
83
102
}
84
103
85
104
// Calls RemovePlugin() to remove a plugin
86
105
// Verifies newly removed plugin no longer exists in GetRegisteredPlugins()
87
- // Verifies PluginExistsWithCorrectTimestamp returns false
106
+ // Verifies PluginExistsWithCorrectUUID returns false
107
+ // Verifies PluginExistsWithCorrectTimestamp returns false (excluded on Windows)
88
108
func Test_ASW_RemovePlugin_Positive (t * testing.T ) {
89
109
// First, add a plugin
90
110
asw := NewActualStateOfWorld ()
91
111
pluginInfo := PluginInfo {
92
112
SocketPath : "/var/lib/kubelet/device-plugins/test-plugin.sock" ,
93
113
Timestamp : time .Now (),
114
+ UUID : uuid .NewUUID (),
94
115
Handler : nil ,
95
116
Name : "test" ,
96
117
}
@@ -109,25 +130,28 @@ func Test_ASW_RemovePlugin_Positive(t *testing.T) {
109
130
t .Fatalf ("Actual state of world length should be zero but it's %d" , len (aswPlugins ))
110
131
}
111
132
133
+ // Check PluginExistsWithCorrectUUID returns false
134
+ if asw .PluginExistsWithCorrectUUID (pluginInfo ) {
135
+ t .Fatalf ("PluginExistsWithCorrectUUID returns true for the removed plugin" )
136
+ }
137
+
112
138
// Check PluginExistsWithCorrectTimestamp returns false
113
- if asw .PluginExistsWithCorrectTimestamp (pluginInfo ) {
139
+ // Skipped on Windows. Time measurements are not as fine-grained on Windows and can lead to
140
+ // 2 consecutive time.Now() calls to be return identical timestamps.
141
+ if goruntime .GOOS != "windows" && asw .PluginExistsWithCorrectTimestamp (pluginInfo ) {
114
142
t .Fatalf ("PluginExistsWithCorrectTimestamp returns true for the removed plugin" )
115
143
}
116
144
}
117
145
118
- // Verifies PluginExistsWithCorrectTimestamp returns false for an existing
119
- // plugin with the wrong timestamp
120
- func Test_ASW_PluginExistsWithCorrectTimestamp_Negative_WrongTimestamp (t * testing.T ) {
121
- // Skip tests that fail on Windows, as discussed during the SIG Testing meeting from January 10, 2023
122
- if runtime .GOOS == "windows" {
123
- t .Skip ("Skipping test that fails on Windows" )
124
- }
125
-
146
+ // Verifies PluginExistsWithCorrectUUID returns false for an existing
147
+ // plugin with the wrong UUID
148
+ func Test_ASW_PluginExistsWithCorrectUUID_Negative_WrongUUID (t * testing.T ) {
126
149
// First, add a plugin
127
150
asw := NewActualStateOfWorld ()
128
151
pluginInfo := PluginInfo {
129
152
SocketPath : "/var/lib/kubelet/device-plugins/test-plugin.sock" ,
130
153
Timestamp : time .Now (),
154
+ UUID : uuid .NewUUID (),
131
155
Handler : nil ,
132
156
Name : "test" ,
133
157
}
@@ -140,9 +164,10 @@ func Test_ASW_PluginExistsWithCorrectTimestamp_Negative_WrongTimestamp(t *testin
140
164
newerPlugin := PluginInfo {
141
165
SocketPath : "/var/lib/kubelet/device-plugins/test-plugin.sock" ,
142
166
Timestamp : time .Now (),
167
+ UUID : uuid .NewUUID (),
143
168
}
144
- // Check PluginExistsWithCorrectTimestamp returns false
145
- if asw .PluginExistsWithCorrectTimestamp (newerPlugin ) {
146
- t .Fatalf ("PluginExistsWithCorrectTimestamp returns true for a plugin with newer timestamp " )
169
+ // Check PluginExistsWithCorrectUUID returns false
170
+ if asw .PluginExistsWithCorrectUUID (newerPlugin ) {
171
+ t .Fatalf ("PluginExistsWithCorrectUUID returns true for a plugin with a different UUID " )
147
172
}
148
173
}
0 commit comments