This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli/cmd: add tests for getKubeconfig
To make sure we do it right, as otherwise it might be destructive. Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
- Loading branch information
Showing
1 changed file
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |