-
Notifications
You must be signed in to change notification settings - Fork 234
/
plugins_test.go
350 lines (312 loc) · 10.4 KB
/
plugins_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
biutils "github.com/jfrog/build-info-go/utils"
"os"
"path"
"path/filepath"
"strings"
"testing"
clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/buger/jsonparser"
commonTests "github.com/jfrog/jfrog-cli-core/v2/common/tests"
"github.com/jfrog/jfrog-cli-core/v2/plugins"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
pluginsutils "github.com/jfrog/jfrog-cli-core/v2/utils/plugins"
coreTests "github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/jfrog/jfrog-cli/inttestutils"
"github.com/jfrog/jfrog-cli/plugins/commands/utils"
"github.com/jfrog/jfrog-cli/utils/cliutils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/stretchr/testify/assert"
)
const officialPluginForTest = "rt-fs"
const officialPluginVersion = "v1.1.5"
const customPluginName = "custom-plugin"
func TestPluginInstallUninstallOfficialRegistry(t *testing.T) {
initPluginsTest(t)
// Create temp jfrog home
cleanUpJfrogHome, err := coreTests.SetJfrogHome()
if err != nil {
return
}
defer cleanUpJfrogHome()
// Set empty plugins server to run against official registry.
oldServer := os.Getenv(utils.PluginsServerEnv)
defer func() {
clientTestUtils.SetEnvAndAssert(t, utils.PluginsServerEnv, oldServer)
}()
clientTestUtils.SetEnvAndAssert(t, utils.PluginsServerEnv, "")
oldRepo := os.Getenv(utils.PluginsRepoEnv)
defer func() {
clientTestUtils.SetEnvAndAssert(t, utils.PluginsRepoEnv, oldRepo)
}()
clientTestUtils.SetEnvAndAssert(t, utils.PluginsRepoEnv, "")
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
// Try installing a plugin with specific version.
err = installAndAssertPlugin(t, jfrogCli, officialPluginVersion)
if err != nil {
return
}
// Try installing the latest version of the plugin. Also verifies replacement was successful.
err = installAndAssertPlugin(t, jfrogCli, "")
if err != nil {
return
}
// Uninstall plugin from home dir.
err = jfrogCli.Exec("plugin", "uninstall", officialPluginForTest)
if err != nil {
assert.NoError(t, err)
return
}
err = verifyPluginInPluginsDir(t, officialPluginForTest, false, false)
if err != nil {
return
}
}
func TestPluginInstallWithProgressBar(t *testing.T) {
initPluginsTest(t)
callback := commonTests.MockProgressInitialization()
defer callback()
// Create temp jfrog home
cleanUpJfrogHome, err := coreTests.SetJfrogHome()
if err != nil {
return
}
defer cleanUpJfrogHome()
// Set empty plugins server to run against official registry.
oldServer := os.Getenv(utils.PluginsServerEnv)
defer func() {
clientTestUtils.SetEnvAndAssert(t, utils.PluginsServerEnv, oldServer)
}()
clientTestUtils.SetEnvAndAssert(t, utils.PluginsServerEnv, "")
oldRepo := os.Getenv(utils.PluginsRepoEnv)
defer func() {
clientTestUtils.SetEnvAndAssert(t, utils.PluginsRepoEnv, oldRepo)
}()
clientTestUtils.SetEnvAndAssert(t, utils.PluginsRepoEnv, "")
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
// Try installing a plugin with specific version.
err = installAndAssertPlugin(t, jfrogCli, officialPluginVersion)
if err != nil {
return
}
// Try installing the latest version of the plugin. Also verifies replacement was successful.
err = installAndAssertPlugin(t, jfrogCli, "")
if err != nil {
return
}
}
func installAndAssertPlugin(t *testing.T, jfrogCli *coreTests.JfrogCli, pluginVersion string) error {
// If version required, concat to plugin name
identifier := officialPluginForTest
if pluginVersion != "" {
identifier += "@" + pluginVersion
}
// Install plugin from registry.
err := jfrogCli.Exec("plugin", "install", identifier)
if err != nil {
assert.NoError(t, err)
return err
}
err = verifyPluginInPluginsDir(t, officialPluginForTest, true, false)
if err != nil {
return err
}
err = verifyPluginSignature(t, jfrogCli)
if err != nil {
return err
}
return verifyPluginVersion(t, jfrogCli, pluginVersion)
}
func verifyPluginSignature(t *testing.T, jfrogCli *coreTests.JfrogCli) error {
// Get signature from plugin.
content, err := tests.GetCmdOutput(t, jfrogCli, officialPluginForTest, plugins.SignatureCommandName)
if err != nil {
return err
}
// Extract the name from the output.
name, err := jsonparser.GetString(content, "name")
if err != nil {
assert.NoError(t, err)
return err
}
assert.Equal(t, officialPluginForTest, name)
// Extract the usage from the output.
usage, err := jsonparser.GetString(content, "usage")
if err != nil {
assert.NoError(t, err)
return err
}
assert.NotEmpty(t, usage)
return nil
}
func verifyPluginVersion(t *testing.T, jfrogCli *coreTests.JfrogCli, expectedVersion string) error {
// Run plugin's -v command.
content, err := tests.GetCmdOutput(t, jfrogCli, officialPluginForTest, "-v")
if err != nil {
return err
}
if expectedVersion != "" {
assert.NoError(t, utils.AssertPluginVersion(string(content), expectedVersion))
}
return err
}
func verifyPluginInPluginsDir(t *testing.T, pluginName string, execShouldExist, resourcesShouldExist bool) error {
pluginsDir, err := coreutils.GetJfrogPluginsDir()
if err != nil {
assert.NoError(t, err)
return err
}
// Check plugins executable exists
actualExists, err := fileutils.IsFileExists(filepath.Join(pluginsDir, pluginName, coreutils.PluginsExecDirName, pluginsutils.GetLocalPluginExecutableName(pluginName)), false)
if err != nil {
assert.NoError(t, err)
return err
}
if execShouldExist {
assert.True(t, actualExists, "expected plugin executable to be preset in plugins dir after installing")
} else {
assert.False(t, actualExists, "expected plugin executable not to be preset in plugins dir after uninstalling")
}
// Check plugins resources directory exists
actualExists, err = fileutils.IsFileExists(filepath.Join(pluginsDir, pluginName, coreutils.PluginsResourcesDirName, "dir", "resource"), false)
if err != nil {
assert.NoError(t, err)
return err
}
if resourcesShouldExist {
assert.True(t, actualExists, "expected resources to be preset in plugins dir after installing")
} else {
assert.False(t, actualExists, "expected resources not to be preset in plugins dir after uninstalling")
}
return nil
}
func initPluginsTest(t *testing.T) {
if !*tests.TestPlugins {
t.Skip("Skipping Plugins test. To run Plugins test add the '-test.plugins=true' option.")
}
}
func TestPublishInstallCustomServer(t *testing.T) {
initPluginsTest(t)
// Create temp jfrog home
cleanUpJfrogHome, err := coreTests.SetJfrogHome()
if err != nil {
return
}
defer cleanUpJfrogHome()
// Create server to use with the command.
_, err = createServerConfigAndReturnPassphrase(t)
defer deleteServerConfig(t)
if err != nil {
assert.NoError(t, err)
return
}
// Set plugins server to run against the configured server.
oldServer := os.Getenv(utils.PluginsServerEnv)
defer func() {
clientTestUtils.SetEnvAndAssert(t, utils.PluginsServerEnv, oldServer)
}()
clientTestUtils.SetEnvAndAssert(t, utils.PluginsServerEnv, tests.ServerId)
oldRepo := os.Getenv(utils.PluginsRepoEnv)
defer func() {
clientTestUtils.SetEnvAndAssert(t, utils.PluginsRepoEnv, oldRepo)
}()
clientTestUtils.SetEnvAndAssert(t, utils.PluginsRepoEnv, tests.RtRepo1)
err = setOnlyLocalArc(t)
if err != nil {
return
}
// Test without resources directory
testPublishAndInstall(t, false)
// Create 'resources' directory for testing
wd, err := os.Getwd()
assert.NoError(t, err)
exists, err := fileutils.IsDirExists(filepath.Join(wd, coreutils.PluginsResourcesDirName), false)
assert.NoError(t, err)
assert.False(t, exists)
err = biutils.CopyDir(filepath.Join(wd, "testdata", "plugins", "plugin-mock", coreutils.PluginsResourcesDirName), filepath.Join(wd, coreutils.PluginsResourcesDirName), true, nil)
assert.NoError(t, err)
// Test with resources directory
testPublishAndInstall(t, true)
assert.NoError(t, os.RemoveAll(filepath.Join(wd, "resources")))
}
func testPublishAndInstall(t *testing.T, resources bool) {
// Publish the CLI as a plugin to the registry.
jfrogCli := coreTests.NewJfrogCli(execMain, "jfrog", "")
err := jfrogCli.Exec("plugin", "p", customPluginName, cliutils.GetVersion())
if err != nil {
assert.NoError(t, err)
return
}
err = verifyPluginExistsInRegistry(t, resources)
if err != nil {
return
}
// Install plugin from registry.
err = jfrogCli.Exec("plugin", "install", customPluginName)
if err != nil {
assert.NoError(t, err)
return
}
err = verifyPluginInPluginsDir(t, customPluginName, true, resources)
if err != nil {
return
}
pluginsDir, err := coreutils.GetJfrogPluginsDir()
if err != nil {
assert.NoError(t, err)
return
}
clientTestUtils.RemoveAllAndAssert(t, filepath.Join(pluginsDir, customPluginName))
// Deleting plugin from Artifactory for other plugin's tests
err = jfrogCli.Exec("rt", "del", tests.RtRepo1+"/"+customPluginName)
assert.NoError(t, err)
}
func verifyPluginExistsInRegistry(t *testing.T, checkResources bool) error {
searchFilePath, err := tests.CreateSpec(tests.SearchAllRepo1)
if err != nil {
assert.NoError(t, err)
return err
}
localArc, err := utils.GetLocalArchitecture()
if err != nil {
assert.NoError(t, err)
return err
}
expectedPath := path.Join(utils.GetPluginDirPath(customPluginName, cliutils.GetVersion(), localArc), pluginsutils.GetLocalPluginExecutableName(customPluginName))
// Expected to find the plugin in the version and latest dir.
expected := []string{
expectedPath,
strings.Replace(expectedPath, cliutils.GetVersion(), utils.LatestVersionName, 1),
}
// Add resources to expected paths if needed
if checkResources {
expectedPath = path.Join(utils.GetPluginDirPath(customPluginName, cliutils.GetVersion(), localArc), coreutils.PluginsResourcesDirName+".zip")
expected = append(expected, expectedPath, strings.Replace(expectedPath, cliutils.GetVersion(), utils.LatestVersionName, 1))
}
inttestutils.VerifyExistInArtifactory(expected, searchFilePath, serverDetails, t)
return nil
}
// Set the local architecture to be the only one in map to avoid building for all architectures.
func setOnlyLocalArc(t *testing.T) error {
localArcName, err := utils.GetLocalArchitecture()
if err != nil {
assert.NoError(t, err)
return err
}
localArc := utils.ArchitecturesMap[localArcName]
utils.ArchitecturesMap = map[string]utils.Architecture{
localArcName: localArc,
}
return nil
}
func InitPluginsTests() {
initArtifactoryCli()
cleanUpOldRepositories()
tests.AddTimestampToGlobalVars()
createRequiredRepos()
}
func CleanPluginsTests() {
deleteCreatedRepos()
}