Skip to content

Commit

Permalink
Added test to main.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgoltzsche committed Aug 5, 2019
1 parent 21a8ae0 commit de2cf16
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
29 changes: 19 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,36 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/mgoltzsche/helm-kustomize-plugin/pkg/helm"
)

const (
envConfig = "KUSTOMIZE_PLUGIN_CONFIG_STRING"
envRoot = "KUSTOMIZE_PLUGIN_CONFIG_ROOT"
)

func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "helm-kustomize-plugin usage: %s FILE\nprovided args: %+v\n", os.Args[0], os.Args[1:])
log.SetFlags(0)
rawCfg := os.Getenv(envConfig)
if len(os.Args) != 2 && rawCfg == "" {
fmt.Fprintf(os.Stderr, "helm-kustomize-plugin usage: %s [FILE]\n\nENV VARS:\n %s\n [%s]\n\nprovided args: %+v\n", os.Args[0], envRoot, envConfig, os.Args[1:])
os.Exit(1)
}
log.SetFlags(0)
f, err := os.Open(os.Args[1])
handleError(err)
defer f.Close()
fmt.Fprintf(os.Stderr, "##ARGS: %s %s\n", os.Args[0], os.Args[1])
cfg, err := helm.ReadGeneratorConfig(f)
if rawCfg == "" {
b, err := ioutil.ReadFile(os.Args[1])
handleError(err)
rawCfg = string(b)
}
cfg, err := helm.ReadGeneratorConfig(strings.NewReader(rawCfg))
handleError(err)
cfg.RootDir = os.Getenv("KUSTOMIZE_PLUGIN_CONFIG_ROOT")
cfg.RootDir = os.Getenv(envRoot)
if cfg.RootDir == "" {
handleError(fmt.Errorf("no KUSTOMIZE_PLUGIN_CONFIG_ROOT env var provided"))
handleError(fmt.Errorf("no %s env var provided", envRoot))
}
cfg.BaseDir, err = os.Getwd()
handleError(err)
Expand Down
49 changes: 49 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestMain(t *testing.T) {
dir, err := ioutil.TempDir("", "helmkustomizeplugintestdir-")
require.NoError(t, err)
defer os.RemoveAll(dir)
file := filepath.Join("example", "jenkins", "jenkins-chart.yaml")
file, err = filepath.Abs(file)
require.NoError(t, err)
outFile := filepath.Join(dir, "rendered.yaml")
runMain(outFile, filepath.Dir(file), file)
b, err := ioutil.ReadFile(outFile)
require.NoError(t, err)
require.Contains(t, string(b), "- host: jenkins.example.org\n")
}

func runMain(outFile string, wd string, args ...string) {
wdOrig, err := os.Getwd()
if err != nil {
panic(err)
}
os.Setenv("KUSTOMIZE_PLUGIN_CONFIG_ROOT", wdOrig)
err = os.Chdir(wd)
if err != nil {
panic(err)
}
defer os.Chdir(wdOrig)
os.Args = append([]string{"testee"}, args...)
stdout := os.Stdout
f, err := os.OpenFile(outFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer f.Close()
os.Stdout = f
defer func() {
os.Stdout = stdout
}()
main()
}

0 comments on commit de2cf16

Please sign in to comment.