Merge pull request #36 from pelotech/kubeconfig-tests
Validation tests for the kubeconfig template
This commit is contained in:
commit
7e25218945
|
@ -2,7 +2,7 @@ FROM alpine/helm
|
|||
MAINTAINER Erin Call <erin@liffft.com>
|
||||
|
||||
COPY build/drone-helm /bin/drone-helm
|
||||
COPY kubeconfig /root/.kube/config.tpl
|
||||
COPY assets/kubeconfig.tpl /root/.kube/config.tpl
|
||||
|
||||
LABEL description="Helm 3 plugin for Drone 3"
|
||||
LABEL base="alpine/helm"
|
||||
|
|
1
go.mod
1
go.mod
|
@ -8,4 +8,5 @@ require (
|
|||
github.com/stretchr/testify v1.4.0
|
||||
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect
|
||||
golang.org/x/tools v0.0.0-20191209225234-22774f7dae43 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.2
|
||||
)
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package run
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/suite"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"text/template"
|
||||
// "github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"testing"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type InitKubeTestSuite struct {
|
||||
|
@ -58,6 +58,59 @@ namespace: Cisco
|
|||
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() {
|
||||
templateFile, err := tempfile("kubeconfig********.yml.tpl", `{{ NonexistentFunction }}`)
|
||||
defer os.Remove(templateFile.Name())
|
||||
|
|
Loading…
Reference in a new issue