github.com/itscaro/cli@v0.0.0-20190705081621-c9db0fe93829/cli/command/plugin/install_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/docker/cli/internal/test"
    11  	"github.com/docker/cli/internal/test/notary"
    12  	"github.com/docker/docker/api/types"
    13  
    14  	"gotest.tools/assert"
    15  )
    16  
    17  func TestInstallErrors(t *testing.T) {
    18  	testCases := []struct {
    19  		description   string
    20  		args          []string
    21  		expectedError string
    22  		installFunc   func(name string, options types.PluginInstallOptions) (io.ReadCloser, error)
    23  	}{
    24  		{
    25  			description:   "insufficient number of arguments",
    26  			args:          []string{},
    27  			expectedError: "requires at least 1 argument",
    28  		},
    29  		{
    30  			description:   "invalid alias",
    31  			args:          []string{"foo", "--alias", "UPPERCASE_ALIAS"},
    32  			expectedError: "invalid",
    33  		},
    34  		{
    35  			description:   "invalid plugin name",
    36  			args:          []string{"UPPERCASE_REPONAME"},
    37  			expectedError: "invalid",
    38  		},
    39  		{
    40  			description:   "installation error",
    41  			args:          []string{"foo"},
    42  			expectedError: "Error installing plugin",
    43  			installFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) {
    44  				return nil, fmt.Errorf("Error installing plugin")
    45  			},
    46  		},
    47  		{
    48  			description:   "installation error due to missing image",
    49  			args:          []string{"foo"},
    50  			expectedError: "docker image pull",
    51  			installFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) {
    52  				return nil, fmt.Errorf("(image) when fetching")
    53  			},
    54  		},
    55  	}
    56  
    57  	for _, tc := range testCases {
    58  		cli := test.NewFakeCli(&fakeClient{pluginInstallFunc: tc.installFunc})
    59  		cmd := newInstallCommand(cli)
    60  		cmd.SetArgs(tc.args)
    61  		cmd.SetOutput(ioutil.Discard)
    62  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
    63  	}
    64  }
    65  
    66  func TestInstallContentTrustErrors(t *testing.T) {
    67  	testCases := []struct {
    68  		description   string
    69  		args          []string
    70  		expectedError string
    71  		notaryFunc    test.NotaryClientFuncType
    72  	}{
    73  		{
    74  			description:   "install plugin, offline notary server",
    75  			args:          []string{"plugin:tag"},
    76  			expectedError: "client is offline",
    77  			notaryFunc:    notary.GetOfflineNotaryRepository,
    78  		},
    79  		{
    80  			description:   "install plugin, uninitialized notary server",
    81  			args:          []string{"plugin:tag"},
    82  			expectedError: "remote trust data does not exist",
    83  			notaryFunc:    notary.GetUninitializedNotaryRepository,
    84  		},
    85  		{
    86  			description:   "install plugin, empty notary server",
    87  			args:          []string{"plugin:tag"},
    88  			expectedError: "No valid trust data for tag",
    89  			notaryFunc:    notary.GetEmptyTargetsNotaryRepository,
    90  		},
    91  	}
    92  
    93  	for _, tc := range testCases {
    94  		cli := test.NewFakeCli(&fakeClient{
    95  			pluginInstallFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) {
    96  				return nil, fmt.Errorf("should not try to install plugin")
    97  
    98  			},
    99  		}, test.EnableContentTrust)
   100  		cli.SetNotaryClient(tc.notaryFunc)
   101  		cmd := newInstallCommand(cli)
   102  		cmd.SetArgs(tc.args)
   103  		cmd.SetOutput(ioutil.Discard)
   104  		assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
   105  	}
   106  }
   107  
   108  func TestInstall(t *testing.T) {
   109  	testCases := []struct {
   110  		description    string
   111  		args           []string
   112  		expectedOutput string
   113  		installFunc    func(name string, options types.PluginInstallOptions) (io.ReadCloser, error)
   114  	}{
   115  		{
   116  			description:    "install with no additional flags",
   117  			args:           []string{"foo"},
   118  			expectedOutput: "Installed plugin foo\n",
   119  			installFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) {
   120  				return ioutil.NopCloser(strings.NewReader("")), nil
   121  			},
   122  		},
   123  		{
   124  			description:    "install with disable flag",
   125  			args:           []string{"--disable", "foo"},
   126  			expectedOutput: "Installed plugin foo\n",
   127  			installFunc: func(name string, options types.PluginInstallOptions) (io.ReadCloser, error) {
   128  				assert.Check(t, options.Disabled)
   129  				return ioutil.NopCloser(strings.NewReader("")), nil
   130  			},
   131  		},
   132  	}
   133  
   134  	for _, tc := range testCases {
   135  		cli := test.NewFakeCli(&fakeClient{pluginInstallFunc: tc.installFunc})
   136  		cmd := newInstallCommand(cli)
   137  		cmd.SetArgs(tc.args)
   138  		assert.NilError(t, cmd.Execute())
   139  		assert.Check(t, strings.Contains(cli.OutBuffer().String(), tc.expectedOutput))
   140  	}
   141  }