github.com/GoogleCloudPlatform/testgrid@v0.0.174/config/fields_test.go (about) 1 /* 2 Copyright 2021 The TestGrid 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 config 18 19 import ( 20 "testing" 21 22 configpb "github.com/GoogleCloudPlatform/testgrid/pb/config" 23 "github.com/google/go-cmp/cmp" 24 ) 25 26 func TestFields(t *testing.T) { 27 cases := []struct { 28 name string 29 config *configpb.Configuration 30 expected map[string]int64 31 }{ 32 { 33 name: "Nil config", 34 expected: map[string]int64{}, 35 }, 36 { 37 name: "Empty config", 38 config: &configpb.Configuration{}, 39 expected: map[string]int64{}, 40 }, 41 { 42 name: "Simple config", 43 config: &configpb.Configuration{ 44 Dashboards: []*configpb.Dashboard{ 45 { 46 Name: "dash_1", 47 DashboardTab: []*configpb.DashboardTab{ 48 { 49 Name: "tab_1", 50 TestGroupName: "test_group_1", 51 }, 52 }, 53 }, 54 }, 55 TestGroups: []*configpb.TestGroup{ 56 { 57 Name: "test_group_1", 58 GcsPrefix: "tests_live_here", 59 DaysOfResults: 1, 60 NumColumnsRecent: 1, 61 }, 62 }, 63 }, 64 expected: map[string]int64{ 65 "dashboards.name": 1, 66 "dashboards.dashboard_tab.name": 1, 67 "dashboards.dashboard_tab.test_group_name": 1, 68 "test_groups.name": 1, 69 "test_groups.gcs_prefix": 1, 70 "test_groups.days_of_results": 1, 71 "test_groups.num_columns_recent": 1, 72 }, 73 }, 74 { 75 name: "Multiple configs", 76 config: &configpb.Configuration{ 77 Dashboards: []*configpb.Dashboard{ 78 { 79 Name: "dash_1", 80 DashboardTab: []*configpb.DashboardTab{ 81 { 82 Name: "tab_1", 83 TestGroupName: "test_group_1", 84 }, 85 }, 86 }, 87 { 88 Name: "dash_2", 89 DashboardTab: []*configpb.DashboardTab{ 90 { 91 Name: "tab_2", 92 TestGroupName: "test_group_2", 93 }, 94 { 95 Name: "tab_3", 96 BugComponent: 1, 97 }, 98 }, 99 HighlightToday: true, 100 }, 101 }, 102 }, 103 expected: map[string]int64{ 104 "dashboards.name": 2, 105 "dashboards.dashboard_tab.name": 3, 106 "dashboards.dashboard_tab.test_group_name": 2, 107 "dashboards.dashboard_tab.bug_component": 1, 108 "dashboards.highlight_today": 1, 109 }, 110 }, 111 { 112 name: "Ignore default values", 113 config: &configpb.Configuration{ 114 Dashboards: []*configpb.Dashboard{ 115 { 116 Name: "", 117 HighlightToday: false, 118 DashboardTab: []*configpb.DashboardTab{ 119 { 120 Name: "DashTab", 121 BugComponent: 0, 122 }, 123 }, 124 }, 125 }, 126 }, 127 expected: map[string]int64{ 128 "dashboards.dashboard_tab.name": 1, 129 }, 130 }, 131 { 132 name: "Nested messages and one-ofs", 133 config: &configpb.Configuration{ 134 Dashboards: []*configpb.Dashboard{ 135 { 136 DashboardTab: []*configpb.DashboardTab{ 137 { 138 Name: "t", 139 BetaAutobugOptions: &configpb.AutoBugOptions{ 140 BetaAutobugComponent: 1, 141 HotlistIdsFromSource: []*configpb.HotlistIdFromSource{ 142 { 143 HotlistIdSource: &configpb.HotlistIdFromSource_Label{"foo"}, 144 }, 145 { 146 HotlistIdSource: &configpb.HotlistIdFromSource_Value{1}, 147 }, 148 }, 149 }, 150 }, 151 }, 152 }, 153 }, 154 TestGroups: []*configpb.TestGroup{ 155 { 156 AutoBugOptions: &configpb.AutoBugOptions{ 157 BetaAutobugComponent: 1, 158 HotlistIdsFromSource: []*configpb.HotlistIdFromSource{ 159 { 160 HotlistIdSource: &configpb.HotlistIdFromSource_Value{2}, 161 }, 162 { 163 HotlistIdSource: &configpb.HotlistIdFromSource_Value{3}, 164 }, 165 }, 166 }, 167 }, 168 }, 169 }, 170 expected: map[string]int64{ 171 "dashboards.dashboard_tab.name": 1, 172 "dashboards.dashboard_tab.beta_autobug_options.beta_autobug_component": 1, 173 "dashboards.dashboard_tab.beta_autobug_options.hotlist_ids_from_source.label": 1, 174 "dashboards.dashboard_tab.beta_autobug_options.hotlist_ids_from_source.value": 1, 175 "test_groups.auto_bug_options.beta_autobug_component": 1, 176 "test_groups.auto_bug_options.hotlist_ids_from_source.value": 2, 177 }, 178 }, 179 } 180 181 for _, tc := range cases { 182 t.Run(tc.name, func(t *testing.T) { 183 result := Fields(tc.config) 184 if diff := cmp.Diff(tc.expected, result); diff != "" { 185 t.Errorf("Incorrect fields (-want +got): %s", diff) 186 } 187 }) 188 } 189 }