github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/cnab/driver_test.go (about)

     1  package cnab
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/docker/cli/cli/command"
     7  	cliflags "github.com/docker/cli/cli/flags"
     8  	"gotest.tools/assert"
     9  )
    10  
    11  func TestRequiresBindMount(t *testing.T) {
    12  	dockerCli, err := command.NewDockerCli()
    13  	assert.NilError(t, err)
    14  	err = dockerCli.Initialize(cliflags.NewClientOptions())
    15  	assert.NilError(t, err)
    16  
    17  	testCases := []struct {
    18  		name               string
    19  		targetContextName  string
    20  		targetOrchestrator string
    21  		expectedRequired   bool
    22  		expectedEndpoint   string
    23  		expectedError      string
    24  	}{
    25  		{
    26  			name:               "kubernetes-orchestrator",
    27  			targetContextName:  "target-context",
    28  			targetOrchestrator: "kubernetes",
    29  			expectedRequired:   false,
    30  			expectedEndpoint:   "",
    31  			expectedError:      "",
    32  		},
    33  		{
    34  			name:               "no-context",
    35  			targetContextName:  "",
    36  			targetOrchestrator: "swarm",
    37  			expectedRequired:   true,
    38  			expectedEndpoint:   "/var/run/docker.sock",
    39  			expectedError:      "",
    40  		},
    41  	}
    42  
    43  	for _, testCase := range testCases {
    44  		t.Run(testCase.name, func(t *testing.T) {
    45  			result, err := RequiredBindMount(testCase.targetContextName, testCase.targetOrchestrator, dockerCli.ContextStore())
    46  			if testCase.expectedError == "" {
    47  				assert.NilError(t, err)
    48  			} else {
    49  				assert.Error(t, err, testCase.expectedError)
    50  			}
    51  			assert.Equal(t, testCase.expectedRequired, result.required)
    52  			assert.Equal(t, testCase.expectedEndpoint, result.endpoint)
    53  		})
    54  	}
    55  }
    56  
    57  func TestIsDockerHostLocal(t *testing.T) {
    58  	testCases := []struct {
    59  		name     string
    60  		host     string
    61  		expected bool
    62  	}{
    63  		{
    64  			name:     "not-local",
    65  			host:     "tcp://not.local.host",
    66  			expected: false,
    67  		},
    68  		{
    69  			name:     "no-endpoint",
    70  			host:     "",
    71  			expected: true,
    72  		},
    73  		{
    74  			name:     "docker-sock",
    75  			host:     "unix:///var/run/docker.sock",
    76  			expected: true,
    77  		},
    78  		{
    79  			name:     "named-pipe",
    80  			host:     "npipe:////./pipe/docker_engine",
    81  			expected: true,
    82  		},
    83  	}
    84  
    85  	for _, testCase := range testCases {
    86  		t.Run(testCase.name, func(t *testing.T) {
    87  			assert.Equal(t, testCase.expected, isDockerHostLocal(testCase.host))
    88  		})
    89  	}
    90  }
    91  
    92  func TestSocketPath(t *testing.T) {
    93  	testCases := []struct {
    94  		name     string
    95  		host     string
    96  		expected string
    97  	}{
    98  		{
    99  			name:     "unixSocket",
   100  			host:     "unix:///my/socket.sock",
   101  			expected: "/my/socket.sock",
   102  		},
   103  		{
   104  			name:     "namedPipe",
   105  			host:     "npipe:////./docker",
   106  			expected: "/var/run/docker.sock",
   107  		},
   108  		{
   109  			name:     "emptyHost",
   110  			host:     "",
   111  			expected: "/var/run/docker.sock",
   112  		},
   113  		{
   114  			name:     "tcpHost",
   115  			host:     "tcp://my/tcp/endpoint",
   116  			expected: "/var/run/docker.sock",
   117  		},
   118  	}
   119  
   120  	for _, testCase := range testCases {
   121  		t.Run(testCase.name, func(t *testing.T) {
   122  			assert.Equal(t, testCase.expected, socketPath(testCase.host))
   123  		})
   124  	}
   125  }