github.com/bdwilliams/libcompose@v0.3.1-0.20160826154243-d81a9bdacff0/docker/client/client_factory_test.go (about)

     1  package client
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestFactoryWithEnv(t *testing.T) {
     8  	cases := []struct {
     9  		envs            map[string]string
    10  		expectedError   string
    11  		expectedVersion string
    12  	}{
    13  		{
    14  			envs:            map[string]string{},
    15  			expectedVersion: "v1.20",
    16  		},
    17  		{
    18  			envs: map[string]string{
    19  				"DOCKER_CERT_PATH": "invalid/path",
    20  			},
    21  			expectedError:   "Could not load X509 key pair: open invalid/path/cert.pem: no such file or directory. Make sure the key is not encrypted",
    22  			expectedVersion: "v1.20",
    23  		},
    24  		{
    25  			envs: map[string]string{
    26  				"DOCKER_API_VERSION": "1.22",
    27  			},
    28  			expectedVersion: "1.22",
    29  		},
    30  	}
    31  	for _, c := range cases {
    32  		recoverEnvs := setupEnvs(t, c.envs)
    33  		factory, err := NewDefaultFactory(Options{})
    34  		if c.expectedError != "" {
    35  			if err == nil || err.Error() != c.expectedError {
    36  				t.Errorf("expected an error %s, got %s, for %v", c.expectedError, err.Error(), c)
    37  			}
    38  		} else {
    39  			if err != nil {
    40  				t.Error(err)
    41  			}
    42  			apiclient := factory.Create(nil)
    43  			version := apiclient.ClientVersion()
    44  			if version != c.expectedVersion {
    45  				t.Errorf("expected %s, got %s, for %v", c.expectedVersion, version, c)
    46  			}
    47  		}
    48  		recoverEnvs(t)
    49  	}
    50  }
    51  
    52  func TestFactoryWithOptions(t *testing.T) {
    53  	cases := []struct {
    54  		options         Options
    55  		expectedError   string
    56  		expectedVersion string
    57  	}{
    58  		{
    59  			options: Options{
    60  				Host: "host",
    61  			},
    62  			expectedError: "unable to parse docker host `host`",
    63  		},
    64  		{
    65  			options: Options{
    66  				Host: "invalid://host",
    67  			},
    68  			expectedVersion: "v1.20",
    69  		},
    70  		{
    71  			options: Options{
    72  				Host:       "tcp://host",
    73  				APIVersion: "v1.22",
    74  			},
    75  			expectedVersion: "v1.22",
    76  		},
    77  	}
    78  	for _, c := range cases {
    79  		factory, err := NewDefaultFactory(c.options)
    80  		if c.expectedError != "" {
    81  			if err == nil || err.Error() != c.expectedError {
    82  				t.Errorf("expected an error %s, got %s, for %v", c.expectedError, err.Error(), c)
    83  			}
    84  		} else {
    85  			if err != nil {
    86  				t.Error(err)
    87  			}
    88  			apiclient := factory.Create(nil)
    89  			version := apiclient.ClientVersion()
    90  			if version != c.expectedVersion {
    91  				t.Errorf("expected %s, got %s, for %v", c.expectedVersion, version, c)
    92  			}
    93  		}
    94  	}
    95  }