2022-05-10 20:57:07 +00:00
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
2023-02-08 17:09:38 +00:00
|
|
|
"bytes"
|
2022-05-23 17:09:58 +00:00
|
|
|
"context"
|
2023-02-08 17:09:38 +00:00
|
|
|
"encoding/json"
|
2022-05-10 20:57:07 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-06-16 16:01:33 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-08 17:09:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-05-10 20:57:07 +00:00
|
|
|
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/cli/clitest"
|
|
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
2023-11-03 17:22:32 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
|
|
"github.com/coder/coder/v2/coderd/database/dbfake"
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
|
|
"github.com/coder/coder/v2/pty/ptytest"
|
|
|
|
"github.com/coder/coder/v2/testutil"
|
2022-05-10 20:57:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestList(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
t.Run("Single", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-11-03 17:22:32 +00:00
|
|
|
client, db := coderdtest.NewWithDatabase(t, nil)
|
2023-10-10 10:14:20 +00:00
|
|
|
owner := coderdtest.CreateFirstUser(t, client)
|
2023-11-03 17:22:32 +00:00
|
|
|
member, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
2023-12-01 01:33:04 +00:00
|
|
|
// setup template
|
|
|
|
r := dbfake.WorkspaceBuild(t, db, database.Workspace{
|
2023-11-03 17:22:32 +00:00
|
|
|
OrganizationID: owner.OrganizationID,
|
|
|
|
OwnerID: memberUser.ID,
|
2023-11-27 10:30:15 +00:00
|
|
|
}).WithAgent().Do()
|
2023-12-01 01:33:04 +00:00
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
inv, root := clitest.New(t, "ls")
|
2023-10-10 10:14:20 +00:00
|
|
|
clitest.SetupConfig(t, member, root)
|
2023-03-23 22:42:20 +00:00
|
|
|
pty := ptytest.New(t).Attach(inv)
|
2022-07-22 14:42:09 +00:00
|
|
|
|
2022-08-01 12:45:05 +00:00
|
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
|
2022-07-22 14:42:09 +00:00
|
|
|
defer cancelFunc()
|
2022-06-16 16:01:33 +00:00
|
|
|
done := make(chan any)
|
2022-05-10 20:57:07 +00:00
|
|
|
go func() {
|
2023-03-23 22:42:20 +00:00
|
|
|
errC := inv.WithContext(ctx).Run()
|
2022-06-16 16:01:33 +00:00
|
|
|
assert.NoError(t, errC)
|
|
|
|
close(done)
|
2022-05-10 20:57:07 +00:00
|
|
|
}()
|
2023-11-27 10:30:15 +00:00
|
|
|
pty.ExpectMatch(r.Workspace.Name)
|
2022-07-01 14:40:03 +00:00
|
|
|
pty.ExpectMatch("Started")
|
2022-05-23 17:09:58 +00:00
|
|
|
cancelFunc()
|
2022-06-16 16:01:33 +00:00
|
|
|
<-done
|
2022-05-10 20:57:07 +00:00
|
|
|
})
|
2023-02-08 17:09:38 +00:00
|
|
|
|
|
|
|
t.Run("JSON", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-11-03 17:22:32 +00:00
|
|
|
client, db := coderdtest.NewWithDatabase(t, nil)
|
2023-10-10 10:14:20 +00:00
|
|
|
owner := coderdtest.CreateFirstUser(t, client)
|
2023-11-03 17:22:32 +00:00
|
|
|
member, memberUser := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
|
2023-12-01 01:33:04 +00:00
|
|
|
_ = dbfake.WorkspaceBuild(t, db, database.Workspace{
|
2023-11-03 17:22:32 +00:00
|
|
|
OrganizationID: owner.OrganizationID,
|
|
|
|
OwnerID: memberUser.ID,
|
2023-11-27 10:30:15 +00:00
|
|
|
}).WithAgent().Do()
|
2023-02-08 17:09:38 +00:00
|
|
|
|
2023-03-23 22:42:20 +00:00
|
|
|
inv, root := clitest.New(t, "list", "--output=json")
|
2023-10-10 10:14:20 +00:00
|
|
|
clitest.SetupConfig(t, member, root)
|
2023-02-08 17:09:38 +00:00
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithTimeout(context.Background(), testutil.WaitLong)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
|
|
|
out := bytes.NewBuffer(nil)
|
2023-03-23 22:42:20 +00:00
|
|
|
inv.Stdout = out
|
|
|
|
err := inv.WithContext(ctx).Run()
|
2023-02-08 17:09:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-11-03 17:22:32 +00:00
|
|
|
var workspaces []codersdk.Workspace
|
|
|
|
require.NoError(t, json.Unmarshal(out.Bytes(), &workspaces))
|
|
|
|
require.Len(t, workspaces, 1)
|
2023-02-08 17:09:38 +00:00
|
|
|
})
|
2022-05-10 20:57:07 +00:00
|
|
|
}
|