|
| 1 | +package diagnostic_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/rs/zerolog" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + |
| 16 | + "github.com/cloudflare/cloudflared/diagnostic" |
| 17 | +) |
| 18 | + |
| 19 | +type SystemCollectorMock struct{} |
| 20 | + |
| 21 | +const ( |
| 22 | + systemInformationKey = "sikey" |
| 23 | + rawInformationKey = "rikey" |
| 24 | + errorKey = "errkey" |
| 25 | +) |
| 26 | + |
| 27 | +func setCtxValuesForSystemCollector( |
| 28 | + systemInfo *diagnostic.SystemInformation, |
| 29 | + rawInfo string, |
| 30 | + err error, |
| 31 | +) context.Context { |
| 32 | + ctx := context.Background() |
| 33 | + ctx = context.WithValue(ctx, systemInformationKey, systemInfo) |
| 34 | + ctx = context.WithValue(ctx, rawInformationKey, rawInfo) |
| 35 | + ctx = context.WithValue(ctx, errorKey, err) |
| 36 | + |
| 37 | + return ctx |
| 38 | +} |
| 39 | + |
| 40 | +func (*SystemCollectorMock) Collect(ctx context.Context) (*diagnostic.SystemInformation, string, error) { |
| 41 | + si, _ := ctx.Value(systemInformationKey).(*diagnostic.SystemInformation) |
| 42 | + ri, _ := ctx.Value(rawInformationKey).(string) |
| 43 | + err, _ := ctx.Value(errorKey).(error) |
| 44 | + |
| 45 | + return si, ri, err |
| 46 | +} |
| 47 | + |
| 48 | +func TestSystemHandler(t *testing.T) { |
| 49 | + t.Parallel() |
| 50 | + |
| 51 | + log := zerolog.Nop() |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + systemInfo *diagnostic.SystemInformation |
| 55 | + rawInfo string |
| 56 | + err error |
| 57 | + statusCode int |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "happy path", |
| 61 | + systemInfo: diagnostic.NewSystemInformation( |
| 62 | + 0, 0, 0, 0, |
| 63 | + "string", "string", "string", "string", |
| 64 | + "string", "string", nil, |
| 65 | + ), |
| 66 | + rawInfo: "", |
| 67 | + err: nil, |
| 68 | + statusCode: http.StatusOK, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "on error and raw info", systemInfo: nil, |
| 72 | + rawInfo: "raw info", err: errors.New("an error"), statusCode: http.StatusOK, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "on error and no raw info", systemInfo: nil, |
| 76 | + rawInfo: "", err: errors.New("an error"), statusCode: http.StatusInternalServerError, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "malformed response", systemInfo: nil, rawInfo: "", err: nil, statusCode: http.StatusInternalServerError, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tCase := range tests { |
| 84 | + t.Run(tCase.name, func(t *testing.T) { |
| 85 | + t.Parallel() |
| 86 | + handler := diagnostic.NewDiagnosticHandler(&log, 0, &SystemCollectorMock{}) |
| 87 | + recorder := httptest.NewRecorder() |
| 88 | + ctx := setCtxValuesForSystemCollector(tCase.systemInfo, tCase.rawInfo, tCase.err) |
| 89 | + request, err := http.NewRequestWithContext(ctx, http.MethodGet, "/diag/syste,", nil) |
| 90 | + require.NoError(t, err) |
| 91 | + handler.SystemHandler(recorder, request) |
| 92 | + |
| 93 | + assert.Equal(t, tCase.statusCode, recorder.Code) |
| 94 | + if tCase.statusCode == http.StatusOK && tCase.systemInfo != nil { |
| 95 | + var response diagnostic.SystemInformation |
| 96 | + |
| 97 | + decoder := json.NewDecoder(recorder.Body) |
| 98 | + err = decoder.Decode(&response) |
| 99 | + require.NoError(t, err) |
| 100 | + assert.Equal(t, tCase.systemInfo, &response) |
| 101 | + } else if tCase.statusCode == http.StatusOK && tCase.rawInfo != "" { |
| 102 | + rawBytes, err := io.ReadAll(recorder.Body) |
| 103 | + require.NoError(t, err) |
| 104 | + assert.Equal(t, tCase.rawInfo, string(rawBytes)) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments