|
| 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 | +} |
0 commit comments