| 1 | package apperr |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/applog" |
| 7 | ) |
| 8 | |
| 9 | func TestIsAvailability(t *testing.T) { |
| 10 | for _, c := range []struct { |
| 11 | r Record |
| 12 | want bool |
| 13 | }{ |
| 14 | {Record{Source: SourceEdge, Kind: KindUnavailable, Status: 502}, true}, |
| 15 | {Record{Source: SourceEdge, Kind: KindHTTP, Status: 503}, true}, // written before the amendment |
| 16 | {Record{Source: SourceEdge, Kind: KindHTTP, Status: 504}, true}, |
| 17 | {Record{Source: SourceEdge, Kind: KindHTTP, Status: 500}, false}, // the app's own failure |
| 18 | {Record{Source: SourceEdge, Kind: KindHTTP, Status: 505}, false}, |
| 19 | {Record{Source: SourceLog, Stream: applog.StreamPlatform, Level: "error"}, true}, |
| 20 | {Record{Source: SourceLog, Stream: applog.StreamApp, Level: "error"}, false}, |
| 21 | {Record{Source: SourceServer, Status: 503}, false}, |
| 22 | } { |
| 23 | if got := IsAvailability(c.r); got != c.want { |
| 24 | t.Errorf("IsAvailability(%+v) = %v, want %v", c.r, got, c.want) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // TestAsAvailabilityRegroupsHistory: records the edge filed before the |
| 30 | // amendment, one per path, collapse into one row per status. |
| 31 | func TestAsAvailabilityRegroupsHistory(t *testing.T) { |
| 32 | a := AsAvailability(Record{Source: SourceEdge, Kind: KindHTTP, Status: 502, Name: "502 GET /.env", Path: "/.env", FP: "old1"}) |
| 33 | b := AsAvailability(Record{Source: SourceEdge, Kind: KindHTTP, Status: 502, Name: "502 GET /phpinfo.php", Path: "/phpinfo.php", FP: "old2"}) |
| 34 | c := AsAvailability(Record{Source: SourceEdge, Kind: KindHTTP, Status: 503, Name: "503 GET /.env", Path: "/.env", FP: "old3"}) |
| 35 | if a.FP != b.FP || a.FP == c.FP { |
| 36 | t.Fatalf("fps = %s %s %s: want one per status", a.FP, b.FP, c.FP) |
| 37 | } |
| 38 | if a.Name != "502 · cause not recorded" || a.Kind != KindUnavailable || a.Path != "/.env" { |
| 39 | t.Fatalf("regrouped = %+v", a) |
| 40 | } |
| 41 | // A new record is already in shape; a 500 is not Availability at all. |
| 42 | fresh := Record{Source: SourceEdge, Kind: KindUnavailable, Status: 502, Cause: CauseRefused, Name: AvailabilityName(502, CauseRefused), FP: "keep"} |
| 43 | if got := AsAvailability(fresh); got.FP != "keep" { |
| 44 | t.Fatalf("a new record was regrouped: %+v", got) |
| 45 | } |
| 46 | if got := AsAvailability(Record{Source: SourceEdge, Kind: KindHTTP, Status: 500, FP: "keep"}); got.FP != "keep" { |
| 47 | t.Fatalf("a 500 was regrouped: %+v", got) |
| 48 | } |
| 49 | if got := AvailabilityName(503, "martian"); got != "503 · martian" { |
| 50 | t.Fatalf("an unknown cause renders as itself, got %q", got) |
| 51 | } |
| 52 | } |
| 53 | |