@@ -20,6 +20,9 @@ import (
20
20
"fmt"
21
21
"io/ioutil"
22
22
"os"
23
+ "path/filepath"
24
+ "reflect"
25
+ "strconv"
23
26
"sync"
24
27
"testing"
25
28
"time"
@@ -29,6 +32,7 @@ import (
29
32
"k8s.io/apimachinery/pkg/util/wait"
30
33
"k8s.io/client-go/tools/record"
31
34
registerapi "k8s.io/kubelet/pkg/apis/pluginregistration/v1"
35
+
32
36
"k8s.io/kubernetes/pkg/kubelet/config"
33
37
"k8s.io/kubernetes/pkg/kubelet/pluginmanager/pluginwatcher"
34
38
)
@@ -39,41 +43,41 @@ var (
39
43
)
40
44
41
45
type fakePluginHandler struct {
42
- validatePluginCalled bool
43
- registerPluginCalled bool
44
- deregisterPluginCalled bool
46
+ events []string
45
47
sync.RWMutex
46
48
}
47
49
48
50
func newFakePluginHandler () * fakePluginHandler {
49
- return & fakePluginHandler {
50
- validatePluginCalled : false ,
51
- registerPluginCalled : false ,
52
- deregisterPluginCalled : false ,
53
- }
51
+ return & fakePluginHandler {}
54
52
}
55
53
56
54
// ValidatePlugin is a fake method
57
55
func (f * fakePluginHandler ) ValidatePlugin (pluginName string , endpoint string , versions []string ) error {
58
56
f .Lock ()
59
57
defer f .Unlock ()
60
- f .validatePluginCalled = true
58
+ f .events = append ( f . events , "validate " + pluginName )
61
59
return nil
62
60
}
63
61
64
62
// RegisterPlugin is a fake method
65
63
func (f * fakePluginHandler ) RegisterPlugin (pluginName , endpoint string , versions []string ) error {
66
64
f .Lock ()
67
65
defer f .Unlock ()
68
- f .registerPluginCalled = true
66
+ f .events = append ( f . events , "register " + pluginName )
69
67
return nil
70
68
}
71
69
72
70
// DeRegisterPlugin is a fake method
73
71
func (f * fakePluginHandler ) DeRegisterPlugin (pluginName string ) {
74
72
f .Lock ()
75
73
defer f .Unlock ()
76
- f .deregisterPluginCalled = true
74
+ f .events = append (f .events , "deregister " + pluginName )
75
+ }
76
+
77
+ func (f * fakePluginHandler ) Reset () {
78
+ f .Lock ()
79
+ defer f .Unlock ()
80
+ f .events = nil
77
81
}
78
82
79
83
func init () {
@@ -90,15 +94,17 @@ func cleanup(t *testing.T) {
90
94
os .MkdirAll (socketDir , 0755 )
91
95
}
92
96
93
- func waitForRegistration (t * testing.T , fakePluginHandler * fakePluginHandler ) {
97
+ func waitForRegistration (t * testing.T , fakePluginHandler * fakePluginHandler , pluginName string ) {
98
+ expected := []string {"validate " + pluginName , "register " + pluginName }
94
99
err := retryWithExponentialBackOff (
95
- time . Duration ( 500 * time .Millisecond ) ,
100
+ 100 * time .Millisecond ,
96
101
func () (bool , error ) {
97
102
fakePluginHandler .Lock ()
98
103
defer fakePluginHandler .Unlock ()
99
- if fakePluginHandler . validatePluginCalled && fakePluginHandler .registerPluginCalled {
104
+ if reflect . DeepEqual ( fakePluginHandler .events , expected ) {
100
105
return true , nil
101
106
}
107
+ t .Logf ("expected %#v, got %#v, will retry" , expected , fakePluginHandler .events )
102
108
return false , nil
103
109
},
104
110
)
@@ -134,19 +140,29 @@ func TestPluginRegistration(t *testing.T) {
134
140
fakeHandler := newFakePluginHandler ()
135
141
pluginManager .AddHandler (registerapi .DevicePlugin , fakeHandler )
136
142
137
- // Add a new plugin
138
- socketPath := fmt .Sprintf ("%s/plugin.sock" , socketDir )
139
- pluginName := "example-plugin"
140
- p := pluginwatcher .NewTestExamplePlugin (pluginName , registerapi .DevicePlugin , socketPath , supportedVersions ... )
141
- require .NoError (t , p .Serve ("v1beta1" , "v1beta2" ))
142
-
143
- // Verify that the plugin is registered
144
- waitForRegistration (t , fakeHandler )
143
+ const maxDepth = 3
144
+ // Make sure the plugin manager is aware of the socket in subdirectories
145
+ for i := 0 ; i < maxDepth ; i ++ {
146
+ fakeHandler .Reset ()
147
+ pluginDir := socketDir
148
+
149
+ for j := 0 ; j < i ; j ++ {
150
+ pluginDir = filepath .Join (pluginDir , strconv .Itoa (j ))
151
+ }
152
+ require .NoError (t , os .MkdirAll (pluginDir , os .ModePerm ))
153
+ socketPath := filepath .Join (pluginDir , fmt .Sprintf ("plugin-%d.sock" , i ))
154
+
155
+ // Add a new plugin
156
+ pluginName := fmt .Sprintf ("example-plugin-%d" , i )
157
+ p := pluginwatcher .NewTestExamplePlugin (pluginName , registerapi .DevicePlugin , socketPath , supportedVersions ... )
158
+ require .NoError (t , p .Serve ("v1beta1" , "v1beta2" ))
159
+
160
+ // Verify that the plugin is registered
161
+ waitForRegistration (t , fakeHandler , pluginName )
162
+ }
145
163
}
146
164
147
- func newTestPluginManager (
148
- sockDir string ) PluginManager {
149
-
165
+ func newTestPluginManager (sockDir string ) PluginManager {
150
166
pm := NewPluginManager (
151
167
sockDir ,
152
168
& record.FakeRecorder {},
0 commit comments