github.com/anuvu/nomad@v0.8.7-atom1/client/driver/docker_linux_test.go (about)

     1  package driver
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/hashicorp/nomad/client/testutil"
    13  	tu "github.com/hashicorp/nomad/testutil"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestDockerDriver_authFromHelper(t *testing.T) {
    18  	dir, err := ioutil.TempDir("", "test-docker-driver_authfromhelper")
    19  	require.NoError(t, err)
    20  	defer os.RemoveAll(dir)
    21  	helperPayload := "{\"Username\":\"hashi\",\"Secret\":\"nomad\"}"
    22  	helperContent := []byte(fmt.Sprintf("#!/bin/sh\ncat > %s/helper-$1.out;echo '%s'", dir, helperPayload))
    23  
    24  	helperFile := filepath.Join(dir, "docker-credential-testnomad")
    25  	err = ioutil.WriteFile(helperFile, helperContent, 0777)
    26  	require.NoError(t, err)
    27  
    28  	path := os.Getenv("PATH")
    29  	os.Setenv("PATH", fmt.Sprintf("%s:%s", path, dir))
    30  	defer os.Setenv("PATH", path)
    31  
    32  	helper := authFromHelper("testnomad")
    33  	creds, err := helper("registry.local:5000/repo/image")
    34  	require.NoError(t, err)
    35  	require.NotNil(t, creds)
    36  	require.Equal(t, "hashi", creds.Username)
    37  	require.Equal(t, "nomad", creds.Password)
    38  
    39  	if _, err := os.Stat(filepath.Join(dir, "helper-get.out")); os.IsNotExist(err) {
    40  		t.Fatalf("Expected helper-get.out to exist")
    41  	}
    42  	content, err := ioutil.ReadFile(filepath.Join(dir, "helper-get.out"))
    43  	require.NoError(t, err)
    44  	require.Equal(t, []byte("https://registry.local:5000"), content)
    45  }
    46  
    47  func TestDockerDriver_PidsLimit(t *testing.T) {
    48  	if !tu.IsTravis() {
    49  		t.Parallel()
    50  	}
    51  	if !testutil.DockerIsConnected(t) {
    52  		t.Skip("Docker not connected")
    53  	}
    54  
    55  	task, _, _ := dockerTask(t)
    56  	task.Config["pids_limit"] = "1"
    57  	task.Config["command"] = "/bin/sh"
    58  	task.Config["args"] = []string{"-c", "sleep 2 & sleep 2"}
    59  
    60  	ctx := testDockerDriverContexts(t, task)
    61  	defer ctx.AllocDir.Destroy()
    62  	d := NewDockerDriver(ctx.DriverCtx)
    63  
    64  	// Copy the image into the task's directory
    65  	copyImage(t, ctx.ExecCtx.TaskDir, "busybox.tar")
    66  
    67  	_, err := d.Prestart(ctx.ExecCtx, task)
    68  	if err != nil {
    69  		t.Fatalf("error in prestart: %v", err)
    70  	}
    71  	resp, err := d.Start(ctx.ExecCtx, task)
    72  	if err != nil {
    73  		t.Fatalf("err: %v", err)
    74  	}
    75  	defer resp.Handle.Kill()
    76  
    77  	select {
    78  	case res := <-resp.Handle.WaitCh():
    79  		if res.Successful() {
    80  			t.Fatalf("expected error, but container exited successful")
    81  		}
    82  	case <-time.After(time.Duration(tu.TestMultiplier()*5) * time.Second):
    83  		t.Fatalf("timeout")
    84  	}
    85  
    86  	// XXX Logging doesn't work on OSX so just test on Linux
    87  	// Check that data was written to the directory.
    88  	outputFile := filepath.Join(ctx.ExecCtx.TaskDir.LogDir, "redis-demo.stderr.0")
    89  	act, err := ioutil.ReadFile(outputFile)
    90  	if err != nil {
    91  		t.Fatalf("Couldn't read expected output: %v", err)
    92  	}
    93  
    94  	exp := "can't fork"
    95  	if !strings.Contains(string(act), exp) {
    96  		t.Fatalf("Expected failed fork: %q", act)
    97  	}
    98  
    99  }