-
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.
Merge pull request #57 from uselagoon/schedule-dbaas-checks
- Loading branch information
Showing
34 changed files
with
1,608 additions
and
199 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
generator "github.com/uselagoon/build-deploy-tool/internal/generator" | ||
"github.com/uselagoon/build-deploy-tool/internal/helpers" | ||
dbaasTemplater "github.com/uselagoon/build-deploy-tool/internal/templating/dbaas" | ||
) | ||
|
||
var dbaasGeneration = &cobra.Command{ | ||
Use: "dbaas", | ||
Aliases: []string{"b"}, | ||
Short: "Generate the ingress templates for a Lagoon build", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return DBaaSTemplateGeneration(true) | ||
}, | ||
} | ||
|
||
// DBaaSTemplateGeneration . | ||
func DBaaSTemplateGeneration(debug bool, | ||
) error { | ||
lagoonBuild, err := generator.NewGenerator( | ||
lagoonYml, | ||
projectVariables, | ||
environmentVariables, | ||
projectName, | ||
environmentName, | ||
environmentType, | ||
activeEnvironment, | ||
standbyEnvironment, | ||
buildType, | ||
branch, | ||
prNumber, | ||
prTitle, | ||
prHeadBranch, | ||
prBaseBranch, | ||
lagoonVersion, | ||
defaultBackupSchedule, | ||
hourlyDefaultBackupRetention, | ||
dailyDefaultBackupRetention, | ||
weeklyDefaultBackupRetention, | ||
monthlyDefaultBackupRetention, | ||
monitoringContact, | ||
monitoringStatusPageID, | ||
fastlyCacheNoCahce, | ||
fastlyAPISecretPrefix, | ||
fastlyServiceID, | ||
ignoreNonStringKeyErrors, | ||
ignoreMissingEnvFiles, | ||
debug, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
templateYAML, err := dbaasTemplater.GenerateDBaaSTemplate(*lagoonBuild.BuildValues) | ||
if err != nil { | ||
return fmt.Errorf("couldn't generate template: %v", err) | ||
} | ||
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, "dbaas"), templateYAML) | ||
return nil | ||
} | ||
|
||
func init() { | ||
templateCmd.AddCommand(dbaasGeneration) | ||
} |
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,216 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/uselagoon/build-deploy-tool/internal/helpers" | ||
) | ||
|
||
func TestDBaaSTemplateGeneration(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 | ||
templatePath string | ||
controllerDevSchedule string | ||
controllerPRSchedule string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test1 - mariadb-dbaas", | ||
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: `[]`, | ||
secretPrefix: "fastly-api-", | ||
lagoonYAML: "../test-resources/template-dbaas/test1/lagoon.yml", | ||
templatePath: "../test-resources/template-dbaas/output", | ||
}, | ||
want: "../test-resources/template-dbaas/test1-results", | ||
}, | ||
} | ||
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) | ||
} | ||
err = os.Setenv("LAGOON_FEATURE_BACKUP_DEV_SCHEDULE", tt.args.controllerDevSchedule) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
err = os.Setenv("LAGOON_FEATURE_BACKUP_PR_SCHEDULE", tt.args.controllerPRSchedule) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
lagoonYml = tt.args.lagoonYAML | ||
|
||
err = os.MkdirAll(tt.args.templatePath, 0755) | ||
if err != nil { | ||
t.Errorf("couldn't create directory %v: %v", savedTemplates, err) | ||
} | ||
savedTemplates = tt.args.templatePath | ||
defer os.RemoveAll(savedTemplates) | ||
|
||
ts := helpers.TestDBaaSHTTPServer() | ||
defer ts.Close() | ||
err = os.Setenv("DBAAS_OPERATOR_HTTP", ts.URL) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
defer os.RemoveAll(savedTemplates) | ||
if err := DBaaSTemplateGeneration(false); (err != nil) != tt.wantErr { | ||
t.Errorf("DBaaSTemplateGeneration() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
files, err := ioutil.ReadDir(savedTemplates) | ||
if err != nil { | ||
t.Errorf("couldn't read directory %v: %v", savedTemplates, err) | ||
} | ||
results, err := ioutil.ReadDir(tt.want) | ||
if err != nil { | ||
t.Errorf("couldn't read directory %v: %v", tt.want, err) | ||
} | ||
if len(files) != len(results) { | ||
for _, f := range files { | ||
f1, err := os.ReadFile(fmt.Sprintf("%s/%s", savedTemplates, f.Name())) | ||
if err != nil { | ||
t.Errorf("couldn't read file %v: %v", savedTemplates, err) | ||
} | ||
fmt.Println(string(f1)) | ||
} | ||
t.Errorf("number of generated templates doesn't match results %v/%v: %v", len(files), len(results), err) | ||
} | ||
fCount := 0 | ||
for _, f := range files { | ||
for _, r := range results { | ||
if f.Name() == r.Name() { | ||
fCount++ | ||
f1, err := os.ReadFile(fmt.Sprintf("%s/%s", savedTemplates, f.Name())) | ||
if err != nil { | ||
t.Errorf("couldn't read file %v: %v", savedTemplates, err) | ||
} | ||
r1, err := os.ReadFile(fmt.Sprintf("%s/%s", tt.want, f.Name())) | ||
if err != nil { | ||
t.Errorf("couldn't read file %v: %v", tt.want, err) | ||
} | ||
if !reflect.DeepEqual(f1, r1) { | ||
fmt.Println(string(f1)) | ||
t.Errorf("resulting templates do not match") | ||
} | ||
} | ||
} | ||
} | ||
if fCount != len(files) { | ||
for _, f := range files { | ||
f1, err := os.ReadFile(fmt.Sprintf("%s/%s", savedTemplates, f.Name())) | ||
if err != nil { | ||
t.Errorf("couldn't read file %v: %v", savedTemplates, err) | ||
} | ||
fmt.Println(string(f1)) | ||
} | ||
t.Errorf("resulting templates do not match") | ||
} | ||
t.Cleanup(func() { | ||
helpers.UnsetEnvVars([]helpers.EnvironmentVariable{{Name: "DBAAS_OPERATOR_HTTP"}}) | ||
}) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.