|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/uselagoon/build-deploy-tool/internal/dbaasclient" |
| 10 | + "github.com/uselagoon/build-deploy-tool/internal/helpers" |
| 11 | +) |
| 12 | + |
| 13 | +func TestIdentifyServices(t *testing.T) { |
| 14 | + type args struct { |
| 15 | + alertContact string |
| 16 | + statusPageID string |
| 17 | + projectName string |
| 18 | + environmentName string |
| 19 | + branch string |
| 20 | + prNumber string |
| 21 | + prHeadBranch string |
| 22 | + prBaseBranch string |
| 23 | + environmentType string |
| 24 | + buildType string |
| 25 | + activeEnvironment string |
| 26 | + standbyEnvironment string |
| 27 | + cacheNoCache string |
| 28 | + serviceID string |
| 29 | + secretPrefix string |
| 30 | + projectVars string |
| 31 | + envVars string |
| 32 | + lagoonVersion string |
| 33 | + lagoonYAML string |
| 34 | + valuesFilePath string |
| 35 | + templatePath string |
| 36 | + } |
| 37 | + tests := []struct { |
| 38 | + name string |
| 39 | + args args |
| 40 | + want []string |
| 41 | + wantServices []identifyServices |
| 42 | + wantErr bool |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "test1 check LAGOON_FASTLY_SERVICE_IDS with secret no values", |
| 46 | + args: args{ |
| 47 | + alertContact: "alertcontact", |
| 48 | + statusPageID: "statuspageid", |
| 49 | + projectName: "example-project", |
| 50 | + environmentName: "main", |
| 51 | + environmentType: "production", |
| 52 | + buildType: "branch", |
| 53 | + lagoonVersion: "v2.7.x", |
| 54 | + branch: "main", |
| 55 | + projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FASTLY_SERVICE_IDS","value":"example.com:service-id:true:annotationscom","scope":"build"}]`, |
| 56 | + envVars: `[]`, |
| 57 | + lagoonYAML: "../test-resources/identify-ingress/test1/lagoon.yml", |
| 58 | + templatePath: "../test-resources/output", |
| 59 | + }, |
| 60 | + want: []string{"node"}, |
| 61 | + wantServices: []identifyServices{{Name: "node", Type: "node"}}, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "test16 autogenerated routes where lagoon.name of service does not match service names", |
| 65 | + args: args{ |
| 66 | + alertContact: "alertcontact", |
| 67 | + statusPageID: "statuspageid", |
| 68 | + projectName: "content-example-com", |
| 69 | + environmentName: "feature-migration", |
| 70 | + environmentType: "development", |
| 71 | + buildType: "branch", |
| 72 | + lagoonVersion: "v2.7.x", |
| 73 | + branch: "feature/migration", |
| 74 | + projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${environment}.${project}.example.com","scope":"internal_system"}]`, |
| 75 | + envVars: `[]`, |
| 76 | + lagoonYAML: "../test-resources/identify-ingress/test16/lagoon.yml", |
| 77 | + templatePath: "../test-resources/output", |
| 78 | + }, |
| 79 | + want: []string{"cli", "nginx-php", "mariadb", "redis"}, |
| 80 | + wantServices: []identifyServices{ |
| 81 | + {Name: "cli", Type: "cli-persistent"}, |
| 82 | + {Name: "nginx-php", Type: "nginx-php-persistent"}, |
| 83 | + {Name: "mariadb", Type: "mariadb-dbaas"}, |
| 84 | + {Name: "redis", Type: "redis"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + for _, tt := range tests { |
| 89 | + t.Run(tt.name, func(t *testing.T) { |
| 90 | + // set the environment variables from args |
| 91 | + err := os.Setenv("MONITORING_ALERTCONTACT", tt.args.alertContact) |
| 92 | + if err != nil { |
| 93 | + t.Errorf("%v", err) |
| 94 | + } |
| 95 | + err = os.Setenv("MONITORING_STATUSPAGEID", tt.args.statusPageID) |
| 96 | + if err != nil { |
| 97 | + t.Errorf("%v", err) |
| 98 | + } |
| 99 | + err = os.Setenv("PROJECT", tt.args.projectName) |
| 100 | + if err != nil { |
| 101 | + t.Errorf("%v", err) |
| 102 | + } |
| 103 | + err = os.Setenv("ENVIRONMENT", tt.args.environmentName) |
| 104 | + if err != nil { |
| 105 | + t.Errorf("%v", err) |
| 106 | + } |
| 107 | + err = os.Setenv("BRANCH", tt.args.branch) |
| 108 | + if err != nil { |
| 109 | + t.Errorf("%v", err) |
| 110 | + } |
| 111 | + err = os.Setenv("LAGOON_GIT_BRANCH", tt.args.branch) |
| 112 | + if err != nil { |
| 113 | + t.Errorf("%v", err) |
| 114 | + } |
| 115 | + err = os.Setenv("PR_NUMBER", tt.args.prNumber) |
| 116 | + if err != nil { |
| 117 | + t.Errorf("%v", err) |
| 118 | + } |
| 119 | + err = os.Setenv("PR_HEAD_BRANCH", tt.args.prHeadBranch) |
| 120 | + if err != nil { |
| 121 | + t.Errorf("%v", err) |
| 122 | + } |
| 123 | + err = os.Setenv("PR_BASE_BRANCH", tt.args.prBaseBranch) |
| 124 | + if err != nil { |
| 125 | + t.Errorf("%v", err) |
| 126 | + } |
| 127 | + err = os.Setenv("ENVIRONMENT_TYPE", tt.args.environmentType) |
| 128 | + if err != nil { |
| 129 | + t.Errorf("%v", err) |
| 130 | + } |
| 131 | + err = os.Setenv("BUILD_TYPE", tt.args.buildType) |
| 132 | + if err != nil { |
| 133 | + t.Errorf("%v", err) |
| 134 | + } |
| 135 | + err = os.Setenv("ACTIVE_ENVIRONMENT", tt.args.activeEnvironment) |
| 136 | + if err != nil { |
| 137 | + t.Errorf("%v", err) |
| 138 | + } |
| 139 | + err = os.Setenv("STANDBY_ENVIRONMENT", tt.args.standbyEnvironment) |
| 140 | + if err != nil { |
| 141 | + t.Errorf("%v", err) |
| 142 | + } |
| 143 | + err = os.Setenv("LAGOON_FASTLY_NOCACHE_SERVICE_ID", tt.args.cacheNoCache) |
| 144 | + if err != nil { |
| 145 | + t.Errorf("%v", err) |
| 146 | + } |
| 147 | + err = os.Setenv("LAGOON_PROJECT_VARIABLES", tt.args.projectVars) |
| 148 | + if err != nil { |
| 149 | + t.Errorf("%v", err) |
| 150 | + } |
| 151 | + err = os.Setenv("LAGOON_ENVIRONMENT_VARIABLES", tt.args.envVars) |
| 152 | + if err != nil { |
| 153 | + t.Errorf("%v", err) |
| 154 | + } |
| 155 | + err = os.Setenv("LAGOON_VERSION", tt.args.lagoonVersion) |
| 156 | + if err != nil { |
| 157 | + t.Errorf("%v", err) |
| 158 | + } |
| 159 | + generator, err := generatorInput(false) |
| 160 | + if err != nil { |
| 161 | + t.Errorf("%v", err) |
| 162 | + } |
| 163 | + generator.LagoonYAML = tt.args.lagoonYAML |
| 164 | + // add dbaasclient overrides for tests |
| 165 | + generator.DBaaSClient = dbaasclient.NewClient(dbaasclient.Client{ |
| 166 | + RetryMax: 5, |
| 167 | + RetryWaitMin: time.Duration(10) * time.Millisecond, |
| 168 | + RetryWaitMax: time.Duration(50) * time.Millisecond, |
| 169 | + }) |
| 170 | + got, got2, err := IdentifyServices(generator) |
| 171 | + if (err != nil) != tt.wantErr { |
| 172 | + t.Errorf("IdentifyServices() error = %v, wantErr %v", err, tt.wantErr) |
| 173 | + return |
| 174 | + } |
| 175 | + if !reflect.DeepEqual(got, tt.want) { |
| 176 | + t.Errorf("IdentifyServices() = %v, want %v", got, tt.want) |
| 177 | + } |
| 178 | + if !reflect.DeepEqual(got2, tt.wantServices) { |
| 179 | + t.Errorf("IdentifyServices() = %v, want %v", got2, tt.wantServices) |
| 180 | + } |
| 181 | + t.Cleanup(func() { |
| 182 | + helpers.UnsetEnvVars(nil) |
| 183 | + }) |
| 184 | + }) |
| 185 | + } |
| 186 | +} |
0 commit comments