-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathauth_workflows_test.go
More file actions
333 lines (272 loc) · 9.3 KB
/
auth_workflows_test.go
File metadata and controls
333 lines (272 loc) · 9.3 KB
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
package cmd
import (
"os"
"runtime"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestAuth_EnvCommand_E2E tests the complete workflow of logging in and
// using the auth env command to retrieve environment variables.
func TestAuth_EnvCommand_E2E(t *testing.T) {
tk := NewTestKit(t)
_ = setupMockAuthDir(t, tk)
// Step 1: Login to cache credentials.
t.Run("login", func(t *testing.T) {
RootCmd.SetArgs([]string{"auth", "login", "--identity=mock-identity"})
err := RootCmd.Execute()
require.NoError(t, err, "Login should succeed")
})
// Step 2: Test auth env with different output formats.
formats := []struct {
name string
format string
expectedOutput []string
}{
{
name: "json format",
format: "json",
expectedOutput: []string{
`"AWS_PROFILE"`,
`"mock-identity"`,
`"AWS_CONFIG_FILE"`,
},
},
{
name: "bash format",
format: "bash",
expectedOutput: []string{
"export AWS_PROFILE=",
"export AWS_CONFIG_FILE=",
"mock-identity",
},
},
{
name: "dotenv format",
format: "dotenv",
expectedOutput: []string{
"AWS_PROFILE=",
"AWS_CONFIG_FILE=",
"mock-identity",
},
},
}
for _, tc := range formats {
t.Run(tc.name, func(t *testing.T) {
_ = NewTestKit(t)
// Capture stdout to verify output.
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
RootCmd.SetArgs([]string{"auth", "env", "--format", tc.format, "--identity=mock-identity"})
start := time.Now()
err := RootCmd.Execute()
duration := time.Since(start)
// Restore stdout.
w.Close()
os.Stdout = oldStdout
// Read captured output.
var buf [4096]byte
n, _ := r.Read(buf[:])
output := string(buf[:n])
require.NoError(t, err, "Auth env should succeed with cached credentials")
// Verify output contains expected content.
for _, expected := range tc.expectedOutput {
assert.Contains(t, output, expected,
"Output should contain %q", expected)
}
// Verify fast execution (using cached credentials).
assert.Less(t, duration, 2*time.Second,
"Auth env with cached credentials should be fast")
t.Logf("auth env --format %s completed in %v", tc.format, duration)
t.Logf("Output:\n%s", output)
})
}
}
// TestAuth_ExecCommand_E2E tests the complete workflow of logging in and
// using the auth exec command to run commands with authenticated environment.
func TestAuth_ExecCommand_E2E(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skipf("Skipping test on Windows: uses Unix commands (echo, sh -c)")
}
tk := NewTestKit(t)
_ = setupMockAuthDir(t, tk)
// Step 1: Login to cache credentials.
t.Run("login", func(t *testing.T) {
RootCmd.SetArgs([]string{"auth", "login", "--identity=mock-identity"})
err := RootCmd.Execute()
require.NoError(t, err, "Login should succeed")
})
// Step 2: Test auth exec with various commands.
testCases := []struct {
name string
command []string
expectedOutput string
}{
{
name: "echo command",
command: []string{"echo", "hello"},
expectedOutput: "hello",
},
{
name: "print env var",
command: []string{"sh", "-c", "echo $AWS_PROFILE"},
expectedOutput: "mock-identity",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_ = NewTestKit(t)
// Build args: auth exec --identity mock-identity -- <command>.
args := []string{"auth", "exec", "--identity=mock-identity", "--"}
args = append(args, tc.command...)
// Capture stdout.
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
RootCmd.SetArgs(args)
start := time.Now()
err := RootCmd.Execute()
duration := time.Since(start)
// Restore stdout.
w.Close()
os.Stdout = oldStdout
// Read captured output.
var buf [4096]byte
n, _ := r.Read(buf[:])
output := string(buf[:n])
require.NoError(t, err, "Auth exec should succeed")
// Verify command output.
assert.Contains(t, output, tc.expectedOutput,
"Command output should contain expected text")
// Verify fast execution.
assert.Less(t, duration, 2*time.Second,
"Auth exec with cached credentials should be fast")
t.Logf("auth exec %v completed in %v", tc.command, duration)
t.Logf("Output:\n%s", output)
})
}
}
// TestAuth_WhoamiCommand_E2E tests the complete workflow of logging in and
// using the auth whoami command to retrieve identity information.
func TestAuth_WhoamiCommand_E2E(t *testing.T) {
tk := NewTestKit(t)
_ = setupMockAuthDir(t, tk)
// Step 1: Login to cache credentials.
t.Run("login", func(t *testing.T) {
RootCmd.SetArgs([]string{"auth", "login", "--identity=mock-identity"})
err := RootCmd.Execute()
require.NoError(t, err, "Login should succeed")
})
// Step 2: Test whoami command.
t.Run("whoami with cached credentials", func(t *testing.T) {
RootCmd.SetArgs([]string{"auth", "whoami", "--identity=mock-identity"})
start := time.Now()
err := RootCmd.Execute()
duration := time.Since(start)
require.NoError(t, err, "Whoami should succeed with cached credentials")
// Verify fast execution.
assert.Less(t, duration, 2*time.Second,
"Whoami with cached credentials should be fast")
t.Logf("auth whoami completed in %v", duration)
})
// Step 3: Test whoami with JSON output.
t.Run("whoami json output", func(t *testing.T) {
// Capture stdout.
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
RootCmd.SetArgs([]string{"auth", "whoami", "--identity=mock-identity", "--output", "json"})
err := RootCmd.Execute()
// Restore stdout.
w.Close()
os.Stdout = oldStdout
// Read captured output.
var buf [4096]byte
n, _ := r.Read(buf[:])
output := string(buf[:n])
require.NoError(t, err, "Whoami with JSON output should succeed")
// Verify JSON output contains expected fields.
assert.Contains(t, output, `"provider"`, "JSON should contain provider field")
assert.Contains(t, output, `"identity"`, "JSON should contain identity field")
assert.Contains(t, output, `"mock-identity"`, "JSON should contain identity name")
assert.Contains(t, output, `"region"`, "JSON should contain region field")
assert.Contains(t, output, `"us-east-1"`, "JSON should contain region value")
t.Logf("JSON output:\n%s", output)
})
}
// TestAuth_ShellCommand_E2E is skipped because 'auth shell' launches an interactive shell
// rather than printing shell export commands. Testing interactive shells requires
// different test infrastructure (PTY simulation).
// The auth env command should be used for generating shell export statements.
func TestAuth_ShellCommand_E2E(t *testing.T) {
t.Skip("auth shell launches interactive shell, not suitable for automated testing")
}
// TestAuth_CompleteWorkflow_E2E tests a complete realistic workflow that
// mimics what a user would do: login, check whoami, get env vars, run command.
func TestAuth_CompleteWorkflow_E2E(t *testing.T) {
tk := NewTestKit(t)
_ = setupMockAuthDir(t, tk)
workflow := []struct {
name string
args []string
}{
{"login", []string{"auth", "login", "--identity=mock-identity"}},
{"whoami", []string{"auth", "whoami", "--identity=mock-identity"}},
{"env", []string{"auth", "env", "--identity=mock-identity"}},
{"list", []string{"auth", "list"}},
{"validate", []string{"auth", "validate"}},
}
totalDuration := time.Duration(0)
for i, step := range workflow {
t.Run(step.name, func(t *testing.T) {
RootCmd.SetArgs(step.args)
start := time.Now()
err := RootCmd.Execute()
duration := time.Since(start)
totalDuration += duration
require.NoError(t, err, "Step %d (%s) should succeed", i+1, step.name)
// After first step (login), subsequent steps should be fast.
if i > 0 {
assert.Less(t, duration, 2*time.Second,
"Step %d (%s) should be fast with cached credentials", i+1, step.name)
}
t.Logf("Step %d: %s completed in %v", i+1, strings.Join(step.args, " "), duration)
})
}
t.Logf("Complete workflow took %v", totalDuration)
// Entire workflow should complete quickly.
assert.Less(t, totalDuration, 10*time.Second,
"Complete workflow should be fast")
}
// TestAuth_MultipleIdentities_E2E tests using multiple commands with a cached identity.
func TestAuth_MultipleIdentities_E2E(t *testing.T) {
tk := NewTestKit(t)
_ = setupMockAuthDir(t, tk)
// Login to identity.
t.Run("login", func(t *testing.T) {
RootCmd.SetArgs([]string{"auth", "login", "--identity=mock-identity"})
err := RootCmd.Execute()
require.NoError(t, err, "Login should succeed")
})
// Verify identity works with whoami.
t.Run("whoami uses cached credentials", func(t *testing.T) {
RootCmd.SetArgs([]string{"auth", "whoami", "--identity=mock-identity"})
start := time.Now()
err := RootCmd.Execute()
duration := time.Since(start)
require.NoError(t, err, "Whoami should succeed with cached credentials")
assert.Less(t, duration, 2*time.Second, "Whoami should be fast with cached credentials")
})
// Verify identity works with env.
t.Run("env uses cached credentials", func(t *testing.T) {
RootCmd.SetArgs([]string{"auth", "env", "--identity=mock-identity"})
start := time.Now()
err := RootCmd.Execute()
duration := time.Since(start)
require.NoError(t, err, "Env should succeed with cached credentials")
assert.Less(t, duration, 2*time.Second, "Env should be fast with cached credentials")
})
}