Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 60ac4a4

Browse files
authored
chore: add golden file tests for resources top (#301)
1 parent 0bee6d1 commit 60ac4a4

File tree

4 files changed

+237
-15
lines changed

4 files changed

+237
-15
lines changed

internal/cmd/envs_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func assertEnv(t *testing.T, name string, envs []coder.Environment) *coder.Envir
134134

135135
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
136136

137+
//nolint:unparam
137138
func randString(length int) string {
138139
const charset = "abcdefghijklmnopqrstuvwxyz"
139140
b := make([]byte, length)

internal/cmd/resourcemanager.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -99,29 +99,34 @@ func runResourceTop(options *resourceTopOptions) func(cmd *cobra.Command, args [
9999
if err != nil {
100100
return xerrors.Errorf("get workspace providers: %w", err)
101101
}
102-
103-
var groups []groupable
104-
var labeler envLabeler
105102
data := entities{
106103
providers: providers.Kubernetes,
107104
users: users,
108105
orgs: orgs,
109106
envs: envs,
110107
images: images,
111108
}
112-
switch options.group {
113-
case "user":
114-
groups, labeler = aggregateByUser(data, *options)
115-
case "org":
116-
groups, labeler = aggregateByOrg(data, *options)
117-
case "provider":
118-
groups, labeler = aggregateByProvider(data, *options)
119-
default:
120-
return xerrors.Errorf("unknown --group %q", options.group)
121-
}
109+
return presentEntites(cmd.OutOrStdout(), data, *options)
110+
}
111+
}
122112

123-
return printResourceTop(cmd.OutOrStdout(), groups, labeler, options.showEmptyGroups, options.sortBy)
113+
func presentEntites(w io.Writer, data entities, options resourceTopOptions) error {
114+
var (
115+
groups []groupable
116+
labeler envLabeler
117+
)
118+
switch options.group {
119+
case "user":
120+
groups, labeler = aggregateByUser(data, options)
121+
case "org":
122+
groups, labeler = aggregateByOrg(data, options)
123+
case "provider":
124+
groups, labeler = aggregateByProvider(data, options)
125+
default:
126+
return xerrors.Errorf("unknown --group %q", options.group)
124127
}
128+
129+
return printResourceTop(w, groups, labeler, options.showEmptyGroups, options.sortBy)
125130
}
126131

127132
type entities struct {
@@ -241,7 +246,7 @@ func (o orgGrouping) environments() []coder.Environment {
241246

242247
func (o orgGrouping) header() string {
243248
plural := "s"
244-
if len(o.org.Members) < 2 {
249+
if len(o.org.Members) == 1 {
245250
plural = ""
246251
}
247252
return fmt.Sprintf("%s\t(%v member%s)", truncate(o.org.Name, 20, "..."), len(o.org.Members), plural)

internal/cmd/resourcemanager_test.go

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"fmt"
7+
"io/ioutil"
8+
"testing"
9+
10+
"cdr.dev/slog/sloggers/slogtest/assert"
11+
12+
"cdr.dev/coder-cli/coder-sdk"
13+
)
14+
15+
var write = flag.Bool("write", false, "write to the golden files")
16+
17+
func Test_resourceManager(t *testing.T) {
18+
// TODO: cleanup
19+
verbose = true
20+
21+
const goldenFile = "resourcemanager_test.golden"
22+
var buff bytes.Buffer
23+
data := mockResourceTopEntities()
24+
tests := []struct {
25+
header string
26+
data entities
27+
options resourceTopOptions
28+
}{
29+
{
30+
header: "By User",
31+
data: data,
32+
options: resourceTopOptions{
33+
group: "user",
34+
sortBy: "cpu",
35+
},
36+
},
37+
{
38+
header: "By Org",
39+
data: data,
40+
options: resourceTopOptions{
41+
group: "org",
42+
sortBy: "cpu",
43+
},
44+
},
45+
{
46+
header: "By Provider",
47+
data: data,
48+
options: resourceTopOptions{
49+
group: "provider",
50+
sortBy: "cpu",
51+
},
52+
},
53+
{
54+
header: "Sort By Memory",
55+
data: data,
56+
options: resourceTopOptions{
57+
group: "user",
58+
sortBy: "memory",
59+
},
60+
},
61+
}
62+
63+
for _, tcase := range tests {
64+
buff.WriteString(fmt.Sprintf("=== TEST: %s\n", tcase.header))
65+
err := presentEntites(&buff, tcase.data, tcase.options)
66+
assert.Success(t, "present entities", err)
67+
}
68+
69+
assertGolden(t, goldenFile, buff.Bytes())
70+
}
71+
72+
func assertGolden(t *testing.T, path string, output []byte) {
73+
if *write {
74+
err := ioutil.WriteFile(path, output, 0777)
75+
assert.Success(t, "write file", err)
76+
return
77+
}
78+
goldenContent, err := ioutil.ReadFile(path)
79+
assert.Success(t, "read golden file", err)
80+
assert.Equal(t, "golden content matches", string(goldenContent), string(output))
81+
}
82+
83+
func mockResourceTopEntities() entities {
84+
orgIDs := [...]string{randString(10), randString(10), randString(10)}
85+
imageIDs := [...]string{randString(10), randString(10), randString(10)}
86+
providerIDs := [...]string{randString(10), randString(10), randString(10)}
87+
userIDs := [...]string{randString(10), randString(10), randString(10)}
88+
envIDs := [...]string{randString(10), randString(10), randString(10), randString(10)}
89+
90+
return entities{
91+
providers: []coder.KubernetesProvider{
92+
{
93+
ID: providerIDs[0],
94+
Name: "mars",
95+
},
96+
{
97+
ID: providerIDs[1],
98+
Name: "underground",
99+
},
100+
},
101+
users: []coder.User{
102+
{
103+
ID: userIDs[0],
104+
Name: "Random",
105+
Email: "random@coder.com",
106+
},
107+
{
108+
ID: userIDs[1],
109+
Name: "Second Random",
110+
Email: "second-random@coder.com",
111+
},
112+
},
113+
orgs: []coder.Organization{
114+
{
115+
ID: orgIDs[0],
116+
Name: "SpecialOrg",
117+
118+
//! these should probably be fixed, but for now they are just for the count
119+
Members: []coder.OrganizationUser{{}, {}},
120+
},
121+
{
122+
ID: orgIDs[1],
123+
Name: "NotSoSpecialOrg",
124+
125+
//! these should probably be fixed, but for now they are just for the count
126+
Members: []coder.OrganizationUser{{}, {}},
127+
},
128+
},
129+
envs: []coder.Environment{
130+
{
131+
ID: envIDs[0],
132+
ResourcePoolID: providerIDs[0],
133+
ImageID: imageIDs[0],
134+
OrganizationID: orgIDs[0],
135+
UserID: userIDs[0],
136+
Name: "dev-env",
137+
ImageTag: "20.04",
138+
CPUCores: 12.2,
139+
MemoryGB: 64.4,
140+
LatestStat: coder.EnvironmentStat{
141+
ContainerStatus: coder.EnvironmentOn,
142+
},
143+
},
144+
{
145+
ID: envIDs[1],
146+
ResourcePoolID: providerIDs[1],
147+
ImageID: imageIDs[1],
148+
OrganizationID: orgIDs[1],
149+
UserID: userIDs[1],
150+
Name: "another-env",
151+
ImageTag: "10.2",
152+
CPUCores: 4,
153+
MemoryGB: 16,
154+
LatestStat: coder.EnvironmentStat{
155+
ContainerStatus: coder.EnvironmentOn,
156+
},
157+
},
158+
{
159+
ID: envIDs[2],
160+
ResourcePoolID: providerIDs[1],
161+
ImageID: imageIDs[1],
162+
OrganizationID: orgIDs[1],
163+
UserID: userIDs[1],
164+
Name: "yet-another-env",
165+
ImageTag: "10.2",
166+
CPUCores: 100,
167+
MemoryGB: 2,
168+
LatestStat: coder.EnvironmentStat{
169+
ContainerStatus: coder.EnvironmentOn,
170+
},
171+
},
172+
},
173+
images: map[string]*coder.Image{
174+
imageIDs[0]: {
175+
Repository: "ubuntu",
176+
OrganizationID: orgIDs[0],
177+
},
178+
imageIDs[1]: {
179+
Repository: "archlinux",
180+
OrganizationID: orgIDs[0],
181+
},
182+
},
183+
}
184+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== TEST: By User
2+
Second Random (second-random@coder.com) [cpu: 104.0] [mem: 18.0 GB]
3+
yet-another-env [cpu: 100.0] [mem: 2.0 GB] [img: archlinux:10.2] [provider: underground] [org: NotSoSpecialOrg]
4+
another-env [cpu: 4.0] [mem: 16.0 GB] [img: archlinux:10.2] [provider: underground] [org: NotSoSpecialOrg]
5+
6+
Random (random@coder.com) [cpu: 12.2] [mem: 64.4 GB]
7+
dev-env [cpu: 12.2] [mem: 64.4 GB] [img: ubuntu:20.04] [provider: mars] [org: SpecialOrg]
8+
9+
=== TEST: By Org
10+
NotSoSpecialOrg (2 members) [cpu: 104.0] [mem: 18.0 GB]
11+
yet-another-env [cpu: 100.0] [mem: 2.0 GB] [img: archlinux:10.2] [user: second-random@coder.com] [provider: underground]
12+
another-env [cpu: 4.0] [mem: 16.0 GB] [img: archlinux:10.2] [user: second-random@coder.com] [provider: underground]
13+
14+
SpecialOrg (2 members) [cpu: 12.2] [mem: 64.4 GB]
15+
dev-env [cpu: 12.2] [mem: 64.4 GB] [img: ubuntu:20.04] [user: random@coder.com] [provider: mars]
16+
17+
=== TEST: By Provider
18+
underground [cpu: 104.0] [mem: 18.0 GB]
19+
yet-another-env [cpu: 100.0] [mem: 2.0 GB] [img: archlinux:10.2] [user: second-random@coder.com]
20+
another-env [cpu: 4.0] [mem: 16.0 GB] [img: archlinux:10.2] [user: second-random@coder.com]
21+
22+
mars [cpu: 12.2] [mem: 64.4 GB]
23+
dev-env [cpu: 12.2] [mem: 64.4 GB] [img: ubuntu:20.04] [user: random@coder.com]
24+
25+
=== TEST: Sort By Memory
26+
Random (random@coder.com) [cpu: 12.2] [mem: 64.4 GB]
27+
dev-env [cpu: 12.2] [mem: 64.4 GB] [img: ubuntu:20.04] [provider: mars] [org: SpecialOrg]
28+
29+
Second Random (second-random@coder.com) [cpu: 104.0] [mem: 18.0 GB]
30+
another-env [cpu: 4.0] [mem: 16.0 GB] [img: archlinux:10.2] [provider: underground] [org: NotSoSpecialOrg]
31+
yet-another-env [cpu: 100.0] [mem: 2.0 GB] [img: archlinux:10.2] [provider: underground] [org: NotSoSpecialOrg]
32+

0 commit comments

Comments
 (0)