|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + fn "knative.dev/func/pkg/functions" |
| 10 | + "knative.dev/func/pkg/mock" |
| 11 | + . "knative.dev/func/pkg/testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestBuild_RegistryInsecurePersists(t *testing.T) { |
| 15 | + testRegistryInsecurePersists(NewBuildCmd, t) |
| 16 | +} |
| 17 | + |
| 18 | +// testRegistryInsecurePersists ensures that the registryInsecure flag |
| 19 | +// value is persisted to func.yaml and remembered across consecutive runs. |
| 20 | +// See issue https://github.com/knative/func/issues/3489 |
| 21 | +func testRegistryInsecurePersists(cmdFn func(factory ClientFactory) *cobra.Command, t *testing.T) { |
| 22 | + root := FromTempDirectory(t) |
| 23 | + |
| 24 | + // Initialize a new function without registryInsecure set |
| 25 | + f := fn.Function{ |
| 26 | + Root: root, |
| 27 | + Name: "myfunc", |
| 28 | + Runtime: "go", |
| 29 | + Registry: "example.com/alice", |
| 30 | + } |
| 31 | + if _, err := fn.New().Init(f); err != nil { |
| 32 | + t.Fatal(err) |
| 33 | + } |
| 34 | + |
| 35 | + var ( |
| 36 | + builder = mock.NewBuilder() |
| 37 | + pusher = mock.NewPusher() |
| 38 | + ) |
| 39 | + |
| 40 | + // Test 1: Initial state - registryInsecure should be false |
| 41 | + t.Run("initial state is false", func(t *testing.T) { |
| 42 | + f, err := fn.NewFunction(root) |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + |
| 47 | + if f.RegistryInsecure { |
| 48 | + t.Fatal("initial registryInsecure should be false") |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + // Test 2: Set registryInsecure to true with flag |
| 53 | + t.Run("sets to true when flag passed", func(t *testing.T) { |
| 54 | + cmd := cmdFn(NewTestClient( |
| 55 | + fn.WithRegistry(TestRegistry), |
| 56 | + fn.WithBuilder(builder), |
| 57 | + fn.WithPusher(pusher), |
| 58 | + )) |
| 59 | + cmd.SetArgs([]string{"--registry-insecure=true"}) |
| 60 | + |
| 61 | + if err := cmd.Execute(); err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + |
| 65 | + // Load the function and verify registryInsecure is true |
| 66 | + f, err := fn.NewFunction(root) |
| 67 | + if err != nil { |
| 68 | + t.Fatal(err) |
| 69 | + } |
| 70 | + |
| 71 | + if !f.RegistryInsecure { |
| 72 | + t.Fatal("registryInsecure should be true when flag passed, but was false") |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + // Test 3: Run build WITHOUT --registry-insecure flag |
| 77 | + // Expected: registryInsecure should remain true (persisted value) |
| 78 | + // This is the key test for issue #3489 |
| 79 | + t.Run("persists true when flag not passed", func(t *testing.T) { |
| 80 | + cmd := cmdFn(NewTestClient( |
| 81 | + fn.WithRegistry(TestRegistry), |
| 82 | + fn.WithBuilder(builder), |
| 83 | + fn.WithPusher(pusher), |
| 84 | + )) |
| 85 | + cmd.SetArgs([]string{}) |
| 86 | + |
| 87 | + if err := cmd.Execute(); err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + |
| 91 | + // Load the function and verify registryInsecure is still true |
| 92 | + f, err := fn.NewFunction(root) |
| 93 | + if err != nil { |
| 94 | + t.Fatal(err) |
| 95 | + } |
| 96 | + |
| 97 | + if !f.RegistryInsecure { |
| 98 | + t.Fatal("registryInsecure should be preserved as true, but was false") |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + // Test 4: Explicitly set --registry-insecure=false |
| 103 | + // Expected: registryInsecure should be cleared (set to false) |
| 104 | + t.Run("clears when flag set to false", func(t *testing.T) { |
| 105 | + cmd := cmdFn(NewTestClient( |
| 106 | + fn.WithRegistry(TestRegistry), |
| 107 | + fn.WithBuilder(builder), |
| 108 | + fn.WithPusher(pusher), |
| 109 | + )) |
| 110 | + cmd.SetArgs([]string{"--registry-insecure=false"}) |
| 111 | + |
| 112 | + if err := cmd.Execute(); err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + |
| 116 | + // Load the function and verify registryInsecure is false |
| 117 | + f, err := fn.NewFunction(root) |
| 118 | + if err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + |
| 122 | + if f.RegistryInsecure { |
| 123 | + t.Fatal("registryInsecure should be false when flag set to false, but was true") |
| 124 | + } |
| 125 | + }) |
| 126 | + |
| 127 | + // Test 5: Run build again WITHOUT flag after clearing |
| 128 | + // Expected: registryInsecure should stay false |
| 129 | + t.Run("stays false when not set", func(t *testing.T) { |
| 130 | + cmd := cmdFn(NewTestClient( |
| 131 | + fn.WithRegistry(TestRegistry), |
| 132 | + fn.WithBuilder(builder), |
| 133 | + fn.WithPusher(pusher), |
| 134 | + )) |
| 135 | + cmd.SetArgs([]string{}) |
| 136 | + |
| 137 | + if err := cmd.Execute(); err != nil { |
| 138 | + t.Fatal(err) |
| 139 | + } |
| 140 | + |
| 141 | + // Load the function and verify registryInsecure is still false |
| 142 | + f, err := fn.NewFunction(root) |
| 143 | + if err != nil { |
| 144 | + t.Fatal(err) |
| 145 | + } |
| 146 | + |
| 147 | + if f.RegistryInsecure { |
| 148 | + t.Fatal("registryInsecure should remain false, but was true") |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + // Test 6: Set back to true and verify multiple consecutive runs |
| 153 | + t.Run("persists across multiple consecutive runs", func(t *testing.T) { |
| 154 | + // First set it to true |
| 155 | + cmd := cmdFn(NewTestClient( |
| 156 | + fn.WithRegistry(TestRegistry), |
| 157 | + fn.WithBuilder(builder), |
| 158 | + fn.WithPusher(pusher), |
| 159 | + )) |
| 160 | + cmd.SetArgs([]string{"--registry-insecure=true"}) |
| 161 | + |
| 162 | + if err := cmd.Execute(); err != nil { |
| 163 | + t.Fatal(err) |
| 164 | + } |
| 165 | + |
| 166 | + // Run 3 times without the flag |
| 167 | + for i := 0; i < 3; i++ { |
| 168 | + cmd := cmdFn(NewTestClient( |
| 169 | + fn.WithRegistry(TestRegistry), |
| 170 | + fn.WithBuilder(builder), |
| 171 | + fn.WithPusher(pusher), |
| 172 | + )) |
| 173 | + cmd.SetArgs([]string{}) |
| 174 | + |
| 175 | + if err := cmd.Execute(); err != nil { |
| 176 | + t.Fatalf("build %d failed: %v", i+1, err) |
| 177 | + } |
| 178 | + |
| 179 | + // Load and verify after each build |
| 180 | + f, err := fn.NewFunction(root) |
| 181 | + if err != nil { |
| 182 | + t.Fatalf("loading function after build %d failed: %v", i+1, err) |
| 183 | + } |
| 184 | + |
| 185 | + if !f.RegistryInsecure { |
| 186 | + t.Fatalf("build %d: registryInsecure should be true, but was false", i+1) |
| 187 | + } |
| 188 | + } |
| 189 | + }) |
| 190 | +} |
| 191 | + |
| 192 | +// TestWarnRegistryInsecureChange ensures that the warning is printed when |
| 193 | +// the registry changes but registryInsecure is still true. |
| 194 | +func TestWarnRegistryInsecureChange(t *testing.T) { |
| 195 | + tests := []struct { |
| 196 | + name string |
| 197 | + cfgRegistry string |
| 198 | + funcRegistry string |
| 199 | + funcInsecure bool |
| 200 | + expectWarning bool |
| 201 | + expectedMessage string |
| 202 | + }{ |
| 203 | + { |
| 204 | + name: "no warning - registry not changed", |
| 205 | + cfgRegistry: "example.com/alice", |
| 206 | + funcRegistry: "example.com/alice", |
| 207 | + funcInsecure: true, |
| 208 | + expectWarning: false, |
| 209 | + }, |
| 210 | + { |
| 211 | + name: "no warning - registryInsecure is false", |
| 212 | + cfgRegistry: "example.com/bob", |
| 213 | + funcRegistry: "example.com/alice", |
| 214 | + funcInsecure: false, |
| 215 | + expectWarning: false, |
| 216 | + }, |
| 217 | + { |
| 218 | + name: "no warning - func registry is empty", |
| 219 | + cfgRegistry: "example.com/bob", |
| 220 | + funcRegistry: "", |
| 221 | + funcInsecure: true, |
| 222 | + expectWarning: false, |
| 223 | + }, |
| 224 | + { |
| 225 | + name: "no warning - cfg registry is empty", |
| 226 | + cfgRegistry: "", |
| 227 | + funcRegistry: "example.com/alice", |
| 228 | + funcInsecure: true, |
| 229 | + expectWarning: false, |
| 230 | + }, |
| 231 | + { |
| 232 | + name: "warning - registry changed and insecure is true", |
| 233 | + cfgRegistry: "example.com/bob", |
| 234 | + funcRegistry: "example.com/alice", |
| 235 | + funcInsecure: true, |
| 236 | + expectWarning: true, |
| 237 | + expectedMessage: "Warning: Registry changed from 'example.com/alice' to 'example.com/bob', but registryInsecure is still true.", |
| 238 | + }, |
| 239 | + } |
| 240 | + |
| 241 | + for _, tt := range tests { |
| 242 | + t.Run(tt.name, func(t *testing.T) { |
| 243 | + f := fn.Function{ |
| 244 | + Registry: tt.funcRegistry, |
| 245 | + RegistryInsecure: tt.funcInsecure, |
| 246 | + } |
| 247 | + |
| 248 | + // Capture output |
| 249 | + var buf bytes.Buffer |
| 250 | + warnRegistryInsecureChange(&buf, tt.cfgRegistry, f) |
| 251 | + |
| 252 | + output := buf.String() |
| 253 | + |
| 254 | + if tt.expectWarning { |
| 255 | + if output == "" { |
| 256 | + t.Fatal("expected warning but got none") |
| 257 | + } |
| 258 | + if tt.expectedMessage != "" && !strings.Contains(output, tt.expectedMessage) { |
| 259 | + t.Fatalf("expected message to contain '%s', got '%s'", tt.expectedMessage, output) |
| 260 | + } |
| 261 | + } else { |
| 262 | + if output != "" { |
| 263 | + t.Fatalf("expected no warning but got: %s", output) |
| 264 | + } |
| 265 | + } |
| 266 | + }) |
| 267 | + } |
| 268 | +} |
0 commit comments