@@ -18,6 +18,7 @@ package cli
18
18
19
19
import (
20
20
"errors"
21
+ "fmt"
21
22
"os"
22
23
"path/filepath"
23
24
"runtime"
@@ -34,6 +35,179 @@ import (
34
35
)
35
36
36
37
var _ = Describe ("Discover external plugins" , func () {
38
+ Context ("with valid plugins root path" , func () {
39
+ var (
40
+ homePath string = os .Getenv ("HOME" )
41
+ customPath string = "/tmp/myplugins"
42
+ // store user's original EXTERNAL_PLUGINS_PATH
43
+ originalPluginPath string
44
+ xdghome string
45
+ // store user's original XDG_CONFIG_HOME
46
+ originalXdghome string
47
+ )
48
+
49
+ When ("XDG_CONFIG_HOME is not set and using the $HOME environment variable" , func () {
50
+ // store and unset the XDG_CONFIG_HOME
51
+ BeforeEach (func () {
52
+ originalXdghome = os .Getenv ("XDG_CONFIG_HOME" )
53
+ err := os .Unsetenv ("XDG_CONFIG_HOME" )
54
+ Expect (err ).To (BeNil ())
55
+ })
56
+
57
+ AfterEach (func () {
58
+ if originalXdghome != "" {
59
+ // restore the original value
60
+ err := os .Setenv ("XDG_CONFIG_HOME" , originalXdghome )
61
+ Expect (err ).To (BeNil ())
62
+ }
63
+ })
64
+
65
+ It ("should return the correct path for the darwin OS" , func () {
66
+ plgPath , err := getPluginsRoot ("darwin" )
67
+ Expect (err ).To (BeNil ())
68
+ Expect (plgPath ).To (Equal (fmt .Sprintf ("%s/Library/Application Support/kubebuilder/plugins" , homePath )))
69
+ })
70
+
71
+ It ("should return the correct path for the linux OS" , func () {
72
+ plgPath , err := getPluginsRoot ("linux" )
73
+ Expect (err ).To (BeNil ())
74
+ Expect (plgPath ).To (Equal (fmt .Sprintf ("%s/.config/kubebuilder/plugins" , homePath )))
75
+ })
76
+
77
+ It ("should return error when the host is not darwin / linux" , func () {
78
+ plgPath , err := getPluginsRoot ("random" )
79
+ Expect (plgPath ).To (Equal ("" ))
80
+ Expect (err ).ToNot (BeNil ())
81
+ Expect (err .Error ()).To (ContainSubstring ("host not supported" ))
82
+ })
83
+ })
84
+
85
+ When ("XDG_CONFIG_HOME is set" , func () {
86
+ BeforeEach (func () {
87
+ // store and set the XDG_CONFIG_HOME
88
+ originalXdghome = os .Getenv ("XDG_CONFIG_HOME" )
89
+ err := os .Setenv ("XDG_CONFIG_HOME" , fmt .Sprintf ("%s/.config" , homePath ))
90
+ Expect (err ).To (BeNil ())
91
+
92
+ xdghome = os .Getenv ("XDG_CONFIG_HOME" )
93
+ })
94
+
95
+ AfterEach (func () {
96
+ if originalXdghome != "" {
97
+ // restore the original value
98
+ err := os .Setenv ("XDG_CONFIG_HOME" , originalXdghome )
99
+ Expect (err ).To (BeNil ())
100
+ } else {
101
+ // unset if it was originally unset
102
+ err := os .Unsetenv ("XDG_CONFIG_HOME" )
103
+ Expect (err ).To (BeNil ())
104
+ }
105
+ })
106
+
107
+ It ("should return the correct path for the darwin OS" , func () {
108
+ plgPath , err := getPluginsRoot ("darwin" )
109
+ Expect (err ).To (BeNil ())
110
+ Expect (plgPath ).To (Equal (fmt .Sprintf ("%s/kubebuilder/plugins" , xdghome )))
111
+ })
112
+
113
+ It ("should return the correct path for the linux OS" , func () {
114
+ plgPath , err := getPluginsRoot ("linux" )
115
+ Expect (err ).To (BeNil ())
116
+ Expect (plgPath ).To (Equal (fmt .Sprintf ("%s/kubebuilder/plugins" , xdghome )))
117
+ })
118
+
119
+ It ("should return error when the host is not darwin / linux" , func () {
120
+ plgPath , err := getPluginsRoot ("random" )
121
+ Expect (plgPath ).To (Equal ("" ))
122
+ Expect (err ).ToNot (BeNil ())
123
+ Expect (err .Error ()).To (ContainSubstring ("host not supported" ))
124
+ })
125
+ })
126
+
127
+ When ("using the custom path" , func () {
128
+ BeforeEach (func () {
129
+ err := os .MkdirAll (customPath , 0750 )
130
+ Expect (err ).To (BeNil ())
131
+
132
+ // store and set the EXTERNAL_PLUGINS_PATH
133
+ originalPluginPath = os .Getenv ("EXTERNAL_PLUGINS_PATH" )
134
+ err = os .Setenv ("EXTERNAL_PLUGINS_PATH" , customPath )
135
+ Expect (err ).To (BeNil ())
136
+ })
137
+
138
+ AfterEach (func () {
139
+ if originalPluginPath != "" {
140
+ // restore the original value
141
+ err := os .Setenv ("EXTERNAL_PLUGINS_PATH" , originalPluginPath )
142
+ Expect (err ).To (BeNil ())
143
+ } else {
144
+ // unset if it was originally unset
145
+ err := os .Unsetenv ("EXTERNAL_PLUGINS_PATH" )
146
+ Expect (err ).To (BeNil ())
147
+ }
148
+ })
149
+
150
+ It ("should return the user given path for darwin OS" , func () {
151
+ plgPath , err := getPluginsRoot ("darwin" )
152
+ Expect (plgPath ).To (Equal (customPath ))
153
+ Expect (err ).To (BeNil ())
154
+ })
155
+
156
+ It ("should return the user given path for linux OS" , func () {
157
+ plgPath , err := getPluginsRoot ("linux" )
158
+ Expect (plgPath ).To (Equal (customPath ))
159
+ Expect (err ).To (BeNil ())
160
+ })
161
+
162
+ It ("should report error when the host is not darwin / linux" , func () {
163
+ plgPath , err := getPluginsRoot ("random" )
164
+ Expect (plgPath ).To (Equal ("" ))
165
+ Expect (err ).ToNot (BeNil ())
166
+ Expect (err .Error ()).To (ContainSubstring ("host not supported" ))
167
+ })
168
+ })
169
+ })
170
+
171
+ Context ("with invalid plugins root path" , func () {
172
+ var originalPluginPath string
173
+
174
+ BeforeEach (func () {
175
+ originalPluginPath = os .Getenv ("EXTERNAL_PLUGINS_PATH" )
176
+ err := os .Setenv ("EXTERNAL_PLUGINS_PATH" , "/non/existent/path" )
177
+ Expect (err ).To (BeNil ())
178
+ })
179
+
180
+ AfterEach (func () {
181
+ if originalPluginPath != "" {
182
+ // restore the original value
183
+ err := os .Setenv ("EXTERNAL_PLUGINS_PATH" , originalPluginPath )
184
+ Expect (err ).To (BeNil ())
185
+ } else {
186
+ // unset if it was originally unset
187
+ err := os .Unsetenv ("EXTERNAL_PLUGINS_PATH" )
188
+ Expect (err ).To (BeNil ())
189
+ }
190
+ })
191
+
192
+ It ("should return an error for the darwin OS" , func () {
193
+ plgPath , err := getPluginsRoot ("darwin" )
194
+ Expect (err ).ToNot (BeNil ())
195
+ Expect (plgPath ).To (Equal ("" ))
196
+ })
197
+
198
+ It ("should return an error for the linux OS" , func () {
199
+ plgPath , err := getPluginsRoot ("linux" )
200
+ Expect (err ).ToNot (BeNil ())
201
+ Expect (plgPath ).To (Equal ("" ))
202
+ })
203
+
204
+ It ("should return an error when the host is not darwin / linux" , func () {
205
+ plgPath , err := getPluginsRoot ("random" )
206
+ Expect (err ).ToNot (BeNil ())
207
+ Expect (plgPath ).To (Equal ("" ))
208
+ })
209
+ })
210
+
37
211
Context ("when plugin executables exist in the expected plugin directories" , func () {
38
212
const (
39
213
filePermissions os.FileMode = 755
@@ -219,18 +393,6 @@ var _ = Describe("Discover external plugins", func() {
219
393
Expect (err ).To (Equal (errPluginsRoot ))
220
394
})
221
395
222
- It ("should fail for any other host that is not supported" , func () {
223
- _ , err := getPluginsRoot ("darwin" )
224
- Expect (err ).To (BeNil ())
225
-
226
- _ , err = getPluginsRoot ("linux" )
227
- Expect (err ).To (BeNil ())
228
-
229
- _ , err = getPluginsRoot ("random" )
230
- Expect (err ).ToNot (BeNil ())
231
- Expect (err .Error ()).To (ContainSubstring ("host not supported" ))
232
- })
233
-
234
396
It ("should skip parsing of directories if plugins root is not a directory" , func () {
235
397
retrievePluginsRoot = func (host string ) (string , error ) {
236
398
return "externalplugin.sh" , nil
@@ -240,18 +402,6 @@ var _ = Describe("Discover external plugins", func() {
240
402
Expect (err ).To (BeNil ())
241
403
})
242
404
243
- It ("should fail for any other host that is not supported" , func () {
244
- _ , err := getPluginsRoot ("darwin" )
245
- Expect (err ).To (BeNil ())
246
-
247
- _ , err = getPluginsRoot ("linux" )
248
- Expect (err ).To (BeNil ())
249
-
250
- _ , err = getPluginsRoot ("random" )
251
- Expect (err ).ToNot (BeNil ())
252
- Expect (err .Error ()).To (ContainSubstring ("host not supported" ))
253
- })
254
-
255
405
It ("should return full path to the external plugins without XDG_CONFIG_HOME" , func () {
256
406
if _ , ok := os .LookupEnv ("XDG_CONFIG_HOME" ); ok {
257
407
err = os .Setenv ("XDG_CONFIG_HOME" , "" )
0 commit comments