|
| 1 | +// Copyright 2025 The Kubernetes Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package add |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils" |
| 13 | + "sigs.k8s.io/kustomize/kyaml/filesys" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAddConfiguration(t *testing.T) { |
| 17 | + fSys := filesys.MakeEmptyDirInMemory() |
| 18 | + testutils_test.WriteTestKustomization(fSys) |
| 19 | + |
| 20 | + cmd := newCmdAddConfiguration(fSys) |
| 21 | + |
| 22 | + if cmd == nil { |
| 23 | + t.Fatal("Expected cmd to not be nil") |
| 24 | + } |
| 25 | + |
| 26 | + if cmd.Use != "configuration" { |
| 27 | + t.Fatalf("Expected Use to be 'configuration', got '%s'", cmd.Use) |
| 28 | + } |
| 29 | + |
| 30 | + if cmd.Short == "" { |
| 31 | + t.Fatal("Expected Short to not be empty") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestAddConfigurationHappyPath(t *testing.T) { |
| 36 | + fSys := filesys.MakeEmptyDirInMemory() |
| 37 | + err := fSys.WriteFile("config1.yaml", []byte("apiVersion: v1\nkind: Config")) |
| 38 | + require.NoError(t, err) |
| 39 | + err = fSys.WriteFile("config2.yaml", []byte("apiVersion: v1\nkind: Config")) |
| 40 | + require.NoError(t, err) |
| 41 | + testutils_test.WriteTestKustomization(fSys) |
| 42 | + |
| 43 | + cmd := newCmdAddConfiguration(fSys) |
| 44 | + args := []string{"config1.yaml", "config2.yaml"} |
| 45 | + require.NoError(t, cmd.RunE(cmd, args)) |
| 46 | + |
| 47 | + content, err := testutils_test.ReadTestKustomization(fSys) |
| 48 | + require.NoError(t, err) |
| 49 | + assert.Contains(t, string(content), "config1.yaml") |
| 50 | + assert.Contains(t, string(content), "config2.yaml") |
| 51 | +} |
| 52 | + |
| 53 | +func TestAddConfigurationDuplicate(t *testing.T) { |
| 54 | + fSys := filesys.MakeEmptyDirInMemory() |
| 55 | + err := fSys.WriteFile("config.yaml", []byte("apiVersion: v1\nkind: Config")) |
| 56 | + require.NoError(t, err) |
| 57 | + testutils_test.WriteTestKustomization(fSys) |
| 58 | + |
| 59 | + cmd := newCmdAddConfiguration(fSys) |
| 60 | + |
| 61 | + // First addition |
| 62 | + args := []string{"config.yaml"} |
| 63 | + require.NoError(t, cmd.RunE(cmd, args)) |
| 64 | + |
| 65 | + content, err := testutils_test.ReadTestKustomization(fSys) |
| 66 | + require.NoError(t, err) |
| 67 | + assert.Contains(t, string(content), "config.yaml") |
| 68 | + |
| 69 | + // Second addition (should skip duplicate) |
| 70 | + require.NoError(t, cmd.RunE(cmd, args)) |
| 71 | + |
| 72 | + content, err = testutils_test.ReadTestKustomization(fSys) |
| 73 | + require.NoError(t, err) |
| 74 | + |
| 75 | + // Count occurrences - should only appear once |
| 76 | + count := strings.Count(string(content), "config.yaml") |
| 77 | + assert.Equal(t, 1, count, "config.yaml should appear exactly once") |
| 78 | +} |
0 commit comments