github.com/skippbox/kompose-origin@v0.0.0-20160524133224-16a9dca7bac2/docker/client/client_test.go (about)

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