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 committed Jun 10, 2020
1 parent 333b80b commit d39d68b
Showing 1 changed file with 169 additions and 0 deletions.
169 changes: 169 additions & 0 deletions cli/cmd/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
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 environemnt variable: %v", kubeconfigEnvVariable, err)
}
}

if k.env != "" {
// Ensure KUBECONFIG IS set.
if err := os.Setenv(kubeconfigEnvVariable, k.env); err != nil {
t.Fatalf("setting %q environemnt 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)
}
})

os.Chdir(tmpDir)

if k.configFile != "" {
path := filepath.Join(tmpDir, "cluster.lokocfg")
if err := ioutil.WriteFile(path, []byte(k.configFile), 0644); err != nil {
t.Fatalf("writing file %q: %v", path, err)
}
}
}

func TestGetKubeconfigFlag(t *testing.T) {
expectedPath := "/foo"

k := &kubeconfigSources{
flag: 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 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
}
}`,
}

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 d39d68b

Please sign in to comment.