|
| 1 | +/* |
| 2 | +Copyright 2025 The Tekton 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 main |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "os" |
| 22 | + "os/signal" |
| 23 | + "syscall" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +func TestParseFlags(t *testing.T) { |
| 29 | + // Save original command line arguments and restore them after the test |
| 30 | + oldArgs := os.Args |
| 31 | + defer func() { os.Args = oldArgs }() |
| 32 | + |
| 33 | + // Save original flagset and restore after test |
| 34 | + oldFlagCommandLine := flag.CommandLine |
| 35 | + defer func() { flag.CommandLine = oldFlagCommandLine }() |
| 36 | + |
| 37 | + testCases := []struct { |
| 38 | + name string |
| 39 | + args []string |
| 40 | + wantResultsDir string |
| 41 | + wantResultNames string |
| 42 | + wantStepResults string |
| 43 | + wantStepNames string |
| 44 | + wantKubernetesSidecarMode bool |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "default values", |
| 48 | + args: []string{"cmd"}, |
| 49 | + wantResultsDir: "/tekton/results", |
| 50 | + wantResultNames: "", |
| 51 | + wantStepResults: "", |
| 52 | + wantStepNames: "", |
| 53 | + wantKubernetesSidecarMode: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "custom values", |
| 57 | + args: []string{"cmd", "-results-dir", "/custom/results", "-result-names", "foo,bar", "-step-results", "{\"step1\":[\"res1\"]}", "-step-names", "step1,step2", "-kubernetes-sidecar-mode", "true"}, |
| 58 | + wantResultsDir: "/custom/results", |
| 59 | + wantResultNames: "foo,bar", |
| 60 | + wantStepResults: "{\"step1\":[\"res1\"]}", |
| 61 | + wantStepNames: "step1,step2", |
| 62 | + wantKubernetesSidecarMode: true, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for _, tc := range testCases { |
| 67 | + t.Run(tc.name, func(t *testing.T) { |
| 68 | + // Reset flag.CommandLine to simulate fresh flag parsing |
| 69 | + flag.CommandLine = flag.NewFlagSet(tc.args[0], flag.ExitOnError) |
| 70 | + |
| 71 | + // Set up the test arguments |
| 72 | + os.Args = tc.args |
| 73 | + |
| 74 | + // Define the variables that would be set by flag.Parse() |
| 75 | + var resultsDir string |
| 76 | + var resultNames string |
| 77 | + var stepResultsStr string |
| 78 | + var stepNames string |
| 79 | + var kubernetesNativeSidecar bool |
| 80 | + |
| 81 | + // Define the flags |
| 82 | + flag.StringVar(&resultsDir, "results-dir", "/tekton/results", "Path to the results directory") |
| 83 | + flag.StringVar(&resultNames, "result-names", "", "comma separated result names") |
| 84 | + flag.StringVar(&stepResultsStr, "step-results", "", "json containing map of step name to results") |
| 85 | + flag.StringVar(&stepNames, "step-names", "", "comma separated step names") |
| 86 | + flag.BoolVar(&kubernetesNativeSidecar, "kubernetes-sidecar-mode", false, "If true, run in Kubernetes native sidecar mode") |
| 87 | + |
| 88 | + // Parse the flags |
| 89 | + flag.Parse() |
| 90 | + |
| 91 | + // Check the results |
| 92 | + if resultsDir != tc.wantResultsDir { |
| 93 | + t.Errorf("resultsDir = %q, want %q", resultsDir, tc.wantResultsDir) |
| 94 | + } |
| 95 | + if resultNames != tc.wantResultNames { |
| 96 | + t.Errorf("resultNames = %q, want %q", resultNames, tc.wantResultNames) |
| 97 | + } |
| 98 | + if stepResultsStr != tc.wantStepResults { |
| 99 | + t.Errorf("stepResultsStr = %q, want %q", stepResultsStr, tc.wantStepResults) |
| 100 | + } |
| 101 | + if stepNames != tc.wantStepNames { |
| 102 | + t.Errorf("stepNames = %q, want %q", stepNames, tc.wantStepNames) |
| 103 | + } |
| 104 | + if kubernetesNativeSidecar != tc.wantKubernetesSidecarMode { |
| 105 | + t.Errorf("kubernetesNativeSidecar = %v, want %v", kubernetesNativeSidecar, tc.wantKubernetesSidecarMode) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// This test is a bit tricky since it involves an infinite loop when kubernetesNativeSidecar is true. |
| 112 | +// We'll use a timeout mechanism to verify the behavior. |
| 113 | +func TestKubernetesSidecarMode(t *testing.T) { |
| 114 | + // Create a channel to signal completion |
| 115 | + done := make(chan bool) |
| 116 | + |
| 117 | + // Start a goroutine that simulates the kubernetes sidecar mode behavior |
| 118 | + go func() { |
| 119 | + // Simulate the kubernetes sidecar mode behavior |
| 120 | + if true { |
| 121 | + // In the real code, this would be an infinite select{} loop |
| 122 | + // For testing, we'll just signal that we've reached this point |
| 123 | + done <- true |
| 124 | + // Then wait to simulate the infinite loop |
| 125 | + time.Sleep(100 * time.Millisecond) |
| 126 | + } |
| 127 | + // This should not be reached when kubernetesNativeSidecar is true |
| 128 | + done <- false |
| 129 | + }() |
| 130 | + |
| 131 | + // Wait for the goroutine to signal or timeout |
| 132 | + select { |
| 133 | + case reached := <-done: |
| 134 | + if !reached { |
| 135 | + t.Error("kubernetes sidecar mode code path was not executed correctly") |
| 136 | + } |
| 137 | + case <-time.After(50 * time.Millisecond): |
| 138 | + t.Error("Timed out waiting for kubernetes sidecar mode code path") |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// TestSignalHandling tests that the signal handling works correctly |
| 143 | +func TestSignalHandling(t *testing.T) { |
| 144 | + // Create channels for test coordination |
| 145 | + setupDone := make(chan bool) |
| 146 | + signalProcessed := make(chan bool) |
| 147 | + |
| 148 | + // Start a goroutine that simulates the signal handling behavior |
| 149 | + go func() { |
| 150 | + // Set up signal handling |
| 151 | + sigCh := make(chan os.Signal, 1) |
| 152 | + signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT) |
| 153 | + |
| 154 | + // Signal that setup is complete |
| 155 | + setupDone <- true |
| 156 | + |
| 157 | + // Wait for signal |
| 158 | + sig := <-sigCh |
| 159 | + |
| 160 | + // Verify we got the expected signal |
| 161 | + if sig == syscall.SIGTERM { |
| 162 | + signalProcessed <- true |
| 163 | + } else { |
| 164 | + signalProcessed <- false |
| 165 | + } |
| 166 | + }() |
| 167 | + |
| 168 | + // Wait for signal handling setup to complete |
| 169 | + select { |
| 170 | + case <-setupDone: |
| 171 | + // Setup completed successfully |
| 172 | + case <-time.After(100 * time.Millisecond): |
| 173 | + t.Fatal("Timed out waiting for signal handler setup") |
| 174 | + } |
| 175 | + |
| 176 | + // Send a SIGTERM signal to the process |
| 177 | + // Note: In a real test environment, we'd use a process.Signal() call |
| 178 | + // but for this test we'll directly send to the channel |
| 179 | + p, err := os.FindProcess(os.Getpid()) |
| 180 | + if err != nil { |
| 181 | + t.Fatalf("Failed to find process: %v", err) |
| 182 | + } |
| 183 | + |
| 184 | + // Send SIGTERM to the process |
| 185 | + err = p.Signal(syscall.SIGTERM) |
| 186 | + if err != nil { |
| 187 | + t.Fatalf("Failed to send signal: %v", err) |
| 188 | + } |
| 189 | + |
| 190 | + // Wait for signal to be processed or timeout |
| 191 | + select { |
| 192 | + case success := <-signalProcessed: |
| 193 | + if !success { |
| 194 | + t.Error("Signal handler received unexpected signal type") |
| 195 | + } |
| 196 | + case <-time.After(100 * time.Millisecond): |
| 197 | + t.Error("Timed out waiting for signal to be processed") |
| 198 | + } |
| 199 | +} |
0 commit comments