2019-09-13 15:24:58 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
|
|
|
"github.com/hashicorp/nomad/client/testutil"
|
2019-12-04 00:15:11 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/freeport"
|
2019-09-13 15:24:58 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
func fakeContainerList(t *testing.T) (nomadContainer, nonNomadContainer docker.APIContainers) {
|
2019-09-13 15:24:58 +00:00
|
|
|
path := "./test-resources/docker/reconciler_containers_list.json"
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
2019-10-17 12:37:18 +00:00
|
|
|
t.Fatalf("failed to open file: %v", err)
|
2019-09-13 15:24:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
var sampleContainerList []docker.APIContainers
|
2019-09-13 15:24:58 +00:00
|
|
|
err = json.NewDecoder(f).Decode(&sampleContainerList)
|
|
|
|
if err != nil {
|
2019-10-17 12:37:18 +00:00
|
|
|
t.Fatalf("failed to decode container list: %v", err)
|
2019-09-13 15:24:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
return sampleContainerList[0], sampleContainerList[1]
|
2019-09-13 15:24:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_HasMount(t *testing.T) {
|
2019-10-17 12:37:18 +00:00
|
|
|
nomadContainer, nonNomadContainer := fakeContainerList(t)
|
2019-09-13 15:24:58 +00:00
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
require.True(t, hasMount(nomadContainer, "/alloc"))
|
|
|
|
require.True(t, hasMount(nomadContainer, "/data"))
|
|
|
|
require.True(t, hasMount(nomadContainer, "/secrets"))
|
|
|
|
require.False(t, hasMount(nomadContainer, "/random"))
|
2019-09-13 15:24:58 +00:00
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
require.False(t, hasMount(nonNomadContainer, "/alloc"))
|
|
|
|
require.False(t, hasMount(nonNomadContainer, "/data"))
|
|
|
|
require.False(t, hasMount(nonNomadContainer, "/secrets"))
|
|
|
|
require.False(t, hasMount(nonNomadContainer, "/random"))
|
|
|
|
}
|
2019-09-13 15:24:58 +00:00
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
func Test_HasNomadName(t *testing.T) {
|
|
|
|
nomadContainer, nonNomadContainer := fakeContainerList(t)
|
2019-09-13 15:24:58 +00:00
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
require.True(t, hasNomadName(nomadContainer))
|
|
|
|
require.False(t, hasNomadName(nonNomadContainer))
|
2019-09-13 15:24:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
// TestDanglingContainerRemoval asserts containers without corresponding tasks
|
|
|
|
// are removed after the creation grace period.
|
2019-09-13 15:24:58 +00:00
|
|
|
func TestDanglingContainerRemoval(t *testing.T) {
|
|
|
|
testutil.DockerCompatible(t)
|
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
// start two containers: one tracked nomad container, and one unrelated container
|
2019-12-04 00:15:11 +00:00
|
|
|
task, cfg, ports := dockerTask(t)
|
|
|
|
defer freeport.Return(ports)
|
2019-09-13 15:24:58 +00:00
|
|
|
require.NoError(t, task.EncodeConcreteDriverConfig(cfg))
|
|
|
|
|
|
|
|
client, d, handle, cleanup := dockerSetup(t, task)
|
|
|
|
defer cleanup()
|
|
|
|
require.NoError(t, d.WaitUntilStarted(task.ID, 5*time.Second))
|
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
nonNomadContainer, err := client.CreateContainer(docker.CreateContainerOptions{
|
2019-09-13 15:24:58 +00:00
|
|
|
Name: "mytest-image-" + uuid.Generate(),
|
|
|
|
Config: &docker.Config{
|
|
|
|
Image: cfg.Image,
|
|
|
|
Cmd: append([]string{cfg.Command}, cfg.Args...),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer client.RemoveContainer(docker.RemoveContainerOptions{
|
2019-10-17 14:28:23 +00:00
|
|
|
ID: nonNomadContainer.ID,
|
2019-09-13 15:24:58 +00:00
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
err = client.StartContainer(nonNomadContainer.ID, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
untrackedNomadContainer, err := client.CreateContainer(docker.CreateContainerOptions{
|
|
|
|
Name: "mytest-image-" + uuid.Generate(),
|
|
|
|
Config: &docker.Config{
|
|
|
|
Image: cfg.Image,
|
|
|
|
Cmd: append([]string{cfg.Command}, cfg.Args...),
|
|
|
|
Labels: map[string]string{
|
|
|
|
dockerLabelAllocID: uuid.Generate(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer client.RemoveContainer(docker.RemoveContainerOptions{
|
|
|
|
ID: untrackedNomadContainer.ID,
|
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
err = client.StartContainer(untrackedNomadContainer.ID, nil)
|
2019-09-13 15:24:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dd := d.Impl().(*Driver)
|
2019-10-17 12:37:18 +00:00
|
|
|
|
|
|
|
reconciler := newReconciler(dd)
|
2019-09-13 15:24:58 +00:00
|
|
|
trackedContainers := map[string]bool{handle.containerID: true}
|
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
tf := reconciler.trackedContainers()
|
|
|
|
require.Contains(t, tf, handle.containerID)
|
2019-10-17 14:28:23 +00:00
|
|
|
require.NotContains(t, tf, untrackedNomadContainer)
|
|
|
|
require.NotContains(t, tf, nonNomadContainer.ID)
|
2019-09-13 15:24:58 +00:00
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
// assert tracked containers should never be untracked
|
|
|
|
untracked, err := reconciler.untrackedContainers(trackedContainers, time.Now())
|
2019-09-13 15:24:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotContains(t, untracked, handle.containerID)
|
2019-10-17 14:28:23 +00:00
|
|
|
require.NotContains(t, untracked, nonNomadContainer.ID)
|
|
|
|
require.Contains(t, untracked, untrackedNomadContainer.ID)
|
2019-09-13 15:24:58 +00:00
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
// assert we recognize nomad containers with appropriate cutoff
|
|
|
|
untracked, err = reconciler.untrackedContainers(map[string]bool{}, time.Now())
|
2019-09-13 15:24:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, untracked, handle.containerID)
|
2019-10-17 14:28:23 +00:00
|
|
|
require.Contains(t, untracked, untrackedNomadContainer.ID)
|
|
|
|
require.NotContains(t, untracked, nonNomadContainer.ID)
|
2019-09-13 15:24:58 +00:00
|
|
|
|
2019-10-17 12:37:18 +00:00
|
|
|
// but ignore if creation happened before cutoff
|
|
|
|
untracked, err = reconciler.untrackedContainers(map[string]bool{}, time.Now().Add(-1*time.Minute))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotContains(t, untracked, handle.containerID)
|
2019-10-17 14:28:23 +00:00
|
|
|
require.NotContains(t, untracked, untrackedNomadContainer.ID)
|
|
|
|
require.NotContains(t, untracked, nonNomadContainer.ID)
|
2019-10-17 12:37:18 +00:00
|
|
|
|
|
|
|
// a full integration tests to assert that containers are removed
|
2019-09-13 15:24:58 +00:00
|
|
|
prestineDriver := dockerDriverHarness(t, nil).Impl().(*Driver)
|
|
|
|
prestineDriver.config.GC.DanglingContainers = ContainerGCConfig{
|
2019-10-17 12:37:18 +00:00
|
|
|
Enabled: true,
|
|
|
|
period: 1 * time.Second,
|
2019-10-17 14:28:23 +00:00
|
|
|
CreationGrace: 0 * time.Second,
|
2019-09-13 15:24:58 +00:00
|
|
|
}
|
2019-10-17 12:37:18 +00:00
|
|
|
nReconciler := newReconciler(prestineDriver)
|
|
|
|
|
|
|
|
require.NoError(t, nReconciler.removeDanglingContainersIteration())
|
2019-09-13 15:24:58 +00:00
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
_, err = client.InspectContainer(nonNomadContainer.ID)
|
2019-09-13 15:24:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = client.InspectContainer(handle.containerID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), NoSuchContainerError)
|
2019-10-17 14:28:23 +00:00
|
|
|
|
|
|
|
_, err = client.InspectContainer(untrackedNomadContainer.ID)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), NoSuchContainerError)
|
2019-09-13 15:24:58 +00:00
|
|
|
}
|
2019-10-17 12:37:18 +00:00
|
|
|
|
|
|
|
// TestDanglingContainerRemoval_Stopped asserts stopped containers without
|
|
|
|
// corresponding tasks are not removed even if after creation grace period.
|
|
|
|
func TestDanglingContainerRemoval_Stopped(t *testing.T) {
|
|
|
|
testutil.DockerCompatible(t)
|
|
|
|
|
2019-12-04 00:15:11 +00:00
|
|
|
_, cfg, ports := dockerTask(t)
|
|
|
|
defer freeport.Return(ports)
|
2019-10-17 12:37:18 +00:00
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
client := newTestDockerClient(t)
|
|
|
|
container, err := client.CreateContainer(docker.CreateContainerOptions{
|
|
|
|
Name: "mytest-image-" + uuid.Generate(),
|
|
|
|
Config: &docker.Config{
|
|
|
|
Image: cfg.Image,
|
|
|
|
Cmd: append([]string{cfg.Command}, cfg.Args...),
|
|
|
|
Labels: map[string]string{
|
|
|
|
dockerLabelAllocID: uuid.Generate(),
|
|
|
|
},
|
|
|
|
},
|
2019-10-17 12:37:18 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer client.RemoveContainer(docker.RemoveContainerOptions{
|
2019-10-17 14:28:23 +00:00
|
|
|
ID: container.ID,
|
2019-10-17 12:37:18 +00:00
|
|
|
Force: true,
|
|
|
|
})
|
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
err = client.StartContainer(container.ID, nil)
|
2019-10-17 12:37:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
err = client.StopContainer(container.ID, 60)
|
2019-10-17 12:37:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
dd := dockerDriverHarness(t, nil).Impl().(*Driver)
|
2019-10-17 12:37:18 +00:00
|
|
|
reconciler := newReconciler(dd)
|
|
|
|
|
|
|
|
// assert nomad container is tracked, and we ignore stopped one
|
|
|
|
tf := reconciler.trackedContainers()
|
2019-10-17 14:28:23 +00:00
|
|
|
require.NotContains(t, tf, container.ID)
|
2019-10-17 12:37:18 +00:00
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
untracked, err := reconciler.untrackedContainers(map[string]bool{}, time.Now())
|
2019-10-17 12:37:18 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-17 14:28:23 +00:00
|
|
|
require.NotContains(t, untracked, container.ID)
|
2019-10-17 12:37:18 +00:00
|
|
|
|
|
|
|
// if we start container again, it'll be marked as untracked
|
2019-10-17 14:28:23 +00:00
|
|
|
require.NoError(t, client.StartContainer(container.ID, nil))
|
2019-10-17 12:37:18 +00:00
|
|
|
|
2019-10-17 14:28:23 +00:00
|
|
|
untracked, err = reconciler.untrackedContainers(map[string]bool{}, time.Now())
|
2019-10-17 12:37:18 +00:00
|
|
|
require.NoError(t, err)
|
2019-10-17 14:28:23 +00:00
|
|
|
require.Contains(t, untracked, container.ID)
|
2019-10-17 12:37:18 +00:00
|
|
|
}
|