|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func TestLogFileReader_Read(t *testing.T) { |
| 12 | + var pathRules []PathTransformingRule |
| 13 | + targetPaths := []string{"/"} |
| 14 | + logFileReader := NewLogFileReader(pathRules, targetPaths) |
| 15 | + |
| 16 | + logTimeString := "2022-06-13T00:26:00.071316Z" |
| 17 | + logTime, err := time.Parse(time.RFC3339, "2022-06-13T00:26:00.071316Z") |
| 18 | + if err != nil { |
| 19 | + fmt.Printf("failed to parse %s: %s", logTimeString, err.Error()) |
| 20 | + } |
| 21 | + |
| 22 | + cases := []struct { |
| 23 | + log string |
| 24 | + wantTargetProcessingTimesMap map[Timestamp]TargetProcessingTimes |
| 25 | + wantTargetRequestCountMap map[Timestamp]RequestCount |
| 26 | + }{ |
| 27 | + { |
| 28 | + log: fmt.Sprintf(`https %s app/my-loadbalancer/50dc6c495c0c9188 192.168.131.39:2817 10.0.0.1:80 0.086 0.048 0.037 200 200 0 57 "GET https://www.example.com:443/ HTTP/1.1" "curl/7.46.0" ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 "Root=1-58337281-1d84f3d73c47ec4e58577259" "www.example.com" "arn:aws:acm:us-east-2:123456789012:certificate/12345678-1234-1234-1234-123456789012" 1 2018-07-02T22:22:48.364000Z "authenticate,forward" "-" "-" "10.0.0.1:80" "200" "-" "-"`, logTimeString), |
| 29 | + wantTargetProcessingTimesMap: map[Timestamp]TargetProcessingTimes{ |
| 30 | + Timestamp(logTime.Unix()): {0.048}, |
| 31 | + }, |
| 32 | + wantTargetRequestCountMap: map[Timestamp]RequestCount{ |
| 33 | + Timestamp(logTime.Unix()): 1, |
| 34 | + }, |
| 35 | + }, |
| 36 | + { |
| 37 | + log: fmt.Sprintf(`https %s app/my-loadbalancer/50dc6c495c0c9188 192.168.131.39:2817 - -1 -1 -1 400 - 235 772 "GET https://www.example.com:443/ HTTP/1.1" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" - - - "-" "-" "-" - 2022-06-13T00:25:59.856000Z "-" "-" "-" "-" "-" "-" "-"`, logTimeString), |
| 38 | + wantTargetProcessingTimesMap: map[Timestamp]TargetProcessingTimes{ |
| 39 | + Timestamp(logTime.Unix()): {-1}, |
| 40 | + }, |
| 41 | + wantTargetRequestCountMap: map[Timestamp]RequestCount{ |
| 42 | + Timestamp(logTime.Unix()): 1, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + for _, tt := range cases { |
| 47 | + metricMap, err := logFileReader.Read( |
| 48 | + strings.NewReader(tt.log), |
| 49 | + ) |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("failed to read the log: %v\n%s", err, tt.log) |
| 52 | + } |
| 53 | + if len(metricMap) != 1 { |
| 54 | + t.Errorf("expected 1 metric, got %d", len(metricMap)) |
| 55 | + } |
| 56 | + |
| 57 | + for _, metric := range metricMap { |
| 58 | + if reflect.DeepEqual(metric.TargetProcessingTimesMap, tt.wantTargetProcessingTimesMap) == false { |
| 59 | + t.Errorf("unexpected got %v, want, %v", metric.TargetProcessingTimesMap, tt.wantTargetProcessingTimesMap) |
| 60 | + } |
| 61 | + if reflect.DeepEqual(metric.RequestCountMap, tt.wantTargetRequestCountMap) == false { |
| 62 | + t.Errorf("unexpected got %v, want, %v", metric.RequestCountMap, tt.wantTargetRequestCountMap) |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments