Skip to content

Commit

Permalink
Added new functions in the lib package
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
VaibhavMalik4187 authored and matthyx committed Jan 5, 2024
1 parent a641cb1 commit 64ea53d
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 156 deletions.
92 changes: 86 additions & 6 deletions ci/lib/basic_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
75 changes: 0 additions & 75 deletions ci/lib/datastructures.go

This file was deleted.

23 changes: 21 additions & 2 deletions ci/lib/prcreated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
44 changes: 23 additions & 21 deletions ci/lib/prmerged.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
}
34 changes: 34 additions & 0 deletions ci/lib/utils.go
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 8 additions & 52 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
*/
}

0 comments on commit 64ea53d

Please sign in to comment.