-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial pass at service cleanup
- Loading branch information
1 parent
a429aff
commit de56df8
Showing
5 changed files
with
347 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
generator "github.com/uselagoon/build-deploy-tool/internal/generator" | ||
"github.com/uselagoon/build-deploy-tool/internal/helpers" | ||
) | ||
|
||
type identifyServices struct { | ||
Name string `json:"name"` | ||
Type string `json:"type"` | ||
} | ||
|
||
var servicesIdentify = &cobra.Command{ | ||
Use: "services", | ||
Aliases: []string{"s"}, | ||
Short: "Identify services that this build would create", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
generator, err := generatorInput(false) | ||
if err != nil { | ||
return err | ||
} | ||
ret, _, err := IdentifyServices(generator) | ||
if err != nil { | ||
return err | ||
} | ||
retJSON, _ := json.Marshal(ret) | ||
fmt.Println(string(retJSON)) | ||
return nil | ||
}, | ||
} | ||
|
||
// IdentifyServices identifies services that this build would create | ||
func IdentifyServices(g generator.GeneratorInput) ([]string, []identifyServices, error) { | ||
lagoonBuild, err := generator.NewGenerator( | ||
g, | ||
) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
services := []string{} | ||
serviceTypes := []identifyServices{} | ||
for _, service := range lagoonBuild.BuildValues.Services { | ||
if service.Type != "" { | ||
services = helpers.AppendIfMissing(services, service.OverrideName) | ||
serviceTypes = AppendIfMissing(serviceTypes, identifyServices{ | ||
Name: service.OverrideName, | ||
Type: service.Type, | ||
}) | ||
} | ||
} | ||
return services, serviceTypes, nil | ||
} | ||
|
||
func init() { | ||
identifyCmd.AddCommand(servicesIdentify) | ||
} | ||
|
||
func AppendIfMissing(slice []identifyServices, i identifyServices) []identifyServices { | ||
for _, ele := range slice { | ||
if ele.Name == i.Name { | ||
return slice | ||
} | ||
} | ||
return append(slice, i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/uselagoon/build-deploy-tool/internal/dbaasclient" | ||
"github.com/uselagoon/build-deploy-tool/internal/helpers" | ||
) | ||
|
||
func TestIdentifyServices(t *testing.T) { | ||
type args struct { | ||
alertContact string | ||
statusPageID string | ||
projectName string | ||
environmentName string | ||
branch string | ||
prNumber string | ||
prHeadBranch string | ||
prBaseBranch string | ||
environmentType string | ||
buildType string | ||
activeEnvironment string | ||
standbyEnvironment string | ||
cacheNoCache string | ||
serviceID string | ||
secretPrefix string | ||
projectVars string | ||
envVars string | ||
lagoonVersion string | ||
lagoonYAML string | ||
valuesFilePath string | ||
templatePath string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
wantServices []identifyServices | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test1 check LAGOON_FASTLY_SERVICE_IDS with secret no values", | ||
args: args{ | ||
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_FASTLY_SERVICE_IDS","value":"example.com:service-id:true:annotationscom","scope":"build"}]`, | ||
envVars: `[]`, | ||
lagoonYAML: "../test-resources/identify-ingress/test1/lagoon.yml", | ||
templatePath: "../test-resources/output", | ||
}, | ||
want: []string{"node"}, | ||
wantServices: []identifyServices{{Name: "node", Type: "node"}}, | ||
}, | ||
{ | ||
name: "test16 autogenerated routes where lagoon.name of service does not match service names", | ||
args: args{ | ||
alertContact: "alertcontact", | ||
statusPageID: "statuspageid", | ||
projectName: "content-example-com", | ||
environmentName: "feature-migration", | ||
environmentType: "development", | ||
buildType: "branch", | ||
lagoonVersion: "v2.7.x", | ||
branch: "feature/migration", | ||
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${environment}.${project}.example.com","scope":"internal_system"}]`, | ||
envVars: `[]`, | ||
lagoonYAML: "../test-resources/identify-ingress/test16/lagoon.yml", | ||
templatePath: "../test-resources/output", | ||
}, | ||
want: []string{"cli", "nginx-php", "mariadb", "redis"}, | ||
wantServices: []identifyServices{ | ||
{Name: "cli", Type: "cli-persistent"}, | ||
{Name: "nginx-php", Type: "nginx-php-persistent"}, | ||
{Name: "mariadb", Type: "mariadb-dbaas"}, | ||
{Name: "redis", Type: "redis"}, | ||
}, | ||
}, | ||
} | ||
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("LAGOON_GIT_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) | ||
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, | ||
}) | ||
got, got2, err := IdentifyServices(generator) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("IdentifyServices() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("IdentifyServices() = %v, want %v", got, tt.want) | ||
} | ||
if !reflect.DeepEqual(got2, tt.wantServices) { | ||
t.Errorf("IdentifyServices() = %v, want %v", got2, tt.wantServices) | ||
} | ||
t.Cleanup(func() { | ||
helpers.UnsetEnvVars(nil) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters