sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/spyglass/testgrid_test.go (about)

     1  /*
     2  Copyright 2019 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 spyglass
    18  
    19  import (
    20  	"testing"
    21  
    22  	tgconf "github.com/GoogleCloudPlatform/testgrid/pb/config"
    23  )
    24  
    25  func TestFindQuery(t *testing.T) {
    26  	testCases := []struct {
    27  		name       string
    28  		dashboards []*tgconf.Dashboard
    29  		jobName    string
    30  		expected   []string
    31  		expectErr  bool
    32  	}{
    33  		{
    34  			name: "simple lookup",
    35  			dashboards: []*tgconf.Dashboard{
    36  				{
    37  					Name: "dashboard-a",
    38  					DashboardTab: []*tgconf.DashboardTab{
    39  						{
    40  							Name:          "tab-a",
    41  							TestGroupName: "test-group-a",
    42  						},
    43  					},
    44  				},
    45  				{
    46  					Name: "dashboard-b",
    47  					DashboardTab: []*tgconf.DashboardTab{
    48  						{
    49  							Name:          "tab-b",
    50  							TestGroupName: "test-group-b",
    51  						},
    52  						{
    53  							Name:          "tab-c",
    54  							TestGroupName: "test-group-c",
    55  						},
    56  						{
    57  							Name:          "tab-d",
    58  							TestGroupName: "test-group-d",
    59  						},
    60  					},
    61  				},
    62  				{
    63  					Name: "dashboard-c",
    64  					DashboardTab: []*tgconf.DashboardTab{
    65  						{
    66  							Name:          "tab-e",
    67  							TestGroupName: "test-group-e",
    68  						},
    69  					},
    70  				},
    71  			},
    72  			jobName:  "test-group-c",
    73  			expected: []string{"dashboard-b#tab-c"},
    74  		},
    75  		{
    76  			name: "ambiguous dashboard",
    77  			dashboards: []*tgconf.Dashboard{
    78  				{
    79  					Name: "dashboard-a",
    80  					DashboardTab: []*tgconf.DashboardTab{
    81  						{
    82  							Name:          "tab-a",
    83  							TestGroupName: "popular-group",
    84  						},
    85  					},
    86  				},
    87  				{
    88  					Name: "dashboard-b",
    89  					DashboardTab: []*tgconf.DashboardTab{
    90  						{
    91  							Name:          "tab-b",
    92  							TestGroupName: "popular-group",
    93  						},
    94  					},
    95  				},
    96  				{
    97  					Name: "dashboard-c",
    98  					DashboardTab: []*tgconf.DashboardTab{
    99  						{
   100  							Name:          "tab-c",
   101  							TestGroupName: "test-group-c",
   102  						},
   103  					},
   104  				},
   105  			},
   106  			jobName:  "popular-group",
   107  			expected: []string{"dashboard-a#tab-a", "dashboard-b#tab-b"},
   108  		},
   109  		{
   110  			name: "ambiguous tab",
   111  			dashboards: []*tgconf.Dashboard{
   112  				{
   113  					Name: "dashboard-a",
   114  					DashboardTab: []*tgconf.DashboardTab{
   115  						{
   116  							Name:          "tab-a",
   117  							TestGroupName: "popular-group",
   118  						},
   119  						{
   120  							Name:          "tab-b",
   121  							TestGroupName: "popular-group",
   122  						},
   123  					},
   124  				},
   125  				{
   126  					Name: "dashboard-b",
   127  					DashboardTab: []*tgconf.DashboardTab{
   128  						{
   129  							Name:          "tab-c",
   130  							TestGroupName: "test-group-c",
   131  						},
   132  					},
   133  				},
   134  			},
   135  			jobName:  "popular-group",
   136  			expected: []string{"dashboard-a#tab-a", "dashboard-a#tab-b"},
   137  		},
   138  		{
   139  			name: "dashboard with more tabs is preferred",
   140  			dashboards: []*tgconf.Dashboard{
   141  				{
   142  					Name: "dashboard-a",
   143  					DashboardTab: []*tgconf.DashboardTab{
   144  						{
   145  							Name:          "tab",
   146  							TestGroupName: "different-group",
   147  						},
   148  						{
   149  							Name:          "tab-b",
   150  							TestGroupName: "popular-group",
   151  						},
   152  					},
   153  				},
   154  				{
   155  					Name: "dashboard-b",
   156  					DashboardTab: []*tgconf.DashboardTab{
   157  						{
   158  							Name:          "tab",
   159  							TestGroupName: "popular-group",
   160  						},
   161  					},
   162  				},
   163  				{
   164  					Name: "dashboard-c",
   165  					DashboardTab: []*tgconf.DashboardTab{
   166  						{
   167  							Name:          "tab-d",
   168  							TestGroupName: "something-else",
   169  						},
   170  					},
   171  				},
   172  			},
   173  			jobName:  "popular-group",
   174  			expected: []string{"dashboard-a#tab-b"},
   175  		},
   176  		{
   177  			name: "tab with base_options can be selected",
   178  			dashboards: []*tgconf.Dashboard{
   179  				{
   180  					Name: "dashboard-a",
   181  					DashboardTab: []*tgconf.DashboardTab{
   182  						{
   183  							Name:          "tab",
   184  							TestGroupName: "group-a",
   185  							BaseOptions:   "some sort of options",
   186  						},
   187  					},
   188  				},
   189  				{
   190  					Name: "dashboard-b",
   191  					DashboardTab: []*tgconf.DashboardTab{
   192  						{
   193  							Name:          "tab",
   194  							TestGroupName: "group-b",
   195  						},
   196  					},
   197  				},
   198  			},
   199  			jobName:  "group-a",
   200  			expected: []string{"dashboard-a#tab"},
   201  		},
   202  		{
   203  			name: "tab without base_options is preferred even if dashboard has fewer tabs",
   204  			dashboards: []*tgconf.Dashboard{
   205  				{
   206  					Name: "dashboard-a",
   207  					DashboardTab: []*tgconf.DashboardTab{
   208  						{
   209  							Name:          "tab-a",
   210  							TestGroupName: "popular-group",
   211  							BaseOptions:   "some sort of options",
   212  						},
   213  						{
   214  							Name:          "tab-b",
   215  							TestGroupName: "somewhere-else",
   216  						},
   217  					},
   218  				},
   219  				{
   220  					Name: "dashboard-b",
   221  					DashboardTab: []*tgconf.DashboardTab{
   222  						{
   223  							Name:          "tab-c",
   224  							TestGroupName: "popular-group",
   225  						},
   226  					},
   227  				},
   228  			},
   229  			jobName:  "popular-group",
   230  			expected: []string{"dashboard-b#tab-c"},
   231  		},
   232  		{
   233  			name: "nonexistent job errors",
   234  			dashboards: []*tgconf.Dashboard{
   235  				{
   236  					Name: "dashboard-a",
   237  					DashboardTab: []*tgconf.DashboardTab{
   238  						{
   239  							Name:          "tab-a",
   240  							TestGroupName: "some-group",
   241  						},
   242  					},
   243  				},
   244  			},
   245  			jobName:   "missing-group",
   246  			expectErr: true,
   247  		},
   248  		{
   249  			name: "configuration with no tabs errors",
   250  			dashboards: []*tgconf.Dashboard{
   251  				{
   252  					Name: "dashboard-a",
   253  				},
   254  			},
   255  			jobName:   "missing-group",
   256  			expectErr: true,
   257  		},
   258  		{
   259  			name:       "configuration with no dashboards errors",
   260  			dashboards: []*tgconf.Dashboard{},
   261  			jobName:    "missing-group",
   262  			expectErr:  true,
   263  		},
   264  	}
   265  
   266  	for _, tc := range testCases {
   267  		t.Run(tc.name, func(t *testing.T) {
   268  			tg := TestGrid{c: &tgconf.Configuration{
   269  				Dashboards: tc.dashboards,
   270  			}}
   271  			result, err := tg.FindQuery(tc.jobName)
   272  			if tc.expectErr {
   273  				if err == nil {
   274  					t.Fatalf("Expected an error, but instead got result %q", result)
   275  				}
   276  				return
   277  			}
   278  			if err != nil {
   279  				t.Fatalf("Unexpected error: %v", err)
   280  			}
   281  			found := false
   282  			for _, expectation := range tc.expected {
   283  				if result == expectation {
   284  					found = true
   285  					break
   286  				}
   287  			}
   288  			if !found {
   289  				t.Fatalf("Expected one of %v, but got %q", tc.expected, result)
   290  			}
   291  		})
   292  	}
   293  }