Add support for custom helm repos

This commit is contained in:
so0k
2017-08-08 17:47:26 +08:00
parent 7ef44c6be3
commit eeca0fe9ed
4 changed files with 258 additions and 82 deletions
+91 -23
View File
@@ -1,6 +1,7 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
@@ -74,15 +75,71 @@ func TestGetHelmCommand(t *testing.T) {
}
func TestResolveSecrets(t *testing.T) {
tag := "v0.1.1"
api := "http://apiserver"
token := "12345"
account := "helm"
os.Setenv("MY_TAG", tag)
os.Setenv("MY_API_SERVER", api)
os.Setenv("MY_KUBERNETES_TOKEN", token)
os.Setenv("MY_SERVICE_ACCOUNT", "helm")
testEnvs := []struct {
prefix string
tag string
api string
token string
account string
}{
{prefix: "PROD", tag: "v0.1.1", api: "http://apiserver", token: "12345", account: "helm"},
{prefix: "STAGING", tag: "12345678", api: "http://apiserver", token: "12345", account: "helm"},
}
for _, env := range testEnvs {
envMap := map[string]string{
"TAG": env.tag,
"API_SERVER": env.api,
"KUBERNETES_TOKEN": env.token,
"SERVICE_ACCOUNT": env.account,
}
for envKey, envValue := range envMap {
os.Setenv(fmt.Sprintf("%s_%s", env.prefix, envKey), envValue)
}
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
Namespace: "default",
SkipTLSVerify: true,
Debug: true,
DryRun: true,
Chart: "./chart/test",
Release: "test-release",
Prefix: env.prefix,
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
},
}
resolveSecrets(plugin)
// test that the subsitution works
fmt.Println(plugin.Config.Values)
if !strings.Contains(plugin.Config.Values, env.tag) {
t.Errorf("env var ${TAG} not resolved %s", env.tag)
}
if strings.Contains(plugin.Config.Values, "${TAG}") {
t.Errorf("env var ${TAG} not resolved %s", env.tag)
}
if plugin.Config.APIServer != env.api {
t.Errorf("env var ${API_SERVER} not resolved %s", env.api)
}
if plugin.Config.Token != env.token {
t.Errorf("env var ${KUBERNETES_TOKEN} not resolved %s", env.token)
}
if plugin.Config.ServiceAccount != env.account {
t.Errorf("env var ${SERVICE_ACCOUNT} not resolved %s", env.account)
}
// clean up
for envKey, _ := range envMap {
os.Unsetenv(fmt.Sprintf("%s_%s", env.prefix, envKey))
}
}
}
func TestDetHelmRepoAdd(t *testing.T) {
plugin := &Plugin{
Config: Config{
HelmCommand: nil,
@@ -94,26 +151,37 @@ func TestResolveSecrets(t *testing.T) {
Release: "test-release",
Prefix: "MY",
Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}",
ClientOnly: true,
HelmRepos: []string{
`"r1=http://r1.example.com"`, //handle quoted strings
`r2=http://r2.example.com`, //and unquoted strings
},
},
}
resolveSecrets(plugin)
// test that the subsitution works
if !strings.Contains(plugin.Config.Values, tag) {
t.Errorf("env var ${TAG} not resolved %s", tag)
}
if strings.Contains(plugin.Config.Values, "${TAG}") {
t.Errorf("env var ${TAG} not resolved %s", tag)
expected := []string{
"repo add r1 http://r1.example.com",
"repo add r2 http://r2.example.com",
}
if plugin.Config.APIServer != api {
t.Errorf("env var ${API_SERVER} not resolved %s", api)
for i, r := range plugin.Config.HelmRepos {
repos, err := doHelmRepoAdd(r)
if err != nil {
t.Error(err)
}
result := strings.Join(repos, " ")
if expected[i] != result {
t.Errorf("Helm cannot add remote repositories - expected %q - got %q",
expected[i],
result,
)
}
}
if plugin.Config.Token != token {
t.Errorf("env var ${KUBERNETES_TOKEN} not resolved %s", token)
}
if plugin.Config.ServiceAccount != account {
t.Errorf("env var ${SERVICE_ACCOUNT} not resolved %s", account)
}
func TestHelmAddRepositoryError(t *testing.T) {
_, err := doHelmRepoAdd("drone-helm=bad://drone-helm.example.com:443/stable")
if err == nil {
t.Errorf("Expect to see error when repo URL is invalid")
}
}