Repository navigation
Remove what the connector's worker side left behind #829
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
Draft
robzolkos
wants to merge
1
commit into
connect-e2e/lock-docs
Choose a base branch
from
connect-e2e/remove-worker-remnants
base: connect-e2e/lock-docs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| //go:build unix | ||
|
|
||
| package commands | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/basecamp/basecamp-cli/internal/appctx" | ||
| "github.com/basecamp/basecamp-cli/internal/connector" | ||
| "github.com/basecamp/basecamp-cli/internal/output" | ||
| ) | ||
|
|
||
| // A second connector for an agent that is already running says so at once, | ||
| // even while the token endpoint is rate-limiting it: the lock is checked | ||
| // before the start waits for a token, not after. | ||
| func TestASecondConnectorIsRefusedBeforeItWaitsForAToken(t *testing.T) { | ||
| f := newOperatorFixture(t) | ||
| dir, err := connectStateDir(f.file, false) | ||
| require.NoError(t, err) | ||
| lock, err := connector.AcquireInstanceLock(dir, f.file.AccountID, f.file.Agent.PersonID, time.Now()) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = lock.Release() }) | ||
|
|
||
| app := newConnectSetupApp(t, f.s, "agent") | ||
| creds, err := app.Auth.GetStore().Load(app.Auth.CredentialKey()) | ||
| require.NoError(t, err) | ||
| creds.ExpiresAt = time.Now().Add(-time.Minute).Unix() | ||
| require.NoError(t, app.Auth.GetStore().Save(app.Auth.CredentialKey(), creds)) | ||
| f.s.mu.Lock() | ||
| f.s.rateLimitMints, f.s.mints = true, 0 | ||
| f.s.mu.Unlock() | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| defer cancel() | ||
| var buf bytes.Buffer | ||
| app.Output = output.New(output.Options{Format: output.FormatJSON, Writer: &buf}) | ||
| cmd := NewConnectCmd() | ||
| cmd.SetArgs(nil) | ||
| cmd.SetContext(appctx.WithApp(ctx, app)) | ||
| cmd.SetOut(&buf) | ||
| cmd.SetErr(&buf) | ||
| cmd.SilenceErrors, cmd.SilenceUsage = true, true | ||
| done := make(chan error, 1) | ||
| go func() { done <- cmd.Execute() }() | ||
|
|
||
| select { | ||
| case err := <-done: | ||
| refusal := usageError(t, err) | ||
| assert.Equal(t, output.CodeLockUnavailable, refusal.Code, buf.String()) | ||
| assert.Contains(t, refusal.Message, fmt.Sprintf("held by pid %d", os.Getpid()), "the refusal names the holder") | ||
| assert.Contains(t, refusal.Hint, "One connector serves every repo") | ||
| case <-time.After(10 * time.Second): | ||
| cancel() | ||
| <-done | ||
| t.Fatalf("a second connector waited for a token instead of refusing: %s", buf.String()) | ||
| } | ||
| f.s.mu.Lock() | ||
| defer f.s.mu.Unlock() | ||
| assert.Zero(t, f.s.mints, "nothing was minted for a connector that cannot run") | ||
| } | ||
|
|
||
| // A start waiting for its token does not hold the agent's lock: until the | ||
| // credential is proven to be that agent, the lock is only looked at, so a | ||
| // profile whose credential is some other agent's cannot keep the real | ||
| // connector out while it waits. | ||
| func TestAStartWaitingForItsTokenHoldsNoLock(t *testing.T) { | ||
| f := newOperatorFixture(t) | ||
| app := newConnectSetupApp(t, f.s, "agent") | ||
| creds, err := app.Auth.GetStore().Load(app.Auth.CredentialKey()) | ||
| require.NoError(t, err) | ||
| creds.ExpiresAt = time.Now().Add(-time.Minute).Unix() | ||
| require.NoError(t, app.Auth.GetStore().Save(app.Auth.CredentialKey(), creds)) | ||
| f.s.mu.Lock() | ||
| f.s.rateLimitMints = true | ||
| f.s.mu.Unlock() | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| defer cancel() | ||
| var out lockedBuffer | ||
| app.Output = output.New(output.Options{Format: output.FormatJSON, Writer: &out}) | ||
| cmd := NewConnectCmd() | ||
| cmd.SetArgs(nil) | ||
| cmd.SetContext(appctx.WithApp(ctx, app)) | ||
| cmd.SetOut(&out) | ||
| cmd.SetErr(&out) | ||
| cmd.SilenceErrors, cmd.SilenceUsage = true, true | ||
| done := make(chan error, 1) | ||
| go func() { done <- cmd.Execute() }() | ||
|
|
||
| require.Eventually(t, func() bool { return strings.Contains(out.String(), "could not get the agent's token yet") }, | ||
| 10*time.Second, 20*time.Millisecond, "the start never waited: %s", out.String()) | ||
| dir, err := connectStateDir(f.file, false) | ||
| require.NoError(t, err) | ||
| lock, err := connector.AcquireInstanceLock(dir, f.file.AccountID, f.file.Agent.PersonID, time.Now()) | ||
| require.NoError(t, err, "a waiting start held the agent's lock") | ||
| require.NoError(t, lock.Release()) | ||
|
|
||
| cancel() | ||
| select { | ||
| case <-done: | ||
| case <-time.After(10 * time.Second): | ||
| t.Fatal("the waiting start did not stop when canceled") | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: The timeout path can still hang the suite indefinitely if cancellation fails to stop the command. Bound this receive as the second test does, then fail with a cancellation-specific message.
Prompt for AI agents