github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/container/run_test.go (about)

     1  package container
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/docker/cli/internal/test"
     9  	"github.com/docker/cli/internal/test/notary"
    10  	"github.com/docker/docker/api/types/container"
    11  	"github.com/docker/docker/api/types/network"
    12  	specs "github.com/opencontainers/image-spec/specs-go/v1"
    13  	"gotest.tools/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  )
    16  
    17  func TestRunLabel(t *testing.T) {
    18  	cli := test.NewFakeCli(&fakeClient{
    19  		createContainerFunc: func(_ *container.Config, _ *container.HostConfig, _ *network.NetworkingConfig, _ *specs.Platform, _ string) (container.ContainerCreateCreatedBody, error) {
    20  			return container.ContainerCreateCreatedBody{
    21  				ID: "id",
    22  			}, nil
    23  		},
    24  		Version: "1.36",
    25  	})
    26  	cmd := NewRunCommand(cli)
    27  	cmd.SetArgs([]string{"--detach=true", "--label", "foo", "busybox"})
    28  	assert.NilError(t, cmd.Execute())
    29  }
    30  
    31  func TestRunCommandWithContentTrustErrors(t *testing.T) {
    32  	testCases := []struct {
    33  		name          string
    34  		args          []string
    35  		expectedError string
    36  		notaryFunc    test.NotaryClientFuncType
    37  	}{
    38  		{
    39  			name:          "offline-notary-server",
    40  			notaryFunc:    notary.GetOfflineNotaryRepository,
    41  			expectedError: "client is offline",
    42  			args:          []string{"image:tag"},
    43  		},
    44  		{
    45  			name:          "uninitialized-notary-server",
    46  			notaryFunc:    notary.GetUninitializedNotaryRepository,
    47  			expectedError: "remote trust data does not exist",
    48  			args:          []string{"image:tag"},
    49  		},
    50  		{
    51  			name:          "empty-notary-server",
    52  			notaryFunc:    notary.GetEmptyTargetsNotaryRepository,
    53  			expectedError: "No valid trust data for tag",
    54  			args:          []string{"image:tag"},
    55  		},
    56  	}
    57  	for _, tc := range testCases {
    58  		cli := test.NewFakeCli(&fakeClient{
    59  			createContainerFunc: func(config *container.Config,
    60  				hostConfig *container.HostConfig,
    61  				networkingConfig *network.NetworkingConfig,
    62  				platform *specs.Platform,
    63  				containerName string,
    64  			) (container.ContainerCreateCreatedBody, error) {
    65  				return container.ContainerCreateCreatedBody{}, fmt.Errorf("shouldn't try to pull image")
    66  			},
    67  		}, test.EnableContentTrust)
    68  		cli.SetNotaryClient(tc.notaryFunc)
    69  		cmd := NewRunCommand(cli)
    70  		cmd.SetArgs(tc.args)
    71  		cmd.SetOut(ioutil.Discard)
    72  		err := cmd.Execute()
    73  		assert.Assert(t, err != nil)
    74  		assert.Assert(t, is.Contains(cli.ErrBuffer().String(), tc.expectedError))
    75  	}
    76  }