forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugins_test.go
148 lines (128 loc) · 3.71 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
package main
import (
"bytes"
"github.com/buger/jsonparser"
"github.com/jfrog/jfrog-cli-core/plugins"
"github.com/jfrog/jfrog-cli-core/utils/coreutils"
coreTests "github.com/jfrog/jfrog-cli-core/utils/tests"
"github.com/jfrog/jfrog-cli/plugins/utils"
"github.com/jfrog/jfrog-cli/utils/tests"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"testing"
)
const jfrogPluginTestsHome = ".jfrogCliPluginsTest"
const pluginTemplateName = "hello-frog"
func TestPluginFullCycle(t *testing.T) {
initPluginsTest(t)
// Create temp jfrog home
oldHome, err := coreTests.SetJfrogHome(jfrogPluginTestsHome)
if err != nil {
return
}
defer os.Setenv(coreutils.HomeDir, oldHome)
// Clean from previous tests.
os.RemoveAll(jfrogPluginTestsHome)
defer os.RemoveAll(jfrogPluginTestsHome)
// Set CI to true to prevent interactive.
oldCi := os.Getenv(coreutils.CI)
os.Setenv(coreutils.CI, "true")
defer os.Setenv(coreutils.CI, oldCi)
jfrogCli := tests.NewJfrogCli(execMain, "jfrog", "")
// Install plugin from registry.
err = jfrogCli.Exec("plugin install " + pluginTemplateName)
if err != nil {
assert.NoError(t, err)
return
}
err = verifyPluginInPluginsDir(t, true)
if err != nil {
return
}
// Redirect to extract signature and commands output.
buffer, previousLog := tests.RedirectLogOutputToBuffer()
defer log.SetLogger(previousLog)
err = verifyPluginSignature(t, jfrogCli, buffer)
if err != nil {
return
}
err = verifyPluginCommand(t, jfrogCli, buffer)
if err != nil {
return
}
// Uninstall plugin from home dir.
err = jfrogCli.Exec("plugin uninstall " + pluginTemplateName)
if err != nil {
assert.NoError(t, err)
return
}
err = verifyPluginInPluginsDir(t, false)
if err != nil {
return
}
}
func verifyPluginSignature(t *testing.T, jfrogCli *tests.JfrogCli, buffer *bytes.Buffer) error {
// Get signature from plugin.
err := jfrogCli.Exec(pluginTemplateName + " " + plugins.SignatureCommandName)
if err != nil {
assert.NoError(t, err)
return err
}
// Write the command output to the origin.
content := buffer.Bytes()
buffer.Reset()
// Extract the the name from the output.
name, err := jsonparser.GetString(content, "name")
if err != nil {
assert.NoError(t, err)
return err
}
assert.Equal(t, pluginTemplateName, name)
// Extract the 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 verifyPluginCommand(t *testing.T, jfrogCli *tests.JfrogCli, buffer *bytes.Buffer) error {
// Run plugin's command.
err := jfrogCli.Exec(pluginTemplateName + " hello world --shout")
if err != nil {
assert.NoError(t, err)
return err
}
// Write the command output to the origin
content := buffer.Bytes()
buffer.Reset()
assert.Contains(t, string(content), "HELLO WORLD")
return nil
}
func verifyPluginInPluginsDir(t *testing.T, shouldExist bool) error {
pluginsDir, err := coreutils.GetJfrogPluginsDir()
if err != nil {
assert.NoError(t, err)
return err
}
actualExists, err := fileutils.IsFileExists(filepath.Join(pluginsDir, utils.GetPluginExecutableName(pluginTemplateName)), false)
if err != nil {
assert.NoError(t, err)
return err
}
if shouldExist {
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")
}
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.")
}
}