github.com/tilt-dev/tilt@v0.36.0/integration/crd_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"io/ioutil"
     9  	"path/filepath"
    10  	"regexp"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestCRD(t *testing.T) {
    20  	f := newK8sFixture(t, "crd")
    21  
    22  	f.TiltUp()
    23  
    24  	ctx, cancel := context.WithTimeout(f.ctx, time.Minute)
    25  	defer cancel()
    26  
    27  	f.WaitUntil(ctx, "Waiting for UM to show up", func() (string, error) {
    28  		out, _ := f.runCommand("kubectl", "get", "um", namespaceFlag)
    29  		return out.String(), nil
    30  	}, "bobo")
    31  
    32  	out, err := f.runCommand("kubectl", "get", "um", namespaceFlag, "-o=yaml")
    33  	require.NoError(t, err)
    34  	contents := out.String()
    35  
    36  	// Make sure image injection didn't replace the name
    37  	// or the non-image field, but did replace the image field.
    38  	assert.Contains(t, contents, "name: bobo\n")
    39  	assert.Contains(t, contents, "nonImage: bobo\n")
    40  	assert.NotContains(t, contents, "image: bobo\n")
    41  	assert.Regexp(t, regexp.MustCompile("imagePullPolicy: (IfNotPresent|Never)\n"), contents)
    42  }
    43  
    44  // Make sure that running 'tilt down' with no resources installed
    45  // yet handles 'not found' correctly
    46  func TestCRDNotFound(t *testing.T) {
    47  	f := newK8sFixture(t, "crd")
    48  
    49  	err := f.tilt.Down(f.ctx, ioutil.Discard)
    50  	require.NoError(t, err)
    51  }
    52  
    53  // Make sure that running 'tilt down' tears down the CRD
    54  // even when the CR doesn't exist.
    55  func TestCRDPartialNotFound(t *testing.T) {
    56  	f := newK8sFixture(t, "crd")
    57  
    58  	out, err := f.runCommand("kubectl", "apply", "-f", filepath.Join(f.dir, "crd.yaml"))
    59  	assert.NoError(t, err)
    60  	assert.Contains(t, out.String(), "uselessmachines.tilt.dev created")
    61  
    62  	_, err = f.runCommand("kubectl", "get", "crd", "uselessmachines.tilt.dev")
    63  	assert.NoError(t, err)
    64  
    65  	err = f.tilt.Down(f.ctx, ioutil.Discard)
    66  	require.NoError(t, err)
    67  
    68  	// Make sure the crds were deleted.
    69  	assert.Eventually(t, func() bool {
    70  		out, err := f.runCommand("kubectl", "get", "crd", "uselessmachines.tilt.dev")
    71  		return err != nil && strings.Contains(out.String(), `"uselessmachines.tilt.dev" not found`)
    72  	}, 3*time.Second, time.Second)
    73  }