2020-10-15 10:48:41 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-17 11:54:43 -08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-11-21 07:02:51 -08:00
|
|
|
"path/filepath"
|
2020-10-15 10:48:41 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-01-17 04:16:04 -08:00
|
|
|
const testKubectlAllInfoTemplate = "{{.Context}} :: {{.Namespace}} :: {{.User}} :: {{.Cluster}}"
|
2021-11-21 07:02:51 -08:00
|
|
|
|
2021-02-10 18:23:00 -08:00
|
|
|
func TestKubectlSegment(t *testing.T) {
|
|
|
|
standardTemplate := "{{.Context}}{{if .Namespace}} :: {{.Namespace}}{{end}}"
|
2021-11-21 07:02:51 -08:00
|
|
|
lsep := string(filepath.ListSeparator)
|
|
|
|
|
2021-02-10 18:23:00 -08:00
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
Template string
|
2021-02-12 12:39:20 -08:00
|
|
|
DisplayError bool
|
2021-02-10 18:23:00 -08:00
|
|
|
KubectlExists bool
|
2021-11-21 07:02:51 -08:00
|
|
|
Kubeconfig string
|
|
|
|
ParseKubeConfig bool
|
2021-02-10 18:23:00 -08:00
|
|
|
Context string
|
|
|
|
Namespace string
|
2022-01-12 14:39:34 -08:00
|
|
|
UserName string
|
2021-11-21 07:02:51 -08:00
|
|
|
Cluster string
|
2021-02-10 18:23:00 -08:00
|
|
|
KubectlErr bool
|
|
|
|
ExpectedEnabled bool
|
|
|
|
ExpectedString string
|
2021-11-21 07:02:51 -08:00
|
|
|
Files map[string]string
|
2021-02-10 18:23:00 -08:00
|
|
|
}{
|
2021-12-15 10:06:08 -08:00
|
|
|
{
|
2021-12-17 11:54:43 -08:00
|
|
|
Case: "kubeconfig incomplete",
|
|
|
|
Template: testKubectlAllInfoTemplate,
|
|
|
|
ParseKubeConfig: true,
|
|
|
|
Kubeconfig: "currentcontextmarker" + lsep + "contextdefinitionincomplete",
|
|
|
|
Files: testKubeConfigFiles,
|
|
|
|
ExpectedString: "ctx :: :: :: ",
|
|
|
|
ExpectedEnabled: true,
|
2021-12-15 10:06:08 -08:00
|
|
|
},
|
2021-12-17 11:54:43 -08:00
|
|
|
{Case: "disabled", Template: standardTemplate, KubectlExists: false, Context: "aaa", Namespace: "bbb", ExpectedEnabled: false},
|
2021-12-15 10:06:08 -08:00
|
|
|
{
|
|
|
|
Case: "all information",
|
|
|
|
Template: testKubectlAllInfoTemplate,
|
|
|
|
KubectlExists: true,
|
|
|
|
Context: "aaa",
|
|
|
|
Namespace: "bbb",
|
2022-01-12 14:39:34 -08:00
|
|
|
UserName: "ccc",
|
2021-12-15 10:06:08 -08:00
|
|
|
Cluster: "ddd",
|
|
|
|
ExpectedString: "aaa :: bbb :: ccc :: ddd",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
},
|
2021-12-17 11:54:43 -08:00
|
|
|
{Case: "no namespace", Template: standardTemplate, KubectlExists: true, Context: "aaa", ExpectedString: "aaa", ExpectedEnabled: true},
|
2021-12-15 10:06:08 -08:00
|
|
|
{
|
|
|
|
Case: "kubectl error",
|
|
|
|
Template: standardTemplate,
|
|
|
|
DisplayError: true,
|
|
|
|
KubectlExists: true,
|
|
|
|
Context: "aaa",
|
|
|
|
Namespace: "bbb",
|
|
|
|
KubectlErr: true,
|
|
|
|
ExpectedString: "KUBECTL ERR :: KUBECTL ERR",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
},
|
2021-11-21 07:02:51 -08:00
|
|
|
{Case: "kubectl error hidden", Template: standardTemplate, DisplayError: false, KubectlExists: true, Context: "aaa", Namespace: "bbb", KubectlErr: true, ExpectedEnabled: false},
|
2021-12-15 10:06:08 -08:00
|
|
|
{
|
|
|
|
Case: "kubeconfig home",
|
|
|
|
Template: testKubectlAllInfoTemplate,
|
|
|
|
ParseKubeConfig: true,
|
|
|
|
Files: testKubeConfigFiles,
|
|
|
|
ExpectedString: "aaa :: bbb :: ccc :: ddd",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "kubeconfig multiple current marker first",
|
|
|
|
Template: testKubectlAllInfoTemplate,
|
|
|
|
ParseKubeConfig: true,
|
|
|
|
Kubeconfig: "" + lsep + "currentcontextmarker" + lsep + "contextdefinition" + lsep + "contextredefinition",
|
|
|
|
Files: testKubeConfigFiles,
|
|
|
|
ExpectedString: "ctx :: ns :: usr :: cl",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "kubeconfig multiple context first",
|
|
|
|
Template: testKubectlAllInfoTemplate, ParseKubeConfig: true,
|
|
|
|
Kubeconfig: "contextdefinition" + lsep + "contextredefinition" + lsep + "currentcontextmarker" + lsep,
|
|
|
|
Files: testKubeConfigFiles,
|
|
|
|
ExpectedString: "ctx :: ns :: usr :: cl",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "kubeconfig error hidden", Template: testKubectlAllInfoTemplate, ParseKubeConfig: true, Kubeconfig: "invalid", Files: testKubeConfigFiles, ExpectedEnabled: false},
|
|
|
|
{
|
|
|
|
Case: "kubeconfig error",
|
|
|
|
Template: testKubectlAllInfoTemplate,
|
|
|
|
ParseKubeConfig: true,
|
|
|
|
Kubeconfig: "invalid",
|
|
|
|
Files: testKubeConfigFiles,
|
|
|
|
DisplayError: true,
|
|
|
|
ExpectedString: "KUBECONFIG ERR :: KUBECONFIG ERR :: KUBECONFIG ERR :: KUBECONFIG ERR",
|
|
|
|
ExpectedEnabled: true,
|
|
|
|
},
|
2020-10-15 10:48:41 -07:00
|
|
|
}
|
2020-11-02 09:32:40 -08:00
|
|
|
|
2021-02-10 18:23:00 -08:00
|
|
|
for _, tc := range cases {
|
2021-12-15 10:06:08 -08:00
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("hasCommand", "kubectl").Return(tc.KubectlExists)
|
2021-12-17 11:54:43 -08:00
|
|
|
var kubeconfig string
|
|
|
|
content, err := ioutil.ReadFile("./test/kubectl.yml")
|
|
|
|
if err == nil {
|
2022-01-12 14:39:34 -08:00
|
|
|
kubeconfig = fmt.Sprintf(string(content), tc.Cluster, tc.UserName, tc.Namespace, tc.Context)
|
2021-12-15 10:06:08 -08:00
|
|
|
}
|
|
|
|
var kubectlErr error
|
|
|
|
if tc.KubectlErr {
|
|
|
|
kubectlErr = &commandError{
|
|
|
|
err: "oops",
|
|
|
|
exitCode: 1,
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 11:54:43 -08:00
|
|
|
env.On("runCommand", "kubectl", []string{"config", "view", "--output", "yaml", "--minify"}).Return(kubeconfig, kubectlErr)
|
2021-12-15 10:06:08 -08:00
|
|
|
env.On("getenv", "KUBECONFIG").Return(tc.Kubeconfig)
|
|
|
|
for path, content := range tc.Files {
|
|
|
|
env.On("getFileContent", path).Return(content)
|
|
|
|
}
|
2022-01-15 11:37:46 -08:00
|
|
|
env.On("homeDir").Return("testhome")
|
2022-01-12 14:39:34 -08:00
|
|
|
env.onTemplate()
|
2021-12-15 10:06:08 -08:00
|
|
|
|
|
|
|
k := &kubectl{
|
|
|
|
env: env,
|
2022-01-01 11:08:08 -08:00
|
|
|
props: properties{
|
2021-12-15 10:06:08 -08:00
|
|
|
SegmentTemplate: tc.Template,
|
|
|
|
DisplayError: tc.DisplayError,
|
|
|
|
ParseKubeConfig: tc.ParseKubeConfig,
|
|
|
|
},
|
2021-02-10 18:23:00 -08:00
|
|
|
}
|
2021-12-15 10:06:08 -08:00
|
|
|
assert.Equal(t, tc.ExpectedEnabled, k.enabled(), tc.Case)
|
2021-11-21 07:02:51 -08:00
|
|
|
if tc.ExpectedEnabled {
|
2021-12-15 10:06:08 -08:00
|
|
|
assert.Equal(t, tc.ExpectedString, k.string(), tc.Case)
|
2021-11-21 07:02:51 -08:00
|
|
|
}
|
2020-11-02 09:32:40 -08:00
|
|
|
}
|
|
|
|
}
|
2021-11-21 07:02:51 -08:00
|
|
|
|
|
|
|
var testKubeConfigFiles = map[string]string{
|
|
|
|
filepath.Join("testhome", ".kube/config"): `
|
|
|
|
apiVersion: v1
|
|
|
|
contexts:
|
|
|
|
- context:
|
|
|
|
cluster: ddd
|
|
|
|
user: ccc
|
|
|
|
namespace: bbb
|
|
|
|
name: aaa
|
|
|
|
current-context: aaa
|
|
|
|
`,
|
|
|
|
"contextdefinition": `
|
|
|
|
apiVersion: v1
|
|
|
|
contexts:
|
|
|
|
- context:
|
|
|
|
cluster: cl
|
|
|
|
user: usr
|
|
|
|
namespace: ns
|
|
|
|
name: ctx
|
|
|
|
`,
|
|
|
|
"currentcontextmarker": `
|
|
|
|
apiVersion: v1
|
|
|
|
current-context: ctx
|
|
|
|
`,
|
|
|
|
"invalid": "this is not yaml",
|
|
|
|
"contextdefinitionincomplete": `
|
|
|
|
apiVersion: v1
|
|
|
|
contexts:
|
|
|
|
- name: ctx
|
|
|
|
`,
|
|
|
|
"contextredefinition": `
|
|
|
|
apiVersion: v1
|
|
|
|
contexts:
|
|
|
|
- context:
|
|
|
|
cluster: wrongcl
|
|
|
|
user: wrongu
|
|
|
|
namespace: wrongns
|
|
|
|
name: ctx
|
|
|
|
`,
|
|
|
|
}
|