|
| 1 | +package machine |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + crcConfig "github.com/crc-org/crc/v2/pkg/crc/config" |
| 9 | + "github.com/crc-org/crc/v2/pkg/crc/machine/state" |
| 10 | + crcOs "github.com/crc-org/crc/v2/pkg/os" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestClient_WhenStopInvokedWithNonExistentVM_ThenThrowError(t *testing.T) { |
| 15 | + // Given |
| 16 | + dir := t.TempDir() |
| 17 | + oldKubeConfigEnvVarValue := os.Getenv("KUBECONFIG") |
| 18 | + kubeConfigPath := filepath.Join(dir, "kubeconfig") |
| 19 | + err := crcOs.CopyFile(filepath.Join("testdata", "kubeconfig.in"), kubeConfigPath) |
| 20 | + assert.NoError(t, err) |
| 21 | + err = os.Setenv("KUBECONFIG", kubeConfigPath) |
| 22 | + assert.NoError(t, err) |
| 23 | + crcConfigStorage := crcConfig.New(crcConfig.NewEmptyInMemoryStorage(), crcConfig.NewEmptyInMemorySecretStorage()) |
| 24 | + client := NewClient("i-dont-exist", false, crcConfigStorage) |
| 25 | + |
| 26 | + // When |
| 27 | + clusterState, stopErr := client.Stop() |
| 28 | + |
| 29 | + // Then |
| 30 | + assert.EqualError(t, stopErr, "Instance is already stopped") |
| 31 | + assert.Equal(t, clusterState, state.Error) |
| 32 | + err = os.Setenv("KUBECONFIG", oldKubeConfigEnvVarValue) |
| 33 | + assert.NoError(t, err) |
| 34 | +} |
| 35 | + |
| 36 | +var testArguments = map[string]struct { |
| 37 | + inputKubeConfigPath string |
| 38 | + expectedKubeConfigPath string |
| 39 | +}{ |
| 40 | + "When KubeConfig contains crc context, then cleanup KubeConfig": { |
| 41 | + "kubeconfig.in", "kubeconfig.out", |
| 42 | + }, |
| 43 | + "When KubeConfig does not contain crc context, then KubeConfig remains unchanged": { |
| 44 | + "kubeconfig.out", "kubeconfig.out", |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +func TestClient_WhenStopInvoked_ThenKubeConfigUpdatedIfRequired(t *testing.T) { |
| 49 | + for name, test := range testArguments { |
| 50 | + t.Run(name, func(t *testing.T) { |
| 51 | + t.Parallel() |
| 52 | + // Given |
| 53 | + dir := t.TempDir() |
| 54 | + oldKubeConfigEnvVarValue := os.Getenv("KUBECONFIG") |
| 55 | + kubeConfigPath := filepath.Join(dir, "kubeconfig") |
| 56 | + err := crcOs.CopyFile(filepath.Join("testdata", test.inputKubeConfigPath), kubeConfigPath) |
| 57 | + assert.NoError(t, err) |
| 58 | + err = os.Setenv("KUBECONFIG", kubeConfigPath) |
| 59 | + assert.NoError(t, err) |
| 60 | + crcConfigStorage := crcConfig.New(crcConfig.NewEmptyInMemoryStorage(), crcConfig.NewEmptyInMemorySecretStorage()) |
| 61 | + client := NewClient("test-client", false, crcConfigStorage) |
| 62 | + |
| 63 | + // When |
| 64 | + clusterState, _ := client.Stop() |
| 65 | + |
| 66 | + // Then |
| 67 | + actualKubeConfigFile, err := os.ReadFile(kubeConfigPath) |
| 68 | + assert.NoError(t, err) |
| 69 | + expectedKubeConfigPath, err := os.ReadFile(filepath.Join("testdata", test.expectedKubeConfigPath)) |
| 70 | + assert.NoError(t, err) |
| 71 | + assert.YAMLEq(t, string(expectedKubeConfigPath), string(actualKubeConfigFile)) |
| 72 | + assert.Equal(t, clusterState, state.Error) |
| 73 | + err = os.Setenv("KUBECONFIG", oldKubeConfigEnvVarValue) |
| 74 | + assert.NoError(t, err) |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
0 commit comments