Thanks to visit codestin.com
Credit goes to github.com

Skip to content

test(agent): fix lifecycle test flakeyness #8230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"go.uber.org/goleak"
"golang.org/x/crypto/ssh"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"tailscale.com/net/speedtest"
"tailscale.com/tailcfg"
Expand Down Expand Up @@ -1099,7 +1100,7 @@ func TestAgent_Lifecycle(t *testing.T) {
t.Parallel()

_, client, _, _, _ := setupAgent(t, agentsdk.Manifest{
StartupScript: "sleep 5",
StartupScript: "sleep 3",
StartupScriptTimeout: time.Nanosecond,
}, 0)

Expand All @@ -1111,10 +1112,10 @@ func TestAgent_Lifecycle(t *testing.T) {
var got []codersdk.WorkspaceAgentLifecycle
assert.Eventually(t, func() bool {
got = client.getLifecycleStates()
return len(got) > 0 && got[len(got)-1] == want[len(want)-1]
return slices.Contains(got, want[len(want)-1])
}, testutil.WaitShort, testutil.IntervalMedium)

require.Equal(t, want, got)
require.Equal(t, want, got[:len(want)])
})

t.Run("StartError", func(t *testing.T) {
Expand All @@ -1133,10 +1134,10 @@ func TestAgent_Lifecycle(t *testing.T) {
var got []codersdk.WorkspaceAgentLifecycle
assert.Eventually(t, func() bool {
got = client.getLifecycleStates()
return len(got) > 0 && got[len(got)-1] == want[len(want)-1]
return slices.Contains(got, want[len(want)-1])
}, testutil.WaitShort, testutil.IntervalMedium)

require.Equal(t, want, got)
require.Equal(t, want, got[:len(want)])
})

t.Run("Ready", func(t *testing.T) {
Expand Down Expand Up @@ -1165,14 +1166,12 @@ func TestAgent_Lifecycle(t *testing.T) {
t.Parallel()

_, client, _, _, closer := setupAgent(t, agentsdk.Manifest{
ShutdownScript: "sleep 5",
ShutdownScript: "sleep 3",
StartupScriptTimeout: 30 * time.Second,
}, 0)

var ready []codersdk.WorkspaceAgentLifecycle
assert.Eventually(t, func() bool {
ready = client.getLifecycleStates()
return len(ready) > 0 && ready[len(ready)-1] == codersdk.WorkspaceAgentLifecycleReady
return slices.Contains(client.getLifecycleStates(), codersdk.WorkspaceAgentLifecycleReady)
}, testutil.WaitShort, testutil.IntervalMedium)

// Start close asynchronously so that we an inspect the state.
Expand All @@ -1187,30 +1186,30 @@ func TestAgent_Lifecycle(t *testing.T) {
})

want := []codersdk.WorkspaceAgentLifecycle{
codersdk.WorkspaceAgentLifecycleStarting,
codersdk.WorkspaceAgentLifecycleReady,
codersdk.WorkspaceAgentLifecycleShuttingDown,
}

var got []codersdk.WorkspaceAgentLifecycle
assert.Eventually(t, func() bool {
got = client.getLifecycleStates()[len(ready):]
return len(got) > 0 && got[len(got)-1] == want[len(want)-1]
got = client.getLifecycleStates()
return slices.Contains(got, want[len(want)-1])
}, testutil.WaitShort, testutil.IntervalMedium)

require.Equal(t, want, got)
require.Equal(t, want, got[:len(want)])
})

t.Run("ShutdownTimeout", func(t *testing.T) {
t.Parallel()

_, client, _, _, closer := setupAgent(t, agentsdk.Manifest{
ShutdownScript: "sleep 5",
ShutdownScript: "sleep 3",
ShutdownScriptTimeout: time.Nanosecond,
}, 0)

var ready []codersdk.WorkspaceAgentLifecycle
assert.Eventually(t, func() bool {
ready = client.getLifecycleStates()
return len(ready) > 0 && ready[len(ready)-1] == codersdk.WorkspaceAgentLifecycleReady
return slices.Contains(client.getLifecycleStates(), codersdk.WorkspaceAgentLifecycleReady)
}, testutil.WaitShort, testutil.IntervalMedium)

// Start close asynchronously so that we an inspect the state.
Expand All @@ -1225,17 +1224,19 @@ func TestAgent_Lifecycle(t *testing.T) {
})

want := []codersdk.WorkspaceAgentLifecycle{
codersdk.WorkspaceAgentLifecycleStarting,
codersdk.WorkspaceAgentLifecycleReady,
codersdk.WorkspaceAgentLifecycleShuttingDown,
codersdk.WorkspaceAgentLifecycleShutdownTimeout,
}

var got []codersdk.WorkspaceAgentLifecycle
assert.Eventually(t, func() bool {
got = client.getLifecycleStates()[len(ready):]
return len(got) > 0 && got[len(got)-1] == want[len(want)-1]
got = client.getLifecycleStates()
return slices.Contains(got, want[len(want)-1])
}, testutil.WaitShort, testutil.IntervalMedium)

require.Equal(t, want, got)
require.Equal(t, want, got[:len(want)])
})

t.Run("ShutdownError", func(t *testing.T) {
Expand All @@ -1246,10 +1247,8 @@ func TestAgent_Lifecycle(t *testing.T) {
ShutdownScriptTimeout: 30 * time.Second,
}, 0)

var ready []codersdk.WorkspaceAgentLifecycle
assert.Eventually(t, func() bool {
ready = client.getLifecycleStates()
return len(ready) > 0 && ready[len(ready)-1] == codersdk.WorkspaceAgentLifecycleReady
return slices.Contains(client.getLifecycleStates(), codersdk.WorkspaceAgentLifecycleReady)
}, testutil.WaitShort, testutil.IntervalMedium)

// Start close asynchronously so that we an inspect the state.
Expand All @@ -1264,17 +1263,19 @@ func TestAgent_Lifecycle(t *testing.T) {
})

want := []codersdk.WorkspaceAgentLifecycle{
codersdk.WorkspaceAgentLifecycleStarting,
codersdk.WorkspaceAgentLifecycleReady,
codersdk.WorkspaceAgentLifecycleShuttingDown,
codersdk.WorkspaceAgentLifecycleShutdownError,
}

var got []codersdk.WorkspaceAgentLifecycle
assert.Eventually(t, func() bool {
got = client.getLifecycleStates()[len(ready):]
return len(got) > 0 && got[len(got)-1] == want[len(want)-1]
got = client.getLifecycleStates()
return slices.Contains(got, want[len(want)-1])
}, testutil.WaitShort, testutil.IntervalMedium)

require.Equal(t, want, got)
require.Equal(t, want, got[:len(want)])
})

t.Run("ShutdownScriptOnce", func(t *testing.T) {
Expand Down