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

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"strings"
    10  	"syscall"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  const cleanupTxt = "cleanup.txt"
    19  
    20  func TestLocalResource(t *testing.T) {
    21  	f := newFixture(t, "local_resource")
    22  
    23  	removeTestFiles := func() {
    24  		require.NoError(t, os.RemoveAll(f.testDirPath(cleanupTxt)))
    25  		require.NoError(t, os.RemoveAll(f.testDirPath("greeting")))
    26  		require.NoError(t, os.RemoveAll(f.testDirPath("probe-success")))
    27  	}
    28  	removeTestFiles()
    29  	t.Cleanup(removeTestFiles)
    30  
    31  	f.TiltUp()
    32  
    33  	const barServeLogMessage = "Running cmd: ./hello.sh bar"
    34  	const readinessProbeSuccessMessage = `[readiness probe: success] fake probe success message`
    35  
    36  	f.logs.AssertEventuallyContains(t, "hello! foo #1", 5*time.Second)
    37  
    38  	// write a sentinel file for the probe to find and change its result
    39  	if assert.NoError(t, ioutil.WriteFile(f.testDirPath("probe-success"), nil, 0777)) {
    40  		f.logs.AssertEventuallyContains(t, readinessProbeSuccessMessage, 5*time.Second)
    41  	}
    42  
    43  	// wait for second resource to start and then ensure that the order in the logs is as expected
    44  	f.logs.AssertEventuallyContains(t, barServeLogMessage, 5*time.Second)
    45  	curLogs := f.logs.String()
    46  	assert.Greater(t, strings.Index(curLogs, barServeLogMessage), strings.Index(curLogs, readinessProbeSuccessMessage),
    47  		"dependent resource started BEFORE other resource ready")
    48  	f.logs.AssertEventuallyContains(t, "hello! bar #1", 5*time.Second)
    49  
    50  	// trigger a service restart by changing a watched file
    51  	if assert.NoError(t, ioutil.WriteFile(f.testDirPath("greeting"), []byte("hola"), 0777)) {
    52  		f.logs.AssertEventuallyContains(t, "hola! foo #1", 5*time.Second)
    53  	}
    54  
    55  	// force the probe into a failure state
    56  	if assert.NoError(t, os.Remove(f.testDirPath("probe-success"))) {
    57  		f.logs.AssertEventuallyContains(t, `[readiness probe: failure] fake probe failure message`, 5*time.Second)
    58  	}
    59  
    60  	// send a SIGTERM and make sure Tilt propagates it to its local_resource processes
    61  	require.NoError(t, f.activeTiltUp.process.Signal(syscall.SIGTERM))
    62  
    63  	select {
    64  	case <-f.activeTiltUp.done:
    65  	case <-time.After(5 * time.Second):
    66  		t.Fatal("Tilt failed to exit within 5 seconds of SIGTERM")
    67  	}
    68  
    69  	// hello.sh writes to cleanup.txt on SIGTERM
    70  	b, err := ioutil.ReadFile(f.testDirPath(cleanupTxt))
    71  	if assert.NoError(t, err) {
    72  		s := string(b)
    73  		require.Contains(t, s, "cleaning up: foo")
    74  		require.Contains(t, s, "cleaning up: bar")
    75  	}
    76  }