github.com/click2cloud/libcompose@v0.4.1-0.20170816121048-7c20f79ac6b9/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  	
    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   				TLSCAFile:   "invalid/ca/file",
   120   				TLSCertFile: "invalid/cert/file",
   121  				TLSKeyFile:  "invalid/key/file",
   122   				TrustKey:    "invalid/trust/key",
   123   				APIVersion:  "v1.22",
   124  			},
   125  			expectedError: "Could not load X509 key pair: open invalid/cert/file: no such file or directory",
   126  		},
   127  		{
   128  			options: Options{
   129  				Host:        "host",
   130   +				TLSVerify:   true,
   131   +				TLSCAFile:   "fixtures/ca.pem",
   132   +				TLSCertFile: "fixtures/cert.pem",
   133   +				TLSKeyFile:  "fixtures/key.pem",
   134   +				APIVersion:  "v1.22",
   135  			},
   136  			expectedError: "unable to parse docker host",
   137  		},
   138  		{
   139  			options: Options{
   140  				Host:        "tcp://host",
   141   +				TLSVerify:   true,
   142   +				TLSCAFile:   "fixtures/ca.pem",
   143   +				TLSCertFile: "fixtures/cert.pem",
   144   +				TLSKeyFile:  "fixtures/key.pem",
   145   +				APIVersion:  "v1.22",
   146  			},
   147  			expectedVersion: "v1.22",
   148  		},
   149  	}
   150  	for _, c := range cases {
   151  		apiclient, err := Create(c.options)
   152  		if c.expectedError != "" {
   153  			if err == nil || !strings.Contains(err.Error(), c.expectedError) {
   154  				t.Errorf("expected an error '%s', got '%s', for %v", c.expectedError, err.Error(), c)
   155  			}
   156  		} else {
   157  			if err != nil {
   158  				t.Error(err)
   159  			}
   160  			version := apiclient.ClientVersion()
   161  			if version != c.expectedVersion {
   162  				t.Errorf("expected %s, got %s, for %v", c.expectedVersion, version, c)
   163  			}
   164  		}
   165  	}
   166  }
   167  
   168  func setupEnvs(t *testing.T, envs map[string]string) func(*testing.T) {
   169  	oldEnvs := map[string]string{}
   170  	for key, value := range envs {
   171  		oldEnv := os.Getenv(key)
   172  		oldEnvs[key] = oldEnv
   173  		err := os.Setenv(key, value)
   174  		if err != nil {
   175  			t.Error(err)
   176  		}
   177  	}
   178  	return func(t *testing.T) {
   179  		for key, value := range oldEnvs {
   180  			err := os.Setenv(key, value)
   181  			if err != nil {
   182  				t.Error(err)
   183  			}
   184  		}
   185  	}
   186  }