-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from bupd/testing
feat: Add E2E Test for Harbor Satellite
- Loading branch information
Showing
12 changed files
with
282 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
# Wether to us the built-in Zot registry or not | ||
bring_own_registry = false | ||
bring_own_registry = true | ||
|
||
# URL of own registry | ||
own_registry_adr = "127.0.0.1:8585" | ||
own_registry_adr = "127.0.0.1:5000" | ||
|
||
# URL of remote registry OR local file path | ||
url_or_file = "https://demo.goharbor.io/v2/myproject/album-server" | ||
# url_or_file = "https://demo.goharbor.io/v2/myproject/album-server" | ||
url_or_file = "http://localhost:5001/v2/library/busybox" | ||
|
||
# For testing purposes : | ||
# https://demo.goharbor.io/v2/myproject/album-server | ||
# /image-list/images.json | ||
# /image-list/images.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ | |
"log": { | ||
"level": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"dagger.io/dagger" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
appDir = "/app" | ||
appBinary = "app" | ||
sourceFile = "main.go" | ||
) | ||
|
||
func TestSatellite(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// Initialize Dagger client | ||
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) | ||
assert.NoError(t, err, "Failed to connect to Dagger") | ||
defer client.Close() | ||
|
||
// 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") | ||
} | ||
|
||
// 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") | ||
|
||
container, err := client.Container(). | ||
From("registry:2"). | ||
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 setting up source registry.") | ||
|
||
return container, nil | ||
} | ||
|
||
// 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") | ||
|
||
container, err := client.Container(). | ||
From("registry:2"). | ||
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 setting up destination registry") | ||
|
||
return container, nil | ||
} | ||
|
||
// Push image to the Source registry | ||
func pushImageToSourceRegistry( | ||
t *testing.T, | ||
ctx context.Context, | ||
client *dagger.Client, | ||
source *dagger.Service, | ||
) { | ||
// socket to connect to host Docker | ||
socket := client.Host().UnixSocket("/var/run/docker.sock") | ||
|
||
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("source", source) | ||
|
||
// 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", "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"}) | ||
|
||
// check pushed images exist | ||
container = container.WithExec([]string{"crane", "catalog", "source:5000", "--insecure"}) | ||
|
||
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, | ||
source *dagger.Service, | ||
dest *dagger.Service, | ||
) { | ||
socket := client.Host().UnixSocket("/var/run/docker.sock") | ||
|
||
// Get the directory | ||
parentDir, err := getProjectDir() | ||
assert.NoError(t, err, "Failed to get Project Directory") | ||
|
||
// Use the directory path in Dagger | ||
dir := client.Host().Directory(parentDir) | ||
|
||
// Get configuration file on the host | ||
configFile := client.Host().File("./testdata/config.toml") | ||
|
||
// Configure and build the Satellite | ||
container := client.Container().From("golang:alpine").WithDirectory(appDir, dir). | ||
WithWorkdir(appDir). | ||
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", "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") | ||
|
||
stdOut, err := container.Stdout(ctx) | ||
assert.NoError(t, err, "Failed to get stdOut in Satellite") | ||
|
||
fmt.Println(stdOut) | ||
} | ||
|
||
// 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, "../..")) | ||
} |
Oops, something went wrong.