Skip to content

Commit

Permalink
create kind network
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu Mccloskey <[email protected]>
  • Loading branch information
nabuskey committed Oct 26, 2023
1 parent 10c5e45 commit 2d9330a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
51 changes: 45 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,20 +44,54 @@ 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 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/kind/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func TestReconcileRegistry(t *testing.T) {
}
defer dockerCli.Close()

kindNetwork, err := dockerCli.NetworkCreate(ctx, "kind", types.NetworkCreate{})
if err != nil {
t.Fatalf("Failed creaking kind network: %v", err)
}
defer dockerCli.NetworkRemove(ctx, kindNetwork.ID)

// Create cluster
cluster, err := NewCluster("testcase", "v1.26.3", "", "", "")
if err != nil {
Expand All @@ -36,6 +42,8 @@ func TestReconcileRegistry(t *testing.T) {
if err != nil {
t.Fatalf("Error getting registry container after reconcile: %v", err)
}
defer dockerCli.ImageRemove(ctx, container.ImageID, types.ImageRemoveOptions{})

if container == nil {
t.Fatal("Expected registry container after reconcile but got nil")
}
Expand Down

0 comments on commit 2d9330a

Please sign in to comment.