-
Notifications
You must be signed in to change notification settings - Fork 11
/
cli_test.go
260 lines (223 loc) · 7.01 KB
/
cli_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
package test
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"github.com/google/shlex"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
pauth "github.com/confluentinc/cli/v4/pkg/auth"
"github.com/confluentinc/cli/v4/pkg/config"
"github.com/confluentinc/cli/v4/pkg/utils"
testserver "github.com/confluentinc/cli/v4/test/test-server"
)
var (
update = flag.Bool("update", false, "update golden files")
debug = flag.Bool("debug", true, "enable verbose output")
testBin = "test/bin/confluent"
)
// CLITest represents a test configuration
type CLITest struct {
// Name to show in go test output; defaults to args if not set
name string
// The CLI command being tested; this is a string of args and flags passed to the binary
args string
// The set of environment variables to be set when the CLI is run
env []string
// The login context; either "cloud" or "onprem"
login string
// Optional Cloud URL if test does not use default server
loginURL string
// The kafka cluster ID to "use"
useKafka string
// Create and use an API Key to set as Kafka credentials
authKafka bool
// Name of a golden output fixture containing expected output
fixture string
// True if audit-log is disabled
disableAuditLog bool
// True iff fixture represents a regex
regex bool
// True iff testing plugins
arePluginsEnabled bool
// Fixed string to check if output contains
contains string
// Fixed string to check that output does not contain
notContains string
// Expected exit code (e.g., 0 for success or 1 for failure)
exitCode int
// If true, don't reset the config/state between tests to enable testing CLI workflows
workflow bool
// An optional function that allows you to specify other calls
wantFunc func(t *testing.T)
input string
}
// CLITestSuite is the CLI integration tests.
type CLITestSuite struct {
suite.Suite
TestBackend *testserver.TestBackend
}
// TestCLI runs the CLI integration test suite.
func TestCLI(t *testing.T) {
suite.Run(t, new(CLITestSuite))
}
func (s *CLITestSuite) SetupSuite() {
req := require.New(s.T())
// dumb but effective
err := os.Chdir("..")
req.NoError(err)
target := "build-for-integration-test"
if runtime.GOOS == "windows" {
target += "-windows"
testBin += ".exe"
}
output, err := exec.Command("make", target).CombinedOutput()
req.NoError(err, string(output))
s.TestBackend = testserver.StartTestBackend(s.T(), true) // by default do not disable audit-log
os.Setenv("DISABLE_AUDIT_LOG", "false")
config.SetTempHomeDir()
}
func (s *CLITestSuite) TearDownSuite() {
s.TestBackend.Close()
}
func (s *CLITestSuite) runIntegrationTest(test CLITest) {
if test.name == "" {
test.name = test.args
}
s.T().Run(test.name, func(t *testing.T) {
isAuditLogDisabled := os.Getenv("DISABLE_AUDIT_LOG") == "true"
if isAuditLogDisabled != test.disableAuditLog {
s.TestBackend.Close()
os.Setenv("DISABLE_AUDIT_LOG", strconv.FormatBool(test.disableAuditLog))
s.TestBackend = testserver.StartTestBackend(t, !test.disableAuditLog)
}
if !test.workflow {
resetConfiguration(t, test.arePluginsEnabled)
}
// Executes login command if test specifies
switch test.login {
case "cloud":
loginString := fmt.Sprintf("login --url %s", s.getLoginURL(true, test))
env := append([]string{pauth.ConfluentCloudEmail + "[email protected]", pauth.ConfluentCloudPassword + "=pass1"}, test.env...)
for _, e := range env {
keyVal := strings.Split(e, "=")
os.Setenv(keyVal[0], keyVal[1])
}
defer func() {
for _, e := range env {
keyVal := strings.Split(e, "=")
os.Unsetenv(keyVal[0])
}
}()
output := runCommand(t, testBin, env, loginString, 0, "")
if *debug {
fmt.Println(output)
}
case "onprem":
loginURL := s.getLoginURL(false, test)
env := []string{pauth.ConfluentPlatformUsername + "[email protected]", pauth.ConfluentPlatformPassword + "=pass1"}
output := runCommand(t, testBin, env, "login --url "+loginURL, 0, "")
if *debug {
fmt.Println(output)
}
}
if test.useKafka != "" {
output := runCommand(t, testBin, []string{}, fmt.Sprintf("kafka cluster use %s", test.useKafka), 0, "")
if *debug {
fmt.Println(output)
}
}
if test.authKafka {
output := runCommand(t, testBin, []string{}, fmt.Sprintf("api-key create --resource %s --use", test.useKafka), 0, "")
if *debug {
fmt.Println(output)
}
}
output := runCommand(t, testBin, test.env, test.args, test.exitCode, test.input)
if *debug {
fmt.Println(output)
}
s.validateTestOutput(test, t, output)
})
}
func (s *CLITestSuite) getLoginURL(isCloud bool, test CLITest) string {
if test.loginURL != "" {
return test.loginURL
}
if isCloud {
return s.TestBackend.GetCloudUrl()
} else {
return s.TestBackend.GetMdsUrl()
}
}
func (s *CLITestSuite) validateTestOutput(test CLITest, t *testing.T, output string) {
if *update && !test.regex && test.fixture != "" {
writeFixture(t, test.fixture, output)
}
actual := utils.NormalizeNewLines(output)
if test.contains != "" {
require.Contains(t, actual, test.contains)
} else if test.notContains != "" {
require.NotContains(t, actual, test.notContains)
} else if test.fixture != "" {
expected := utils.NormalizeNewLines(LoadFixture(t, test.fixture))
if test.regex {
require.Regexp(t, expected, actual)
} else {
require.Equal(t, expected, actual)
}
}
if test.wantFunc != nil {
test.wantFunc(t)
}
}
func runCommand(t *testing.T, binaryName string, env []string, argString string, exitCode int, input string) string {
dir, err := os.Getwd()
require.NoError(t, err)
// HACK: google/shlex does not support non-POSIX shell parsing
if runtime.GOOS == "windows" {
argString = strings.ReplaceAll(argString, `\'`, "SINGLE QUOTE")
argString = strings.ReplaceAll(argString, `\"`, "DOUBLE QUOTE")
argString = strings.ReplaceAll(argString, `\`, `\\`)
argString = strings.ReplaceAll(argString, "SINGLE QUOTE", `\'`)
argString = strings.ReplaceAll(argString, "DOUBLE QUOTE", `\"`)
}
args, err := shlex.Split(argString)
require.NoError(t, err)
cmd := exec.Command(filepath.Join(dir, binaryName), args...)
cmd.Env = append(os.Environ(), env...)
cmd.Stdin = strings.NewReader(input)
out, err := cmd.CombinedOutput()
if exitCode == 0 {
require.NoError(t, err, string(out))
}
require.Equal(t, exitCode, cmd.ProcessState.ExitCode(), string(out))
return string(out)
}
func resetConfiguration(t *testing.T, arePluginsEnabled bool) {
// HACK: delete your current config to isolate tests cases for non-workflow tests...
// probably don't really want to do this or devs will get mad
cfg := config.New()
cfg.DisablePlugins = !arePluginsEnabled
cfg.EnableColor = false
err := cfg.Save()
require.NoError(t, err)
}
func writeFixture(t *testing.T, fixture, content string) {
path := fixturePath(t, fixture)
dir := filepath.Dir(path)
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatal(err)
}
}
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
t.Fatal(err)
}
}