-
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
24d525b
commit cf2a8b9
Showing
5 changed files
with
264 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 := generator.GenerateInput(*rootCmd, 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,103 @@ | ||
package cmd | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"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 TestIdentifyServices(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args testdata.TestData | ||
templatePath string | ||
want []string | ||
wantServices []identifyServices | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test1 single service", | ||
args: testdata.GetSeedData( | ||
testdata.TestData{ | ||
ProjectName: "example-project", | ||
EnvironmentName: "main", | ||
Branch: "main", | ||
LagoonYAML: "../internal/testdata/node/lagoon.yml", | ||
}, true), | ||
templatePath: "testdata/output", | ||
want: []string{"node"}, | ||
wantServices: []identifyServices{{Name: "node", Type: "node"}}, | ||
}, | ||
{ | ||
name: "test2 complex servives", | ||
args: testdata.GetSeedData( | ||
testdata.TestData{ | ||
ProjectName: "example-project", | ||
EnvironmentName: "main", | ||
Branch: "main", | ||
EnvironmentType: "development", | ||
LagoonYAML: "../internal/testdata/complex/lagoon.yml", | ||
}, true), | ||
templatePath: "testdata/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"}, | ||
}, | ||
}, | ||
{ | ||
name: "test3 complex servives where one is removed", | ||
args: testdata.GetSeedData( | ||
testdata.TestData{ | ||
ProjectName: "example-project", | ||
EnvironmentName: "main", | ||
Branch: "main", | ||
EnvironmentType: "development", | ||
LagoonYAML: "../internal/testdata/complex/lagoon.yml", | ||
EnvVariables: []lagoon.EnvironmentVariable{ | ||
{ | ||
Name: "LAGOON_SERVICE_TYPES", | ||
Value: "redis:none", | ||
Scope: "build", | ||
}, | ||
}, | ||
}, true), | ||
templatePath: "testdata/output", | ||
want: []string{"cli", "nginx-php", "mariadb"}, | ||
wantServices: []identifyServices{ | ||
{Name: "cli", Type: "cli-persistent"}, | ||
{Name: "nginx-php", Type: "nginx-php-persistent"}, | ||
{Name: "mariadb", Type: "mariadb-dbaas"}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// set the environment variables from args | ||
savedTemplates := tt.templatePath | ||
generator, err := testdata.SetupEnvironment(*rootCmd, savedTemplates, tt.args) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
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