|
| 1 | +package healthsdk_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + |
| 8 | + "github.com/coder/coder/v2/coderd/healthcheck/health" |
| 9 | + "github.com/coder/coder/v2/coderd/util/ptr" |
| 10 | + "github.com/coder/coder/v2/codersdk/healthsdk" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSummarize(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + t.Run("HealthcheckReport", func(t *testing.T) { |
| 17 | + unhealthy := healthsdk.BaseReport{ |
| 18 | + Error: ptr.Ref("test error"), |
| 19 | + Warnings: []health.Message{{Code: "TEST", Message: "testing"}}, |
| 20 | + } |
| 21 | + hr := healthsdk.HealthcheckReport{ |
| 22 | + AccessURL: healthsdk.AccessURLReport{ |
| 23 | + BaseReport: unhealthy, |
| 24 | + }, |
| 25 | + Database: healthsdk.DatabaseReport{ |
| 26 | + BaseReport: unhealthy, |
| 27 | + }, |
| 28 | + DERP: healthsdk.DERPHealthReport{ |
| 29 | + BaseReport: unhealthy, |
| 30 | + }, |
| 31 | + ProvisionerDaemons: healthsdk.ProvisionerDaemonsReport{ |
| 32 | + BaseReport: unhealthy, |
| 33 | + }, |
| 34 | + Websocket: healthsdk.WebsocketReport{ |
| 35 | + BaseReport: unhealthy, |
| 36 | + }, |
| 37 | + WorkspaceProxy: healthsdk.WorkspaceProxyReport{ |
| 38 | + BaseReport: unhealthy, |
| 39 | + }, |
| 40 | + } |
| 41 | + expected := []string{ |
| 42 | + "Access URL: Error: test error", |
| 43 | + "Access URL: Warn: TEST: testing", |
| 44 | + "Database: Error: test error", |
| 45 | + "Database: Warn: TEST: testing", |
| 46 | + "DERP: Error: test error", |
| 47 | + "DERP: Warn: TEST: testing", |
| 48 | + "Provisioner Daemons: Error: test error", |
| 49 | + "Provisioner Daemons: Warn: TEST: testing", |
| 50 | + "Websocket: Error: test error", |
| 51 | + "Websocket: Warn: TEST: testing", |
| 52 | + "Workspace Proxies: Error: test error", |
| 53 | + "Workspace Proxies: Warn: TEST: testing", |
| 54 | + } |
| 55 | + actual := hr.Summarize() |
| 56 | + assert.Equal(t, expected, actual) |
| 57 | + }) |
| 58 | + |
| 59 | + for _, tt := range []struct { |
| 60 | + name string |
| 61 | + br healthsdk.BaseReport |
| 62 | + pfx string |
| 63 | + expected []string |
| 64 | + }{ |
| 65 | + { |
| 66 | + name: "empty", |
| 67 | + br: healthsdk.BaseReport{}, |
| 68 | + pfx: "", |
| 69 | + expected: []string{}, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "no prefix", |
| 73 | + br: healthsdk.BaseReport{ |
| 74 | + Error: ptr.Ref("testing"), |
| 75 | + Warnings: []health.Message{ |
| 76 | + { |
| 77 | + Code: "TEST01", |
| 78 | + Message: "testing one", |
| 79 | + }, |
| 80 | + { |
| 81 | + Code: "TEST02", |
| 82 | + Message: "testing two", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + pfx: "", |
| 87 | + expected: []string{ |
| 88 | + "Error: testing", |
| 89 | + "Warn: TEST01: testing one", |
| 90 | + "Warn: TEST02: testing two", |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "prefix", |
| 95 | + br: healthsdk.BaseReport{ |
| 96 | + Error: ptr.Ref("testing"), |
| 97 | + Warnings: []health.Message{ |
| 98 | + { |
| 99 | + Code: "TEST01", |
| 100 | + Message: "testing one", |
| 101 | + }, |
| 102 | + { |
| 103 | + Code: "TEST02", |
| 104 | + Message: "testing two", |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + pfx: "TEST:", |
| 109 | + expected: []string{ |
| 110 | + "TEST: Error: testing", |
| 111 | + "TEST: Warn: TEST01: testing one", |
| 112 | + "TEST: Warn: TEST02: testing two", |
| 113 | + }, |
| 114 | + }, |
| 115 | + } { |
| 116 | + tt := tt |
| 117 | + t.Run(tt.name, func(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + actual := tt.br.Summarize(tt.pfx) |
| 120 | + if len(tt.expected) == 0 { |
| 121 | + assert.Empty(t, actual) |
| 122 | + return |
| 123 | + } |
| 124 | + assert.Equal(t, tt.expected, actual) |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
0 commit comments