github.1git.de/docker/cli@v26.1.3+incompatible/cli-plugins/manager/hooks_test.go (about)

     1  package manager
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  )
     8  
     9  func TestGetNaiveFlags(t *testing.T) {
    10  	testCases := []struct {
    11  		args          []string
    12  		expectedFlags map[string]string
    13  	}{
    14  		{
    15  			args:          []string{"docker"},
    16  			expectedFlags: map[string]string{},
    17  		},
    18  		{
    19  			args: []string{"docker", "build", "-q", "--file", "test.Dockerfile", "."},
    20  			expectedFlags: map[string]string{
    21  				"q":    "",
    22  				"file": "",
    23  			},
    24  		},
    25  		{
    26  			args: []string{"docker", "--context", "a-context", "pull", "-q", "--progress", "auto", "alpine"},
    27  			expectedFlags: map[string]string{
    28  				"context":  "",
    29  				"q":        "",
    30  				"progress": "",
    31  			},
    32  		},
    33  	}
    34  
    35  	for _, tc := range testCases {
    36  		assert.DeepEqual(t, getNaiveFlags(tc.args), tc.expectedFlags)
    37  	}
    38  }
    39  
    40  func TestPluginMatch(t *testing.T) {
    41  	testCases := []struct {
    42  		commandString string
    43  		pluginConfig  map[string]string
    44  		expectedMatch string
    45  		expectedOk    bool
    46  	}{
    47  		{
    48  			commandString: "image ls",
    49  			pluginConfig: map[string]string{
    50  				"hooks": "image",
    51  			},
    52  			expectedMatch: "image",
    53  			expectedOk:    true,
    54  		},
    55  		{
    56  			commandString: "context ls",
    57  			pluginConfig: map[string]string{
    58  				"hooks": "build",
    59  			},
    60  			expectedMatch: "",
    61  			expectedOk:    false,
    62  		},
    63  		{
    64  			commandString: "context ls",
    65  			pluginConfig: map[string]string{
    66  				"hooks": "context ls",
    67  			},
    68  			expectedMatch: "context ls",
    69  			expectedOk:    true,
    70  		},
    71  		{
    72  			commandString: "image ls",
    73  			pluginConfig: map[string]string{
    74  				"hooks": "image ls,image",
    75  			},
    76  			expectedMatch: "image ls",
    77  			expectedOk:    true,
    78  		},
    79  		{
    80  			commandString: "image ls",
    81  			pluginConfig: map[string]string{
    82  				"hooks": "",
    83  			},
    84  			expectedMatch: "",
    85  			expectedOk:    false,
    86  		},
    87  		{
    88  			commandString: "image inspect",
    89  			pluginConfig: map[string]string{
    90  				"hooks": "image i",
    91  			},
    92  			expectedMatch: "",
    93  			expectedOk:    false,
    94  		},
    95  		{
    96  			commandString: "image inspect",
    97  			pluginConfig: map[string]string{
    98  				"hooks": "image",
    99  			},
   100  			expectedMatch: "image",
   101  			expectedOk:    true,
   102  		},
   103  	}
   104  
   105  	for _, tc := range testCases {
   106  		match, ok := pluginMatch(tc.pluginConfig, tc.commandString)
   107  		assert.Equal(t, ok, tc.expectedOk)
   108  		assert.Equal(t, match, tc.expectedMatch)
   109  	}
   110  }