From 64ea53d39fa20bb53049dd75b418d95f95679d82 Mon Sep 17 00:00:00 2001 From: VaibhavMalik4187 Date: Fri, 8 Dec 2023 16:49:36 +0530 Subject: [PATCH] Added new functions in the lib package * Deleted the datastructures.go file as it was not being used. * Completed the CheckSecrets and EnvironmentTest functions in basic-tests.go * Moved several functions to semantically more correct locations. Signed-off-by: VaibhavMalik4187 --- ci/lib/basic_tests.go | 92 +++++++++++++++++++++++++++++++++++++--- ci/lib/datastructures.go | 75 -------------------------------- ci/lib/prcreated.go | 23 +++++++++- ci/lib/prmerged.go | 44 ++++++++++--------- ci/lib/utils.go | 34 +++++++++++++++ ci/main.go | 60 ++++---------------------- 6 files changed, 172 insertions(+), 156 deletions(-) delete mode 100644 ci/lib/datastructures.go create mode 100644 ci/lib/utils.go diff --git a/ci/lib/basic_tests.go b/ci/lib/basic_tests.go index 6e4ae24..e2c3cf7 100644 --- a/ci/lib/basic_tests.go +++ b/ci/lib/basic_tests.go @@ -2,17 +2,83 @@ package lib import ( "context" + "fmt" "dagger.io/dagger" ) -func CheckSecrets(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { - // TODO: Write the code for the Check-secret job +type BasicTestsInputs struct { + GOVERSION string + GO111MODULE string + CGOENABLED bool + BUILDPATH string + UNITTESTSPATH string + FAILED_THRESHOLD int +} + +type BasicTestsSecrets struct { + SNYK_TOKEN string + GITGUARDIAN_API_KEY string +} + +// Check if secrets are set +func CheckSecrets(secrets BasicTestsSecrets) bool { + if secrets.GITGUARDIAN_API_KEY == "" || secrets.SNYK_TOKEN == "" { + return false + } + return true +} + +func UnitTest(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { + fmt.Println("Running unit tests...") + // create a cache volume + goBuildCache := client.CacheVolume("goBuild") + goPkgCache := client.CacheVolume("goPkg") + // run tests + out, err := client.Container(). + From("golang:1.20-bullseye"). + WithDirectory("/src", src). + WithMountedCache("/go/pkg", goPkgCache). + WithMountedCache("/root/.cache/go-build", goBuildCache). + WithWorkdir("/src"). + WithExec([]string{"go", "test", "-v", "./..."}). + Stderr(ctx) + if err != nil { + return err + } + fmt.Println(out) return nil } -func EnvironmentTest(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { - // TODO: Write the code for the Environment-Test job +func EnvironmentTest(ctx context.Context, client *dagger.Client, src *dagger.Directory, inputs BasicTestsInputs) error { + testCmd := []string{"go", "test", "-v"} + if inputs.CGOENABLED { + testCmd = append(testCmd, "-race", "./...") + } else { + testCmd = append(testCmd, "./...") + } + + fmt.Println("Running environment tests...") + + // Create a cache volume + goBuildCache := client.CacheVolume("goBuild") + goPkgCache := client.CacheVolume("goPkg") + + // Run tests + out, err := client.Container(). + From("golang:1.20-bullseye"). + WithDirectory("/src", src). + WithMountedCache("/go/pkg", goPkgCache). + WithMountedCache("/root/.cache/go-build", goBuildCache). + WithWorkdir("/src"). + WithExec(testCmd). + Stderr(ctx) + + if err != nil { + return err + } + + fmt.Println(out) return nil } @@ -21,7 +87,21 @@ func BasicTest(ctx context.Context, client *dagger.Client, src *dagger.Directory return nil } -func BasicTests(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { - // TODO implement +func BasicTests(ctx context.Context, client *dagger.Client, src *dagger.Directory, inputs BasicTestsInputs, secrets BasicTestsSecrets) error { + // Step 1: Check if the secrets are set + secretsSet := CheckSecrets(secrets) + if !secretsSet { + fmt.Println("Secrets are not set") + fmt.Printf("SNYK_TOKEN: %s\n", secrets.SNYK_TOKEN) + fmt.Printf("GITGUARDIAN_API_KEY: %s\n", secrets.GITGUARDIAN_API_KEY) + + // return errors.New("secrets are not set") + } + + err := EnvironmentTest(ctx, client, src, inputs) + if err != nil { + fmt.Println("Environment test failed") + } + return nil } diff --git a/ci/lib/datastructures.go b/ci/lib/datastructures.go deleted file mode 100644 index f7940a5..0000000 --- a/ci/lib/datastructures.go +++ /dev/null @@ -1,75 +0,0 @@ -package lib - -type AutoGenerated struct { - Name string `yaml:"name"` - On On `yaml:"on"` - Jobs Jobs `yaml:"jobs"` -} -type PullRequest struct { - Types []string `yaml:"types"` - Branches []string `yaml:"branches"` - PathsIgnore []string `yaml:"paths-ignore"` -} -type GOVERSION struct { - Required bool `yaml:"required"` - Type string `yaml:"type"` -} -type CGOENABLED struct { - Required bool `yaml:"required"` - Type string `yaml:"type"` - Default int `yaml:"default"` -} -type BUILDPATH struct { - Required bool `yaml:"required"` - Type string `yaml:"type"` - Default string `yaml:"default"` -} -type UNITTESTSPATH struct { - Required bool `yaml:"required"` - Type string `yaml:"type"` - Default string `yaml:"default"` -} -type Inputs struct { - GOVERSION GOVERSION `yaml:"GO_VERSION"` - CGOENABLED CGOENABLED `yaml:"CGO_ENABLED"` - BUILDPATH BUILDPATH `yaml:"BUILD_PATH"` - UNITTESTSPATH UNITTESTSPATH `yaml:"UNIT_TESTS_PATH"` -} -type SNYKTOKEN struct { - Required bool `yaml:"required"` -} -type GITGUARDIANAPIKEY struct { - Required bool `yaml:"required"` -} -type Secrets struct { - SNYKTOKEN SNYKTOKEN `yaml:"SNYK_TOKEN"` - GITGUARDIANAPIKEY GITGUARDIANAPIKEY `yaml:"GITGUARDIAN_API_KEY"` -} -type WorkflowCall struct { - Inputs Inputs `yaml:"inputs"` - Secrets Secrets `yaml:"secrets"` -} -type On struct { - PullRequest PullRequest `yaml:"pull_request"` - WorkflowCall WorkflowCall `yaml:"workflow_call"` -} -type Permissions struct { - PullRequests string `yaml:"pull-requests"` - SecurityEvents string `yaml:"security-events"` -} -type With struct { - GOVERSION string `yaml:"GO_VERSION"` - GO111MODULE string `yaml:"GO111MODULE"` - CGOENABLED string `yaml:"CGO_ENABLED"` - UNITTESTSPATH string `yaml:"UNIT_TESTS_PATH"` - BUILDPATH string `yaml:"BUILD_PATH"` -} -type Test struct { - Permissions Permissions `yaml:"permissions"` - Uses string `yaml:"uses"` - With With `yaml:"with"` - Secrets string `yaml:"secrets"` -} -type Jobs struct { - Test Test `yaml:"test"` -} diff --git a/ci/lib/prcreated.go b/ci/lib/prcreated.go index 9921e7b..4008f24 100644 --- a/ci/lib/prcreated.go +++ b/ci/lib/prcreated.go @@ -2,13 +2,32 @@ package lib import ( "context" + "fmt" + "os" "dagger.io/dagger" ) +// Run basic tests on PR func PrCreated(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { - // run basic tests on PR - if err := BasicTests(ctx, client, src); err != nil { + basicTestsInputs := BasicTestsInputs{ + GOVERSION: os.Getenv("GO_VERSION"), + CGOENABLED: os.Getenv("CGO_ENABLED") == "1", + BUILDPATH: os.Getenv("BUILD_PATH"), + UNITTESTSPATH: os.Getenv("UNITTESTS_PATH"), + } + + basicTestsSecrets := BasicTestsSecrets{ + SNYK_TOKEN: os.Getenv("SNYK_TOKEN"), + GITGUARDIAN_API_KEY: os.Getenv("GITGUARDIAN_API_KEY"), + } + + fmt.Printf("GOVERSION: %s\n", basicTestsInputs.GOVERSION) + fmt.Printf("CGOENABLED: %t\n", basicTestsInputs.CGOENABLED) + fmt.Printf("BUILDPATH: %s\n", basicTestsInputs.BUILDPATH) + fmt.Printf("UNITTESTSPATH: %s\n", basicTestsInputs.UNITTESTSPATH) + + if err := BasicTests(ctx, client, src, basicTestsInputs, basicTestsSecrets); err != nil { return err } diff --git a/ci/lib/prmerged.go b/ci/lib/prmerged.go index 5b97292..14c0667 100644 --- a/ci/lib/prmerged.go +++ b/ci/lib/prmerged.go @@ -32,27 +32,6 @@ func DockerBuild(ctx context.Context, client *dagger.Client, src *dagger.Directo return imageDigest, nil } -func UnitTest(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { - fmt.Println("Running unit tests...") - // create a cache volume - goBuildCache := client.CacheVolume("goBuild") - goPkgCache := client.CacheVolume("goPkg") - // run tests - out, err := client.Container(). - From("golang:1.20-bullseye"). - WithDirectory("/src", src). - WithMountedCache("/go/pkg", goPkgCache). - WithMountedCache("/root/.cache/go-build", goBuildCache). - WithWorkdir("/src"). - WithExec([]string{"go", "test", "./..."}). - Stderr(ctx) - if err != nil { - return err - } - fmt.Println(out) - return nil -} - func BuildPush(ctx context.Context, client *dagger.Client, src *dagger.Directory, platforms []dagger.Platform, imageRepo string) (string, error) { fmt.Println("Building multi-platform image...") platformVariants := make([]*dagger.Container, 0, len(platforms)) @@ -79,3 +58,26 @@ func BuildPush(ctx context.Context, client *dagger.Client, src *dagger.Directory fmt.Println("Pushed multi-platform image w/ digest: ", imageDigest) return imageDigest, nil } + +func PrMerged(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { + // Build image + imagePrerelease, err := DockerBuild(ctx, client, src, platforms, imageRepo) + if err != nil { + return err + } + fmt.Println(imagePrerelease) + + // run system tests + if err := SystemTest(ctx, client, imagePrerelease); err != nil { + return err + } + + // create release and retag image + imageRelease, err := ReleaseRetag(ctx, client, imagePrerelease) + if err != nil { + return err + } + fmt.Println(imageRelease) + + return nil +} diff --git a/ci/lib/utils.go b/ci/lib/utils.go new file mode 100644 index 0000000..d677358 --- /dev/null +++ b/ci/lib/utils.go @@ -0,0 +1,34 @@ +package lib + +import ( + "context" + "os" + + "dagger.io/dagger" +) + +// the platforms to build for and push in a multi-platform image +var platforms = []dagger.Platform{ + "linux/amd64", + "linux/arm64", +} + +// the system tests to run on the newly built image +var TestNames = []string{ + "unit", +} + +// the container registry for the multi-platform image +const imageRepo = "quay.io/matthiasb_1/synchronizer" + +func InitDagger(ctx context.Context) (*dagger.Client, *dagger.Directory, error) { + client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) + if err != nil { + return nil, nil, err + } + + // get reference to the local project + // git checkout is performed by the calling CI system (e.g. GitHub Actions) + src := client.Host().Directory(".") + return client, src, nil +} diff --git a/ci/main.go b/ci/main.go index 6456e8f..460f254 100644 --- a/ci/main.go +++ b/ci/main.go @@ -3,73 +3,29 @@ package main import ( "context" "fmt" - "os" - "dagger.io/dagger" "github.com/kubescape/synchronizer/ci/lib" ) -// the platforms to build for and push in a multi-platform image -var platforms = []dagger.Platform{ - "linux/amd64", - "linux/arm64", -} - -// the system tests to run on the newly built image -var TestNames = []string{ - "unit", -} - -// the container registry for the multi-platform image -const imageRepo = "quay.io/matthiasb_1/synchronizer" - func main() { ctx := context.Background() // Initialize Dagger client - client, src, err := InitDagger(ctx) + client, src, err := lib.InitDagger(ctx) if err != nil { fmt.Println(err) } defer client.Close() - err = PrMerged(ctx, client, src) + err = lib.PrCreated(ctx, client, src) if err != nil { fmt.Println(err) } -} - -func InitDagger(ctx context.Context) (*dagger.Client, *dagger.Directory, error) { - client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) - if err != nil { - return nil, nil, err - } - - // get reference to the local project - // git checkout is performed by the calling CI system (e.g. GitHub Actions) - src := client.Host().Directory(".") - return client, src, nil -} - -func PrMerged(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { - // Build image - imagePrerelease, err := lib.DockerBuild(ctx, client, src, platforms, imageRepo) - if err != nil { - return err - } - fmt.Println(imagePrerelease) - - // run system tests - if err := lib.SystemTest(ctx, client, imagePrerelease); err != nil { - return err - } - - // create release and retag image - imageRelease, err := lib.ReleaseRetag(ctx, client, imagePrerelease) - if err != nil { - return err - } - fmt.Println(imageRelease) - return nil + /* + err = lib.PrMerged(ctx, client, src) + if err != nil { + fmt.Println(err) + } + */ }