|
| 1 | +/* |
| 2 | + * Copyright The Kmesh Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package kmeshctl_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "istio.io/istio/pkg/test/shell" |
| 26 | +) |
| 27 | + |
| 28 | +// TestKmeshctlAuthz verifies the kmeshctl authz (enable/disable/status) against a Kmesh Daemon pod. |
| 29 | +func TestKmeshctlAuthz(t *testing.T) { |
| 30 | + // 1) Grab the first Kmesh daemon pod from the cluster: |
| 31 | + podName, err := getFirstKmeshPod() |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("could not retrieve a kmesh daemon pod name: %v", err) |
| 34 | + } |
| 35 | + t.Logf("Using Kmesh daemon pod: %s", podName) |
| 36 | + |
| 37 | + // 2) Enable Authz on the Kmesh Daemon Pod |
| 38 | + t.Run("enable-authz", func(t *testing.T) { |
| 39 | + cmd := fmt.Sprintf("kmeshctl authz enable %s", podName) |
| 40 | + out, err := shell.Execute(true, cmd) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("failed to enable authz on pod %q: %v\noutput: %s", podName, err, out) |
| 43 | + } |
| 44 | + t.Logf("enable-authz output:\n%s", out) |
| 45 | + }) |
| 46 | + |
| 47 | + // 3) Check that Authz is enabled |
| 48 | + t.Run("verify-authz-enabled", func(t *testing.T) { |
| 49 | + // Wait a moment for Kmesh Daemon to process. |
| 50 | + time.Sleep(2 * time.Second) |
| 51 | + |
| 52 | + cmd := fmt.Sprintf("kmeshctl authz status %s", podName) |
| 53 | + out, err := shell.Execute(true, cmd) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("failed to check authz status: %v\noutput: %s", err, out) |
| 56 | + } |
| 57 | + t.Logf("status output:\n%s", out) |
| 58 | + |
| 59 | + // We assume the status output includes "true" or "enabled" if authz is on. |
| 60 | + if !strings.Contains(out, "true") && !strings.Contains(strings.ToLower(out), "enabled") { |
| 61 | + t.Fatalf("expected authz to be enabled, got: %s", out) |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + // 4) Disable Authz on the Kmesh Daemon Pod |
| 66 | + t.Run("disable-authz", func(t *testing.T) { |
| 67 | + cmd := fmt.Sprintf("kmeshctl authz disable %s", podName) |
| 68 | + out, err := shell.Execute(true, cmd) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("failed to disable authz on pod %q: %v\noutput: %s", podName, err, out) |
| 71 | + } |
| 72 | + t.Logf("disable-authz output:\n%s", out) |
| 73 | + }) |
| 74 | + |
| 75 | + // 5) Check that Authz is disabled |
| 76 | + t.Run("verify-authz-disabled", func(t *testing.T) { |
| 77 | + // Wait a moment for Kmesh Daemon to process. |
| 78 | + time.Sleep(2 * time.Second) |
| 79 | + |
| 80 | + cmd := fmt.Sprintf("kmeshctl authz status %s", podName) |
| 81 | + out, err := shell.Execute(true, cmd) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("failed to check authz status: %v\noutput: %s", err, out) |
| 84 | + } |
| 85 | + t.Logf("status output:\n%s", out) |
| 86 | + |
| 87 | + // We assume the status output includes "false" or "disabled" if authz is off. |
| 88 | + if !strings.Contains(out, "false") && !strings.Contains(strings.ToLower(out), "disabled") { |
| 89 | + t.Fatalf("expected authz to be disabled, got: %s", out) |
| 90 | + } |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +// getFirstKmeshPod uses kubectl to find the first Kmesh Daemon pod (label app=kmesh) in kmesh-system. |
| 95 | +func getFirstKmeshPod() (string, error) { |
| 96 | + cmd := `kubectl get pods -n kmesh-system -l app=kmesh -o jsonpath='{.items[0].metadata.name}'` |
| 97 | + out, err := shell.Execute(true, cmd) |
| 98 | + if err != nil { |
| 99 | + return "", fmt.Errorf("error retrieving kmesh daemon pod name: %v", err) |
| 100 | + } |
| 101 | + trimmed := strings.TrimSpace(out) |
| 102 | + if trimmed == "" { |
| 103 | + return "", fmt.Errorf("no Kmesh daemon pod found in kmesh-system namespace") |
| 104 | + } |
| 105 | + return trimmed, nil |
| 106 | +} |
0 commit comments