Skip to content

Commit

Permalink
tests: add a file dep + verify service restart for local_resource (#4311
Browse files Browse the repository at this point in the history
)

Add a single file dep and change it, ensuring that the service is
restarted appropriately.
  • Loading branch information
milas committed Mar 12, 2021
1 parent 969b62d commit c9ca431
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion integration/local_resource/Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ p = probe(initial_delay_secs=0,
failure_threshold=1,
exec=exec_action(command=['./probe.sh']))

local_resource('foo', serve_cmd='./hello.sh foo', readiness_probe=p)
local_resource('foo', serve_cmd='./hello.sh foo', deps=['greeting'], readiness_probe=p)

# readiness probe explicitly set to None
local_resource('bar', serve_cmd='./hello.sh bar',
Expand Down
7 changes: 6 additions & 1 deletion integration/local_resource/hello.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ fi

n=1
msg="$*"
greeting="hello"

if [[ -f greeting ]]; then
greeting=$(cat greeting)
fi

cleanup() {
echo "cleaning up: $msg"
Expand All @@ -22,7 +27,7 @@ cleanup() {
trap cleanup SIGTERM

while true; do
echo "hello! $msg #$n"
echo "$greeting! $msg #$n"
# run sleep in the background so the main thread is not blocked
# otherwise, the signal handler doesn't run until the current sleep
# finishes
Expand Down
37 changes: 24 additions & 13 deletions integration/local_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ func TestLocalResource(t *testing.T) {
f := newFixture(t, "local_resource")
defer f.TearDown()

t.Cleanup(func() {
_ = os.Remove(f.testDirPath(cleanupTxt))
_ = os.Remove(f.testDirPath("probe-success"))
})
removeTestFiles := func() {
require.NoError(t, os.RemoveAll(f.testDirPath(cleanupTxt)))
require.NoError(t, os.RemoveAll(f.testDirPath("greeting")))
require.NoError(t, os.RemoveAll(f.testDirPath("probe-success")))
}
removeTestFiles()
t.Cleanup(removeTestFiles)

f.TiltWatch()

Expand All @@ -33,8 +36,9 @@ func TestLocalResource(t *testing.T) {
require.NoError(t, f.logs.WaitUntilContains("hello! foo #1", 5*time.Second))

// write a sentinel file for the probe to find and change its result
require.NoError(t, ioutil.WriteFile(f.testDirPath("probe-success"), nil, 0777))
require.NoError(t, f.logs.WaitUntilContains(readinessProbeSuccessMessage, 5*time.Second))
if assert.NoError(t, ioutil.WriteFile(f.testDirPath("probe-success"), nil, 0777)) {
assert.NoError(t, f.logs.WaitUntilContains(readinessProbeSuccessMessage, 5*time.Second))
}

// wait for second resource to start and then ensure that the order in the logs is as expected
require.NoError(t, f.logs.WaitUntilContains(barServeLogMessage, 5*time.Second))
Expand All @@ -43,8 +47,15 @@ func TestLocalResource(t *testing.T) {
"dependent resource started BEFORE other resource ready")
require.NoError(t, f.logs.WaitUntilContains("hello! bar #1", 5*time.Second))

require.NoError(t, os.Remove(f.testDirPath("probe-success")))
require.NoError(t, f.logs.WaitUntilContains(`[readiness probe: failure] fake probe failure message`, 5*time.Second))
// trigger a service restart by changing a watched file
if assert.NoError(t, ioutil.WriteFile(f.testDirPath("greeting"), []byte("hola"), 0777)) {
assert.NoError(t, f.logs.WaitUntilContains("hola! foo #1", 5*time.Second))
}

// force the probe into a failure state
if assert.NoError(t, os.Remove(f.testDirPath("probe-success"))) {
assert.NoError(t, f.logs.WaitUntilContains(`[readiness probe: failure] fake probe failure message`, 5*time.Second))
}

// send a SIGTERM and make sure Tilt propagates it to its local_resource processes
require.NoError(t, f.activeTiltUp.process.Signal(syscall.SIGTERM))
Expand All @@ -57,9 +68,9 @@ func TestLocalResource(t *testing.T) {

// hello.sh writes to cleanup.txt on SIGTERM
b, err := ioutil.ReadFile(f.testDirPath(cleanupTxt))
require.NoError(t, err)
s := string(b)

require.Contains(t, s, "cleaning up: foo")
require.Contains(t, s, "cleaning up: bar")
if assert.NoError(t, err) {
s := string(b)
require.Contains(t, s, "cleaning up: foo")
require.Contains(t, s, "cleaning up: bar")
}
}

0 comments on commit c9ca431

Please sign in to comment.