2017-02-24 21:20:40 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
docker "github.com/fsouza/go-dockerclient"
|
2017-09-29 16:58:48 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/uuid"
|
2017-02-24 21:20:40 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockImageClient struct {
|
|
|
|
pulled map[string]int
|
|
|
|
idToName map[string]string
|
|
|
|
removed map[string]int
|
|
|
|
pullDelay time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockImageClient(idToName map[string]string, pullDelay time.Duration) *mockImageClient {
|
|
|
|
return &mockImageClient{
|
|
|
|
pulled: make(map[string]int),
|
|
|
|
removed: make(map[string]int),
|
|
|
|
idToName: idToName,
|
|
|
|
pullDelay: pullDelay,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockImageClient) PullImage(opts docker.PullImageOptions, auth docker.AuthConfiguration) error {
|
|
|
|
time.Sleep(m.pullDelay)
|
|
|
|
m.pulled[opts.Repository]++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockImageClient) InspectImage(id string) (*docker.Image, error) {
|
|
|
|
return &docker.Image{
|
|
|
|
ID: m.idToName[id],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockImageClient) RemoveImage(id string) error {
|
|
|
|
m.removed[id]++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCoordinator_ConcurrentPulls(t *testing.T) {
|
2017-07-21 19:06:39 +00:00
|
|
|
t.Parallel()
|
2017-02-24 21:20:40 +00:00
|
|
|
image := "foo"
|
2017-09-29 16:58:48 +00:00
|
|
|
imageID := uuid.Generate()
|
2017-02-24 21:20:40 +00:00
|
|
|
mapping := map[string]string{imageID: image}
|
|
|
|
|
|
|
|
// Add a delay so we can get multiple queued up
|
|
|
|
mock := newMockImageClient(mapping, 10*time.Millisecond)
|
|
|
|
config := &dockerCoordinatorConfig{
|
|
|
|
logger: testLogger(),
|
|
|
|
cleanup: true,
|
|
|
|
client: mock,
|
|
|
|
removeDelay: 100 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a coordinator
|
|
|
|
coordinator := NewDockerCoordinator(config)
|
|
|
|
|
|
|
|
id := ""
|
|
|
|
for i := 0; i < 10; i++ {
|
2017-03-01 02:19:13 +00:00
|
|
|
go func() {
|
2017-09-29 16:58:48 +00:00
|
|
|
id, _ = coordinator.PullImage(image, nil, uuid.Generate())
|
2017-03-01 02:19:13 +00:00
|
|
|
}()
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 02:19:13 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
p := mock.pulled[image]
|
2017-07-23 02:04:36 +00:00
|
|
|
if p >= 10 {
|
2017-03-01 02:19:13 +00:00
|
|
|
return false, fmt.Errorf("Wrong number of pulls: %d", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the reference count
|
2017-03-26 00:05:53 +00:00
|
|
|
if references := coordinator.imageRefCount[id]; len(references) != 10 {
|
|
|
|
return false, fmt.Errorf("Got reference count %d; want %d", len(references), 10)
|
2017-03-01 02:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure there is no pull future
|
|
|
|
if len(coordinator.pullFutures) != 0 {
|
|
|
|
return false, fmt.Errorf("Pull future exists after pull finished")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCoordinator_Pull_Remove(t *testing.T) {
|
2017-07-21 19:06:39 +00:00
|
|
|
t.Parallel()
|
2017-02-24 21:20:40 +00:00
|
|
|
image := "foo"
|
2017-09-29 16:58:48 +00:00
|
|
|
imageID := uuid.Generate()
|
2017-02-24 21:20:40 +00:00
|
|
|
mapping := map[string]string{imageID: image}
|
|
|
|
|
|
|
|
// Add a delay so we can get multiple queued up
|
|
|
|
mock := newMockImageClient(mapping, 10*time.Millisecond)
|
|
|
|
config := &dockerCoordinatorConfig{
|
|
|
|
logger: testLogger(),
|
|
|
|
cleanup: true,
|
|
|
|
client: mock,
|
|
|
|
removeDelay: 1 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a coordinator
|
|
|
|
coordinator := NewDockerCoordinator(config)
|
|
|
|
|
|
|
|
id := ""
|
2017-03-26 00:05:53 +00:00
|
|
|
callerIDs := make([]string, 10, 10)
|
2017-02-24 21:20:40 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
2017-09-29 16:58:48 +00:00
|
|
|
callerIDs[i] = uuid.Generate()
|
2017-03-26 00:05:53 +00:00
|
|
|
id, _ = coordinator.PullImage(image, nil, callerIDs[i])
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the reference count
|
2017-03-26 00:05:53 +00:00
|
|
|
if references := coordinator.imageRefCount[id]; len(references) != 10 {
|
|
|
|
t.Fatalf("Got reference count %d; want %d", len(references), 10)
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove some
|
|
|
|
for i := 0; i < 8; i++ {
|
2017-03-26 00:05:53 +00:00
|
|
|
coordinator.RemoveImage(id, callerIDs[i])
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the reference count
|
2017-03-26 00:05:53 +00:00
|
|
|
if references := coordinator.imageRefCount[id]; len(references) != 2 {
|
|
|
|
t.Fatalf("Got reference count %d; want %d", len(references), 2)
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all
|
2017-03-26 00:05:53 +00:00
|
|
|
for i := 8; i < 10; i++ {
|
|
|
|
coordinator.RemoveImage(id, callerIDs[i])
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the reference count
|
2017-03-26 00:05:53 +00:00
|
|
|
if references := coordinator.imageRefCount[id]; len(references) != 0 {
|
|
|
|
t.Fatalf("Got reference count %d; want %d", len(references), 0)
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that only one delete happened
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
removes := mock.removed[id]
|
|
|
|
return removes == 1, fmt.Errorf("Wrong number of removes: %d", removes)
|
|
|
|
}, func(err error) {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
})
|
2017-02-28 03:09:13 +00:00
|
|
|
|
|
|
|
// Make sure there is no future still
|
|
|
|
if _, ok := coordinator.deleteFuture[id]; ok {
|
|
|
|
t.Fatal("Got delete future")
|
|
|
|
}
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCoordinator_Remove_Cancel(t *testing.T) {
|
2017-07-21 19:06:39 +00:00
|
|
|
t.Parallel()
|
2017-02-24 21:20:40 +00:00
|
|
|
image := "foo"
|
2017-09-29 16:58:48 +00:00
|
|
|
imageID := uuid.Generate()
|
2017-02-24 21:20:40 +00:00
|
|
|
mapping := map[string]string{imageID: image}
|
|
|
|
|
|
|
|
mock := newMockImageClient(mapping, 1*time.Millisecond)
|
|
|
|
config := &dockerCoordinatorConfig{
|
|
|
|
logger: testLogger(),
|
|
|
|
cleanup: true,
|
|
|
|
client: mock,
|
2017-03-01 02:19:13 +00:00
|
|
|
removeDelay: 100 * time.Millisecond,
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a coordinator
|
|
|
|
coordinator := NewDockerCoordinator(config)
|
2017-09-29 16:58:48 +00:00
|
|
|
callerID := uuid.Generate()
|
2017-02-24 21:20:40 +00:00
|
|
|
|
|
|
|
// Pull image
|
2017-03-26 00:05:53 +00:00
|
|
|
id, _ := coordinator.PullImage(image, nil, callerID)
|
2017-02-24 21:20:40 +00:00
|
|
|
|
|
|
|
// Check the reference count
|
2017-03-26 00:05:53 +00:00
|
|
|
if references := coordinator.imageRefCount[id]; len(references) != 1 {
|
|
|
|
t.Fatalf("Got reference count %d; want %d", len(references), 1)
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove image
|
2017-03-26 00:05:53 +00:00
|
|
|
coordinator.RemoveImage(id, callerID)
|
2017-02-24 21:20:40 +00:00
|
|
|
|
|
|
|
// Check the reference count
|
2017-03-26 00:05:53 +00:00
|
|
|
if references := coordinator.imageRefCount[id]; len(references) != 0 {
|
|
|
|
t.Fatalf("Got reference count %d; want %d", len(references), 0)
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pull image again within delay
|
2017-03-26 00:05:53 +00:00
|
|
|
id, _ = coordinator.PullImage(image, nil, callerID)
|
2017-02-24 21:20:40 +00:00
|
|
|
|
|
|
|
// Check the reference count
|
2017-03-26 00:05:53 +00:00
|
|
|
if references := coordinator.imageRefCount[id]; len(references) != 1 {
|
|
|
|
t.Fatalf("Got reference count %d; want %d", len(references), 1)
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check that only no delete happened
|
|
|
|
if removes := mock.removed[id]; removes != 0 {
|
|
|
|
t.Fatalf("Image deleted when it shouldn't have")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDockerCoordinator_No_Cleanup(t *testing.T) {
|
2017-07-21 19:06:39 +00:00
|
|
|
t.Parallel()
|
2017-02-24 21:20:40 +00:00
|
|
|
image := "foo"
|
2017-09-29 16:58:48 +00:00
|
|
|
imageID := uuid.Generate()
|
2017-02-24 21:20:40 +00:00
|
|
|
mapping := map[string]string{imageID: image}
|
|
|
|
|
|
|
|
mock := newMockImageClient(mapping, 1*time.Millisecond)
|
|
|
|
config := &dockerCoordinatorConfig{
|
|
|
|
logger: testLogger(),
|
|
|
|
cleanup: false,
|
|
|
|
client: mock,
|
|
|
|
removeDelay: 1 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a coordinator
|
|
|
|
coordinator := NewDockerCoordinator(config)
|
2017-09-29 16:58:48 +00:00
|
|
|
callerID := uuid.Generate()
|
2017-02-24 21:20:40 +00:00
|
|
|
|
|
|
|
// Pull image
|
2017-03-26 00:05:53 +00:00
|
|
|
id, _ := coordinator.PullImage(image, nil, callerID)
|
2017-02-24 21:20:40 +00:00
|
|
|
|
|
|
|
// Check the reference count
|
2017-03-26 00:05:53 +00:00
|
|
|
if references := coordinator.imageRefCount[id]; len(references) != 0 {
|
|
|
|
t.Fatalf("Got reference count %d; want %d", len(references), 0)
|
2017-02-24 21:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove image
|
2017-03-26 00:05:53 +00:00
|
|
|
coordinator.RemoveImage(id, callerID)
|
2017-02-24 21:20:40 +00:00
|
|
|
|
|
|
|
// Check that only no delete happened
|
|
|
|
if removes := mock.removed[id]; removes != 0 {
|
|
|
|
t.Fatalf("Image deleted when it shouldn't have")
|
|
|
|
}
|
|
|
|
}
|