|
1 | 1 | package prometheusmetrics_test
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 | 4 | "context"
|
6 | 5 | "database/sql"
|
7 | 6 | "encoding/json"
|
8 | 7 | "fmt"
|
9 | 8 | "os"
|
| 9 | + "reflect" |
10 | 10 | "sync/atomic"
|
11 | 11 | "testing"
|
12 | 12 | "time"
|
@@ -417,14 +417,16 @@ func TestAgentStats(t *testing.T) {
|
417 | 417 | IgnoreErrors: true,
|
418 | 418 | }), registry, db, time.Now().Add(-time.Minute), time.Millisecond)
|
419 | 419 | require.NoError(t, err)
|
| 420 | + t.Cleanup(closeFunc) |
420 | 421 |
|
421 | 422 | // then
|
422 | 423 | goldenFile, err := os.ReadFile("testdata/agent-stats.json")
|
423 | 424 | require.NoError(t, err)
|
424 |
| - goldenFile = bytes.TrimSpace(goldenFile) |
| 425 | + golden := map[string]int{} |
| 426 | + err = json.Unmarshal(goldenFile, &golden) |
| 427 | + require.NoError(t, err) |
425 | 428 |
|
426 | 429 | collected := map[string]int{}
|
427 |
| - var out []byte |
428 | 430 | var executionSeconds bool
|
429 | 431 | assert.Eventually(t, func() bool {
|
430 | 432 | metrics, err := registry.Gather()
|
@@ -454,18 +456,11 @@ func TestAgentStats(t *testing.T) {
|
454 | 456 | require.FailNowf(t, "unexpected metric collected", "metric: %s", metric.GetName())
|
455 | 457 | }
|
456 | 458 | }
|
457 |
| - |
458 |
| - out, err = json.MarshalIndent(collected, "", " ") |
459 |
| - require.NoError(t, err) |
460 |
| - out = bytes.ReplaceAll(out, []byte{'\r', '\n'}, []byte{'\n'}) // comparison fix for Windows |
461 |
| - |
462 |
| - return executionSeconds && string(goldenFile) == string(out) |
| 459 | + return executionSeconds && reflect.DeepEqual(golden, collected) |
463 | 460 | }, testutil.WaitShort, testutil.IntervalFast)
|
464 | 461 |
|
465 | 462 | // Keep this assertion, so that "go test" can print differences instead of "Condition never satisfied"
|
466 |
| - assert.Equal(t, string(goldenFile), string(out)) |
467 |
| - |
468 |
| - closeFunc() |
| 463 | + assert.EqualValues(t, golden, collected) |
469 | 464 | }
|
470 | 465 |
|
471 | 466 | func prepareWorkspaceAndAgent(t *testing.T, client *codersdk.Client, user codersdk.CreateFirstUserResponse, workspaceNum int) *agentsdk.Client {
|
|
0 commit comments