github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/spyglass/lenses/buildlog/lens_test.go (about)

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package buildlog
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestGroupLines(t *testing.T) {
    24  	lorem := []string{
    25  		"Lorem ipsum dolor sit amet",
    26  		"consectetur adipiscing elit",
    27  		"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
    28  		"Ut enim ad minim veniam",
    29  		"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat",
    30  		"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur",
    31  		"Excepteur sint occaecat cupidatat non proident",
    32  		"sunt in culpa qui officia deserunt mollit anim id est laborum",
    33  	}
    34  	tests := []struct {
    35  		name   string
    36  		lines  []string
    37  		groups []LineGroup
    38  	}{
    39  		{
    40  			name:   "Test empty log",
    41  			lines:  []string{},
    42  			groups: []LineGroup{},
    43  		},
    44  		{
    45  			name:  "Test error highlighting",
    46  			lines: []string{"This is an ErRoR message"},
    47  			groups: []LineGroup{
    48  				{
    49  					Start:      0,
    50  					End:        1,
    51  					Skip:       false,
    52  					ByteOffset: 0,
    53  					ByteLength: 24,
    54  				},
    55  			},
    56  		},
    57  		{
    58  			name:  "Test skip all",
    59  			lines: lorem,
    60  			groups: []LineGroup{
    61  				{
    62  					Start:      0,
    63  					End:        8,
    64  					Skip:       true,
    65  					ByteOffset: 0,
    66  					ByteLength: 437,
    67  				},
    68  			},
    69  		},
    70  		{
    71  			name: "Test skip none",
    72  			lines: []string{
    73  				"a", "b", "c", "d", "e",
    74  				"Failed to immanentize the eschaton.",
    75  				"a", "b", "c", "d", "e",
    76  			},
    77  			groups: []LineGroup{
    78  				{
    79  					Start:      0,
    80  					End:        11,
    81  					Skip:       false,
    82  					ByteOffset: 0,
    83  					ByteLength: 55,
    84  				},
    85  			},
    86  		},
    87  		{
    88  			name: "Test skip threshold",
    89  			lines: []string{
    90  				"a", "b", "c", "d", // skip threshold unmet
    91  				"a", "b", "c", "d", "e", "Failed to immanentize the eschaton.", "a", "b", "c", "d", "e",
    92  				"a", "b", "c", "d", "e", // skip threshold met
    93  			},
    94  			groups: []LineGroup{
    95  				{
    96  					Start:      0,
    97  					End:        4,
    98  					Skip:       false,
    99  					ByteOffset: 0,
   100  					ByteLength: 7,
   101  				},
   102  				{
   103  					Start:      4,
   104  					End:        15,
   105  					Skip:       false,
   106  					ByteOffset: 8,
   107  					ByteLength: 55,
   108  				},
   109  				{
   110  					Start:      15,
   111  					End:        20,
   112  					Skip:       true,
   113  					ByteOffset: 64,
   114  					ByteLength: 9,
   115  				},
   116  			},
   117  		},
   118  		{
   119  			name: "Test nearby errors",
   120  			lines: []string{
   121  				"a", "b", "c",
   122  				"don't panic",
   123  				"a", "b", "c",
   124  				"don't panic",
   125  				"a", "b", "c",
   126  			},
   127  			groups: []LineGroup{
   128  				{
   129  					Start:      0,
   130  					End:        11,
   131  					Skip:       false,
   132  					ByteOffset: 0,
   133  					ByteLength: 41,
   134  				},
   135  			},
   136  		},
   137  		{
   138  			name: "Test separated errors",
   139  			lines: []string{
   140  				"a", "b", "c",
   141  				"don't panic",
   142  				"a", "b", "c", "d", "e",
   143  				"a", "b", "c",
   144  				"a", "b", "c", "d", "e",
   145  				"don't panic",
   146  				"a", "b", "c",
   147  			},
   148  			groups: []LineGroup{
   149  				{
   150  					Start:      0,
   151  					End:        9,
   152  					Skip:       false,
   153  					ByteOffset: 0,
   154  					ByteLength: 27,
   155  				},
   156  				{
   157  					Start:      9,
   158  					End:        12,
   159  					Skip:       false,
   160  					ByteOffset: 28,
   161  					ByteLength: 5,
   162  				},
   163  				{
   164  					Start:      12,
   165  					End:        21,
   166  					Skip:       false,
   167  					ByteOffset: 34,
   168  					ByteLength: 27,
   169  				},
   170  			},
   171  		},
   172  	}
   173  	for _, test := range tests {
   174  		t.Run(test.name, func(t *testing.T) {
   175  			got := groupLines(highlightLines(test.lines, 0))
   176  			if len(got) != len(test.groups) {
   177  				t.Fatalf("Expected %d groups, got %d", len(test.groups), len(got))
   178  			}
   179  			for j, exp := range test.groups {
   180  				if got[j].Start != exp.Start || got[j].End != exp.End {
   181  					t.Fatalf("Group %d expected lines [%d, %d), got [%d, %d)", j, exp.Start, exp.End, got[j].Start, got[j].End)
   182  				}
   183  				if got[j].Skip != exp.Skip {
   184  					t.Errorf("Lines [%d, %d) expected Skip = %t", exp.Start, exp.End, exp.Skip)
   185  				}
   186  				if got[j].ByteOffset != exp.ByteOffset {
   187  					t.Errorf("Group %d expected ByteOffset %d, got %d.", j, exp.ByteOffset, got[j].ByteOffset)
   188  				}
   189  				if got[j].ByteLength != exp.ByteLength {
   190  					t.Errorf("Group %d expected ByteLength %d, got %d.", j, exp.ByteLength, got[j].ByteLength)
   191  				}
   192  			}
   193  		})
   194  	}
   195  }