-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration_test.go
More file actions
1033 lines (893 loc) · 31.5 KB
/
integration_test.go
File metadata and controls
1033 lines (893 loc) · 31.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build integration
// Integration tests for kubectl-readonly using kind
//
// These tests verify the actual binary behavior against a real Kubernetes cluster.
// They test both invocation modes:
// 1. Direct: kubectl-readonly get pods
// 2. Plugin: kubectl readonly get pods
//
// Prerequisites:
// - kind cluster running (the test will create one if needed)
// - kubectl installed
// - kubectl-readonly binary built (make build)
//
// Run with: make test-integration
// Or manually: go test -tags=integration -v ./...
package main
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
const (
kindClusterName = "kubectl-readonly-test"
testNamespace = "readonly-test"
)
// testEnv holds the test environment
type testEnv struct {
binaryPath string
pluginDir string
clusterExists bool
t *testing.T
}
// setupTestEnv prepares the test environment with a kind cluster
func setupTestEnv(t *testing.T) *testEnv {
t.Helper()
env := &testEnv{t: t}
// Find the binary
binaryPath := "./kubectl-readonly"
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
if path, err := exec.LookPath("kubectl-readonly"); err == nil {
binaryPath = path
} else {
t.Fatal("kubectl-readonly binary not found. Run 'make build' first.")
}
}
absPath, err := filepath.Abs(binaryPath)
if err != nil {
t.Fatalf("Failed to get absolute path: %v", err)
}
env.binaryPath = absPath
// Create plugin directory with symlink
// Note: We use os.MkdirTemp instead of t.TempDir() because globalEnv is shared
// across all tests. t.TempDir() would clean up the directory when the first
// test that created it finishes, breaking subsequent plugin tests.
pluginDir, err := os.MkdirTemp("", "kubectl-readonly-test-*")
if err != nil {
t.Fatalf("Failed to create plugin directory: %v", err)
}
env.pluginDir = pluginDir
pluginPath := filepath.Join(env.pluginDir, "kubectl-readonly")
if err := os.Symlink(absPath, pluginPath); err != nil {
t.Fatalf("Failed to create plugin symlink: %v", err)
}
// Check if kind cluster exists
env.clusterExists = env.kindClusterExists()
return env
}
// kindClusterExists checks if our test cluster is running
func (e *testEnv) kindClusterExists() bool {
cmd := exec.Command("kind", "get", "clusters")
out, err := cmd.Output()
if err != nil {
return false
}
for _, line := range strings.Split(string(out), "\n") {
if strings.TrimSpace(line) == kindClusterName {
return true
}
}
return false
}
// ensureCluster creates a kind cluster if it doesn't exist
func (e *testEnv) ensureCluster() {
e.t.Helper()
if e.clusterExists {
e.t.Logf("Using existing kind cluster: %s", kindClusterName)
return
}
e.t.Logf("Creating kind cluster: %s", kindClusterName)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, "kind", "create", "cluster", "--name", kindClusterName, "--wait", "60s")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
e.t.Fatalf("Failed to create kind cluster: %v", err)
}
e.clusterExists = true
}
// setupTestResources creates test resources in the cluster
func (e *testEnv) setupTestResources() {
e.t.Helper()
// Create test namespace
e.kubectl("create", "namespace", testNamespace, "--dry-run=client", "-o", "yaml")
e.kubectl("apply", "-f", "-", "--input", fmt.Sprintf(`
apiVersion: v1
kind: Namespace
metadata:
name: %s
`, testNamespace))
// Create a test pod that stays running (using sleep to avoid nginx completing)
e.kubectl("apply", "-f", "-", "--input", fmt.Sprintf(`
apiVersion: v1
kind: Pod
metadata:
name: test-pod
namespace: %s
labels:
app: test
spec:
containers:
- name: busybox
image: busybox:stable
command: ["sleep", "3600"]
restartPolicy: Always
`, testNamespace))
// Create a test secret
e.kubectl("apply", "-f", "-", "--input", fmt.Sprintf(`
apiVersion: v1
kind: Secret
metadata:
name: test-secret
namespace: %s
type: Opaque
data:
username: YWRtaW4=
password: c2VjcmV0cGFzc3dvcmQ=
`, testNamespace))
// Create a test configmap
e.kubectl("apply", "-f", "-", "--input", fmt.Sprintf(`
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
namespace: %s
data:
config.yaml: |
key: value
`, testNamespace))
// Create a test deployment
e.kubectl("apply", "-f", "-", "--input", fmt.Sprintf(`
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
namespace: %s
spec:
replicas: 1
selector:
matchLabels:
app: test-deployment
template:
metadata:
labels:
app: test-deployment
spec:
containers:
- name: nginx
image: nginx:alpine
`, testNamespace))
// Wait for test pod to be ready
e.t.Log("Waiting for test pod to be ready...")
if !e.waitForPodRunning("test-pod", testNamespace, 60*time.Second) {
e.t.Log("Warning: test-pod did not reach Running state within timeout")
}
}
// kubectl runs a kubectl command
func (e *testEnv) kubectl(args ...string) (string, error) {
// Handle --input flag for stdin
var input string
filteredArgs := make([]string, 0, len(args))
for i := 0; i < len(args); i++ {
if args[i] == "--input" && i+1 < len(args) {
input = args[i+1]
i++
} else {
filteredArgs = append(filteredArgs, args[i])
}
}
cmd := exec.Command("kubectl", filteredArgs...)
if input != "" {
cmd.Stdin = strings.NewReader(input)
}
out, err := cmd.CombinedOutput()
return string(out), err
}
// runDirect runs kubectl-readonly directly
func (e *testEnv) runDirect(args ...string) (stdout, stderr string, exitCode int) {
e.t.Helper()
cmd := exec.Command(e.binaryPath, args...)
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err := cmd.Run()
exitCode = 0
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode = exitErr.ExitCode()
}
}
return outBuf.String(), errBuf.String(), exitCode
}
// runPlugin runs kubectl readonly (plugin mode)
func (e *testEnv) runPlugin(args ...string) (stdout, stderr string, exitCode int) {
e.t.Helper()
kubectlPath, err := exec.LookPath("kubectl")
if err != nil {
e.t.Skip("kubectl not found")
}
// Add plugin dir to PATH
env := os.Environ()
for i, v := range env {
if strings.HasPrefix(v, "PATH=") {
env[i] = "PATH=" + e.pluginDir + ":" + v[5:]
break
}
}
fullArgs := append([]string{"readonly"}, args...)
cmd := exec.Command(kubectlPath, fullArgs...)
cmd.Env = env
var outBuf, errBuf bytes.Buffer
cmd.Stdout = &outBuf
cmd.Stderr = &errBuf
err = cmd.Run()
exitCode = 0
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
exitCode = exitErr.ExitCode()
} else {
exitCode = -1
}
}
return outBuf.String(), errBuf.String(), exitCode
}
// waitForPodRunning waits for a pod to be in Running state
func (e *testEnv) waitForPodRunning(podName, namespace string, timeout time.Duration) bool {
e.t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
stdout, _, _ := e.runDirect("get", "pod", podName, "-n", namespace, "-o", "jsonpath={.status.phase}")
if strings.TrimSpace(stdout) == "Running" {
return true
}
time.Sleep(500 * time.Millisecond)
}
return false
}
// waitForPodExists waits for a pod to exist (any state)
func (e *testEnv) waitForPodExists(podName, namespace string, timeout time.Duration) bool {
e.t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
stdout, _, exitCode := e.runDirect("get", "pod", podName, "-n", namespace)
if exitCode == 0 && strings.Contains(stdout, podName) {
return true
}
time.Sleep(500 * time.Millisecond)
}
return false
}
// waitForPodGone waits for a pod to be deleted or in Terminating state
func (e *testEnv) waitForPodGone(podName, namespace string, timeout time.Duration) bool {
e.t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
stdout, _, exitCode := e.runDirect("get", "pod", podName, "-n", namespace)
// Pod is gone (not found)
if exitCode != 0 || !strings.Contains(stdout, podName) {
return true
}
// Pod is terminating
if strings.Contains(stdout, "Terminating") {
return true
}
time.Sleep(500 * time.Millisecond)
}
return false
}
// cleanupTestResources removes test resources
func (e *testEnv) cleanupTestResources() {
e.t.Helper()
e.kubectl("delete", "namespace", testNamespace, "--ignore-not-found")
}
// =============================================================================
// Test Setup - Run once for all integration tests
// =============================================================================
var globalEnv *testEnv
func TestMain(m *testing.M) {
// This only runs when integration tag is set
code := m.Run()
// Cleanup the plugin directory if it was created
if globalEnv != nil && globalEnv.pluginDir != "" {
os.RemoveAll(globalEnv.pluginDir)
}
os.Exit(code)
}
func getTestEnv(t *testing.T) *testEnv {
t.Helper()
if globalEnv == nil {
globalEnv = setupTestEnv(t)
globalEnv.ensureCluster()
globalEnv.setupTestResources()
// Cleanup will happen when the cluster is deleted or manually
}
globalEnv.t = t
return globalEnv
}
// =============================================================================
// Direct Mode Tests - kubectl-readonly
// =============================================================================
func TestIntegration_Direct_GetPods(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runDirect("get", "pods", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "test-pod") {
t.Errorf("Expected output to contain 'test-pod', got: %s", stdout)
}
}
func TestIntegration_Direct_GetPodsAllNamespaces(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runDirect("get", "pods", "-A")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, testNamespace) {
t.Errorf("Expected output to contain namespace '%s', got: %s", testNamespace, stdout)
}
}
func TestIntegration_Direct_DescribePod(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runDirect("describe", "pod", "test-pod", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "test-pod") {
t.Errorf("Expected output to contain 'test-pod', got: %s", stdout)
}
if !strings.Contains(stdout, "busybox") {
t.Errorf("Expected output to contain 'busybox', got: %s", stdout)
}
}
func TestIntegration_Direct_GetSecretMetadata(t *testing.T) {
env := getTestEnv(t)
// Should be able to list secrets (metadata only)
stdout, stderr, exitCode := env.runDirect("get", "secrets", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "test-secret") {
t.Errorf("Expected output to contain 'test-secret', got: %s", stdout)
}
// Should NOT contain the actual secret values
if strings.Contains(stdout, "secretpassword") || strings.Contains(stdout, "c2VjcmV0cGFzc3dvcmQ") {
t.Errorf("Output should not contain secret values: %s", stdout)
}
}
func TestIntegration_Direct_GetSecretValuesBlocked(t *testing.T) {
env := getTestEnv(t)
// Should be blocked when trying to get secret values
_, stderr, exitCode := env.runDirect("get", "secret", "test-secret", "-n", testNamespace, "-o", "yaml")
if exitCode == 0 {
t.Error("Expected non-zero exit code for secret value access")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error message about read-only, got: %s", stderr)
}
}
func TestIntegration_Direct_GetSecretJsonBlocked(t *testing.T) {
env := getTestEnv(t)
_, stderr, exitCode := env.runDirect("get", "secret", "test-secret", "-n", testNamespace, "-o", "json")
if exitCode == 0 {
t.Error("Expected non-zero exit code for secret JSON access")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error message about read-only, got: %s", stderr)
}
}
func TestIntegration_Direct_DescribeSecret(t *testing.T) {
env := getTestEnv(t)
// Describe should work (shows metadata, not values)
stdout, stderr, exitCode := env.runDirect("describe", "secret", "test-secret", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "test-secret") {
t.Errorf("Expected output to contain 'test-secret', got: %s", stdout)
}
// Describe shows byte counts, not actual values
if strings.Contains(stdout, "secretpassword") {
t.Errorf("Output should not contain actual secret values: %s", stdout)
}
}
func TestIntegration_Direct_GetConfigMap(t *testing.T) {
env := getTestEnv(t)
// ConfigMaps should be fully accessible (not sensitive)
stdout, stderr, exitCode := env.runDirect("get", "configmap", "test-configmap", "-n", testNamespace, "-o", "yaml")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "key: value") {
t.Errorf("Expected output to contain configmap data, got: %s", stdout)
}
}
func TestIntegration_Direct_GetDeployment(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runDirect("get", "deployment", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "test-deployment") {
t.Errorf("Expected output to contain 'test-deployment', got: %s", stdout)
}
}
func TestIntegration_Direct_DeleteBlocked(t *testing.T) {
env := getTestEnv(t)
_, stderr, exitCode := env.runDirect("delete", "pod", "test-pod", "-n", testNamespace)
if exitCode == 0 {
t.Error("Expected non-zero exit code for delete command")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error about read-only, got: %s", stderr)
}
// Verify pod still exists
stdout, _, _ := env.runDirect("get", "pod", "test-pod", "-n", testNamespace)
if !strings.Contains(stdout, "test-pod") {
t.Error("Pod should still exist after blocked delete")
}
}
func TestIntegration_Direct_ApplyBlocked(t *testing.T) {
env := getTestEnv(t)
_, stderr, exitCode := env.runDirect("apply", "-f", "-")
if exitCode == 0 {
t.Error("Expected non-zero exit code for apply command")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error about read-only, got: %s", stderr)
}
}
func TestIntegration_Direct_ExecBlocked(t *testing.T) {
env := getTestEnv(t)
_, stderr, exitCode := env.runDirect("exec", "test-pod", "-n", testNamespace, "--", "ls")
if exitCode == 0 {
t.Error("Expected non-zero exit code for exec command")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error about read-only, got: %s", stderr)
}
}
func TestIntegration_Direct_Version(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runDirect("version", "--client")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "Client Version") {
t.Errorf("Expected 'Client Version' in output, got: %s", stdout)
}
}
func TestIntegration_Direct_GetNamespaces(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runDirect("get", "namespaces")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "default") {
t.Errorf("Expected 'default' namespace, got: %s", stdout)
}
if !strings.Contains(stdout, testNamespace) {
t.Errorf("Expected '%s' namespace, got: %s", testNamespace, stdout)
}
}
func TestIntegration_Direct_ConfigGetContexts(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runDirect("config", "get-contexts")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, kindClusterName) {
t.Errorf("Expected context '%s', got: %s", kindClusterName, stdout)
}
}
func TestIntegration_Direct_RolloutStatusBlocked(t *testing.T) {
env := getTestEnv(t)
// rollout restart should be blocked
_, stderr, exitCode := env.runDirect("rollout", "restart", "deployment/test-deployment", "-n", testNamespace)
if exitCode == 0 {
t.Error("Expected non-zero exit code for rollout restart")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error about read-only, got: %s", stderr)
}
}
func TestIntegration_Direct_RolloutStatusAllowed(t *testing.T) {
env := getTestEnv(t)
// rollout status should be allowed
stdout, stderr, exitCode := env.runDirect("rollout", "status", "deployment/test-deployment", "-n", testNamespace, "--timeout=10s")
// May return non-zero if deployment isn't ready, but should execute
if exitCode != 0 && strings.Contains(stderr, "not safe for read-only") {
t.Errorf("rollout status should be allowed, got: %s", stderr)
}
_ = stdout // Status output varies
}
// =============================================================================
// Plugin Mode Tests - kubectl readonly
// =============================================================================
func TestIntegration_Plugin_GetPods(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runPlugin("get", "pods", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "test-pod") {
t.Errorf("Expected output to contain 'test-pod', got: %s", stdout)
}
}
func TestIntegration_Plugin_GetPodsAllNamespaces(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runPlugin("get", "pods", "-A")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, testNamespace) {
t.Errorf("Expected output to contain namespace '%s', got: %s", testNamespace, stdout)
}
}
func TestIntegration_Plugin_DescribePod(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runPlugin("describe", "pod", "test-pod", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "test-pod") {
t.Errorf("Expected output to contain 'test-pod', got: %s", stdout)
}
}
func TestIntegration_Plugin_GetSecretMetadata(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runPlugin("get", "secrets", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "test-secret") {
t.Errorf("Expected output to contain 'test-secret', got: %s", stdout)
}
}
func TestIntegration_Plugin_GetSecretValuesBlocked(t *testing.T) {
env := getTestEnv(t)
_, stderr, exitCode := env.runPlugin("get", "secret", "test-secret", "-n", testNamespace, "-o", "yaml")
if exitCode == 0 {
t.Error("Expected non-zero exit code for secret value access")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error message about read-only, got: %s", stderr)
}
}
func TestIntegration_Plugin_DeleteBlocked(t *testing.T) {
env := getTestEnv(t)
_, stderr, exitCode := env.runPlugin("delete", "pod", "test-pod", "-n", testNamespace)
if exitCode == 0 {
t.Error("Expected non-zero exit code for delete command")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error about read-only, got: %s", stderr)
}
}
func TestIntegration_Plugin_ExecBlocked(t *testing.T) {
env := getTestEnv(t)
_, stderr, exitCode := env.runPlugin("exec", "test-pod", "-n", testNamespace, "--", "ls")
if exitCode == 0 {
t.Error("Expected non-zero exit code for exec command")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected error about read-only, got: %s", stderr)
}
}
func TestIntegration_Plugin_Version(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runPlugin("version", "--client")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "Client Version") {
t.Errorf("Expected 'Client Version' in output, got: %s", stdout)
}
}
func TestIntegration_Plugin_GetNamespaces(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runPlugin("get", "namespaces")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, "default") {
t.Errorf("Expected 'default' namespace, got: %s", stdout)
}
}
func TestIntegration_Plugin_ConfigGetContexts(t *testing.T) {
env := getTestEnv(t)
stdout, stderr, exitCode := env.runPlugin("config", "get-contexts")
if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d. Stderr: %s", exitCode, stderr)
}
if !strings.Contains(stdout, kindClusterName) {
t.Errorf("Expected context '%s', got: %s", kindClusterName, stdout)
}
}
// =============================================================================
// Consistency Tests - Both modes should behave identically
// =============================================================================
func TestIntegration_Consistency_SameResults(t *testing.T) {
env := getTestEnv(t)
testCases := []struct {
name string
args []string
}{
{"get_pods", []string{"get", "pods", "-n", testNamespace}},
{"get_secrets", []string{"get", "secrets", "-n", testNamespace}},
{"get_namespaces", []string{"get", "namespaces"}},
{"describe_pod", []string{"describe", "pod", "test-pod", "-n", testNamespace}},
{"version", []string{"version", "--client"}},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
directOut, _, directExit := env.runDirect(tc.args...)
pluginOut, _, pluginExit := env.runPlugin(tc.args...)
if directExit != pluginExit {
t.Errorf("Exit codes differ: direct=%d, plugin=%d", directExit, pluginExit)
}
// Outputs should be identical (or very similar)
if directOut != pluginOut {
// Allow minor differences but flag significant ones
if len(directOut) == 0 || len(pluginOut) == 0 {
t.Errorf("One mode returned empty output:\nDirect: %s\nPlugin: %s", directOut, pluginOut)
}
}
})
}
}
func TestIntegration_Consistency_SameBlocking(t *testing.T) {
env := getTestEnv(t)
blockedCommands := []struct {
name string
args []string
}{
{"delete", []string{"delete", "pod", "test-pod", "-n", testNamespace}},
{"apply", []string{"apply", "-f", "-"}},
{"exec", []string{"exec", "test-pod", "-n", testNamespace, "--", "ls"}},
{"secret_yaml", []string{"get", "secret", "test-secret", "-n", testNamespace, "-o", "yaml"}},
{"secret_json", []string{"get", "secret", "test-secret", "-n", testNamespace, "-o", "json"}},
}
for _, tc := range blockedCommands {
t.Run(tc.name, func(t *testing.T) {
_, directErr, directExit := env.runDirect(tc.args...)
_, pluginErr, pluginExit := env.runPlugin(tc.args...)
// Both should be blocked (non-zero exit)
if directExit == 0 {
t.Errorf("Direct mode should block %s", tc.name)
}
if pluginExit == 0 {
t.Errorf("Plugin mode should block %s", tc.name)
}
// Both should have similar error messages
if !strings.Contains(directErr, "not safe for read-only") {
t.Errorf("Direct mode error message unexpected: %s", directErr)
}
if !strings.Contains(pluginErr, "not safe for read-only") {
t.Errorf("Plugin mode error message unexpected: %s", pluginErr)
}
})
}
}
// =============================================================================
// Special Flags Tests
// =============================================================================
func TestIntegration_CheckFlag_Direct(t *testing.T) {
env := getTestEnv(t)
// Allowed command
stdout, _, exitCode := env.runDirect("--readonly-check-ok", "get", "pods")
if exitCode != 0 {
t.Errorf("Expected exit code 0 for allowed command check")
}
if !strings.Contains(stdout, "OK") {
t.Errorf("Expected 'OK' in output, got: %s", stdout)
}
// Blocked command
stdout, _, exitCode = env.runDirect("--readonly-check-ok", "delete", "pods")
if exitCode == 0 {
t.Errorf("Expected non-zero exit code for blocked command check")
}
if !strings.Contains(stdout, "BLOCKED") {
t.Errorf("Expected 'BLOCKED' in output, got: %s", stdout)
}
}
func TestIntegration_CheckFlag_Plugin(t *testing.T) {
env := getTestEnv(t)
// Allowed command
stdout, _, exitCode := env.runPlugin("--readonly-check-ok", "get", "pods")
if exitCode != 0 {
t.Errorf("Expected exit code 0 for allowed command check")
}
if !strings.Contains(stdout, "OK") {
t.Errorf("Expected 'OK' in output, got: %s", stdout)
}
// Blocked command
stdout, _, exitCode = env.runPlugin("--readonly-check-ok", "delete", "pods")
if exitCode == 0 {
t.Errorf("Expected non-zero exit code for blocked command check")
}
if !strings.Contains(stdout, "BLOCKED") {
t.Errorf("Expected 'BLOCKED' in output, got: %s", stdout)
}
}
func TestIntegration_VersionFlag(t *testing.T) {
env := getTestEnv(t)
// Direct mode
stdout, _, exitCode := env.runDirect("--readonly-version")
if exitCode != 0 {
t.Error("Expected exit code 0 for version flag")
}
if !strings.Contains(stdout, "kubectl-readonly") {
t.Errorf("Expected 'kubectl-readonly' in version output, got: %s", stdout)
}
// Plugin mode
stdout, _, exitCode = env.runPlugin("--readonly-version")
if exitCode != 0 {
t.Error("Expected exit code 0 for version flag in plugin mode")
}
if !strings.Contains(stdout, "kubectl-readonly") {
t.Errorf("Expected 'kubectl-readonly' in version output, got: %s", stdout)
}
}
// =============================================================================
// End-to-End Tests - Full scenarios proving readonly actually blocks
// =============================================================================
// TestIntegration_E2E_DeleteBlocked_ThenKubectlWorks proves that:
// 1. Create a pod with kubectl (works)
// 2. Try to delete with kubectl-readonly (blocked)
// 3. Try to delete with kubectl readonly plugin (blocked)
// 4. Pod still exists
// 5. Delete with kubectl directly (works)
// 6. Pod is gone
func TestIntegration_E2E_DeleteBlocked_ThenKubectlWorks(t *testing.T) {
env := getTestEnv(t)
podName := "e2e-delete-test"
// 1. Create a pod with kubectl (using sleep to keep it running)
_, err := env.kubectl("apply", "-f", "-", "--input", fmt.Sprintf(`
apiVersion: v1
kind: Pod
metadata:
name: %s
namespace: %s
spec:
containers:
- name: busybox
image: busybox:stable
command: ["sleep", "3600"]
restartPolicy: Always
`, podName, testNamespace))
if err != nil {
t.Fatalf("Failed to create test pod: %v", err)
}
// Wait for pod to exist and be running
if !env.waitForPodExists(podName, testNamespace, 30*time.Second) {
t.Fatalf("Pod should exist after creation")
}
// Verify pod exists
stdout, _, exitCode := env.runDirect("get", "pod", podName, "-n", testNamespace)
if exitCode != 0 || !strings.Contains(stdout, podName) {
t.Fatalf("Pod should exist after creation, got: %s", stdout)
}
// 2. Try to delete with kubectl-readonly direct mode (should be blocked)
_, stderr, exitCode := env.runDirect("delete", "pod", podName, "-n", testNamespace)
if exitCode == 0 {
t.Error("kubectl-readonly should have blocked delete")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected readonly error, got: %s", stderr)
}
// 3. Try to delete with kubectl readonly plugin mode (should be blocked)
_, stderr, exitCode = env.runPlugin("delete", "pod", podName, "-n", testNamespace)
if exitCode == 0 {
t.Error("kubectl readonly plugin should have blocked delete")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected readonly error in plugin mode, got: %s", stderr)
}
// 4. Verify pod STILL exists (wasn't deleted)
stdout, _, exitCode = env.runDirect("get", "pod", podName, "-n", testNamespace)
if exitCode != 0 || !strings.Contains(stdout, podName) {
t.Error("Pod should still exist after blocked delete attempts")
}
// 5. Delete with kubectl directly (should work)
_, err = env.kubectl("delete", "pod", podName, "-n", testNamespace, "--wait=false")
if err != nil {
t.Errorf("kubectl delete should have worked: %v", err)
}
// 6. Verify pod is gone (or terminating)
if !env.waitForPodGone(podName, testNamespace, 30*time.Second) {
t.Errorf("Pod should be deleted or terminating within timeout")
}
}
// TestIntegration_E2E_ExecBlocked_ThenKubectlWorks proves that:
// 1. kubectl-readonly exec is blocked
// 2. kubectl exec actually works (proving it's not a permission issue)
func TestIntegration_E2E_ExecBlocked_ThenKubectlWorks(t *testing.T) {
env := getTestEnv(t)
// Wait for test-pod to be ready
if !env.waitForPodRunning("test-pod", testNamespace, 60*time.Second) {
t.Skip("test-pod did not reach Running state, skipping exec test")
}
// 1. Try exec with kubectl-readonly (should be blocked)
_, stderr, exitCode := env.runDirect("exec", "test-pod", "-n", testNamespace, "--", "echo", "hello")
if exitCode == 0 {
t.Error("kubectl-readonly should have blocked exec")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected readonly error, got: %s", stderr)
}
// 2. Try exec with kubectl readonly plugin (should be blocked)
_, stderr, exitCode = env.runPlugin("exec", "test-pod", "-n", testNamespace, "--", "echo", "hello")
if exitCode == 0 {
t.Error("kubectl readonly plugin should have blocked exec")
}
if !strings.Contains(stderr, "not safe for read-only") {
t.Errorf("Expected readonly error in plugin mode, got: %s", stderr)
}
// 3. kubectl exec directly should work (proves it's not RBAC blocking us)
stdout, err := env.kubectl("exec", "test-pod", "-n", testNamespace, "--", "echo", "hello")
if err != nil {
// Pod might not be ready, skip this check
t.Logf("kubectl exec failed (pod might not be ready): %v", err)
} else if !strings.Contains(stdout, "hello") {
t.Errorf("kubectl exec should have worked and returned 'hello', got: %s", stdout)
}
}
// TestIntegration_E2E_SecretValuesBlocked proves that:
// 1. kubectl-readonly get secrets (metadata) works
// 2. kubectl-readonly get secrets -o yaml is blocked
// 3. kubectl get secrets -o yaml works and shows actual values
func TestIntegration_E2E_SecretValuesBlocked(t *testing.T) {
env := getTestEnv(t)
// 1. Get secret metadata with kubectl-readonly (should work)
stdout, stderr, exitCode := env.runDirect("get", "secret", "test-secret", "-n", testNamespace)
if exitCode != 0 {
t.Errorf("Should be able to get secret metadata: %s", stderr)
}