Skip to content

Commit

Permalink
Merge pull request #57 from uselagoon/schedule-dbaas-checks
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Jul 7, 2022
2 parents c3500e7 + 2015dd3 commit f86a623
Show file tree
Hide file tree
Showing 34 changed files with 1,608 additions and 199 deletions.
68 changes: 68 additions & 0 deletions cmd/template_dbaas.go
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)
}
216 changes: 216 additions & 0 deletions cmd/template_dbaas_test.go
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"}})
})
})
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.17

require (
github.com/PaesslerAG/gval v1.1.2
github.com/amazeeio/dbaas-operator v0.3.0
github.com/compose-spec/compose-go v1.2.7
github.com/cxmcc/unixsums v0.0.0-20131125091133-89564297d82f
github.com/google/go-cmp v0.5.7
Expand Down Expand Up @@ -58,7 +59,7 @@ require (
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
sigs.k8s.io/controller-runtime v0.9.5 // indirect
sigs.k8s.io/controller-runtime v0.9.6 // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
)
Expand Down
Loading

0 comments on commit f86a623

Please sign in to comment.