From ab7638a480b48610637a799fea01261b8ad573bb Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Wed, 10 Jun 2020 10:12:21 +0200 Subject: [PATCH] cli/cmd: add tests for getKubeconfig To make sure we do it right, as otherwise it might be destructive. Signed-off-by: Mateusz Gozdek --- cli/cmd/utils_internal_test.go | 205 +++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 cli/cmd/utils_internal_test.go diff --git a/cli/cmd/utils_internal_test.go b/cli/cmd/utils_internal_test.go new file mode 100644 index 000000000..c5af31772 --- /dev/null +++ b/cli/cmd/utils_internal_test.go @@ -0,0 +1,205 @@ +// Copyright 2020 The Lokomotive Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + + "github.com/spf13/viper" +) + +type kubeconfigSources struct { + flag string + env string + configFile string +} + +func prepareKubeconfigSource(t *testing.T, k *kubeconfigSources) { + // Ensure viper flag is NOT empty. + viper.Set(kubeconfigFlag, k.flag) + + if k.env == "" { + // Ensure KUBECONFIG is not set. + if err := os.Unsetenv(kubeconfigEnvVariable); err != nil { + t.Fatalf("unsetting %q environment variable: %v", kubeconfigEnvVariable, err) + } + } + + if k.env != "" { + // Ensure KUBECONFIG IS set. + if err := os.Setenv(kubeconfigEnvVariable, k.env); err != nil { + t.Fatalf("setting %q environment variable: %v", kubeconfigEnvVariable, err) + } + } + + // Ensure there is no lokocfg configuration in working directory. + tmpDir, err := ioutil.TempDir("", "lokoctl-tests-") + if err != nil { + t.Fatalf("creating tmp dir: %v", err) + } + + t.Cleanup(func() { + if err := os.RemoveAll(tmpDir); err != nil { + t.Logf("removing temp dir %q: %v", tmpDir, err) + } + }) + + if err := os.Chdir(tmpDir); err != nil { + t.Fatalf("changing working directory to %q: %v", tmpDir, err) + } + + if k.configFile != "" { + path := filepath.Join(tmpDir, "cluster.lokocfg") + if err := ioutil.WriteFile(path, []byte(k.configFile), 0600); err != nil { + t.Fatalf("writing file %q: %v", path, err) + } + } +} + +func TestGetKubeconfigFlag(t *testing.T) { + expectedPath := "/foo" + + k := &kubeconfigSources{ + configFile: `cluster "packet" { + asset_dir = "/bad" + + cluster_name = "" + controller_count = 0 + facility = "" + management_cidrs = [] + node_private_cidr = "" + project_id = "" + ssh_pubkeys = [] + dns { + provider = "" + zone = "" + } + worker_pool "foo" { + count = 0 + } +}`, + flag: expectedPath, + env: "/badpath", + } + + prepareKubeconfigSource(t, k) + + kubeconfig, err := getKubeconfig() + if err != nil { + t.Fatalf("getting kubeconfig: %v", err) + } + + if kubeconfig != expectedPath { + t.Fatalf("expected %q, got %q", expectedPath, kubeconfig) + } +} + +func TestGetKubeconfigConfigFile(t *testing.T) { + expectedPath := assetsKubeconfig("/foo") + + k := &kubeconfigSources{ + configFile: `cluster "packet" { + asset_dir = "/foo" + + cluster_name = "" + controller_count = 0 + facility = "" + management_cidrs = [] + node_private_cidr = "" + project_id = "" + ssh_pubkeys = [] + dns { + provider = "" + zone = "" + } + worker_pool "foo" { + count = 0 + } +}`, + + env: "/badpath", + } + + prepareKubeconfigSource(t, k) + + kubeconfig, err := getKubeconfig() + if err != nil { + t.Fatalf("getting kubeconfig: %v", err) + } + + if kubeconfig != expectedPath { + t.Fatalf("expected %q, got %q", expectedPath, kubeconfig) + } +} + +func TestGetKubeconfigBadConfigFile(t *testing.T) { + expectedPath := "" + + k := &kubeconfigSources{ + configFile: `cluster "packet" { + asset_dir = "/foo" +}`, + } + + prepareKubeconfigSource(t, k) + + kubeconfig, err := getKubeconfig() + if err == nil { + t.Errorf("getting kubeconfig with bad configuration should fail") + } + + if kubeconfig != expectedPath { + t.Fatalf("if getting kubeconfig fails, empty path should be returned") + } +} + +func TestGetKubeconfigEnvVariable(t *testing.T) { + expectedPath := "/foo" + + k := &kubeconfigSources{ + env: expectedPath, + } + + prepareKubeconfigSource(t, k) + + kubeconfig, err := getKubeconfig() + if err != nil { + t.Fatalf("getting kubeconfig: %v", err) + } + + if kubeconfig != expectedPath { + t.Fatalf("expected %q, got %q", expectedPath, kubeconfig) + } +} + +func TestGetKubeconfigDefault(t *testing.T) { + expectedPath := expandKubeconfigPath(defaultKubeconfigPath) + + k := &kubeconfigSources{} + + prepareKubeconfigSource(t, k) + + kubeconfig, err := getKubeconfig() + if err != nil { + t.Fatalf("getting kubeconfig: %v", err) + } + + if kubeconfig != expectedPath { + t.Fatalf("expected %q, got %q", expectedPath, kubeconfig) + } +}