Skip to content
Snippets Groups Projects
Verified Commit b4d6a6bb authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: ci pipeline

parent 0d3bdc7c
No related branches found
No related tags found
No related merge requests found
package jobqueue
const (
DOCKER_TEST_HOST_IP = "172.17.0.1"
)
...@@ -3,9 +3,6 @@ package jobqueue ...@@ -3,9 +3,6 @@ package jobqueue
import ( import (
"context" "context"
"fmt" "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/docker/go-connections/nat"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"net" "net"
...@@ -13,95 +10,129 @@ import ( ...@@ -13,95 +10,129 @@ import (
"time" "time"
) )
func startTestSMTPDockerImageAndContainer(t *testing.T, port string, ctx context.Context) error { func startTestSMTPDockerImageAndContainer(t *testing.T, ctx context.Context) (string, error) {
t.Helper() t.Helper()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) smtpPortMap := nat.PortMap{
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{ "1025/tcp": []nat.PortBinding{
{ {
HostIP: host, HostIP: "0.0.0.0",
HostPort: port, HostPort: "",
}, },
}, },
"8025/tcp": []nat.PortBinding{ "8025/tcp": []nat.PortBinding{
{ {
HostIP: host, HostIP: "0.0.0.0",
HostPort: "8025", 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)
} }
}() smtpCmd := []string{} // Ihr Command hier, falls nötig
smtpVolume := "" // Ihr Volume hier
smtpPorts, err := startTestDockerImageAndContainer(t, ctx, "axllent/mailpit", smtpPortMap, smtpCmd, smtpVolume)
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil { if err != nil {
// empty error means container exited normally (see container_wait.go) return "", err
if err.Error() == "" { }
return nil
} pp := *smtpPorts
return err px, ok := pp["1025/tcp"]
} if !ok {
case <-statusCh: return "", fmt.Errorf("1025/tcp port not found")
}
}
smtpPort := px[0].HostPort
return nil
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) { func TestMailRunner(t *testing.T) {
...@@ -112,18 +143,21 @@ func TestMailRunner(t *testing.T) { ...@@ -112,18 +143,21 @@ func TestMailRunner(t *testing.T) {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
}) })
listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") //listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0")
if err != nil { //if err != nil {
t.Errorf("Unexpected error: %v", err) // t.Errorf("Unexpected error: %v", err)
return // return
} //}
portAsInt := listener.Addr().(*net.TCPAddr).Port //portAsInt := listener.Addr().(*net.TCPAddr).Port
portAsString := fmt.Sprintf("%d", portAsInt) //portAsString := fmt.Sprintf("%d", portAsInt)
_ = listener.Close() //_ = listener.Close()
var portAsString string
var err error
done := make(chan bool) done := make(chan bool)
go func() { go func() {
err = startTestSMTPDockerImageAndContainer(t, portAsString, ctx) portAsString, err = startTestSMTPDockerImageAndContainer(t, ctx)
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
cancel() cancel()
......
...@@ -3,105 +3,142 @@ package jobqueue ...@@ -3,105 +3,142 @@ package jobqueue
import ( import (
"context" "context"
"fmt" "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/docker/go-connections/nat"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"net" "net"
"os" "os"
"strconv"
"testing" "testing"
"time" "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() t.Helper()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) sftpPortMap := nat.PortMap{
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{ "22/tcp": []nat.PortBinding{
{ {
HostIP: host, HostIP: "0.0.0.0",
HostPort: port, HostPort: "",
},
}, },
}, },
} }
if volume != "" { sftpCmd := []string{"demo:secret:::upload"}
hostConfig.Binds = append(hostConfig.Binds, volume+":/home/demo/upload") sftpVolume := volume // Ihr Volume hier
} sftpPorts, err := startTestDockerImageAndContainer(t, ctx, "atmoz/sftp:alpine", sftpPortMap, sftpCmd, sftpVolume)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: imageName,
Cmd: []string{"demo:secret:::upload"},
}, hostConfig, nil, nil, "")
if err != nil { if err != nil {
return err return "", err
} }
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { pp := *sftpPorts
return err px, ok := pp["22/tcp"]
} if !ok {
return "", fmt.Errorf("22/tcp port not found")
go func() { }
<-ctx.Done()
return px[0].HostPort, nil
timeout := 0 //
stopOptions := container.StopOptions{ //sshPorts, ok := sftpPorts["22/tcp"]
Timeout: &timeout, //if !ok {
Signal: "SIGKILL", // return "", fmt.Errorf("22/tcp port not found")
} //}
newCtx, _ := context.WithTimeout(context.Background(), 60*time.Second) //
if err := cli.ContainerStop(newCtx, resp.ID, stopOptions); err != nil { //sshPort, ok := sshPorts[0].HostPort
t.Errorf("ContainerStop returned error: %v", err) //if !ok {
} // return "", fmt.Errorf("22/tcp port not found")
if err := cli.ContainerRemove(newCtx, resp.ID, types.ContainerRemoveOptions{ //}
Force: true, //
}); err != nil { //// check if port is open
t.Errorf("ContainerRemove returned error: %v", err) //return sshPort, nil
}
//t.Helper()
}() //
//cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning) //if err != nil {
select { // return err
case err := <-errCh: //}
if err != nil { //
// empty error means container exited normally (see container_wait.go) //imageName := "atmoz/sftp:alpine"
if err.Error() == "" { //
return nil //reader, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
} //if err != nil {
// return err
return err //}
} //
case <-statusCh: ////if debug image pull, comment out the following lines
////_, _ = io.Copy(os.Stdout, reader)
} //_ = reader
//
return nil //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) { func TestSFTPCRunnerLocalToRemote(t *testing.T) {
...@@ -112,18 +149,21 @@ func TestSFTPCRunnerLocalToRemote(t *testing.T) { ...@@ -112,18 +149,21 @@ func TestSFTPCRunnerLocalToRemote(t *testing.T) {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
}) })
listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") //listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0")
if err != nil { //if err != nil {
t.Errorf("Unexpected error: %v", err) // t.Errorf("Unexpected error: %v", err)
return // return
} //}
portAsInt := listener.Addr().(*net.TCPAddr).Port //portAsInt := listener.Addr().(*net.TCPAddr).Port
portAsString := fmt.Sprintf("%d", portAsInt) //portAsString := fmt.Sprintf("%d", portAsInt)
_ = listener.Close() //_ = listener.Close()
var portAsString string
var err error
done := make(chan bool) done := make(chan bool)
go func() { go func() {
err = startSFTPTestDockerImageAndContainer(t, portAsString, "", ctx) portAsString, err = startSFTPTestDockerImageAndContainer(t, "", ctx)
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
cancel() cancel()
...@@ -162,6 +202,8 @@ func TestSFTPCRunnerLocalToRemote(t *testing.T) { ...@@ -162,6 +202,8 @@ func TestSFTPCRunnerLocalToRemote(t *testing.T) {
} }
} }
portAsInt, err := strconv.Atoi(portAsString)
sftpRunnable := &SFTPRunnable{ sftpRunnable := &SFTPRunnable{
Host: DOCKER_TEST_HOST_IP, Host: DOCKER_TEST_HOST_IP,
Port: portAsInt, Port: portAsInt,
...@@ -205,14 +247,14 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) { ...@@ -205,14 +247,14 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) {
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
}) })
listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0") //listener, err := net.Listen("tcp", DOCKER_TEST_HOST_IP+":0")
if err != nil { //if err != nil {
t.Errorf("Unexpected error: %v", err) // t.Errorf("Unexpected error: %v", err)
return // return
} //}
portAsInt := listener.Addr().(*net.TCPAddr).Port //portAsInt := listener.Addr().(*net.TCPAddr).Port
portAsString := fmt.Sprintf("%d", portAsInt) //portAsString := fmt.Sprintf("%d", portAsInt)
_ = listener.Close() //_ = listener.Close()
tempSrcDir := t.TempDir() tempSrcDir := t.TempDir()
// create 4 test files // create 4 test files
...@@ -224,9 +266,12 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) { ...@@ -224,9 +266,12 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) {
} }
} }
var portAsString string
var err error
done := make(chan bool) done := make(chan bool)
go func() { go func() {
err = startSFTPTestDockerImageAndContainer(t, portAsString, tempSrcDir, ctx) portAsString, err = startSFTPTestDockerImageAndContainer(t, tempSrcDir, ctx)
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
cancel() cancel()
...@@ -257,6 +302,9 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) { ...@@ -257,6 +302,9 @@ func TestSFTPCRunnerRemoteToLocal(t *testing.T) {
tempDir := t.TempDir() tempDir := t.TempDir()
portAsInt, err := strconv.Atoi(portAsString)
assert.NoError(t, err)
sftpRunnable := &SFTPRunnable{ sftpRunnable := &SFTPRunnable{
Host: DOCKER_TEST_HOST_IP, Host: DOCKER_TEST_HOST_IP,
Port: portAsInt, Port: portAsInt,
......
utils.go 0 → 100644
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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment