diff --git a/docker.go b/docker.go deleted file mode 100644 index fd5ecb4a140739451f8291e5d3fe32959cf59b86..0000000000000000000000000000000000000000 --- a/docker.go +++ /dev/null @@ -1,5 +0,0 @@ -package jobqueue - -const ( - DOCKER_TEST_HOST_IP = "172.17.0.1" -) diff --git a/runnable-mail_test.go b/runnable-mail_test.go index bc6e5ca5bc2884adf9c4ae185174b7be95ac7a0b..8398d36d175a74dbc153c5b444afa7e3fa8b5b2b 100644 --- a/runnable-mail_test.go +++ b/runnable-mail_test.go @@ -3,9 +3,6 @@ package jobqueue import ( "context" "fmt" - "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/container" - "github.com/docker/docker/client" "github.com/docker/go-connections/nat" "github.com/stretchr/testify/assert" "net" @@ -13,95 +10,129 @@ import ( "time" ) -func startTestSMTPDockerImageAndContainer(t *testing.T, port string, ctx context.Context) error { +func startTestSMTPDockerImageAndContainer(t *testing.T, ctx context.Context) (string, error) { t.Helper() - cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) - if err != nil { - return err - } - - imageName := "axllent/mailpit" - - reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{}) - if err != nil { - return err - } - - // if debug image pull, comment out the following lines - //_, _ = io.Copy(os.Stdout, reader) - _ = reader - - host, _, _ := net.SplitHostPort(cli.DaemonHost()) - if host == "" || host == "unix" { - host = DOCKER_TEST_HOST_IP - } - - hostConfig := &container.HostConfig{ - PortBindings: nat.PortMap{ - "1025/tcp": []nat.PortBinding{ - { - HostIP: host, - HostPort: port, - }, + smtpPortMap := nat.PortMap{ + "1025/tcp": []nat.PortBinding{ + { + HostIP: "0.0.0.0", + HostPort: "", }, - "8025/tcp": []nat.PortBinding{ - { - HostIP: host, - HostPort: "8025", - }, + }, + "8025/tcp": []nat.PortBinding{ + { + HostIP: "0.0.0.0", + HostPort: "8025", }, }, } - resp, err := cli.ContainerCreate(ctx, &container.Config{ - Image: imageName, - }, hostConfig, nil, nil, "") + smtpCmd := []string{} // Ihr Command hier, falls nötig + smtpVolume := "" // Ihr Volume hier + smtpPorts, err := startTestDockerImageAndContainer(t, ctx, "axllent/mailpit", smtpPortMap, smtpCmd, smtpVolume) if err != nil { - return err + return "", err } - if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { - return err - } - - go func() { - <-ctx.Done() - - timeout := 0 - stopOptions := container.StopOptions{ - Timeout: &timeout, - Signal: "SIGKILL", - } - newCtx, _ := context.WithTimeout(context.Background(), 60*time.Second) - if err := cli.ContainerStop(newCtx, resp.ID, stopOptions); err != nil { - t.Errorf("ContainerStop returned error: %v", err) - } - if err := cli.ContainerRemove(newCtx, resp.ID, types.ContainerRemoveOptions{ - Force: true, - }); err != nil { - t.Errorf("ContainerRemove returned error: %v", err) - } - - }() - - statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning) - select { - case err := <-errCh: - if err != nil { - // empty error means container exited normally (see container_wait.go) - if err.Error() == "" { - return nil - } - - return err - } - case <-statusCh: + pp := *smtpPorts + px, ok := pp["1025/tcp"] + if !ok { + return "", fmt.Errorf("1025/tcp port not found") } - return nil + smtpPort := px[0].HostPort + + return smtpPort, nil + + //cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + //if err != nil { + // return err + //} + // + //imageName := "axllent/mailpit" + // + //reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{}) + //if err != nil { + // return err + //} + // + //// if debug image pull, comment out the following lines + ////_, _ = io.Copy(os.Stdout, reader) + //_ = reader + // + //host, _, _ := net.SplitHostPort(cli.DaemonHost()) + //if host == "" || host == "unix" { + // host = DOCKER_TEST_HOST_IP + //} + // + //hostConfig := &container.HostConfig{ + // PortBindings: nat.PortMap{ + // "1025/tcp": []nat.PortBinding{ + // { + // HostIP: host, + // HostPort: port, + // }, + // }, + // "8025/tcp": []nat.PortBinding{ + // { + // HostIP: host, + // HostPort: "8025", + // }, + // }, + // }, + //} + // + //resp, err := cli.ContainerCreate(ctx, &container.Config{ + // Image: imageName, + //}, hostConfig, nil, nil, "") + // + //if err != nil { + // return err + //} + // + //if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { + // return err + //} + // + //go func() { + // <-ctx.Done() + // + // timeout := 0 + // stopOptions := container.StopOptions{ + // Timeout: &timeout, + // Signal: "SIGKILL", + // } + // newCtx, _ := context.WithTimeout(context.Background(), 60*time.Second) + // if err := cli.ContainerStop(newCtx, resp.ID, stopOptions); err != nil { + // t.Errorf("ContainerStop returned error: %v", err) + // } + // if err := cli.ContainerRemove(newCtx, resp.ID, types.ContainerRemoveOptions{ + // Force: true, + // }); err != nil { + // t.Errorf("ContainerRemove returned error: %v", err) + // } + // + //}() + // + //statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning) + //select { + //case err := <-errCh: + // if err != nil { + // // empty error means container exited normally (see container_wait.go) + // if err.Error() == "" { + // return nil + // } + // + // return err + // } + //case <-statusCh: + // + //} + // + //return nil } func TestMailRunner(t *testing.T) { @@ -112,18 +143,21 @@ func TestMailRunner(t *testing.T) { time.Sleep(1 * time.Second) }) - listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") - if err != nil { - t.Errorf("Unexpected error: %v", err) - return - } - portAsInt := listener.Addr().(*net.TCPAddr).Port - portAsString := fmt.Sprintf("%d", portAsInt) - _ = listener.Close() + //listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") + //if err != nil { + // t.Errorf("Unexpected error: %v", err) + // return + //} + //portAsInt := listener.Addr().(*net.TCPAddr).Port + //portAsString := fmt.Sprintf("%d", portAsInt) + //_ = listener.Close() + + var portAsString string + var err error done := make(chan bool) go func() { - err = startTestSMTPDockerImageAndContainer(t, portAsString, ctx) + portAsString, err = startTestSMTPDockerImageAndContainer(t, ctx) if err != nil { t.Errorf("Unexpected error: %v", err) cancel() diff --git a/runnable-sftp_test.go b/runnable-sftp_test.go index 0df2b11e033a91cdcf5371fb4fdb0893b41eefd5..53db3cb9afd533c055269162e8867e5c26687706 100644 --- a/runnable-sftp_test.go +++ b/runnable-sftp_test.go @@ -3,105 +3,142 @@ package jobqueue import ( "context" "fmt" - "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/container" - "github.com/docker/docker/client" "github.com/docker/go-connections/nat" "github.com/stretchr/testify/assert" "net" "os" + "strconv" "testing" "time" ) -func startSFTPTestDockerImageAndContainer(t *testing.T, port string, volume string, ctx context.Context) error { +func startSFTPTestDockerImageAndContainer(t *testing.T, volume string, ctx context.Context) (string, error) { t.Helper() - cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) - if err != nil { - return err - } - - imageName := "atmoz/sftp:alpine" - - reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{}) - if err != nil { - return err - } - - //if debug image pull, comment out the following lines - //_, _ = io.Copy(os.Stdout, reader) - _ = reader - - host, _, _ := net.SplitHostPort(cli.DaemonHost()) - if host == "" || host == "unix" { - host = DOCKER_TEST_HOST_IP - } - - hostConfig := &container.HostConfig{ - PortBindings: nat.PortMap{ - "22/tcp": []nat.PortBinding{ - { - HostIP: host, - HostPort: port, - }, + sftpPortMap := nat.PortMap{ + "22/tcp": []nat.PortBinding{ + { + HostIP: "0.0.0.0", + HostPort: "", }, }, } - if volume != "" { - hostConfig.Binds = append(hostConfig.Binds, volume+":/home/demo/upload") - } - - resp, err := cli.ContainerCreate(ctx, &container.Config{ - Image: imageName, - Cmd: []string{"demo:secret:::upload"}, - }, hostConfig, nil, nil, "") - + sftpCmd := []string{"demo:secret:::upload"} + sftpVolume := volume // Ihr Volume hier + sftpPorts, err := startTestDockerImageAndContainer(t, ctx, "atmoz/sftp:alpine", sftpPortMap, sftpCmd, sftpVolume) if err != nil { - return err + return "", err } - if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { - return err + pp := *sftpPorts + px, ok := pp["22/tcp"] + if !ok { + return "", fmt.Errorf("22/tcp port not found") } - go func() { - <-ctx.Done() - - timeout := 0 - stopOptions := container.StopOptions{ - Timeout: &timeout, - Signal: "SIGKILL", - } - newCtx, _ := context.WithTimeout(context.Background(), 60*time.Second) - if err := cli.ContainerStop(newCtx, resp.ID, stopOptions); err != nil { - t.Errorf("ContainerStop returned error: %v", err) - } - if err := cli.ContainerRemove(newCtx, resp.ID, types.ContainerRemoveOptions{ - Force: true, - }); err != nil { - t.Errorf("ContainerRemove returned error: %v", err) - } - - }() - - statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning) - select { - case err := <-errCh: - if err != nil { - // empty error means container exited normally (see container_wait.go) - if err.Error() == "" { - return nil - } - - return err - } - case <-statusCh: - - } - - return nil + return px[0].HostPort, nil + // + //sshPorts, ok := sftpPorts["22/tcp"] + //if !ok { + // return "", fmt.Errorf("22/tcp port not found") + //} + // + //sshPort, ok := sshPorts[0].HostPort + //if !ok { + // return "", fmt.Errorf("22/tcp port not found") + //} + // + //// check if port is open + //return sshPort, nil + + //t.Helper() + // + //cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + //if err != nil { + // return err + //} + // + //imageName := "atmoz/sftp:alpine" + // + //reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{}) + //if err != nil { + // return err + //} + // + ////if debug image pull, comment out the following lines + ////_, _ = io.Copy(os.Stdout, reader) + //_ = reader + // + //host, _, _ := net.SplitHostPort(cli.DaemonHost()) + //if host == "" || host == "unix" { + // host = DOCKER_TEST_HOST_IP + //} + // + //hostConfig := &container.HostConfig{ + // PortBindings: nat.PortMap{ + // "22/tcp": []nat.PortBinding{ + // { + // HostIP: host, + // HostPort: port, + // }, + // }, + // }, + //} + // + //if volume != "" { + // hostConfig.Binds = append(hostConfig.Binds, volume+":/home/demo/upload") + //} + // + //resp, err := cli.ContainerCreate(ctx, &container.Config{ + // Image: imageName, + // Cmd: []string{"demo:secret:::upload"}, + //}, hostConfig, nil, nil, "") + // + //if err != nil { + // return err + //} + // + //if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { + // return err + //} + // + //go func() { + // <-ctx.Done() + // + // timeout := 0 + // stopOptions := container.StopOptions{ + // Timeout: &timeout, + // Signal: "SIGKILL", + // } + // newCtx, _ := context.WithTimeout(context.Background(), 60*time.Second) + // if err := cli.ContainerStop(newCtx, resp.ID, stopOptions); err != nil { + // t.Errorf("ContainerStop returned error: %v", err) + // } + // if err := cli.ContainerRemove(newCtx, resp.ID, types.ContainerRemoveOptions{ + // Force: true, + // }); err != nil { + // t.Errorf("ContainerRemove returned error: %v", err) + // } + // + //}() + // + //statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning) + //select { + //case err := <-errCh: + // if err != nil { + // // empty error means container exited normally (see container_wait.go) + // if err.Error() == "" { + // return nil + // } + // + // return err + // } + //case <-statusCh: + // + //} + // + //return nil } func TestSFTPCRunnerLocalToRemote(t *testing.T) { @@ -112,18 +149,21 @@ func TestSFTPCRunnerLocalToRemote(t *testing.T) { time.Sleep(1 * time.Second) }) - listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") - if err != nil { - t.Errorf("Unexpected error: %v", err) - return - } - portAsInt := listener.Addr().(*net.TCPAddr).Port - portAsString := fmt.Sprintf("%d", portAsInt) - _ = listener.Close() + //listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") + //if err != nil { + // t.Errorf("Unexpected error: %v", err) + // return + //} + //portAsInt := listener.Addr().(*net.TCPAddr).Port + //portAsString := fmt.Sprintf("%d", portAsInt) + //_ = listener.Close() + + var portAsString string + var err error done := make(chan bool) go func() { - err = startSFTPTestDockerImageAndContainer(t, portAsString, "", ctx) + portAsString, err = startSFTPTestDockerImageAndContainer(t, "", ctx) if err != nil { t.Errorf("Unexpected error: %v", err) cancel() @@ -162,6 +202,8 @@ func TestSFTPCRunnerLocalToRemote(t *testing.T) { } } + portAsInt, err := strconv.Atoi(portAsString) + sftpRunnable := &SFTPRunnable{ Host: DOCKER_TEST_HOST_IP, Port: portAsInt, @@ -205,14 +247,14 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) { time.Sleep(1 * time.Second) }) - listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") - if err != nil { - t.Errorf("Unexpected error: %v", err) - return - } - portAsInt := listener.Addr().(*net.TCPAddr).Port - portAsString := fmt.Sprintf("%d", portAsInt) - _ = listener.Close() + //listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") + //if err != nil { + // t.Errorf("Unexpected error: %v", err) + // return + //} + //portAsInt := listener.Addr().(*net.TCPAddr).Port + //portAsString := fmt.Sprintf("%d", portAsInt) + //_ = listener.Close() tempSrcDir := t.TempDir() // create 4 test files @@ -224,9 +266,12 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) { } } + var portAsString string + var err error + done := make(chan bool) go func() { - err = startSFTPTestDockerImageAndContainer(t, portAsString, tempSrcDir, ctx) + portAsString, err = startSFTPTestDockerImageAndContainer(t, tempSrcDir, ctx) if err != nil { t.Errorf("Unexpected error: %v", err) cancel() @@ -257,6 +302,9 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) { tempDir := t.TempDir() + portAsInt, err := strconv.Atoi(portAsString) + assert.NoError(t, err) + sftpRunnable := &SFTPRunnable{ Host: DOCKER_TEST_HOST_IP, Port: portAsInt, diff --git a/utils.go b/utils.go new file mode 100644 index 0000000000000000000000000000000000000000..ad993b5c5de4fee634785f3fd8fb51d2f5b3de25 --- /dev/null +++ b/utils.go @@ -0,0 +1,81 @@ +package jobqueue + +import ( + "context" + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/client" + "github.com/docker/go-connections/nat" + "testing" + "time" +) + +const ( + DOCKER_TEST_HOST_IP = "172.17.0.1" +) + +func startTestDockerImageAndContainer(t *testing.T, ctx context.Context, imageName string, portMap nat.PortMap, cmd []string, volume string) (*nat.PortMap, error) { + t.Helper() + + cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) + if err != nil { + return nil, err + } + + reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{}) + if err != nil { + return nil, err + } + _ = reader + + //host, _, _ := net.SplitHostPort(cli.DaemonHost()) + //if host == "" || host == "unix" { + // host = DOCKER_TEST_HOST_IP + //} + + hostConfig := &container.HostConfig{ + PortBindings: portMap, + } + + if volume != "" { + hostConfig.Binds = append(hostConfig.Binds, volume+":/home/demo/upload") + } + + resp, err := cli.ContainerCreate(ctx, &container.Config{ + Image: imageName, + Cmd: cmd, + }, hostConfig, nil, nil, "") + + if err != nil { + return nil, err + } + + if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { + return nil, err + } + + go func() { + <-ctx.Done() + + timeout := 0 + stopOptions := container.StopOptions{ + Timeout: &timeout, + } + newCtx, _ := context.WithTimeout(context.Background(), 60*time.Second) + if err := cli.ContainerStop(newCtx, resp.ID, stopOptions); err != nil { + t.Errorf("ContainerStop returned error: %v", err) + } + if err := cli.ContainerRemove(newCtx, resp.ID, types.ContainerRemoveOptions{ + Force: true, + }); err != nil { + t.Errorf("ContainerRemove returned error: %v", err) + } + }() + + inspected, err := cli.ContainerInspect(ctx, resp.ID) + if err != nil { + return nil, err + } + + return &inspected.NetworkSettings.Ports, nil +}