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

POC Test for Paring Mappings with Envconfig #812

Closed
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
104 changes: 104 additions & 0 deletions internal/broker/envconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package broker

import (
"os"

Check failure on line 4 in internal/broker/envconfig_test.go

View workflow job for this annotation

GitHub Actions / run-go-linter

File is not `gofmt`-ed with `-s` (gofmt)

Check failure on line 4 in internal/broker/envconfig_test.go

View workflow job for this annotation

GitHub Actions / run-go-linter

File is not `goimports`-ed (goimports)
"testing"

"github.com/vrischmann/envconfig"
"github.com/stretchr/testify/assert"
)
type MyConfig struct {
SapConveregedCloudMapping []struct {
BtpRegion string
SkrRegion string
} `envconfig:""`
}

func TestEnvConfig(t *testing.T) {

t.Run("should parse one to one mapping", func(t *testing.T) {
// given
os.Setenv("APP_SAP_CONVEREGED_CLOUD_MAPPING", "{cf-eu20-staging,eu-de-1}")
var cfg MyConfig

// when
err := envconfig.InitWithPrefix(&cfg, "APP")

// then
assert.NoError(t, err)
assert.NotNil(t, cfg.SapConveregedCloudMapping)
assert.Equal(t, 1, len(cfg.SapConveregedCloudMapping))
assert.Equal(t, "cf-eu20-staging", cfg.SapConveregedCloudMapping[0].BtpRegion)
assert.Equal(t, "eu-de-1", cfg.SapConveregedCloudMapping[0].SkrRegion)

})

t.Run("should parse one to many mapping", func(t *testing.T) {
// given
os.Setenv("APP_SAP_CONVEREGED_CLOUD_MAPPING", "{cf-eu20-staging,eu-de-1},{cf-eu20-staging,eu-de-2}")
var cfg MyConfig

// when
err := envconfig.InitWithPrefix(&cfg, "APP")

// then
assert.NoError(t, err)
assert.NotNil(t, cfg.SapConveregedCloudMapping)
assert.Equal(t, 2, len(cfg.SapConveregedCloudMapping))
assert.Equal(t, "cf-eu20-staging", cfg.SapConveregedCloudMapping[0].BtpRegion)
assert.Equal(t, "eu-de-1", cfg.SapConveregedCloudMapping[0].SkrRegion)

assert.Equal(t, "cf-eu20-staging", cfg.SapConveregedCloudMapping[1].BtpRegion)
assert.Equal(t, "eu-de-2", cfg.SapConveregedCloudMapping[1].SkrRegion)
})

t.Run("does not parse mappings with newlines", func(t *testing.T) {
// given
os.Setenv("APP_SAP_CONVEREGED_CLOUD_MAPPING", `
{cf-eu20-staging,eu-de-1},
{cf-eu20-staging,eu-de-2}
`)
var cfg MyConfig

// when
err := envconfig.InitWithPrefix(&cfg, "APP")

// then
assert.NoError(t, err)
assert.NotNil(t, cfg.SapConveregedCloudMapping)
assert.Equal(t, 2, len(cfg.SapConveregedCloudMapping))
assert.NotEqual(t, "cf-eu20-staging", cfg.SapConveregedCloudMapping[0].BtpRegion)
assert.Equal(t, "eu-de-1", cfg.SapConveregedCloudMapping[0].SkrRegion)

assert.NotEqual(t, "cf-eu20-staging", cfg.SapConveregedCloudMapping[1].BtpRegion)
assert.NotEqual(t, "eu-de-2", cfg.SapConveregedCloudMapping[1].SkrRegion)
})


t.Run("should parse multiple one to many mappings", func(t *testing.T) {
// given
os.Setenv("APP_SAP_CONVEREGED_CLOUD_MAPPING", `{cf-eu20-staging,eu-de-1},{cf-eu20-staging,eu-de-2},{cf-eu21-staging,eu-pl-1},{cf-eu21-staging,eu-pl-2}`)
var cfg MyConfig

// when
err := envconfig.InitWithPrefix(&cfg, "APP")

// then
assert.NoError(t, err)
assert.NotNil(t, cfg.SapConveregedCloudMapping)
assert.Equal(t, 4, len(cfg.SapConveregedCloudMapping))

assert.Equal(t, "cf-eu20-staging", cfg.SapConveregedCloudMapping[0].BtpRegion)
assert.Equal(t, "eu-de-1", cfg.SapConveregedCloudMapping[0].SkrRegion)

assert.Equal(t, "cf-eu20-staging", cfg.SapConveregedCloudMapping[1].BtpRegion)
assert.Equal(t, "eu-de-2", cfg.SapConveregedCloudMapping[1].SkrRegion)

assert.Equal(t, "cf-eu21-staging", cfg.SapConveregedCloudMapping[2].BtpRegion)
assert.Equal(t, "eu-pl-1", cfg.SapConveregedCloudMapping[2].SkrRegion)

assert.Equal(t, "cf-eu21-staging", cfg.SapConveregedCloudMapping[3].BtpRegion)
assert.Equal(t, "eu-pl-2", cfg.SapConveregedCloudMapping[3].SkrRegion)
})

}
Loading