github.com/bigcommerce/nomad@v0.9.3-bc/drivers/docker/driver_linux_test.go (about)

     1  package docker
     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.IsCI() {
    49  		t.Parallel()
    50  	}
    51  	testutil.DockerCompatible(t)
    52  	require := require.New(t)
    53  
    54  	task, cfg, _ := dockerTask(t)
    55  	cfg.PidsLimit = 1
    56  	cfg.Command = "/bin/sh"
    57  	cfg.Args = []string{"-c", "sleep 2 & sleep 2"}
    58  	require.NoError(task.EncodeConcreteDriverConfig(cfg))
    59  
    60  	_, driver, _, cleanup := dockerSetup(t, task)
    61  	defer cleanup()
    62  
    63  	driver.WaitUntilStarted(task.ID, time.Duration(tu.TestMultiplier()*5)*time.Second)
    64  
    65  	// XXX Logging doesn't work on OSX so just test on Linux
    66  	// Check that data was written to the directory.
    67  	outputFile := filepath.Join(task.TaskDir().LogDir, "redis-demo.stderr.0")
    68  	exp := "can't fork"
    69  	tu.WaitForResult(func() (bool, error) {
    70  		act, err := ioutil.ReadFile(outputFile)
    71  		if err != nil {
    72  			return false, err
    73  		}
    74  		if !strings.Contains(string(act), exp) {
    75  			return false, fmt.Errorf("Expected %q in output %q", exp, string(act))
    76  		}
    77  		return true, nil
    78  	}, func(err error) {
    79  		require.NoError(err)
    80  	})
    81  }