istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tools/bug-report/pkg/processlog/processlog_test.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package processlog
    16  
    17  import (
    18  	"path/filepath"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/google/go-cmp/cmp"
    23  
    24  	"istio.io/istio/pilot/test/util"
    25  	"istio.io/istio/pkg/test/env"
    26  	"istio.io/istio/tools/bug-report/pkg/config"
    27  )
    28  
    29  func TestProcessLogsFormat(t *testing.T) {
    30  	testDataDir := filepath.Join(env.IstioSrc, "tools/bug-report/pkg/testdata/")
    31  
    32  	tests := []struct {
    33  		name              string
    34  		inputLogFilePath  string
    35  		wantOutputLogPath string
    36  		startTime         string
    37  		endTime           string
    38  		timeFilterApplied bool
    39  	}{
    40  		{
    41  			name:              "input_log_of_text_format",
    42  			inputLogFilePath:  "input/format_txt.log",
    43  			wantOutputLogPath: "output/format_txt_no_time_filter.log",
    44  			timeFilterApplied: false,
    45  		},
    46  		{
    47  			name:              "input_log_of_json_format",
    48  			inputLogFilePath:  "input/format_json.log",
    49  			wantOutputLogPath: "output/format_json_no_time_filter.log",
    50  			timeFilterApplied: false,
    51  		},
    52  		{
    53  			name:              "input_log_of_text_format",
    54  			inputLogFilePath:  "input/format_txt.log",
    55  			wantOutputLogPath: "output/format_txt_with_time_filter.log",
    56  			startTime:         "2020-06-29T23:37:27.336155Z",
    57  			endTime:           "2020-06-29T23:37:27.349559Z",
    58  			timeFilterApplied: true,
    59  		},
    60  		{
    61  			name:              "input_log_of_json_format",
    62  			inputLogFilePath:  "input/format_json.log",
    63  			wantOutputLogPath: "output/format_json_with_time_filter.log",
    64  			startTime:         "2023-05-10T17:43:55.356647Z",
    65  			endTime:           "2023-05-10T17:43:55.356691Z",
    66  			timeFilterApplied: true,
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  			inputLog := string(util.ReadFile(t, filepath.Join(testDataDir, tt.inputLogFilePath)))
    72  			wantOutputLog := string(util.ReadFile(t, filepath.Join(testDataDir, tt.wantOutputLogPath)))
    73  			start, _ := time.Parse(time.RFC3339Nano, tt.startTime)
    74  			end, _ := time.Parse(time.RFC3339Nano, tt.endTime)
    75  			c := config.BugReportConfig{StartTime: start, EndTime: end, TimeFilterApplied: tt.timeFilterApplied}
    76  			gotOutputLog, _ := Process(&c, inputLog)
    77  			if wantOutputLog != gotOutputLog {
    78  				t.Errorf("got:\n%s\nwant:\n%s\n\ndiff (-got, +want):\n%s\n", gotOutputLog, wantOutputLog, cmp.Diff(gotOutputLog, wantOutputLog))
    79  			}
    80  		})
    81  	}
    82  }