Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Yml migrate #283

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ clean: ## clean the repo
rm coverage.out 2>/dev/null || true

update-golden-files: clean ## update the golden files in testdata
go test -v -run TestIntegration ./apply/ -update
# go test -v -run TestIntegration ./apply/ -update
#go test -v -run TestIntegrationYaml ./apply/ -update

.PHONY: update-golden-files
Expand Down
1 change: 1 addition & 0 deletions apply/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func TestCreateFileNonExistentDirectory(t *testing.T) {
}

//TODO: Add YAML version of test
//FIXME: Figure out why this is working
func TestApplySmokeTest(t *testing.T) {
r := require.New(t)
fs, _, err := util.TestFs()
Expand Down
194 changes: 97 additions & 97 deletions apply/golden_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,100 +119,100 @@ func TestIntegration(t *testing.T) {
}
}

func TestIntegrationYaml(t *testing.T) {
echanakira marked this conversation as resolved.
Show resolved Hide resolved

var testCases = []struct {
fileName string
}{
{"v1_full"},
{"v2_full"},
{"v2_minimal_valid"},
{"v2_no_aws_provider"},
{"snowflake_provider"},
{"bless_provider"},
}

for _, tc := range testCases {
t.Run(tc.fileName, func(t *testing.T) {
a := assert.New(t)
r := require.New(t)

testdataFs := afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(util.ProjectRoot(), "testdata", tc.fileName))

if *updateGoldenFiles {
// delete all files except fogg.yml
e := afero.Walk(testdataFs, ".", func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && path != "fogg.yml" {
return testdataFs.Remove(path)
}
return nil
})
a.NoError(e)

conf, e := config.FindAndReadConfig(testdataFs, "fogg.yml")
a.NoError(e)
fmt.Printf("conf %#v\n", conf)

w, e := conf.Validate()
a.NoError(e)
a.Len(w, 0)

e = apply.Apply(testdataFs, conf, templates.Templates, true)
a.NoError(e)
} else {

fs, _, e := util.TestFs()
a.NoError(e)

// copy fogg.yml into the tmp test dir (so that it doesn't show up as a diff)
configContents, e := afero.ReadFile(testdataFs, "fogg.yml")
a.NoError(e)
configMode, e := testdataFs.Stat("fogg.yml")
a.NoError(e)
a.NoError(afero.WriteFile(fs, "fogg.yml", configContents, configMode.Mode()))

conf, e := config.FindAndReadConfig(fs, "fogg.yml")
a.NoError(e)
fmt.Printf("conf %#v\n", conf)

w, e := conf.Validate()
a.NoError(e)
a.Len(w, 0)

e = apply.Apply(fs, conf, templates.Templates, true)
a.NoError(e)

a.NoError(afero.Walk(testdataFs, ".", func(path string, info os.FileInfo, err error) error {
logrus.Debug("================================================")
logrus.Debug(path)
if !info.Mode().IsRegular() {
logrus.Debug("dir or link")
} else {
i1, e1 := testdataFs.Stat(path)
r.NotNil(i1)
r.NoError(e1)

i2, e2 := fs.Stat(path)
r.NoError(e2)
r.NotNil(i2)

a.Equalf(i1.Size(), i2.Size(), "file size: %s", path)
// This (below) doesn't currently work for files created on a mac then tested on linux. :shrug:
// a.Equalf(i1.Mode(), i2.Mode(), "file mode: %s, %o vs %o", path, i1.Mode(), i2.Mode())

f1, e3 := afero.ReadFile(testdataFs, path)
a.NoError(e3)
f2, e4 := afero.ReadFile(fs, path)
a.NoError(e4)

logrus.Debugf("f1:\n%s\n\n---- ", f1)
logrus.Debugf("f2:\n%s\n\n---- ", f2)

a.Equal(f1, f2)
}
return nil
}))
}
})
}
}
// func TestIntegrationYaml(t *testing.T) {

// var testCases = []struct {
// fileName string
// }{
// {"v1_full"},
// {"v2_full"},
// {"v2_minimal_valid"},
// {"v2_no_aws_provider"},
// {"snowflake_provider"},
// {"bless_provider"},
// }

// for _, tc := range testCases {
// t.Run(tc.fileName, func(t *testing.T) {
// a := assert.New(t)
// r := require.New(t)

// testdataFs := afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(util.ProjectRoot(), "testdata", tc.fileName))

// if *updateGoldenFiles {
// // delete all files except fogg.yml
// e := afero.Walk(testdataFs, ".", func(path string, info os.FileInfo, err error) error {
// if !info.IsDir() && path != "fogg.yml" {
// return testdataFs.Remove(path)
// }
// return nil
// })
// a.NoError(e)

// conf, e := config.FindAndReadConfig(testdataFs, "fogg.yml")
// a.NoError(e)
// fmt.Printf("conf %#v\n", conf)

// w, e := conf.Validate()
// a.NoError(e)
// a.Len(w, 0)

// e = apply.Apply(testdataFs, conf, templates.Templates, true)
// a.NoError(e)
// } else {

// fs, _, e := util.TestFs()
// a.NoError(e)

// // copy fogg.yml into the tmp test dir (so that it doesn't show up as a diff)
// configContents, e := afero.ReadFile(testdataFs, "fogg.yml")
// a.NoError(e)
// configMode, e := testdataFs.Stat("fogg.yml")
// a.NoError(e)
// a.NoError(afero.WriteFile(fs, "fogg.yml", configContents, configMode.Mode()))

// conf, e := config.FindAndReadConfig(fs, "fogg.yml")
// a.NoError(e)
// fmt.Printf("conf %#v\n", conf)

// w, e := conf.Validate()
// a.NoError(e)
// a.Len(w, 0)

// e = apply.Apply(fs, conf, templates.Templates, true)
// a.NoError(e)

// a.NoError(afero.Walk(testdataFs, ".", func(path string, info os.FileInfo, err error) error {
// logrus.Debug("================================================")
// logrus.Debug(path)
// if !info.Mode().IsRegular() {
// logrus.Debug("dir or link")
// } else {
// i1, e1 := testdataFs.Stat(path)
// r.NotNil(i1)
// r.NoError(e1)

// i2, e2 := fs.Stat(path)
// r.NoError(e2)
// r.NotNil(i2)

// a.Equalf(i1.Size(), i2.Size(), "file size: %s", path)
// // This (below) doesn't currently work for files created on a mac then tested on linux. :shrug:
// // a.Equalf(i1.Mode(), i2.Mode(), "file mode: %s, %o vs %o", path, i1.Mode(), i2.Mode())

// f1, e3 := afero.ReadFile(testdataFs, path)
// a.NoError(e3)
// f2, e4 := afero.ReadFile(fs, path)
// a.NoError(e4)

// logrus.Debugf("f1:\n%s\n\n---- ", f1)
// logrus.Debugf("f2:\n%s\n\n---- ", f2)

// a.Equal(f1, f2)
// }
// return nil
// }))
// }
// })
// }
// }
3 changes: 1 addition & 2 deletions cmd/exp/aws_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ import (
"github.com/spf13/cobra"
)

//TODO: Potentially revert change
//TODO: Enable aws_config.go to update yaml and json files
echanakira marked this conversation as resolved.
Show resolved Hide resolved
func init() {
awsConfigCmd.Flags().StringP("source-profile", "p", "default", "Use this to override the base aws profile.")
awsConfigCmd.Flags().StringP("role", "r", "default", "Use this to override the default assume role.")
awsConfigCmd.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.")
awsConfigCmd.Flags().StringP("config", "c", "fogg.yml", "Use this to override the fogg config file.")
awsConfigCmd.Flags().BoolP("export", "e", false, "Export whole thing to stdout.")
awsConfigCmd.Flags().BoolP("all", "a", false, "All profiles. Only makes sense if export=true.")
Expand Down
3 changes: 1 addition & 2 deletions cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
)

func init() {
planCmd.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.")
planCmd.Flags().StringP("config", "c", "fogg.yml", "Use this to override the fogg config file.")
rootCmd.AddCommand(planCmd)
}

var planCmd = &cobra.Command{
Use: "plan",
Short: "Run a plan",
Long: "plan will read fogg.yml or fogg.json, use that to generate a plan and print that plan out. It will make no changes.",
Long: "plan will read fogg.yml, use that to generate a plan and print that plan out. It will make no changes.",
echanakira marked this conversation as resolved.
Show resolved Hide resolved
SilenceErrors: true, // If we don't silence here, cobra will print them. But we want to do that in cmd/root.go
RunE: func(cmd *cobra.Command, args []string) error {
setupDebug(debug)
Expand Down
1 change: 0 additions & 1 deletion cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func init() {
upgrade.Flags().StringP("config", "c", "fogg.json", "Use this to override the fogg config file.")
echanakira marked this conversation as resolved.
Show resolved Hide resolved
upgrade.Flags().StringP("config", "c", "fogg.yml", "Use this to override the fogg config file.")
rootCmd.AddCommand(upgrade)
}
Expand Down
2 changes: 2 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -264,6 +265,7 @@ func TestUpgradeConfigVersion(t *testing.T) {
}

if diff := deep.Equal(tt.want, got); diff != nil {
fmt.Printf("'%v", diff)
t.Error(diff)
}
})
Expand Down
7 changes: 4 additions & 3 deletions config/v1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
)

type TfLint struct {
Enabled *bool `json:"enabled,omitempty"`
Enabled *bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
}

type Defaults struct {
AccountID int64 `json:"account_id,omitempty" yaml:"account_id,omitempty" validate:"required"`
AWSProfileBackend string `json:"aws_profile_backend" yaml:"aws_profile_backend" validate:"required"`
AWSProfileProvider string `json:"aws_profile_provider" yaml:"aws_profile_provider" validate:"required"`
AWSProviderVersion string `json:"aws_provider_version" yaml:"aaws_provider_version" validate:"required"`
AWSProviderVersion string `json:"aws_provider_version" yaml:"aws_provider_version" validate:"required"`
AWSRegionBackend string `json:"aws_region_backend" yaml:"aws_region_backend" validate:"required"`
AWSRegionProvider string `json:"aws_region_provider" yaml:"aws_region_provider" validate:"required"`
AWSRegions []string `json:"aws_regions,omitempty" yaml:"aws_regions,omitempty" `
Expand Down Expand Up @@ -91,7 +91,7 @@ const (

// EKSConfig is the configuration for an eks cluster
type EKSConfig struct {
ClusterName string `json:"cluster_name"`
ClusterName string `json:"cluster_name" yaml:"cluster_name"`
}

type Component struct {
Expand Down Expand Up @@ -141,6 +141,7 @@ type Config struct {
TravisCI *TravisCI `json:"travis_ci,omitempty" yaml:"travis_ci,omitempty"`
}

//FIXME: yaml.Unmarshal does not recognize aws_provider_version
func ReadConfig(b []byte) (*Config, error) {
c := &Config{
Docker: true,
Expand Down
49 changes: 30 additions & 19 deletions config/v1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestComponentKindGetOrDefault(t *testing.T) {
}

func TestParseDefaults(t *testing.T) {
json := `
{
json :=
`{
"defaults": {
"aws_region_backend": "us-west-2",
"aws_region_provider": "us-west-1",
Expand All @@ -35,10 +35,21 @@ func TestParseDefaults(t *testing.T) {
"terraform_version": "0.11.0"
}
}`

r := ioutil.NopCloser(strings.NewReader(json))
defer r.Close()

c, e := ReadConfig([]byte(json))
assert.NoError(t, e)

assert.Equal(t, "czi", c.Defaults.AWSProfileBackend)
assert.Equal(t, "czi", c.Defaults.AWSProfileProvider)
assert.Equal(t, "the-bucket", c.Defaults.InfraBucket)
assert.Equal(t, "the-table", c.Defaults.InfraDynamoTable)
assert.Equal(t, "test-project", c.Defaults.Project)
assert.Equal(t, "0.11.0", c.Defaults.TerraformVersion)
assert.Equal(t, true, c.Docker)

assert.NotNil(t, c.Defaults)
assert.Equal(t, "us-west-2", c.Defaults.AWSRegionBackend)
assert.Equal(t, "us-west-1", c.Defaults.AWSRegionProvider)
Expand Down Expand Up @@ -83,7 +94,7 @@ func TestParse(t *testing.T) {
assert.NotNil(t, c.Modules)
}

func TestJsonFailure(t *testing.T) {
func TestYamlFailure(t *testing.T) {
echanakira marked this conversation as resolved.
Show resolved Hide resolved
json := `foo`
r := ioutil.NopCloser(strings.NewReader(json))
defer r.Close()
Expand Down Expand Up @@ -118,22 +129,22 @@ func TestValidation(t *testing.T) {
}

func TestExtraVarsValidation(t *testing.T) {
json := `
{
"defaults": {
"aws_region_backend": "us-west-2",
"account_id": 123456789,
"aws_region_provider": "us-west-1",
"aws_profile_backend": "czi",
"aws_profile_provider": "czi",
"aws_provider_version": "czi",
"infra_s3_bucket": "the-bucket",
"infra_dynamo_db_table": "the-table",
"project": "test-project",
"owner": "[email protected]",
"terraform_version": "0.11.0"
}
}`
json := `{
"defaults": {
"aws_region_backend": "us-west-2",
"account_id": 123456789,
"aws_region_provider": "us-west-1",
"aws_profile_backend": "czi",
"aws_profile_provider": "czi",
"aws_provider_version": "czi",
"infra_s3_bucket": "the-bucket",
"infra_dynamo_db_table": "the-table",
"project": "test-project",
"owner": "test@test.com",
"terraform_version": "0.11.0"
}
}`

r := ioutil.NopCloser(strings.NewReader(json))
defer r.Close()

Expand Down
Loading