github.com/ilhicas/nomad@v0.11.8/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  	"github.com/hashicorp/nomad/helper/freeport"
    14  	tu "github.com/hashicorp/nomad/testutil"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestDockerDriver_authFromHelper(t *testing.T) {
    19  	dir, err := ioutil.TempDir("", "test-docker-driver_authfromhelper")
    20  	require.NoError(t, err)
    21  	defer os.RemoveAll(dir)
    22  	helperPayload := "{\"Username\":\"hashi\",\"Secret\":\"nomad\"}"
    23  	helperContent := []byte(fmt.Sprintf("#!/bin/sh\ncat > %s/helper-$1.out;echo '%s'", dir, helperPayload))
    24  
    25  	helperFile := filepath.Join(dir, "docker-credential-testnomad")
    26  	err = ioutil.WriteFile(helperFile, helperContent, 0777)
    27  	require.NoError(t, err)
    28  
    29  	path := os.Getenv("PATH")
    30  	os.Setenv("PATH", fmt.Sprintf("%s:%s", path, dir))
    31  	defer os.Setenv("PATH", path)
    32  
    33  	helper := authFromHelper("testnomad")
    34  	creds, err := helper("registry.local:5000/repo/image")
    35  	require.NoError(t, err)
    36  	require.NotNil(t, creds)
    37  	require.Equal(t, "hashi", creds.Username)
    38  	require.Equal(t, "nomad", creds.Password)
    39  
    40  	if _, err := os.Stat(filepath.Join(dir, "helper-get.out")); os.IsNotExist(err) {
    41  		t.Fatalf("Expected helper-get.out to exist")
    42  	}
    43  	content, err := ioutil.ReadFile(filepath.Join(dir, "helper-get.out"))
    44  	require.NoError(t, err)
    45  	require.Equal(t, []byte("https://registry.local:5000"), content)
    46  }
    47  
    48  func TestDockerDriver_PidsLimit(t *testing.T) {
    49  	if !tu.IsCI() {
    50  		t.Parallel()
    51  	}
    52  	testutil.DockerCompatible(t)
    53  	require := require.New(t)
    54  
    55  	task, cfg, ports := dockerTask(t)
    56  	defer freeport.Return(ports)
    57  	cfg.PidsLimit = 1
    58  	cfg.Command = "/bin/sh"
    59  	cfg.Args = []string{"-c", "sleep 5 & sleep 5 & sleep 5"}
    60  	require.NoError(task.EncodeConcreteDriverConfig(cfg))
    61  
    62  	_, driver, _, cleanup := dockerSetup(t, task, nil)
    63  	defer cleanup()
    64  
    65  	driver.WaitUntilStarted(task.ID, time.Duration(tu.TestMultiplier()*5)*time.Second)
    66  
    67  	// XXX Logging doesn't work on OSX so just test on Linux
    68  	// Check that data was written to the directory.
    69  	outputFile := filepath.Join(task.TaskDir().LogDir, "redis-demo.stderr.0")
    70  	exp := "can't fork"
    71  	tu.WaitForResult(func() (bool, error) {
    72  		act, err := ioutil.ReadFile(outputFile)
    73  		if err != nil {
    74  			return false, err
    75  		}
    76  		if !strings.Contains(string(act), exp) {
    77  			return false, fmt.Errorf("Expected %q in output %q", exp, string(act))
    78  		}
    79  		return true, nil
    80  	}, func(err error) {
    81  		require.NoError(err)
    82  	})
    83  }