|
| 1 | +/* |
| 2 | +Copyright The Kubernetes 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 | + "errors" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + "sync" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +var runWithArgsForTestMu sync.Mutex |
| 29 | + |
| 30 | +func TestConfigCommandCreatesMissingExplicitConfigOnSet(t *testing.T) { |
| 31 | + t.Parallel() |
| 32 | + |
| 33 | + configPath := filepath.Join(t.TempDir(), "nested", "crictl.yaml") |
| 34 | + |
| 35 | + err := runWithArgsForTest( |
| 36 | + t, |
| 37 | + "crictl", |
| 38 | + "--config", |
| 39 | + configPath, |
| 40 | + "config", |
| 41 | + "--set", |
| 42 | + "debug=true", |
| 43 | + ) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("run config --set: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + content, err := os.ReadFile(configPath) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("read config file: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + if !strings.Contains(string(content), "debug: true") { |
| 54 | + t.Fatalf("expected config file to contain debug=true, got:\n%s", string(content)) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestConfigCommandCreatesMissingExplicitConfigOnPositionalSet(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + configPath := filepath.Join(t.TempDir(), "nested", "crictl.yaml") |
| 62 | + |
| 63 | + err := runWithArgsForTest( |
| 64 | + t, |
| 65 | + "crictl", |
| 66 | + "--config", |
| 67 | + configPath, |
| 68 | + "config", |
| 69 | + "debug", |
| 70 | + "true", |
| 71 | + ) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("run config positional set: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + content, err := os.ReadFile(configPath) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("read config file: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + if !strings.Contains(string(content), "debug: true") { |
| 82 | + t.Fatalf("expected config file to contain debug=true, got:\n%s", string(content)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestConfigCommandListDoesNotCreateMissingExplicitConfig(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + |
| 89 | + configPath := filepath.Join(t.TempDir(), "nested", "crictl.yaml") |
| 90 | + |
| 91 | + err := runWithArgsForTest( |
| 92 | + t, |
| 93 | + "crictl", |
| 94 | + "--config", |
| 95 | + configPath, |
| 96 | + "config", |
| 97 | + "--list", |
| 98 | + ) |
| 99 | + if err == nil { |
| 100 | + t.Fatal("expected config --list to fail for a missing explicit config file") |
| 101 | + } |
| 102 | + |
| 103 | + _, err = os.Stat(configPath) |
| 104 | + if !errors.Is(err, os.ErrNotExist) { |
| 105 | + t.Fatalf("expected config file to remain absent, got err=%v", err) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func runWithArgsForTest(t *testing.T, args ...string) error { |
| 110 | + t.Helper() |
| 111 | + |
| 112 | + runWithArgsForTestMu.Lock() |
| 113 | + t.Cleanup(func() { |
| 114 | + runWithArgsForTestMu.Unlock() |
| 115 | + }) |
| 116 | + |
| 117 | + oldArgs := os.Args |
| 118 | + os.Args = args |
| 119 | + |
| 120 | + t.Cleanup(func() { |
| 121 | + os.Args = oldArgs |
| 122 | + }) |
| 123 | + |
| 124 | + return run() |
| 125 | +} |
0 commit comments