mirror of
https://github.com/sablierapp/sablier.git
synced 2025-12-21 13:23:03 +01:00
* add tmp test * add strategy config * strategy config * pause / unpause strategy impl * Fix tests * fix compilation * add pause / unpause tests * add doc * add config test * start if not paused * remove test files
159 lines
3.4 KiB
Go
159 lines
3.4 KiB
Go
package docker_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/neilotoole/slogt"
|
|
"github.com/sablierapp/sablier/pkg/provider/docker"
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestDockerClassicProvider_Start(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping test in short mode.")
|
|
}
|
|
|
|
ctx := context.Background()
|
|
type args struct {
|
|
do func(dind *dindContainer) (string, error)
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
err error
|
|
}{
|
|
{
|
|
name: "non existing container start",
|
|
args: args{
|
|
do: func(dind *dindContainer) (string, error) {
|
|
return "non-existent", nil
|
|
},
|
|
},
|
|
err: fmt.Errorf("cannot start container non-existent: Error response from daemon: No such container: non-existent"),
|
|
},
|
|
{
|
|
name: "container start as expected",
|
|
args: args{
|
|
do: func(dind *dindContainer) (string, error) {
|
|
c, err := dind.CreateMimic(ctx, MimicOptions{})
|
|
return c.ID, err
|
|
},
|
|
},
|
|
err: nil,
|
|
},
|
|
}
|
|
c := setupDinD(t)
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
p, err := docker.New(ctx, c.client, slogt.New(t), "stop")
|
|
assert.NilError(t, err)
|
|
|
|
name, err := tt.args.do(c)
|
|
assert.NilError(t, err)
|
|
|
|
err = p.InstanceStart(t.Context(), name)
|
|
if tt.err != nil {
|
|
assert.Error(t, err, tt.err.Error())
|
|
} else {
|
|
assert.NilError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDockerClassicProvider_Unpause(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping test in short mode.")
|
|
}
|
|
|
|
ctx := context.Background()
|
|
type args struct {
|
|
do func(dind *dindContainer) (string, error)
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
err error
|
|
}{
|
|
{
|
|
name: "non existing container unpause",
|
|
args: args{
|
|
do: func(dind *dindContainer) (string, error) {
|
|
return "non-existent", nil
|
|
},
|
|
},
|
|
err: fmt.Errorf("cannot inspect container non-existent before unpausing: Error response from daemon: No such container: non-existent"),
|
|
},
|
|
{
|
|
name: "container starts because was not paused",
|
|
args: args{
|
|
do: func(dind *dindContainer) (string, error) {
|
|
c, err := dind.CreateMimic(ctx, MimicOptions{})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = dind.client.ContainerStart(ctx, c.ID, container.StartOptions{})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = dind.client.ContainerStop(ctx, c.ID, container.StopOptions{})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return c.ID, nil
|
|
},
|
|
},
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "container unpause as expected",
|
|
args: args{
|
|
do: func(dind *dindContainer) (string, error) {
|
|
c, err := dind.CreateMimic(ctx, MimicOptions{})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = dind.client.ContainerStart(ctx, c.ID, container.StartOptions{})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = dind.client.ContainerPause(ctx, c.ID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return c.ID, nil
|
|
},
|
|
},
|
|
err: nil,
|
|
},
|
|
}
|
|
c := setupDinD(t)
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
p, err := docker.New(ctx, c.client, slogt.New(t), "pause")
|
|
assert.NilError(t, err)
|
|
|
|
name, err := tt.args.do(c)
|
|
assert.NilError(t, err)
|
|
|
|
err = p.InstanceStart(t.Context(), name)
|
|
if tt.err != nil {
|
|
assert.Error(t, err, tt.err.Error())
|
|
} else {
|
|
assert.NilError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|