Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
Signed-off-by: bupd <[email protected]>

add: .gitignore

This commit adds standard go gitignore file to the repo.

Signed-off-by: bupd <[email protected]>
  • Loading branch information
bupd committed Jun 25, 2024
1 parent 62cfeb9 commit 3ceb90a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 141 deletions.
29 changes: 28 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
.env
# 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
13 changes: 1 addition & 12 deletions internal/replicate/replicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
175 changes: 60 additions & 115 deletions test/e2e/satellite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"context"
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"testing"
Expand All @@ -20,45 +18,42 @@ 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,
) (*dagger.Service, error) {
// 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).
Expand All @@ -67,169 +62,119 @@ 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,
) (*dagger.Service, error) {
// 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
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()).
WithExec([]string{"cat", "config.toml"}).
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 {
return "", err
}
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
}
19 changes: 9 additions & 10 deletions test/e2e/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strings"
Expand All @@ -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
Expand All @@ -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)
}
}
Loading

0 comments on commit 3ceb90a

Please sign in to comment.