Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
cli/cmd: add tests for getKubeconfig
Browse files Browse the repository at this point in the history
To make sure we do it right, as otherwise it might be destructive.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian authored and iaguis committed Jun 18, 2020
1 parent c61ba20 commit ab7638a
Showing 1 changed file with 205 additions and 0 deletions.
205 changes: 205 additions & 0 deletions cli/cmd/utils_internal_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ab7638a

Please sign in to comment.