From 3ceb90a6dea4c1c8818b589adda211b991ee5f63 Mon Sep 17 00:00:00 2001 From: bupd Date: Tue, 25 Jun 2024 20:47:11 +0530 Subject: [PATCH] improve error handling Signed-off-by: bupd add: .gitignore This commit adds standard go gitignore file to the repo. Signed-off-by: bupd --- .gitignore | 29 +++++- internal/replicate/replicate.go | 13 +-- test/e2e/satellite_test.go | 175 +++++++++++--------------------- test/e2e/test.go | 19 ++-- test/e2e/testdata/config.toml | 6 +- 5 files changed, 101 insertions(+), 141 deletions(-) diff --git a/.gitignore b/.gitignore index 2eea525..86a460d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,28 @@ -.env \ No newline at end of file +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work +/bin + +/harbor +dist/ + +# Remove DS_Store +.DS_Store diff --git a/internal/replicate/replicate.go b/internal/replicate/replicate.go index 84812e1..aa73454 100644 --- a/internal/replicate/replicate.go +++ b/internal/replicate/replicate.go @@ -194,21 +194,10 @@ func CopyImage(imageName string) error { return fmt.Errorf("failed to remove directory: %w", err) } - // // Use crane.Copy to copy the image directly without pulling & storing - // // this only works when remote & local registries are same. - // err := crane.Copy(imageName, destRef, crane.WithAuth(auth), crane.Insecure) - // if err != nil { - // fmt.Printf("Failed to copy image: %v\n", err) - // return fmt.Errorf("failed to copy image: %w", err) - // } else { - // fmt.Println("Image copied successfully") - // fmt.Printf("Copied image from %s to: %s\n", imageName, destRef) - // } - return nil } -// Split the imageName by "/" and take only the parts after the hostname +// take only the parts after the hostname func removeHostName(imageName string) string { parts := strings.Split(imageName, "/") if len(parts) > 1 { diff --git a/test/e2e/satellite_test.go b/test/e2e/satellite_test.go index b9cd438..2f55ee8 100644 --- a/test/e2e/satellite_test.go +++ b/test/e2e/satellite_test.go @@ -3,8 +3,6 @@ package main import ( "context" "fmt" - "log" - "log/slog" "os" "path/filepath" "testing" @@ -20,37 +18,35 @@ const ( sourceFile = "main.go" ) -func TestSetupContainerRegistry(t *testing.T) { +func TestSatellite(t *testing.T) { ctx := context.Background() // Initialize Dagger client client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) - if err != nil { - log.Fatalf("Failed to connect to Dagger: %v", err) - } + assert.NoError(t, err, "Failed to connect to Dagger") defer client.Close() - // Set up remote Registry - remote, err := setupRemoteRegistry(t, client, ctx) - assert.NoError(t, err, "Failed to set up remote registry") - // Set up the container registry - registry, err := setupContainerRegistry(t, client, ctx) - assert.NoError(t, err, "Failed to set up container registry") - // reg, _ := registry.Hostname(ctx) - // fmt.Println(reg) - - // Push the image to the registry - pushImageToRegistry(t, ctx, client, remote) - assert.NoError(t, err, "Failed to upload image to registry") - - // Implement the Satellite Testing - stdOut := buildSatellite(t, client, ctx, remote, registry) - assert.NoError(t, err, "Failed to build Satellite") - fmt.Println(stdOut) + // Set up Source Registry + source, err := setupSourceRegistry(t, client, ctx) + assert.NoError(t, err, "Failed to set up source registry") + + // Set up Destination registry + dest, err := setupDestinationRegistry(t, client, ctx) + assert.NoError(t, err, "Failed to set up destination registry") + + // Push images to Source registry + pushImageToSourceRegistry(t, ctx, client, source) + assert.NoError(t, err, "Failed to upload image to source registry") + + // Build & Run Satellite + buildSatellite(t, client, ctx, source, dest) + assert.NoError(t, err, "Failed to build and run Satellite") + + return } -// Setup Container Registry as a Dagger Service -func setupRemoteRegistry( +// Setup Source Registry as a Dagger Service +func setupSourceRegistry( t *testing.T, client *dagger.Client, ctx context.Context, @@ -58,7 +54,6 @@ func setupRemoteRegistry( // socket to connect to host Docker socket := client.Host().UnixSocket("/var/run/docker.sock") - // Pull the Harbor registry image container, err := client.Container(). From("registry:2"). WithExposedPort(5000). @@ -67,14 +62,13 @@ func setupRemoteRegistry( WithEnvVariable("CACHEBUSTER", time.Now().String()). AsService().Start(ctx) - assert.NoError(t, err, "Failed in setting up remote registry.") + assert.NoError(t, err, "Failed setting up source registry.") - // Return the registry URL return container, nil } -// Setup Container Registry as a Dagger Service -func setupContainerRegistry( +// Setup Destination Registry as a Dagger Service +func setupDestinationRegistry( t *testing.T, client *dagger.Client, ctx context.Context, @@ -82,54 +76,52 @@ func setupContainerRegistry( // socket to connect to host Docker socket := client.Host().UnixSocket("/var/run/docker.sock") - // Pull the registry image container, err := client.Container(). From("registry:2"). - WithExposedPort(5000, dagger.ContainerWithExposedPortOpts{Protocol: "TCP"}). + WithExposedPort(5000). WithUnixSocket("/var/run/docker.sock", socket). WithEnvVariable("DOCKER_HOST", "unix:///var/run/docker.sock"). WithEnvVariable("CACHEBUSTER", time.Now().String()). AsService().Start(ctx) - assert.NoError(t, err, "Failed in setting up registry") + assert.NoError(t, err, "Failed setting up destination registry") - // Return the registry URL return container, nil } -// Upload image to the registry -func pushImageToRegistry( +// Push image to the Source registry +func pushImageToSourceRegistry( t *testing.T, ctx context.Context, client *dagger.Client, - registry *dagger.Service, + source *dagger.Service, ) { - // // socket to connect to host Docker + // socket to connect to host Docker socket := client.Host().UnixSocket("/var/run/docker.sock") - // newUrl := strings.Replace(srvAddr, "tcp://", "", 1) - // fmt.Println(newUrl) container := client.Container(). From("docker:dind"). WithUnixSocket("/var/run/docker.sock", socket). WithEnvVariable("DOCKER_HOST", "unix:///var/run/docker.sock"). WithEnvVariable("CACHEBUSTER", time.Now().String()). - WithServiceBinding("remote", registry) + WithServiceBinding("source", source) - // add crane push images + // add crane & push images container = container.WithExec([]string{"apk", "add", "crane"}). WithExec([]string{"docker", "pull", "busybox:1.36"}). WithExec([]string{"docker", "pull", "busybox:stable"}). - WithExec([]string{"crane", "copy", "busybox:1.36", "remote:5000/library/busybox:1.36", "--insecure"}). - WithExec([]string{"crane", "copy", "busybox:stable", "remote:5000/library/busybox:stable", "--insecure"}). - WithExec([]string{"crane", "digest", "remote:5000/library/busybox:1.36", "--insecure"}). - WithExec([]string{"crane", "digest", "remote:5000/library/busybox:stable", "--insecure"}) + WithExec([]string{"crane", "copy", "busybox:1.36", "source:5000/library/busybox:1.36", "--insecure"}). + WithExec([]string{"crane", "copy", "busybox:stable", "source:5000/library/busybox:stable", "--insecure"}). + WithExec([]string{"crane", "digest", "source:5000/library/busybox:1.36", "--insecure"}). + WithExec([]string{"crane", "digest", "source:5000/library/busybox:stable", "--insecure"}) - container = container.WithExec([]string{"crane", "catalog", "remote:5000", "--insecure"}) + // check pushed images exist + container = container.WithExec([]string{"crane", "catalog", "source:5000", "--insecure"}) - prints, err := container.Stdout(ctx) - assert.NoError(t, err, "Failed to push image to remote registry") - fmt.Println(prints) + stdOut, err := container.Stdout(ctx) + assert.NoError(t, err, "Failed to print stdOut in pushing Image to Source") + + fmt.Println(stdOut) } // buildSatellite and test test the connection @@ -137,39 +129,26 @@ func buildSatellite( t *testing.T, client *dagger.Client, ctx context.Context, - remote *dagger.Service, - registry *dagger.Service, -) *dagger.Container { - // Get the directory of project located one level up from the current working directory + source *dagger.Service, + dest *dagger.Service, +) { + socket := client.Host().UnixSocket("/var/run/docker.sock") + + // Get the directory parentDir, err := getProjectDir() - if err != nil { - log.Fatalf("Error getting parentDirectory: %v", err) - } + assert.NoError(t, err, "Failed to get Project Directory") - // Use the parent directory path in Dagger + // Use the directory path in Dagger dir := client.Host().Directory(parentDir) - // Create the configuration file on the host + // Get configuration file on the host configFile := client.Host().File("./testdata/config.toml") - // File path to write the config.toml - // filePath := "./testdata/config.toml" - - // Generate the config file - // err = generateConfigFile(filePath, srvAddr) - // if err != nil { - // log.Fatalf("Failed to generate config file: %v", err) - // } - - // Pull the image from Docker Hub - socket := client.Host().UnixSocket("/var/run/docker.sock") - - // Configure and build the container - + // Configure and build the Satellite container := client.Container().From("golang:alpine").WithDirectory(appDir, dir). WithWorkdir(appDir). - WithServiceBinding("remote", remote). - WithServiceBinding("reg", registry). + WithServiceBinding("source", source). + WithServiceBinding("dest", dest). WithUnixSocket("/var/run/docker.sock", socket). WithEnvVariable("DOCKER_HOST", "unix:///var/run/docker.sock"). WithEnvVariable("CACHEBUSTER", time.Now().String()). @@ -177,28 +156,21 @@ func buildSatellite( WithFile("./config.toml", configFile). WithExec([]string{"cat", "config.toml"}). WithExec([]string{"apk", "add", "crane"}). - WithExec([]string{"crane", "-v", "catalog", "reg:5000", "--insecure"}). - WithExec([]string{"crane", "-v", "catalog", "remote:5000", "--insecure"}). - WithExec([]string{"crane", "digest", "remote:5000/library/busybox:stable", "--insecure"}). + WithExec([]string{"crane", "-v", "catalog", "source:5000", "--insecure"}). + WithExec([]string{"crane", "digest", "source:5000/library/busybox:stable", "--insecure"}). WithExec([]string{"go", "build", "-o", appBinary, sourceFile}). WithExposedPort(9090). WithExec([]string{"go", "run", "./test/e2e/test.go"}) assert.NoError(t, err, "Test failed in buildSatellite") - // service, err := container.AsService().Start(ctx) - // if err != nil { - // log.Fatalf("Error in running Satellite: %v", err) - // } - slog.Info("Satellite is running and accessible") + stdOut, err := container.Stdout(ctx) + assert.NoError(t, err, "Failed to get stdOut in Satellite") - prints, err := container.Stdout(ctx) - fmt.Println(prints) - - return container + fmt.Println(stdOut) } -// getProjectDir gets the directory of the project +// Gets the directory of the project func getProjectDir() (string, error) { currentDir, err := os.Getwd() if err != nil { @@ -206,30 +178,3 @@ func getProjectDir() (string, error) { } return filepath.Abs(filepath.Join(currentDir, "../..")) } - -func generateConfigFile(filePath string, srvAddr string) error { - // Define the TOML content - configContent := ` -# Auto-generated -bring_own_registry = true -url_or_file = "https://demo.goharbor.io/v2/library/registry" -` - // addr := strings.TrimPrefix(srvAddr, "http://") - - configContent = configContent + fmt.Sprintf("own_registry_adr = \"%s\"", "reg:5000") - - // Create or open the file - file, err := os.Create(filePath) - if err != nil { - return err - } - defer file.Close() - - // Write the TOML content to the file - _, err = file.WriteString(configContent) - if err != nil { - return err - } - - return nil -} diff --git a/test/e2e/test.go b/test/e2e/test.go index 2cd30ec..53ad8f7 100644 --- a/test/e2e/test.go +++ b/test/e2e/test.go @@ -3,6 +3,7 @@ package main import ( "bufio" "fmt" + "log" "os" "os/exec" "strings" @@ -15,14 +16,12 @@ func main() { // Get stdout pipe stdout, err := cmd.StdoutPipe() if err != nil { - fmt.Println("Error creating stdout pipe:", err) - os.Exit(1) + log.Fatalf("Error creating stdout pipe: %v", err) } // Start the command if err := cmd.Start(); err != nil { - fmt.Println("Error starting command:", err) - os.Exit(1) + log.Fatalf("Error starting command: %v", err) } // Create a scanner to read the command output line by line @@ -35,20 +34,20 @@ func main() { // Check if the line contains "----" if len(lineParts) > 2 { fmt.Println("Satellite is Working...\nExiting...") - cmd.Process.Kill() // Kill the process - os.Exit(0) // Exit the program + if err := cmd.Process.Kill(); err != nil { + fmt.Println("Error killing process:", err) + } + os.Exit(0) // Exit the program } } // Handle any scanner error if err := scanner.Err(); err != nil { - fmt.Println("Error reading stdout:", err) - os.Exit(1) + log.Fatalf("Error reading stdout: %v", err) } // Wait for the command to finish if err := cmd.Wait(); err != nil { - fmt.Println("Command execution failed:", err) - os.Exit(1) + log.Fatalf("Command execution failed: %v", err) } } diff --git a/test/e2e/testdata/config.toml b/test/e2e/testdata/config.toml index 1343914..06d9736 100644 --- a/test/e2e/testdata/config.toml +++ b/test/e2e/testdata/config.toml @@ -1,7 +1,7 @@ -# Auto-generated bring_own_registry = true -own_registry_adr = "reg:5000" -url_or_file = "http://remote:5000/v2/library/busybox" +own_registry_adr = "dest:5000" +url_or_file = "http://source:5000/v2/library/busybox" +# Additional test cases need to be handled. # url_or_file = "https://demo.goharbor.io/v2/myproject/album-server" # url_or_file = "http://localhost:5001/v2/library/busybox"