Skip to content

Commit

Permalink
test: consolidate test seed data (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Jan 7, 2024
1 parent dfd5406 commit 24d525b
Show file tree
Hide file tree
Showing 236 changed files with 2,078 additions and 5,185 deletions.
2 changes: 1 addition & 1 deletion cmd/identify_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var featureFlagIdentify = &cobra.Command{
Aliases: []string{"f"},
Short: "Identify if a feature flag has been enabled",
RunE: func(cmd *cobra.Command, args []string) error {
generator, err := generatorInput(false)
generator, err := generator.GenerateInput(*rootCmd, false)
if err != nil {
return err
}
Expand Down
279 changes: 100 additions & 179 deletions cmd/identify_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package cmd
import (
"os"
"testing"
"time"

"github.com/uselagoon/build-deploy-tool/internal/dbaasclient"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
"github.com/uselagoon/build-deploy-tool/internal/testdata"
)

func TestIdentifyFeatureFlag(t *testing.T) {
Expand Down Expand Up @@ -35,67 +35,65 @@ func TestIdentifyFeatureFlag(t *testing.T) {
templatePath string
}
tests := []struct {
name string
args args
vars []helpers.EnvironmentVariable
want string
wantErr bool
name string
args testdata.TestData
templatePath string
varName string
vars []helpers.EnvironmentVariable
want string
wantErr bool
}{
{
name: "test1 check if flag is defined in lagoon project variables",
args: args{
name: "ROOTLESS_WORKLOAD",
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
want: "enabled",
name: "test1 check if flag is defined in lagoon project variables",
varName: "ROOTLESS_WORKLOAD",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/node/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD",
Value: "enabled",
Scope: "build",
},
},
}, true),
templatePath: "testdata/output",
want: "enabled",
},
{
name: "test2 check if flag is defined in lagoon environment variables",
args: args{
name: "ROOTLESS_WORKLOAD",
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]`,
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
want: "enabled",
name: "test2 check if flag is defined in lagoon environment variables",
varName: "ROOTLESS_WORKLOAD",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/node/lagoon.yml",
EnvVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD",
Value: "enabled",
Scope: "build",
},
},
}, true),
templatePath: "testdata/output",
want: "enabled",
},
{
name: "test3 check if force flag is defined in build variables",
args: args{
name: "ROOTLESS_WORKLOAD",
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
name: "test3 check if force flag is defined in build variables",
varName: "ROOTLESS_WORKLOAD",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/node/lagoon.yml",
}, true),
templatePath: "testdata/output",
vars: []helpers.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_FORCE_ROOTLESS_WORKLOAD",
Expand All @@ -105,22 +103,16 @@ func TestIdentifyFeatureFlag(t *testing.T) {
want: "enabled",
},
{
name: "test4 check if force flag is defined in build variables and default flag is ignored",
args: args{
name: "ROOTLESS_WORKLOAD",
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
name: "test4 check if force flag is defined in build variables and default flag is ignored",
varName: "ROOTLESS_WORKLOAD",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/node/lagoon.yml",
}, true),
templatePath: "testdata/output",
vars: []helpers.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_FORCE_ROOTLESS_WORKLOAD",
Expand All @@ -134,22 +126,22 @@ func TestIdentifyFeatureFlag(t *testing.T) {
want: "enabled",
},
{
name: "test5 check if force flag is defined in build variables and one defined in lagoon project variables is ignored",
args: args{
name: "ROOTLESS_WORKLOAD",
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
name: "test5 check if force flag is defined in build variables and one defined in lagoon project variables is ignored",
varName: "ROOTLESS_WORKLOAD",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/node/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD",
Value: "enabled",
Scope: "build",
},
},
}, true),
vars: []helpers.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_FORCE_ROOTLESS_WORKLOAD",
Expand All @@ -163,22 +155,22 @@ func TestIdentifyFeatureFlag(t *testing.T) {
want: "disabled",
},
{
name: "test6 check if default flag is ignored and lagoon project variable is used",
args: args{
name: "ROOTLESS_WORKLOAD",
alertContact: "alertcontact",
statusPageID: "statuspageid",
projectName: "example-project",
environmentName: "main",
environmentType: "production",
buildType: "branch",
lagoonVersion: "v2.7.x",
branch: "main",
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]`,
envVars: `[]`,
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
name: "test6 check if default flag is ignored and lagoon project variable is used",
varName: "ROOTLESS_WORKLOAD",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/node/lagoon.yml",
ProjectVariables: []lagoon.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD",
Value: "enabled",
Scope: "build",
},
},
}, true),
vars: []helpers.EnvironmentVariable{
{
Name: "LAGOON_FEATURE_FLAG_DEFAULT_ROOTLESS_WORKLOAD",
Expand All @@ -191,89 +183,18 @@ func TestIdentifyFeatureFlag(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set the environment variables from args
err := os.Setenv("MONITORING_ALERTCONTACT", tt.args.alertContact)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("MONITORING_STATUSPAGEID", tt.args.statusPageID)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("PROJECT", tt.args.projectName)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("ENVIRONMENT", tt.args.environmentName)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("BRANCH", tt.args.branch)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("PR_NUMBER", tt.args.prNumber)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("PR_HEAD_BRANCH", tt.args.prHeadBranch)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("PR_BASE_BRANCH", tt.args.prBaseBranch)
savedTemplates := tt.templatePath
generator, err := testdata.SetupEnvironment(*rootCmd, savedTemplates, tt.args)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("ENVIRONMENT_TYPE", tt.args.environmentType)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("BUILD_TYPE", tt.args.buildType)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("ACTIVE_ENVIRONMENT", tt.args.activeEnvironment)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("STANDBY_ENVIRONMENT", tt.args.standbyEnvironment)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_FASTLY_NOCACHE_SERVICE_ID", tt.args.cacheNoCache)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_PROJECT_VARIABLES", tt.args.projectVars)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_ENVIRONMENT_VARIABLES", tt.args.envVars)
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_VERSION", tt.args.lagoonVersion)
if err != nil {
t.Errorf("%v", err)
}
generator, err := generatorInput(false)
if err != nil {
t.Errorf("%v", err)
}
generator.LagoonYAML = tt.args.lagoonYAML
// add dbaasclient overrides for tests
generator.DBaaSClient = dbaasclient.NewClient(dbaasclient.Client{
RetryMax: 5,
RetryWaitMin: time.Duration(10) * time.Millisecond,
RetryWaitMax: time.Duration(50) * time.Millisecond,
})

for _, envVar := range tt.vars {
err = os.Setenv(envVar.Name, envVar.Value)
if err != nil {
t.Errorf("%v", err)
}
}
got, err := IdentifyFeatureFlag(generator, tt.args.name)
got, err := IdentifyFeatureFlag(generator, tt.varName)
if (err != nil) != tt.wantErr {
t.Errorf("IdentifyFeatureFlag() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
6 changes: 3 additions & 3 deletions cmd/identify_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var primaryIngressIdentify = &cobra.Command{
Aliases: []string{"pi"},
Short: "Identify the primary ingress for a specific environment",
RunE: func(cmd *cobra.Command, args []string) error {
generator, err := generatorInput(false)
generator, err := generator.GenerateInput(*rootCmd, false)
if err != nil {
return err
}
Expand All @@ -37,7 +37,7 @@ var ingressIdentify = &cobra.Command{
Aliases: []string{"i"},
Short: "Identify all ingress for a specific environment",
RunE: func(cmd *cobra.Command, args []string) error {
generator, err := generatorInput(false)
generator, err := generator.GenerateInput(*rootCmd, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -73,7 +73,7 @@ var autogenIngressIdentify = &cobra.Command{
Aliases: []string{"ci"},
Short: "Identify all created ingress object names for a specific environment",
RunE: func(cmd *cobra.Command, args []string) error {
generator, err := generatorInput(false)
generator, err := generator.GenerateInput(*rootCmd, false)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 24d525b

Please sign in to comment.