sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/repository/overrides_test.go (about)

     1  /*
     2  Copyright 2019 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 repository
    18  
    19  import (
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/adrg/xdg"
    25  	. "github.com/onsi/gomega"
    26  
    27  	clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    28  	"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
    29  	"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
    30  )
    31  
    32  func TestOverrides(t *testing.T) {
    33  	configDirectory, err := xdg.ConfigFile(config.ConfigFolderXDG)
    34  	NewWithT(t).Expect(err).ToNot(HaveOccurred())
    35  
    36  	tests := []struct {
    37  		name            string
    38  		configVarClient config.VariablesClient
    39  		envVars         map[string]string
    40  		expectedPath    string
    41  	}{
    42  		{
    43  			name:            "returns default overrides path if no config provided",
    44  			configVarClient: test.NewFakeVariableClient(),
    45  			expectedPath:    filepath.Join(configDirectory, overrideFolder, "infrastructure-myinfra", "v1.0.1", "infra-comp.yaml"),
    46  		},
    47  		{
    48  			name:            "returns default overrides path if config variable is empty",
    49  			configVarClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, ""),
    50  			expectedPath:    filepath.Join(configDirectory, overrideFolder, "infrastructure-myinfra", "v1.0.1", "infra-comp.yaml"),
    51  		},
    52  		{
    53  			name:            "returns default overrides path if config variable is whitespace",
    54  			configVarClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, "   "),
    55  			expectedPath:    filepath.Join(configDirectory, overrideFolder, "infrastructure-myinfra", "v1.0.1", "infra-comp.yaml"),
    56  		},
    57  		{
    58  			name:            "uses overrides folder from the config variables",
    59  			configVarClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, "/Users/foobar/workspace/releases"),
    60  			expectedPath:    "/Users/foobar/workspace/releases/infrastructure-myinfra/v1.0.1/infra-comp.yaml",
    61  		},
    62  		{
    63  			name:            "uses overrides folder from the config variables with evaluated env vars",
    64  			configVarClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, "${TEST_REPO_PATH}/releases"),
    65  			envVars: map[string]string{
    66  				"TEST_REPO_PATH": "/tmp/test",
    67  			},
    68  			expectedPath: "/tmp/test/releases/infrastructure-myinfra/v1.0.1/infra-comp.yaml",
    69  		},
    70  	}
    71  
    72  	for _, tt := range tests {
    73  		t.Run(tt.name, func(t *testing.T) {
    74  			g := NewWithT(t)
    75  
    76  			for k, v := range tt.envVars {
    77  				g.Expect(os.Setenv(k, v)).To(Succeed())
    78  			}
    79  			defer func() {
    80  				for k := range tt.envVars {
    81  					g.Expect(os.Unsetenv(k)).To(Succeed())
    82  				}
    83  			}()
    84  			provider := config.NewProvider("myinfra", "", clusterctlv1.InfrastructureProviderType)
    85  			override := newOverride(&newOverrideInput{
    86  				configVariablesClient: tt.configVarClient,
    87  				provider:              provider,
    88  				version:               "v1.0.1",
    89  				filePath:              "infra-comp.yaml",
    90  			})
    91  
    92  			overridePath, err := override.Path()
    93  			g.Expect(err).ToNot(HaveOccurred())
    94  			g.Expect(overridePath).To(Equal(tt.expectedPath))
    95  		})
    96  	}
    97  }
    98  
    99  func TestGetLocalOverrides(t *testing.T) {
   100  	t.Run("returns contents of file successfully", func(t *testing.T) {
   101  		g := NewWithT(t)
   102  
   103  		tmpDir := createTempDir(t)
   104  		defer os.RemoveAll(tmpDir)
   105  
   106  		createLocalTestProviderFile(t, tmpDir, "infrastructure-myinfra/v1.0.1/infra-comp.yaml", "foo: bar")
   107  
   108  		info := &newOverrideInput{
   109  			configVariablesClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, tmpDir),
   110  			provider:              config.NewProvider("myinfra", "", clusterctlv1.InfrastructureProviderType),
   111  			version:               "v1.0.1",
   112  			filePath:              "infra-comp.yaml",
   113  		}
   114  
   115  		b, err := getLocalOverride(info)
   116  		g.Expect(err).ToNot(HaveOccurred())
   117  		g.Expect(string(b)).To(Equal("foo: bar"))
   118  	})
   119  
   120  	t.Run("doesn't return error if file does not exist", func(t *testing.T) {
   121  		g := NewWithT(t)
   122  
   123  		info := &newOverrideInput{
   124  			configVariablesClient: test.NewFakeVariableClient().WithVar(overrideFolderKey, "do-not-exist"),
   125  			provider:              config.NewProvider("myinfra", "", clusterctlv1.InfrastructureProviderType),
   126  			version:               "v1.0.1",
   127  			filePath:              "infra-comp.yaml",
   128  		}
   129  
   130  		_, err := getLocalOverride(info)
   131  		g.Expect(err).ToNot(HaveOccurred())
   132  	})
   133  }