github.com/xiaobinqt/libcompose@v1.1.0/docker/client/client_test.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	cliconfig "github.com/docker/cli/cli/config"
    10  	"github.com/docker/go-connections/tlsconfig"
    11  )
    12  
    13  // TestCreateWithEnv creates client(s) using environment variables, using an empty Options.
    14  func TestCreateWithEnv(t *testing.T) {
    15  	cases := []struct {
    16  		envs            map[string]string
    17  		expectedError   string
    18  		expectedVersion string
    19  	}{
    20  		{
    21  			envs:            map[string]string{},
    22  			expectedVersion: "v1.20",
    23  		},
    24  		{
    25  			envs: map[string]string{
    26  				"DOCKER_CERT_PATH": "invalid/path",
    27  			},
    28  			expectedError: "Could not load X509 key pair: open invalid/path/cert.pem: no such file or directory",
    29  		},
    30  		{
    31  			envs: map[string]string{
    32  				"DOCKER_HOST": "host",
    33  			},
    34  			expectedError: "unable to parse docker host",
    35  		},
    36  		{
    37  			envs: map[string]string{
    38  				"DOCKER_HOST": "invalid://url",
    39  			},
    40  			expectedVersion: "v1.20",
    41  		},
    42  		{
    43  			envs: map[string]string{
    44  				"DOCKER_API_VERSION": "anything",
    45  			},
    46  			expectedVersion: "anything",
    47  		},
    48  		{
    49  			envs: map[string]string{
    50  				"DOCKER_API_VERSION": "1.22",
    51  			},
    52  			expectedVersion: "1.22",
    53  		},
    54  	}
    55  	for _, c := range cases {
    56  		recoverEnvs := setupEnvs(t, c.envs)
    57  		apiclient, err := Create(Options{})
    58  		if c.expectedError != "" {
    59  			if err == nil || !strings.Contains(err.Error(), c.expectedError) {
    60  				t.Errorf("expected an error '%s', got '%s', for %v", c.expectedError, err.Error(), c)
    61  			}
    62  		} else {
    63  			if err != nil {
    64  				t.Error(err)
    65  			}
    66  			version := apiclient.ClientVersion()
    67  			if version != c.expectedVersion {
    68  				t.Errorf("expected %s, got %s, for %v", c.expectedVersion, version, c)
    69  			}
    70  		}
    71  		recoverEnvs(t)
    72  	}
    73  }
    74  
    75  func TestCreateWithOptions(t *testing.T) {
    76  	cases := []struct {
    77  		options         Options
    78  		expectedError   string
    79  		expectedVersion string
    80  	}{
    81  		{
    82  			options: Options{
    83  				Host: "host",
    84  			},
    85  			expectedError: "unable to parse docker host",
    86  		},
    87  		{
    88  			options: Options{
    89  				Host: "invalid://host",
    90  			},
    91  			expectedVersion: "v1.20",
    92  		},
    93  		{
    94  			options: Options{
    95  				Host:       "tcp://host",
    96  				APIVersion: "anything",
    97  			},
    98  			expectedVersion: "anything",
    99  		},
   100  		{
   101  			options: Options{
   102  				Host:       "tcp://host",
   103  				APIVersion: "v1.22",
   104  			},
   105  			expectedVersion: "v1.22",
   106  		},
   107  		{
   108  			options: Options{
   109  				Host:       "tcp://host",
   110  				TLS:        true,
   111  				APIVersion: "v1.22",
   112  			},
   113  			expectedError: fmt.Sprintf("Could not load X509 key pair: open %s/cert.pem: no such file or directory", cliconfig.Dir()),
   114  		},
   115  		{
   116  			options: Options{
   117  				Host: "tcp://host",
   118  				TLS:  true,
   119  				TLSOptions: tlsconfig.Options{
   120  					CertFile: "invalid/cert/file",
   121  					CAFile:   "invalid/ca/file",
   122  					KeyFile:  "invalid/key/file",
   123  				},
   124  				TrustKey:   "invalid/trust/key",
   125  				APIVersion: "v1.22",
   126  			},
   127  			expectedError: "Could not load X509 key pair: open invalid/cert/file: no such file or directory",
   128  		},
   129  		{
   130  			options: Options{
   131  				Host:      "host",
   132  				TLSVerify: true,
   133  				TLSOptions: tlsconfig.Options{
   134  					CertFile: "fixtures/cert.pem",
   135  					CAFile:   "fixtures/ca.pem",
   136  					KeyFile:  "fixtures/key.pem",
   137  				},
   138  				APIVersion: "v1.22",
   139  			},
   140  			expectedError: "unable to parse docker host",
   141  		},
   142  		{
   143  			options: Options{
   144  				Host:      "tcp://host",
   145  				TLSVerify: true,
   146  				TLSOptions: tlsconfig.Options{
   147  					CertFile: "fixtures/cert.pem",
   148  					CAFile:   "fixtures/ca.pem",
   149  					KeyFile:  "fixtures/key.pem",
   150  				},
   151  				APIVersion: "v1.22",
   152  			},
   153  			expectedVersion: "v1.22",
   154  		},
   155  	}
   156  	for _, c := range cases {
   157  		apiclient, err := Create(c.options)
   158  		if c.expectedError != "" {
   159  			if err == nil || !strings.Contains(err.Error(), c.expectedError) {
   160  				t.Errorf("expected an error '%s', got '%s', for %v", c.expectedError, err.Error(), c)
   161  			}
   162  		} else {
   163  			if err != nil {
   164  				t.Error(err)
   165  			}
   166  			version := apiclient.ClientVersion()
   167  			if version != c.expectedVersion {
   168  				t.Errorf("expected %s, got %s, for %v", c.expectedVersion, version, c)
   169  			}
   170  		}
   171  	}
   172  }
   173  
   174  func setupEnvs(t *testing.T, envs map[string]string) func(*testing.T) {
   175  	oldEnvs := map[string]string{}
   176  	for key, value := range envs {
   177  		oldEnv := os.Getenv(key)
   178  		oldEnvs[key] = oldEnv
   179  		err := os.Setenv(key, value)
   180  		if err != nil {
   181  			t.Error(err)
   182  		}
   183  	}
   184  	return func(t *testing.T) {
   185  		for key, value := range oldEnvs {
   186  			err := os.Setenv(key, value)
   187  			if err != nil {
   188  				t.Error(err)
   189  			}
   190  		}
   191  	}
   192  }