github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/docker/env_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/tilt-dev/clusterid"
    11  	"github.com/tilt-dev/tilt/internal/k8s"
    12  	"github.com/tilt-dev/tilt/internal/testutils"
    13  )
    14  
    15  type EnvTest struct {
    16  	product     clusterid.Product
    17  	kubecontext string
    18  	dockerHost  string
    19  	warning     string
    20  	match       bool
    21  }
    22  
    23  type fakeDaemonClient struct {
    24  	host string
    25  }
    26  
    27  func (f *fakeDaemonClient) DaemonHost() string {
    28  	return f.host
    29  }
    30  
    31  func TestColimaEnv(t *testing.T) {
    32  
    33  	table := []EnvTest{
    34  		{
    35  			product:     clusterid.ProductColima,
    36  			kubecontext: "colima",
    37  			dockerHost:  "unix://~/.colima/docker.sock",
    38  			match:       true,
    39  		},
    40  		{
    41  			product:     clusterid.ProductColima,
    42  			kubecontext: "colima",
    43  			dockerHost:  "unix://~/.colima/default/docker.sock",
    44  			match:       true,
    45  		},
    46  		{
    47  			product:     clusterid.ProductColima,
    48  			kubecontext: "colima-test",
    49  			dockerHost:  "unix://~/.colima-test/docker.sock",
    50  			match:       true,
    51  		},
    52  		{
    53  			product:     clusterid.ProductColima,
    54  			kubecontext: "colima-test",
    55  			dockerHost:  "unix://~/.colima/test/docker.sock",
    56  			match:       true,
    57  		},
    58  		{
    59  			product:     clusterid.ProductColima,
    60  			kubecontext: "colima-test",
    61  			dockerHost:  "unix://~/.colima/docker.sock",
    62  			match:       false,
    63  			warning:     "connected to Kubernetes on Colima profile test, but building on Docker on Colima profile default",
    64  		},
    65  		{
    66  			product:     clusterid.ProductColima,
    67  			kubecontext: "colima",
    68  			dockerHost:  "unix://~/.colima-test/docker.sock",
    69  			match:       false,
    70  			warning:     "connected to Kubernetes on Colima profile default, but building on Docker on Colima profile test",
    71  		},
    72  		{
    73  			product:     clusterid.ProductColima,
    74  			kubecontext: "colima-test",
    75  			dockerHost:  "unix://~/.docker/desktop/docker.sock",
    76  			match:       false,
    77  			warning:     "connected to Kubernetes running on Colima, but building on a non-Colima Docker socket",
    78  		},
    79  		{
    80  			product:     clusterid.ProductOrbstack,
    81  			kubecontext: "orbstack",
    82  			dockerHost:  "unix://~/.orbstack/run/docker.sock",
    83  			match:       true,
    84  		},
    85  		{
    86  			product:     clusterid.ProductOrbstack,
    87  			kubecontext: "orbstack",
    88  			dockerHost:  "unix://~/.docker/desktop/docker.sock",
    89  			match:       false,
    90  			warning:     "connected to Kubernetes running on Orbstack, but building on a non-Orbstack Docker socket",
    91  		},
    92  		// colima on linux
    93  		{
    94  			product:     clusterid.ProductColima,
    95  			kubecontext: "colima",
    96  			dockerHost:  "unix://~/.config/colima/default/docker.sock",
    97  			match:       true,
    98  		},
    99  		{
   100  			product:     clusterid.ProductColima,
   101  			kubecontext: "colima-test",
   102  			dockerHost:  "unix://~/.config/colima/test/docker.sock",
   103  			match:       true,
   104  		},
   105  	}
   106  
   107  	for i, c := range table {
   108  		t.Run(fmt.Sprintf("env-%d", i), func(t *testing.T) {
   109  			out := &bytes.Buffer{}
   110  			ctx, _, _ := testutils.ForkedCtxAndAnalyticsForTest(out)
   111  			env := Env{
   112  				Client: &fakeDaemonClient{
   113  					host: c.dockerHost,
   114  				},
   115  			}
   116  
   117  			result := willBuildToKubeContext(ctx, c.product, k8s.KubeContext(c.kubecontext), env)
   118  			assert.Equal(t, result, c.match)
   119  			if c.warning != "" {
   120  				assert.Contains(t, out.String(), c.warning)
   121  			} else {
   122  				assert.Equal(t, out.String(), c.warning)
   123  			}
   124  		})
   125  	}
   126  }