Put kubeconfig tests in a separate suite [#5]
As I started writing tests for EKS config, dealing with the repeated setup/verification code was cumbersome. This should make it much easier to add new tests.
This commit is contained in:
@@ -2,7 +2,6 @@ package run
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
yaml "gopkg.in/yaml.v2"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -58,59 +57,6 @@ namespace: Cisco
|
|||||||
suite.Equal(want, string(conf))
|
suite.Equal(want, string(conf))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (suite *InitKubeTestSuite) TestExecuteGeneratesConfig() {
|
|
||||||
configFile, err := tempfile("kubeconfig********.yml", "")
|
|
||||||
defer os.Remove(configFile.Name())
|
|
||||||
suite.Require().NoError(err)
|
|
||||||
|
|
||||||
cfg := Config{
|
|
||||||
Namespace: "marshmallow",
|
|
||||||
}
|
|
||||||
init := InitKube{
|
|
||||||
ConfigFile: configFile.Name(),
|
|
||||||
TemplateFile: "../../assets/kubeconfig.tpl", // the actual kubeconfig template
|
|
||||||
APIServer: "https://kube.cluster/peanut",
|
|
||||||
ServiceAccount: "chef",
|
|
||||||
Token: "eWVhaCB3ZSB0b2tpbic=",
|
|
||||||
Certificate: "d293LCB5b3UgYXJlIHNvIGNvb2wgZm9yIHNtb2tpbmcgd2VlZCDwn5mE",
|
|
||||||
}
|
|
||||||
suite.Require().NoError(init.Prepare(cfg))
|
|
||||||
suite.Require().NoError(init.Execute(cfg))
|
|
||||||
|
|
||||||
contents, err := ioutil.ReadFile(configFile.Name())
|
|
||||||
suite.Require().NoError(err)
|
|
||||||
|
|
||||||
// each setting should be reflected in the generated file
|
|
||||||
expectations := []string{
|
|
||||||
"namespace: marshmallow",
|
|
||||||
"server: https://kube.cluster/peanut",
|
|
||||||
"user: chef",
|
|
||||||
"name: chef",
|
|
||||||
"token: eWVhaCB3ZSB0b2tpbic",
|
|
||||||
"certificate-authority-data: d293LCB5b3UgYXJlIHNvIGNvb2wgZm9yIHNtb2tpbmcgd2VlZCDwn5mE",
|
|
||||||
}
|
|
||||||
for _, expected := range expectations {
|
|
||||||
suite.Contains(string(contents), expected)
|
|
||||||
}
|
|
||||||
|
|
||||||
// the generated config should be valid yaml, with no repeated keys
|
|
||||||
conf := map[string]interface{}{}
|
|
||||||
suite.NoError(yaml.UnmarshalStrict(contents, &conf))
|
|
||||||
|
|
||||||
// test the other branch of the certificate/SkipTLSVerify conditional
|
|
||||||
init.SkipTLSVerify = true
|
|
||||||
init.Certificate = ""
|
|
||||||
|
|
||||||
suite.Require().NoError(init.Prepare(cfg))
|
|
||||||
suite.Require().NoError(init.Execute(cfg))
|
|
||||||
contents, err = ioutil.ReadFile(configFile.Name())
|
|
||||||
suite.Require().NoError(err)
|
|
||||||
suite.Contains(string(contents), "insecure-skip-tls-verify: true")
|
|
||||||
|
|
||||||
conf = map[string]interface{}{}
|
|
||||||
suite.NoError(yaml.UnmarshalStrict(contents, &conf))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (suite *InitKubeTestSuite) TestPrepareParseError() {
|
func (suite *InitKubeTestSuite) TestPrepareParseError() {
|
||||||
templateFile, err := tempfile("kubeconfig********.yml.tpl", `{{ NonexistentFunction }}`)
|
templateFile, err := tempfile("kubeconfig********.yml.tpl", `{{ NonexistentFunction }}`)
|
||||||
defer os.Remove(templateFile.Name())
|
defer os.Remove(templateFile.Name())
|
||||||
|
|||||||
92
internal/run/kubeconfig_test.go
Normal file
92
internal/run/kubeconfig_test.go
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
package run
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type KubeconfigTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
configFile *os.File
|
||||||
|
initKube InitKube
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) BeforeTest(_, _ string) {
|
||||||
|
file, err := ioutil.TempFile("", "kubeconfig********.yml")
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
file.Close()
|
||||||
|
suite.configFile = file
|
||||||
|
|
||||||
|
// set up an InitKube with the bare minimum configuration
|
||||||
|
suite.initKube = InitKube{
|
||||||
|
ConfigFile: file.Name(),
|
||||||
|
TemplateFile: "../../assets/kubeconfig.tpl", // the actual kubeconfig template
|
||||||
|
APIServer: "a",
|
||||||
|
Token: "b",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) AfterTest(_, _ string) {
|
||||||
|
if suite.configFile != nil {
|
||||||
|
os.Remove(suite.configFile.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKubeconfigTestSuite(t *testing.T) {
|
||||||
|
suite.Run(t, new(KubeconfigTestSuite))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) TestSetsNamespace() {
|
||||||
|
cfg := Config{
|
||||||
|
Namespace: "marshmallow",
|
||||||
|
}
|
||||||
|
contents := suite.generateKubeconfig(cfg)
|
||||||
|
suite.Contains(contents, "namespace: marshmallow")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) TestSetsAPIServer() {
|
||||||
|
suite.initKube.APIServer = "https://kube.cluster/peanut"
|
||||||
|
contents := suite.generateKubeconfig(Config{})
|
||||||
|
suite.Contains(contents, "server: https://kube.cluster/peanut")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) TestSetsServiceAccount() {
|
||||||
|
suite.initKube.ServiceAccount = "chef"
|
||||||
|
contents := suite.generateKubeconfig(Config{})
|
||||||
|
suite.Contains(contents, "user: chef")
|
||||||
|
suite.Contains(contents, "name: chef")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) TestSetsToken() {
|
||||||
|
suite.initKube.Token = "eWVhaCB3ZSB0b2tpbic"
|
||||||
|
contents := suite.generateKubeconfig(Config{})
|
||||||
|
suite.Contains(contents, "token: eWVhaCB3ZSB0b2tpbic")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) TestSetsCertificate() {
|
||||||
|
suite.initKube.Certificate = "d293LCB5b3UgYXJlIHNvIGNvb2wgZm9yIHNtb2tpbmcgd2VlZCDwn5mE"
|
||||||
|
contents := suite.generateKubeconfig(Config{})
|
||||||
|
suite.Contains(contents, "certificate-authority-data: d293LCB5b3UgYXJlIHNvIGNvb2wgZm9yIHNtb2tpbmcgd2VlZCDwn5mE")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) TestSetsSkipTLSVerify() {
|
||||||
|
suite.initKube.SkipTLSVerify = true
|
||||||
|
contents := suite.generateKubeconfig(Config{})
|
||||||
|
suite.Contains(contents, "insecure-skip-tls-verify: true")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *KubeconfigTestSuite) generateKubeconfig(cfg Config) string {
|
||||||
|
suite.Require().NoError(suite.initKube.Prepare(cfg))
|
||||||
|
suite.Require().NoError(suite.initKube.Execute(cfg))
|
||||||
|
|
||||||
|
contents, err := ioutil.ReadFile(suite.configFile.Name())
|
||||||
|
suite.Require().NoError(err)
|
||||||
|
|
||||||
|
conf := map[string]interface{}{}
|
||||||
|
suite.NoError(yaml.UnmarshalStrict(contents, &conf))
|
||||||
|
|
||||||
|
return string(contents)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user