Skip to content

chore: remove dbfake.WorkspaceWithAgent #10879

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
Nov 27, 2023
Merged
Show file tree
Hide file tree
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
30 changes: 18 additions & 12 deletions cli/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,25 @@ func TestWorkspaceAgent(t *testing.T) {

client, db := coderdtest.NewWithDatabase(t, nil)
user := coderdtest.CreateFirstUser(t, client)
ws, authToken := dbfake.WorkspaceWithAgent(t, db, database.Workspace{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
})
r := dbfake.NewWorkspaceBuilder(t, db).
Seed(database.Workspace{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
}).
WithAgent().
Do()
logDir := t.TempDir()
inv, _ := clitest.New(t,
"agent",
"--auth", "token",
"--agent-token", authToken,
"--agent-token", r.AgentToken,
"--agent-url", client.URL.String(),
"--log-dir", logDir,
)

clitest.Start(t, inv)

coderdtest.AwaitWorkspaceAgents(t, client, ws.ID)
coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID)

require.Eventually(t, func() bool {
info, err := os.Stat(filepath.Join(logDir, "coder-agent.log"))
Expand Down Expand Up @@ -209,16 +212,19 @@ func TestWorkspaceAgent(t *testing.T) {

client, db := coderdtest.NewWithDatabase(t, nil)
user := coderdtest.CreateFirstUser(t, client)
ws, authToken := dbfake.WorkspaceWithAgent(t, db, database.Workspace{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
})
r := dbfake.NewWorkspaceBuilder(t, db).
Seed(database.Workspace{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
}).
WithAgent().
Do()

logDir := t.TempDir()
inv, _ := clitest.New(t,
"agent",
"--auth", "token",
"--agent-token", authToken,
"--agent-token", r.AgentToken,
"--agent-url", client.URL.String(),
"--log-dir", logDir,
)
Expand All @@ -227,7 +233,7 @@ func TestWorkspaceAgent(t *testing.T) {

clitest.Start(t, inv)

resources := coderdtest.AwaitWorkspaceAgents(t, client, ws.ID)
resources := coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID)
require.Len(t, resources, 1)
require.Len(t, resources[0].Agents, 1)
require.Len(t, resources[0].Agents[0].Subsystems, 2)
Expand Down
21 changes: 12 additions & 9 deletions cli/configssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ func TestConfigSSH(t *testing.T) {
})
owner := coderdtest.CreateFirstUser(t, client)
member, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
ws, authToken := dbfake.WorkspaceWithAgent(t, db, database.Workspace{
OrganizationID: owner.OrganizationID,
OwnerID: memberUser.ID,
})
_ = agenttest.New(t, client.URL, authToken)
resources := coderdtest.AwaitWorkspaceAgents(t, client, ws.ID)
r := dbfake.NewWorkspaceBuilder(t, db).
Seed(database.Workspace{
OrganizationID: owner.OrganizationID,
OwnerID: memberUser.ID,
}).
WithAgent().
Do()
_ = agenttest.New(t, client.URL, r.AgentToken)
resources := coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID)
agentConn, err := client.DialWorkspaceAgent(context.Background(), resources[0].Agents[0].ID, nil)
require.NoError(t, err)
defer agentConn.Close()
Expand Down Expand Up @@ -153,7 +156,7 @@ func TestConfigSSH(t *testing.T) {

home := filepath.Dir(filepath.Dir(sshConfigFile))
// #nosec
sshCmd := exec.Command("ssh", "-F", sshConfigFile, hostname+ws.Name, "echo", "test")
sshCmd := exec.Command("ssh", "-F", sshConfigFile, hostname+r.Workspace.Name, "echo", "test")
pty = ptytest.New(t)
// Set HOME because coder config is included from ~/.ssh/coder.
sshCmd.Env = append(sshCmd.Env, fmt.Sprintf("HOME=%s", home))
Expand Down Expand Up @@ -572,10 +575,10 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
client, db := coderdtest.NewWithDatabase(t, nil)
user := coderdtest.CreateFirstUser(t, client)
if tt.hasAgent {
_, _ = dbfake.WorkspaceWithAgent(t, db, database.Workspace{
_ = dbfake.NewWorkspaceBuilder(t, db).Seed(database.Workspace{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
})
}).WithAgent().Do()
}

// Prepare ssh config files.
Expand Down
19 changes: 11 additions & 8 deletions cli/gitssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,21 @@ func prepareTestGitSSH(ctx context.Context, t *testing.T) (*agentsdk.Client, str
require.NoError(t, err)

// setup template
ws, agentToken := dbfake.WorkspaceWithAgent(t, db, database.Workspace{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
})
r := dbfake.NewWorkspaceBuilder(t, db).
Seed(database.Workspace{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
}).
WithAgent().
Do()
// start workspace agent
agentClient := agentsdk.New(client.URL)
agentClient.SetSessionToken(agentToken)
_ = agenttest.New(t, client.URL, agentToken, func(o *agent.Options) {
agentClient.SetSessionToken(r.AgentToken)
_ = agenttest.New(t, client.URL, r.AgentToken, func(o *agent.Options) {
o.Client = agentClient
})
_ = coderdtest.AwaitWorkspaceAgents(t, client, ws.ID)
return agentClient, agentToken, pubkey
_ = coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID)
return agentClient, r.AgentToken, pubkey
}

func serveSSHForGitSSH(t *testing.T, handler func(ssh.Session), pubkeys ...gossh.PublicKey) *net.TCPAddr {
Expand Down
10 changes: 5 additions & 5 deletions cli/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func TestList(t *testing.T) {
client, db := coderdtest.NewWithDatabase(t, nil)
owner := coderdtest.CreateFirstUser(t, client)
member, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
ws, _ := dbfake.WorkspaceWithAgent(t, db, database.Workspace{
r := dbfake.NewWorkspaceBuilder(t, db).Seed(database.Workspace{
OrganizationID: owner.OrganizationID,
OwnerID: memberUser.ID,
})
}).WithAgent().Do()
inv, root := clitest.New(t, "ls")
clitest.SetupConfig(t, member, root)
pty := ptytest.New(t).Attach(inv)
Expand All @@ -41,7 +41,7 @@ func TestList(t *testing.T) {
assert.NoError(t, errC)
close(done)
}()
pty.ExpectMatch(ws.Name)
pty.ExpectMatch(r.Workspace.Name)
pty.ExpectMatch("Started")
cancelFunc()
<-done
Expand All @@ -52,10 +52,10 @@ func TestList(t *testing.T) {
client, db := coderdtest.NewWithDatabase(t, nil)
owner := coderdtest.CreateFirstUser(t, client)
member, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
dbfake.WorkspaceWithAgent(t, db, database.Workspace{
dbfake.NewWorkspaceBuilder(t, db).Seed(database.Workspace{
OrganizationID: owner.OrganizationID,
OwnerID: memberUser.ID,
})
}).WithAgent().Do()

inv, root := clitest.New(t, "list", "--output=json")
clitest.SetupConfig(t, member, root)
Expand Down
10 changes: 5 additions & 5 deletions cli/portforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,17 @@ func runAgent(t *testing.T, client *codersdk.Client, owner uuid.UUID, db databas
require.NoError(t, err, "specified user does not exist")
require.Greater(t, len(user.OrganizationIDs), 0, "user has no organizations")
orgID := user.OrganizationIDs[0]
ws, agentToken := dbfake.WorkspaceWithAgent(t, db, database.Workspace{
r := dbfake.NewWorkspaceBuilder(t, db).Seed(database.Workspace{
OrganizationID: orgID,
OwnerID: owner,
})
_ = agenttest.New(t, client.URL, agentToken,
}).WithAgent().Do()
_ = agenttest.New(t, client.URL, r.AgentToken,
func(o *agent.Options) {
o.SSHMaxTimeout = 60 * time.Second
},
)
coderdtest.AwaitWorkspaceAgents(t, client, ws.ID)
return ws
coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID)
return r.Workspace
}

// setupTestListener starts accepting connections and echoing a single packet.
Expand Down
16 changes: 8 additions & 8 deletions cli/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,30 @@ func setupTestSchedule(t *testing.T, sched *cron.Schedule) (ownerClient, memberC
memberClient, memberUser := coderdtest.CreateAnotherUserMutators(t, ownerClient, owner.OrganizationID, nil, func(r *codersdk.CreateUserRequest) {
r.Username = "testuser2" // ensure deterministic ordering
})
_, _ = dbfake.WorkspaceWithAgent(t, db, database.Workspace{
_ = dbfake.NewWorkspaceBuilder(t, db).Seed(database.Workspace{
Name: "a-owner",
OwnerID: owner.UserID,
OrganizationID: owner.OrganizationID,
AutostartSchedule: sql.NullString{String: sched.String(), Valid: true},
Ttl: sql.NullInt64{Int64: 8 * time.Hour.Nanoseconds(), Valid: true},
})
_, _ = dbfake.WorkspaceWithAgent(t, db, database.Workspace{
}).WithAgent().Do()
_ = dbfake.NewWorkspaceBuilder(t, db).Seed(database.Workspace{
Name: "b-owner",
OwnerID: owner.UserID,
OrganizationID: owner.OrganizationID,
AutostartSchedule: sql.NullString{String: sched.String(), Valid: true},
})
_, _ = dbfake.WorkspaceWithAgent(t, db, database.Workspace{
}).WithAgent().Do()
_ = dbfake.NewWorkspaceBuilder(t, db).Seed(database.Workspace{
Name: "c-member",
OwnerID: memberUser.ID,
OrganizationID: owner.OrganizationID,
Ttl: sql.NullInt64{Int64: 8 * time.Hour.Nanoseconds(), Valid: true},
})
_, _ = dbfake.WorkspaceWithAgent(t, db, database.Workspace{
}).WithAgent().Do()
_ = dbfake.NewWorkspaceBuilder(t, db).Seed(database.Workspace{
Name: "d-member",
OwnerID: memberUser.ID,
OrganizationID: owner.OrganizationID,
})
}).WithAgent().Do()

// Need this for LatestBuild.Deadline
resp, err := ownerClient.Workspaces(context.Background(), codersdk.WorkspaceFilter{})
Expand Down
35 changes: 19 additions & 16 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ func setupWorkspaceForAgent(t *testing.T, mutations ...func([]*proto.Agent) []*p
client.SetLogger(slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug))
first := coderdtest.CreateFirstUser(t, client)
userClient, user := coderdtest.CreateAnotherUser(t, client, first.OrganizationID)
workspace, agentToken := dbfake.WorkspaceWithAgent(t, store, database.Workspace{
OrganizationID: first.OrganizationID,
OwnerID: user.ID,
}, mutations...)
r := dbfake.NewWorkspaceBuilder(t, store).
Seed(database.Workspace{
OrganizationID: first.OrganizationID,
OwnerID: user.ID,
}).
WithAgent(mutations...).
Do()

return userClient, workspace, agentToken
return userClient, r.Workspace, r.AgentToken
}

func TestSSH(t *testing.T) {
Expand Down Expand Up @@ -127,11 +130,11 @@ func TestSSH(t *testing.T) {
client.SetLogger(slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug))
first := coderdtest.CreateFirstUser(t, client)
userClient, user := coderdtest.CreateAnotherUser(t, client, first.OrganizationID)
workspace, agentToken := dbfake.WorkspaceWithAgent(t, store, database.Workspace{
r := dbfake.NewWorkspaceBuilder(t, store).Seed(database.Workspace{
OrganizationID: first.OrganizationID,
OwnerID: user.ID,
})
inv, root := clitest.New(t, "ssh", workspace.Name)
}).WithAgent().Do()
inv, root := clitest.New(t, "ssh", r.Workspace.Name)
clitest.SetupConfig(t, userClient, root)
pty := ptytest.New(t).Attach(inv)

Expand All @@ -144,14 +147,14 @@ func TestSSH(t *testing.T) {
})
pty.ExpectMatch("Waiting")

_ = agenttest.New(t, client.URL, agentToken)
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
_ = agenttest.New(t, client.URL, r.AgentToken)
coderdtest.AwaitWorkspaceAgents(t, client, r.Workspace.ID)

// Ensure the agent is connected.
pty.WriteLine("echo hell'o'")
pty.ExpectMatchContext(ctx, "hello")

_ = dbfake.NewWorkspaceBuildBuilder(t, store, workspace).
_ = dbfake.NewWorkspaceBuildBuilder(t, store, r.Workspace).
Seed(database.WorkspaceBuild{
Transition: database.WorkspaceTransitionStop,
BuildNumber: 2,
Expand Down Expand Up @@ -466,15 +469,15 @@ func TestSSH(t *testing.T) {
client.SetLogger(slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug))
first := coderdtest.CreateFirstUser(t, client)
userClient, user := coderdtest.CreateAnotherUser(t, client, first.OrganizationID)
workspace, agentToken := dbfake.WorkspaceWithAgent(t, store, database.Workspace{
r := dbfake.NewWorkspaceBuilder(t, store).Seed(database.Workspace{
OrganizationID: first.OrganizationID,
OwnerID: user.ID,
})
}).WithAgent().Do()

_, _ = tGoContext(t, func(ctx context.Context) {
// Run this async so the SSH command has to wait for
// the build and agent to connect.
_ = agenttest.New(t, client.URL, agentToken)
_ = agenttest.New(t, client.URL, r.AgentToken)
<-ctx.Done()
})

Expand All @@ -489,7 +492,7 @@ func TestSSH(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

inv, root := clitest.New(t, "ssh", "--stdio", workspace.Name)
inv, root := clitest.New(t, "ssh", "--stdio", r.Workspace.Name)
clitest.SetupConfig(t, userClient, root)
inv.Stdin = clientOutput
inv.Stdout = serverInput
Expand Down Expand Up @@ -520,7 +523,7 @@ func TestSSH(t *testing.T) {
err = session.Shell()
require.NoError(t, err)

_ = dbfake.NewWorkspaceBuildBuilder(t, store, workspace).
_ = dbfake.NewWorkspaceBuildBuilder(t, store, r.Workspace).
Seed(database.WorkspaceBuild{
Transition: database.WorkspaceTransitionStop,
BuildNumber: 2,
Expand Down
13 changes: 0 additions & 13 deletions coderd/database/dbfake/dbfake.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,6 @@ func Workspace(t testing.TB, db database.Store, seed database.Workspace) databas
return r.Workspace
}

// WorkspaceWithAgent is a helper that generates a workspace with a single resource
// that has an agent attached to it. The agent token is returned.
func WorkspaceWithAgent(
t testing.TB, db database.Store, seed database.Workspace,
mutations ...func([]*sdkproto.Agent) []*sdkproto.Agent,
) (
database.Workspace, string,
) {
t.Helper()
r := NewWorkspaceBuilder(t, db).Seed(seed).WithAgent(mutations...).Do()
return r.Workspace, r.AgentToken
}

type WorkspaceBuildBuilder struct {
t testing.TB
db database.Store
Expand Down
Loading