Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start registry container #69

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions pkg/controllers/gitserver/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package gitserver
import (
"context"
"fmt"
"github.com/cnoe-io/idpbuilder/pkg/kind"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"io"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -39,23 +44,58 @@ func TestReconcileGitServerImage(t *testing.T) {
},
}

_, err = r.reconcileGitServerImage(ctx, controllerruntime.Request{}, &resource)
dockerClient, err := docker.GetDockerClient()
if err != nil {
t.Errorf("Getting docker client: %v", err)
}
defer dockerClient.Close()
reader, err := dockerClient.ImagePull(ctx, "docker.io/library/registry:2", types.ImagePullOptions{})
defer reader.Close()
// blocks until pull is completed
io.Copy(os.Stdout, reader)
if err != nil {
t.Errorf("reconcile error: %v", err)
t.Fatalf("failed pulilng registry image: %v", err)
}

if !strings.HasPrefix(resource.Status.ImageID, "sha256") {
t.Errorf("Invalid or no Image ID in status: %q", resource.Status.ImageID)
resp, err := dockerClient.ContainerCreate(ctx, &container.Config{
Image: "docker.io/library/registry:2",
Tty: false,
ExposedPorts: nat.PortSet{
nat.Port(fmt.Sprintf("%d/tcp", kind.InternalRegistryPort)): struct{}{},
},
}, &container.HostConfig{
PortBindings: nat.PortMap{
nat.Port(fmt.Sprintf("%d/tcp", kind.InternalRegistryPort)): []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: fmt.Sprintf("%d", kind.ExposedRegistryPort),
},
},
},
}, nil, nil, "testcase-registry")
if err != nil {
t.Fatalf("failed creating registry container %v", err)
}

dockerClient, err := docker.GetDockerClient()
defer dockerClient.ContainerRemove(ctx, resp.ID, types.ContainerRemoveOptions{Force: true})

err = dockerClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
if err != nil {
t.Errorf("Getting docker client: %v", err)
t.Fatalf("failed starting container %v", err)
}

_, err = r.reconcileGitServerImage(ctx, controllerruntime.Request{}, &resource)
if err != nil {
t.Fatalf("reconcile error: %v", err)
}

if !strings.HasPrefix(resource.Status.ImageID, "sha256") {
t.Fatalf("Invalid or no Image ID in status: %q", resource.Status.ImageID)
}
imageNameID := fmt.Sprintf("%s@%s", GetImageTag(&resource), resource.Status.ImageID)
_, err = dockerClient.ImageRemove(ctx, imageNameID, types.ImageRemoveOptions{})
if err != nil {
t.Errorf("Removing docker image: %v", err)
}
}

Loading