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

Add test to show random struct key sorting in backend config #176

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
84 changes: 84 additions & 0 deletions pkg/utils/json_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package utils_test

import (
"os"
"path"
"testing"

// "utils"

c2 "github.com/cloudposse/atmos/pkg/component"
c "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/utils"

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

func TestBackendConfig(t *testing.T) {
var err error
var component string
var stack string

// Add atmos.yaml for test config
r, err := os.Open("../component/atmos.yaml")
if err != nil {
panic(err)
}
defer r.Close()
w, err := os.Create("./atmos.yaml")
if err != nil {
panic(err)
}
defer w.Close()
w.ReadFrom(r)

var tenant1Ue2DevTestTestComponent map[string]any
component = "test/test-component"
stack = "tenant1-ue2-dev"

tenant1Ue2DevTestTestComponent, err = c2.ProcessComponentInStack(component, stack)
assert.Nil(t, err)
assert.NotNil(t, tenant1Ue2DevTestTestComponent)

var componentBackendConfig = map[string]any{
"terraform": map[string]any{
"backend": map[string]any{
"s3": tenant1Ue2DevTestTestComponent["backend"],
},
},
}

var backendFilePath = path.Join(
c.Config.BasePath,
c.Config.Components.Terraform.BasePath,
"test", // info.ComponentFolderPrefix,
"test-component", // info.FinalComponent,
"backend.tf.json",
)

err = utils.WriteToFileAsJSON(backendFilePath, componentBackendConfig, 0644)
assert.Nil(t, err)

data, err := os.ReadFile(backendFilePath)
assert.Nil(t, err)

assert.Equal(t, utils.RemoveWhitespace(`{
"terraform": {
"backend": {
"s3": {
"encrypt": true,
"key": "terraform.tfstate",
"region": "us-east-1",
"role_arn": null,
"workspace_key_prefix": "app",
"acl": "bucket-owner-full-control",
"bucket": "sts-gbl-tfstate-backend",
"dynamodb_table": "sts-gbl-tfstate-backend-lock"
}
}
}
}`), utils.RemoveWhitespace(string(data)))

// Remove config file
os.Remove("./atmos.yaml")
}
14 changes: 14 additions & 0 deletions pkg/utils/string_utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package utils

import (
"strings"
)

// UniqueStrings returns a unique subset of the string slice provided
func UniqueStrings(input []string) []string {
u := make([]string, 0, len(input))
Expand All @@ -14,3 +18,13 @@ func UniqueStrings(input []string) []string {

return u
}

func RemoveWhitespace(input string) string {
replaceable := [...]string{"\t", "\n", " "}

for _, item := range replaceable {
input = strings.Replace(input, item, "", -1)
}

return input
}