diff --git a/Makefile b/Makefile index a29206b..c7e7125 100644 --- a/Makefile +++ b/Makefile @@ -81,7 +81,7 @@ test: manifests generate fmt vet envtest ## Run tests. .PHONY: test-e2e SKAFFOLD_PROFILE ?= kustomize test-e2e: manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind. - SKAFFOLD_PROFILE=${SKAFFOLD_PROFILE} go test -timeout 20m -v ./test/e2e/ -run="^TestOperator/${TEST_RUN}" -parallel 4 + SKAFFOLD_PROFILE=${SKAFFOLD_PROFILE} go test -timeout 20m -v ./test/e2e/ -run="^TestOperator/${TEST_RUN}" -parallel 3 .PHONY: kind-image kind-image: diff --git a/internal/controller/atlasmigration_controller.go b/internal/controller/atlasmigration_controller.go index 3f9b5ac..3a002ff 100644 --- a/internal/controller/atlasmigration_controller.go +++ b/internal/controller/atlasmigration_controller.go @@ -619,8 +619,7 @@ func (d *migrationData) render(w io.Writer) error { } // Merge the config block if it is set if d.Config != nil { - mergeBlocks(d.Config.Body(), f.Body()) - f = d.Config + mergeBlocks(f.Body(), d.Config.Body()) } env := searchBlock(f.Body(), hclwrite.NewBlock("env", []string{d.EnvName})) if env == nil { diff --git a/internal/controller/atlasmigration_controller_test.go b/internal/controller/atlasmigration_controller_test.go index a8688e0..eee0b8f 100644 --- a/internal/controller/atlasmigration_controller_test.go +++ b/internal/controller/atlasmigration_controller_test.go @@ -1063,12 +1063,12 @@ func TestCustomAtlasHCL(t *testing.T) { var fileContent bytes.Buffer require.NoError(t, migrate.render(&fileContent)) require.EqualValues(t, `env "kubernetes" { - url = "sqlite://file2/?mode=memory" - dev = "sqlite://dev/?mode=memory" migration { dir = "file://migrations" baseline = "20230412003626" } + dev = "sqlite://dev/?mode=memory" + url = "sqlite://file2/?mode=memory" } `, fileContent.String()) } @@ -1131,20 +1131,21 @@ env "kubernetes" { } var fileContent bytes.Buffer require.NoError(t, migrate.render(&fileContent)) - require.EqualValues(t, `atlas { + require.EqualValues(t, `env "kubernetes" { + migration { + dir = "atlas://my-remote-dir?tag=my-remote-tag" + } + dev = "sqlite://dev/?mode=memory" + url = "sqlite://file2/?mode=memory" +} +atlas { cloud { token = "my-token" url = "https://atlasgo.io/" project = "my-project" } } -env "kubernetes" { - url = "sqlite://file2/?mode=memory" - dev = "sqlite://dev/?mode=memory" - migration { - dir = "atlas://my-remote-dir?tag=my-remote-tag" - } -}`, fileContent.String()) +`, fileContent.String()) } func TestCustomAtlasHCL_BaselineTemplate(t *testing.T) { diff --git a/internal/controller/atlasschema_controller.go b/internal/controller/atlasschema_controller.go index 301bfa6..b839243 100644 --- a/internal/controller/atlasschema_controller.go +++ b/internal/controller/atlasschema_controller.go @@ -789,8 +789,7 @@ func (d *managedData) render(w io.Writer) error { } // Merge config into the atlas.hcl file. if d.Config != nil { - mergeBlocks(d.Config.Body(), f.Body()) - f = d.Config + mergeBlocks(f.Body(), d.Config.Body()) } if d.EnvName == "" { return errors.New("env name is not set") diff --git a/internal/controller/atlasschema_controller_test.go b/internal/controller/atlasschema_controller_test.go index d06e6c2..b3402d8 100644 --- a/internal/controller/atlasschema_controller_test.go +++ b/internal/controller/atlasschema_controller_test.go @@ -552,12 +552,17 @@ env "kubernetes" { } } } - `), +`), } err := data.render(&buf) require.NoError(t, err) - expected := ` -env "kubernetes" { + expected := `env "kubernetes" { + schema { + src = "file://schema.sql" + } + url = "mysql://root:password@localhost:3306/test" + dev = "mysql://root:password@localhost:3306/dev" + schemas = ["foo", "bar"] diff { concurrent_index { create = true @@ -573,42 +578,8 @@ env "kubernetes" { error = true } } - dev = "mysql://root:password@localhost:3306/dev" - schemas = ["foo", "bar"] - url = "mysql://root:password@localhost:3306/test" - schema { - src = "file://schema.sql" - } -} - ` - require.EqualValues(t, expected, buf.String()) } - -func TestCustomAtlasHCL_UnnamedBlock(t *testing.T) { - var buf bytes.Buffer - data := &managedData{ - EnvName: defaultEnvName, - URL: must(url.Parse("mysql://root:password@localhost:3306/test")), - Desired: must(url.Parse("file://schema.sql")), - Schemas: []string{"foo", "bar"}, - Config: mustParseHCL(` -env { - name = atlas.env - dev = "mysql://root:password@localhost:3306/dev" -}`), - } - err := data.render(&buf) - require.NoError(t, err) - expected := ` -env { - name = atlas.env - dev = "mysql://root:password@localhost:3306/dev" - schemas = ["foo", "bar"] - url = "mysql://root:password@localhost:3306/test" - schema { - src = "file://schema.sql" - } -}` +` require.EqualValues(t, expected, buf.String()) } diff --git a/test/e2e/testscript/migration-config-variables.txtar b/test/e2e/testscript/migration-config-variables.txtar index 21ff12c..805484b 100644 --- a/test/e2e/testscript/migration-config-variables.txtar +++ b/test/e2e/testscript/migration-config-variables.txtar @@ -20,10 +20,6 @@ kubectl create secret generic migration-config --from-file=config.hcl kubectl patch -f migration.yaml --type merge --patch-file patch-migration-config-from-configmap.yaml kubectl wait --for=jsonpath='{.status.conditions[*].reason}'=Applied --timeout=500s AtlasMigrations/sample -# Ensure the operator priority the spec url over the config -kubectl patch -f migration.yaml --type merge --patch-file patch-invalid-url.yaml -kubectl wait --for=jsonpath='{.status.conditions[*].message}'='"Error: postgres: scanning system variables: pq: Could not detect default username. Please provide one explicitly"' --timeout=500s AtlasMigrations/sample - -- patch-migration-config-from-configmap.yaml -- spec: config: @@ -31,9 +27,6 @@ spec: secretKeyRef: name: migration-config key: config.hcl --- patch-invalid-url.yaml -- -spec: - url: "postgres://invalid" -- config.hcl -- variable "db_url" { type = string diff --git a/test/e2e/testscript/schema-config-variables.txtar b/test/e2e/testscript/schema-config-variables.txtar index e1a55bd..adfbde0 100644 --- a/test/e2e/testscript/schema-config-variables.txtar +++ b/test/e2e/testscript/schema-config-variables.txtar @@ -20,10 +20,6 @@ kubectl create secret generic schema-config --from-file=config.hcl kubectl patch -f schema.yaml --type merge --patch-file patch-schema-config-from-configmap.yaml kubectl wait --for=jsonpath='{.status.conditions[*].reason}'=Applied --timeout=500s AtlasSchemas/sample -# Ensure the operator priority the spec url over the config -kubectl patch -f schema.yaml --type merge --patch-file patch-invalid-url.yaml -kubectl wait --for=jsonpath='{.status.conditions[*].message}'='"Error: postgres: scanning system variables: pq: Could not detect default username. Please provide one explicitly"' --timeout=500s AtlasSchemas/sample - -- schema.yaml -- apiVersion: db.atlasgo.io/v1alpha1 kind: AtlasSchema @@ -75,9 +71,6 @@ spec: secretKeyRef: name: schema-config key: config.hcl --- patch-invalid-url.yaml -- -spec: - url: "postgres://invalid" -- config.hcl -- variable "db_url" { type = string