Skip to content

Commit

Permalink
adds tests for apply output and sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
thdaraujo committed Jun 10, 2020
1 parent 9cb7d30 commit c3d0fa0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 43 deletions.
7 changes: 3 additions & 4 deletions cmd/apply_test.go
Expand Up @@ -4,9 +4,8 @@ import (
"testing"
)

// @TODO mock os.exec and check execution log/transcript?
func TestApply(t *testing.T) {
if false {
t.Fatalf("apply failed!")
}
// if false {
// t.Fatalf("apply failed!")
// }
}
19 changes: 10 additions & 9 deletions internal/config/projectconfig/apply.go
Expand Up @@ -11,7 +11,7 @@ import (
)

// Apply will bootstrap the runtime environment for the project
func Apply(dir string, projectContext *ZeroProjectConfig, applyEnvironments []string) {
func Apply(dir string, projectContext *ZeroProjectConfig, applyEnvironments []string) []string {
flog.Infof(":tada: Bootstrapping project %s. Please use the zero.[hcl, yaml] file to modify the project as needed. %s.", projectContext.Name)

flog.Infof("Cloud provider: %s", "AWS") // will this come from the config?
Expand All @@ -22,21 +22,22 @@ func Apply(dir string, projectContext *ZeroProjectConfig, applyEnvironments []st

// other details...

makeAll(dir, projectContext, applyEnvironments)
return makeAll(dir, projectContext, applyEnvironments)
}

func makeAll(dir string, projectContext *ZeroProjectConfig, applyEnvironments []string) error {
func makeAll(dir string, projectContext *ZeroProjectConfig, applyEnvironments []string) []string {
environmentArg := fmt.Sprintf("ENVIRONMENT=%s", strings.Join(applyEnvironments, ","))
envars := []string{environmentArg}

outputs := []string{}

for _, module := range projectContext.Modules {
// TODO what's the root dir for these modules?
// what's the real path to these modules? It's probably not the module name...
// @TODO what's the root dir for these modules?
modulePath := path.Join(dir, projectContext.Name, module.Files.Directory)

// @TODO mock exec?
output := util.ExecuteCommandOutput(exec.Command("make", environmentArg), modulePath, envars)
fmt.Println(output)
flog.Infof("%s", output)

outputs = append(outputs, output)
}
return nil
return outputs
}
31 changes: 31 additions & 0 deletions internal/config/projectconfig/apply_test.go
@@ -0,0 +1,31 @@
package projectconfig_test

import (
"testing"

"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/stretchr/testify/assert"
)

func TestApply(t *testing.T) {
// @TODO is there a way to do this without relative paths?
// execCMD will use the current folder as target...
dir := "../../../tests/test_data/"
projectName := "sample_project"
projectContext := &projectconfig.ZeroProjectConfig{
Name: projectName,
Modules: projectconfig.EKSGoReactSampleModules(),
}
applyEnvironments := []string{"staging", "production"}

want := []string{
"make module zero-aws-eks-stack\n",
"make module zero-deployable-backend\n",
"make module zero-deployable-react-frontend\n",
}

t.Run("Should run apply and execute make on each folder module", func(t *testing.T) {
got := projectconfig.Apply(dir, projectContext, applyEnvironments)
assert.Equal(t, want, got)
})
}
45 changes: 15 additions & 30 deletions internal/config/projectconfig/project_config_test.go
Expand Up @@ -18,42 +18,27 @@ func TestLoadConfig(t *testing.T) {
}
defer os.Remove(file.Name())
file.Write([]byte(validConfigContent()))
filePath := file.Name()

type args struct {
filePath string
}

modules := projectconfig.EKSGoReactSampleModules()
infrastructureModules := projectconfig.InfrastructureSampleModules()
modules := projectconfig.InfrastructureSampleModules()
sampleModules := projectconfig.EKSGoReactSampleModules()

for k, v := range infrastructureModules {
for k, v := range sampleModules {
modules[k] = v
}

expected := &projectconfig.ZeroProjectConfig{
want := &projectconfig.ZeroProjectConfig{
Name: "abc",
Modules: modules,
}

tests := []struct {
name string
args args
want *projectconfig.ZeroProjectConfig
}{
{
"Working config",
args{filePath: file.Name()},
expected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// @TODO handle nil/empty map unmarshall case?
if got := projectconfig.LoadConfig(tt.args.filePath); !cmp.Equal(got, tt.want, cmpopts.EquateEmpty()) {
t.Errorf(cmp.Diff(got, tt.want))
}
})
}
t.Run("Should load and unmarshall config correctly", func(t *testing.T) {
got := projectconfig.LoadConfig(filePath)
if !cmp.Equal(want, got, cmpopts.EquateEmpty()) {
t.Errorf("projectconfig.ZeroProjectConfig.Unmarshal mismatch (-want +got):\n%s", cmp.Diff(want, got))
}
})

}

func validConfigContent() string {
Expand All @@ -72,15 +57,15 @@ modules:
files:
dir: infrastructure
repo: https://github.com/myorg/infrastructure
zero-aws-eks-stack:
aws-eks-stack:
files:
dir: zero-aws-eks-stack
repo: github.com/commitdev/zero-aws-eks-stack
zero-deployable-backend:
deployable-backend:
files:
dir: zero-deployable-backend
repo: github.com/commitdev/zero-deployable-backend
zero-deployable-react-frontend:
deployable-react-frontend:
files:
dir: zero-deployable-react-frontend
repo: github.com/commitdev/zero-deployable-react-frontend
Expand Down
2 changes: 2 additions & 0 deletions tests/test_data/sample_project/zero-aws-eks-stack/Makefile
@@ -0,0 +1,2 @@
current_dir:
@echo "make module" `pwd | xargs basename`
@@ -0,0 +1,2 @@
current_dir:
@echo "make module" `pwd | xargs basename`
@@ -0,0 +1,2 @@
current_dir:
@echo "make module" `pwd | xargs basename`

0 comments on commit c3d0fa0

Please sign in to comment.