github.com/grahambrereton-form3/tilt@v0.10.18/internal/docker/client_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/windmilleng/tilt/internal/container"
    13  	"github.com/windmilleng/tilt/internal/k8s"
    14  	"github.com/windmilleng/tilt/internal/minikube"
    15  )
    16  
    17  type buildkitTestCase struct {
    18  	v        types.Version
    19  	env      Env
    20  	expected bool
    21  }
    22  
    23  func TestSupportsBuildkit(t *testing.T) {
    24  	cases := []buildkitTestCase{
    25  		{types.Version{APIVersion: "1.37", Experimental: true}, Env{}, false},
    26  		{types.Version{APIVersion: "1.37", Experimental: false}, Env{}, false},
    27  		{types.Version{APIVersion: "1.38", Experimental: true}, Env{}, true},
    28  		{types.Version{APIVersion: "1.38", Experimental: false}, Env{}, false},
    29  		{types.Version{APIVersion: "1.39", Experimental: true}, Env{}, true},
    30  		{types.Version{APIVersion: "1.39", Experimental: false}, Env{}, true},
    31  		{types.Version{APIVersion: "1.40", Experimental: true}, Env{}, true},
    32  		{types.Version{APIVersion: "1.40", Experimental: false}, Env{}, true},
    33  		{types.Version{APIVersion: "garbage", Experimental: false}, Env{}, false},
    34  		{types.Version{APIVersion: "1.39", Experimental: true}, Env{IsMinikube: true}, false},
    35  	}
    36  
    37  	for i, c := range cases {
    38  		t.Run(fmt.Sprintf("Case%d", i), func(t *testing.T) {
    39  			assert.Equal(t, c.expected, SupportsBuildkit(c.v, c.env))
    40  		})
    41  	}
    42  }
    43  
    44  type builderVersionTestCase struct {
    45  	v        string
    46  	bkEnv    string
    47  	expected types.BuilderVersion
    48  }
    49  
    50  func TestProvideBuilderVersion(t *testing.T) {
    51  	cases := []builderVersionTestCase{
    52  		{"1.37", "", types.BuilderV1},
    53  		{"1.37", "0", types.BuilderV1},
    54  		{"1.37", "1", types.BuilderV1},
    55  		{"1.40", "", types.BuilderBuildKit},
    56  		{"1.40", "0", types.BuilderV1},
    57  		{"1.40", "1", types.BuilderBuildKit},
    58  	}
    59  
    60  	for i, c := range cases {
    61  		t.Run(fmt.Sprintf("Case%d", i), func(t *testing.T) {
    62  			os.Setenv("DOCKER_BUILDKIT", c.bkEnv)
    63  			defer os.Setenv("DOCKER_BUILDKIT", "")
    64  
    65  			v, err := getDockerBuilderVersion(
    66  				types.Version{APIVersion: c.v}, Env{})
    67  			assert.NoError(t, err)
    68  			assert.Equal(t, c.expected, v)
    69  		})
    70  	}
    71  }
    72  
    73  type versionTestCase struct {
    74  	v        types.Version
    75  	expected bool
    76  }
    77  
    78  func TestSupported(t *testing.T) {
    79  	cases := []versionTestCase{
    80  		{types.Version{APIVersion: "1.22"}, false},
    81  		{types.Version{APIVersion: "1.23"}, true},
    82  		{types.Version{APIVersion: "1.39"}, true},
    83  		{types.Version{APIVersion: "1.40"}, true},
    84  		{types.Version{APIVersion: "garbage"}, false},
    85  	}
    86  
    87  	for i, c := range cases {
    88  		t.Run(fmt.Sprintf("Case%d", i), func(t *testing.T) {
    89  			assert.Equal(t, c.expected, SupportedVersion(c.v))
    90  		})
    91  	}
    92  }
    93  
    94  type provideEnvTestCase struct {
    95  	env             k8s.Env
    96  	runtime         container.Runtime
    97  	osEnv           map[string]string
    98  	mkEnv           map[string]string
    99  	expectedCluster Env
   100  	expectedLocal   Env
   101  }
   102  
   103  func TestProvideEnv(t *testing.T) {
   104  	envVars := []string{
   105  		"DOCKER_TLS_VERIFY",
   106  		"DOCKER_HOST",
   107  		"DOCKER_CERT_PATH",
   108  		"DOCKER_API_VERSION",
   109  	}
   110  
   111  	cases := []provideEnvTestCase{
   112  		{},
   113  		{
   114  			env: k8s.EnvUnknown,
   115  			osEnv: map[string]string{
   116  				"DOCKER_TLS_VERIFY":  "1",
   117  				"DOCKER_HOST":        "tcp://192.168.99.100:2376",
   118  				"DOCKER_CERT_PATH":   "/home/nick/.minikube/certs",
   119  				"DOCKER_API_VERSION": "1.35",
   120  			},
   121  			expectedCluster: Env{
   122  				TLSVerify:  "1",
   123  				Host:       "tcp://192.168.99.100:2376",
   124  				CertPath:   "/home/nick/.minikube/certs",
   125  				APIVersion: "1.35",
   126  			},
   127  			expectedLocal: Env{
   128  				TLSVerify:  "1",
   129  				Host:       "tcp://192.168.99.100:2376",
   130  				CertPath:   "/home/nick/.minikube/certs",
   131  				APIVersion: "1.35",
   132  			},
   133  		},
   134  		{
   135  			env:             k8s.EnvMicroK8s,
   136  			runtime:         container.RuntimeDocker,
   137  			expectedCluster: Env{Host: microK8sDockerHost},
   138  			expectedLocal:   Env{},
   139  		},
   140  		{
   141  			env:     k8s.EnvMicroK8s,
   142  			runtime: container.RuntimeCrio,
   143  		},
   144  		{
   145  			env:     k8s.EnvMinikube,
   146  			runtime: container.RuntimeDocker,
   147  			mkEnv: map[string]string{
   148  				"DOCKER_TLS_VERIFY":  "1",
   149  				"DOCKER_HOST":        "tcp://192.168.99.100:2376",
   150  				"DOCKER_CERT_PATH":   "/home/nick/.minikube/certs",
   151  				"DOCKER_API_VERSION": "1.35",
   152  			},
   153  			expectedCluster: Env{
   154  				TLSVerify:  "1",
   155  				Host:       "tcp://192.168.99.100:2376",
   156  				CertPath:   "/home/nick/.minikube/certs",
   157  				APIVersion: "1.35",
   158  				IsMinikube: true,
   159  			},
   160  		},
   161  		{
   162  			env:     k8s.EnvMinikube,
   163  			runtime: container.RuntimeDocker,
   164  			mkEnv: map[string]string{
   165  				"DOCKER_TLS_VERIFY":  "1",
   166  				"DOCKER_HOST":        "tcp://192.168.99.100:2376",
   167  				"DOCKER_CERT_PATH":   "/home/nick/.minikube/certs",
   168  				"DOCKER_API_VERSION": "1.35",
   169  			},
   170  			osEnv: map[string]string{
   171  				"DOCKER_HOST": "tcp://registry.local:80",
   172  			},
   173  			expectedCluster: Env{
   174  				Host: "tcp://registry.local:80",
   175  			},
   176  			expectedLocal: Env{
   177  				Host: "tcp://registry.local:80",
   178  			},
   179  		},
   180  		{
   181  			// Test the case where the user has already run
   182  			// eval $(minikube docker-env)
   183  			env:     k8s.EnvMinikube,
   184  			runtime: container.RuntimeDocker,
   185  			mkEnv: map[string]string{
   186  				"DOCKER_TLS_VERIFY": "1",
   187  				"DOCKER_HOST":       "tcp://192.168.99.100:2376",
   188  				"DOCKER_CERT_PATH":  "/home/nick/.minikube/certs",
   189  			},
   190  			osEnv: map[string]string{
   191  				"DOCKER_TLS_VERIFY": "1",
   192  				"DOCKER_HOST":       "tcp://192.168.99.100:2376",
   193  				"DOCKER_CERT_PATH":  "/home/nick/.minikube/certs",
   194  			},
   195  			expectedCluster: Env{
   196  				TLSVerify:  "1",
   197  				Host:       "tcp://192.168.99.100:2376",
   198  				CertPath:   "/home/nick/.minikube/certs",
   199  				IsMinikube: true,
   200  			},
   201  			expectedLocal: Env{
   202  				TLSVerify:  "1",
   203  				Host:       "tcp://192.168.99.100:2376",
   204  				CertPath:   "/home/nick/.minikube/certs",
   205  				IsMinikube: true,
   206  			},
   207  		},
   208  		{
   209  			env:     k8s.EnvMinikube,
   210  			runtime: container.RuntimeCrio,
   211  			mkEnv: map[string]string{
   212  				"DOCKER_TLS_VERIFY":  "1",
   213  				"DOCKER_HOST":        "tcp://192.168.99.100:2376",
   214  				"DOCKER_CERT_PATH":   "/home/nick/.minikube/certs",
   215  				"DOCKER_API_VERSION": "1.35",
   216  			},
   217  		},
   218  		{
   219  			env: k8s.EnvUnknown,
   220  			osEnv: map[string]string{
   221  				"DOCKER_TLS_VERIFY":  "1",
   222  				"DOCKER_HOST":        "localhost:2376",
   223  				"DOCKER_CERT_PATH":   "/home/nick/.minikube/certs",
   224  				"DOCKER_API_VERSION": "1.35",
   225  			},
   226  			expectedCluster: Env{
   227  				TLSVerify:  "1",
   228  				Host:       "tcp://localhost:2376",
   229  				CertPath:   "/home/nick/.minikube/certs",
   230  				APIVersion: "1.35",
   231  			},
   232  			expectedLocal: Env{
   233  				TLSVerify:  "1",
   234  				Host:       "tcp://localhost:2376",
   235  				CertPath:   "/home/nick/.minikube/certs",
   236  				APIVersion: "1.35",
   237  			},
   238  		},
   239  	}
   240  
   241  	for i, c := range cases {
   242  		t.Run(fmt.Sprintf("Case%d", i), func(t *testing.T) {
   243  			origEnv := map[string]string{}
   244  			for _, k := range envVars {
   245  				origEnv[k] = os.Getenv(k)
   246  				os.Setenv(k, c.osEnv[k])
   247  			}
   248  
   249  			defer func() {
   250  				for k := range c.osEnv {
   251  					os.Setenv(k, origEnv[k])
   252  				}
   253  			}()
   254  
   255  			mkClient := minikube.FakeClient{DockerEnvMap: c.mkEnv}
   256  			cluster, err := ProvideClusterEnv(context.Background(), c.env, c.runtime, mkClient)
   257  			if assert.NoError(t, err) {
   258  				assert.Equal(t, c.expectedCluster, Env(cluster))
   259  			}
   260  
   261  			local, err := ProvideLocalEnv(context.Background(), cluster)
   262  			if assert.NoError(t, err) {
   263  				assert.Equal(t, c.expectedLocal, Env(local))
   264  			}
   265  		})
   266  	}
   267  }