github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/integration/daemon/daemon_test.go (about)

     1  package daemon // import "github.com/docker/docker/integration/daemon"
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"runtime"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/testutil/daemon"
    12  	"gotest.tools/v3/assert"
    13  	"gotest.tools/v3/skip"
    14  
    15  	is "gotest.tools/v3/assert/cmp"
    16  )
    17  
    18  func TestConfigDaemonLibtrustID(t *testing.T) {
    19  	skip.If(t, runtime.GOOS != "linux")
    20  
    21  	d := daemon.New(t)
    22  	defer d.Stop(t)
    23  
    24  	trustKey := filepath.Join(d.RootDir(), "key.json")
    25  	err := ioutil.WriteFile(trustKey, []byte(`{"crv":"P-256","d":"dm28PH4Z4EbyUN8L0bPonAciAQa1QJmmyYd876mnypY","kid":"WTJ3:YSIP:CE2E:G6KJ:PSBD:YX2Y:WEYD:M64G:NU2V:XPZV:H2CR:VLUB","kty":"EC","x":"Mh5-JINSjaa_EZdXDttri255Z5fbCEOTQIZjAcScFTk","y":"eUyuAjfxevb07hCCpvi4Zi334Dy4GDWQvEToGEX4exQ"}`), 0644)
    26  	assert.NilError(t, err)
    27  
    28  	config := filepath.Join(d.RootDir(), "daemon.json")
    29  	err = ioutil.WriteFile(config, []byte(`{"deprecated-key-path": "`+trustKey+`"}`), 0644)
    30  	assert.NilError(t, err)
    31  
    32  	d.Start(t, "--config-file", config)
    33  	info := d.Info(t)
    34  	assert.Equal(t, info.ID, "WTJ3:YSIP:CE2E:G6KJ:PSBD:YX2Y:WEYD:M64G:NU2V:XPZV:H2CR:VLUB")
    35  }
    36  
    37  func TestDaemonConfigValidation(t *testing.T) {
    38  	skip.If(t, runtime.GOOS != "linux")
    39  
    40  	d := daemon.New(t)
    41  	dockerBinary, err := d.BinaryPath()
    42  	assert.NilError(t, err)
    43  	params := []string{"--validate", "--config-file"}
    44  
    45  	dest := os.Getenv("DOCKER_INTEGRATION_DAEMON_DEST")
    46  	if dest == "" {
    47  		dest = os.Getenv("DEST")
    48  	}
    49  	testdata := filepath.Join(dest, "..", "..", "integration", "daemon", "testdata")
    50  
    51  	const (
    52  		validOut  = "configuration OK"
    53  		failedOut = "unable to configure the Docker daemon with file"
    54  	)
    55  
    56  	tests := []struct {
    57  		name        string
    58  		args        []string
    59  		expectedOut string
    60  	}{
    61  		{
    62  			name:        "config with no content",
    63  			args:        append(params, filepath.Join(testdata, "empty-config-1.json")),
    64  			expectedOut: validOut,
    65  		},
    66  		{
    67  			name:        "config with {}",
    68  			args:        append(params, filepath.Join(testdata, "empty-config-2.json")),
    69  			expectedOut: validOut,
    70  		},
    71  		{
    72  			name:        "invalid config",
    73  			args:        append(params, filepath.Join(testdata, "invalid-config-1.json")),
    74  			expectedOut: failedOut,
    75  		},
    76  		{
    77  			name:        "malformed config",
    78  			args:        append(params, filepath.Join(testdata, "malformed-config.json")),
    79  			expectedOut: failedOut,
    80  		},
    81  		{
    82  			name:        "valid config",
    83  			args:        append(params, filepath.Join(testdata, "valid-config-1.json")),
    84  			expectedOut: validOut,
    85  		},
    86  	}
    87  	for _, tc := range tests {
    88  		tc := tc
    89  		t.Run(tc.name, func(t *testing.T) {
    90  			t.Parallel()
    91  			cmd := exec.Command(dockerBinary, tc.args...)
    92  			out, err := cmd.CombinedOutput()
    93  			assert.Check(t, is.Contains(string(out), tc.expectedOut))
    94  			if tc.expectedOut == failedOut {
    95  				assert.ErrorContains(t, err, "", "expected an error, but got none")
    96  			} else {
    97  				assert.NilError(t, err)
    98  			}
    99  		})
   100  	}
   101  }