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

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