Refactor tests for configure-helper.sh by moving environment config to testdata.

This commit is contained in:
immutablet
2019-10-30 23:34:24 -07:00
parent ee4e1465ea
commit 576edaf072
8 changed files with 197 additions and 76 deletions

View File

@@ -106,35 +106,38 @@ func (c *ManifestTestCase) mustCreateManifestDstDir() {
}
}
func (c *ManifestTestCase) mustCreateEnv(envTemplate string, env interface{}) string {
func (c *ManifestTestCase) mustInvokeFunc(env interface{}, scriptName, targetTemplate string, templates ...string) {
envScriptPath := c.mustCreateEnv(env, targetTemplate, templates...)
args := fmt.Sprintf("source %q ; source %q; %s", envScriptPath, scriptName, c.manifestFuncName)
cmd := exec.Command("bash", "-c", args)
bs, err := cmd.CombinedOutput()
if err != nil {
c.t.Logf("%q", bs)
c.t.Fatalf("Failed to run %q: %v", scriptName, err)
}
c.t.Logf("%s", string(bs))
}
func (c *ManifestTestCase) mustCreateEnv(env interface{}, target string, templates ...string) string {
f, err := os.Create(filepath.Join(c.kubeHome, envScriptFileName))
if err != nil {
c.t.Fatalf("Failed to create envScript: %v", err)
}
defer f.Close()
t := template.Must(template.New("env").Parse(envTemplate))
t, err := template.ParseFiles(templates...)
if err != nil {
c.t.Fatalf("Failed to parse files %q, err: %v", templates, err)
}
if err = t.Execute(f, env); err != nil {
c.t.Fatalf("Failed to execute template: %v", err)
if err = t.ExecuteTemplate(f, target, env); err != nil {
c.t.Fatalf("Failed to execute template %s, err: %v", target, err)
}
return f.Name()
}
func (c *ManifestTestCase) mustInvokeFunc(envTemplate, scriptName string, env interface{}) {
envScriptPath := c.mustCreateEnv(envTemplate, env)
args := fmt.Sprintf("source %s ; source %s; %s", envScriptPath, scriptName, c.manifestFuncName)
cmd := exec.Command("bash", "-c", args)
bs, err := cmd.CombinedOutput()
if err != nil {
c.t.Logf("%s", bs)
c.t.Fatalf("Failed to run %s: %v", scriptName, err)
}
c.t.Logf("%s", string(bs))
}
func (c *ManifestTestCase) mustLoadPodFromManifest() {
json, err := ioutil.ReadFile(c.manifestDestination)
if err != nil {