sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/cmd/tide/main_test.go (about) 1 /* 2 Copyright 2017 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 main 18 19 import ( 20 "flag" 21 "reflect" 22 "testing" 23 "time" 24 25 "k8s.io/apimachinery/pkg/util/sets" 26 27 "sigs.k8s.io/prow/pkg/config" 28 "sigs.k8s.io/prow/pkg/flagutil" 29 configflagutil "sigs.k8s.io/prow/pkg/flagutil/config" 30 ) 31 32 func Test_gatherOptions(t *testing.T) { 33 cases := []struct { 34 name string 35 args map[string]string 36 del sets.Set[string] 37 expected func(*options) 38 err bool 39 }{ 40 { 41 name: "minimal flags work", 42 expected: func(o *options) { 43 o.controllerManager.TimeoutListingProwJobs = 30 * time.Second 44 o.controllerManager.TimeoutListingProwJobsDefault = 30 * time.Second 45 }, 46 }, 47 { 48 name: "explicitly set --config-path", 49 args: map[string]string{ 50 "--config-path": "/random/value", 51 }, 52 expected: func(o *options) { 53 o.config.ConfigPath = "/random/value" 54 o.controllerManager.TimeoutListingProwJobs = 30 * time.Second 55 o.controllerManager.TimeoutListingProwJobsDefault = 30 * time.Second 56 }, 57 }, 58 { 59 name: "expicitly set --dry-run=false", 60 args: map[string]string{ 61 "--dry-run": "false", 62 }, 63 expected: func(o *options) { 64 o.dryRun = false 65 o.controllerManager.TimeoutListingProwJobs = 30 * time.Second 66 o.controllerManager.TimeoutListingProwJobsDefault = 30 * time.Second 67 }, 68 }, 69 { 70 name: "gcs-credentials-file sets the credentials on the storage client", 71 args: map[string]string{ 72 "-gcs-credentials-file": "/creds", 73 }, 74 expected: func(o *options) { 75 o.storage = flagutil.StorageClientOptions{ 76 GCSCredentialsFile: "/creds", 77 } 78 o.controllerManager.TimeoutListingProwJobs = 30 * time.Second 79 o.controllerManager.TimeoutListingProwJobsDefault = 30 * time.Second 80 }, 81 }, 82 { 83 name: "s3-credentials-file sets the credentials on the storage client", 84 args: map[string]string{ 85 "-s3-credentials-file": "/creds", 86 }, 87 expected: func(o *options) { 88 o.storage = flagutil.StorageClientOptions{ 89 S3CredentialsFile: "/creds", 90 } 91 o.controllerManager.TimeoutListingProwJobs = 30 * time.Second 92 o.controllerManager.TimeoutListingProwJobsDefault = 30 * time.Second 93 }, 94 }, 95 } 96 97 for _, tc := range cases { 98 t.Run(tc.name, func(t *testing.T) { 99 expected := &options{ 100 port: 8888, 101 config: configflagutil.ConfigOptions{ 102 ConfigPathFlagName: "config-path", 103 JobConfigPathFlagName: "job-config-path", 104 ConfigPath: "yo", 105 SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml", 106 InRepoConfigCacheSize: 200, 107 }, 108 dryRun: true, 109 syncThrottle: 800, 110 statusThrottle: 400, 111 maxRecordsPerPool: 1000, 112 instrumentationOptions: flagutil.DefaultInstrumentationOptions(), 113 } 114 expectedfs := flag.NewFlagSet("fake-flags", flag.PanicOnError) 115 expected.github.AddFlags(expectedfs) 116 if tc.expected != nil { 117 tc.expected(expected) 118 } 119 120 argMap := map[string]string{ 121 "--config-path": "yo", 122 } 123 for k, v := range tc.args { 124 argMap[k] = v 125 } 126 for k := range tc.del { 127 delete(argMap, k) 128 } 129 130 var args []string 131 for k, v := range argMap { 132 args = append(args, k+"="+v) 133 } 134 fs := flag.NewFlagSet("fake-flags", flag.PanicOnError) 135 actual := gatherOptions(fs, args...) 136 switch err := actual.Validate(); { 137 case err != nil: 138 if !tc.err { 139 t.Errorf("unexpected error: %v", err) 140 } 141 case tc.err: 142 t.Errorf("failed to receive expected error") 143 case !reflect.DeepEqual(*expected, actual): 144 t.Errorf("%#v != expected %#v", actual, *expected) 145 } 146 }) 147 } 148 } 149 150 func TestProvider(t *testing.T) { 151 tests := []struct { 152 name string 153 provider string 154 tideConfig config.Tide 155 expect string 156 }{ 157 { 158 name: "default", 159 provider: "", 160 tideConfig: config.Tide{}, 161 expect: "github", 162 }, 163 { 164 name: "only-gerrit-config", 165 provider: "", 166 tideConfig: config.Tide{Gerrit: &config.TideGerritConfig{ 167 Queries: config.GerritOrgRepoConfigs{ 168 config.GerritOrgRepoConfig{}, 169 }, 170 }}, 171 expect: "gerrit", 172 }, 173 { 174 name: "only-github-config", 175 provider: "", 176 tideConfig: config.Tide{TideGitHubConfig: config.TideGitHubConfig{ 177 Queries: config.TideQueries{ 178 {}, 179 }, 180 }}, 181 expect: "github", 182 }, 183 { 184 name: "both-config", 185 provider: "", 186 tideConfig: config.Tide{ 187 TideGitHubConfig: config.TideGitHubConfig{ 188 Queries: config.TideQueries{ 189 {}, 190 }, 191 }, 192 Gerrit: &config.TideGerritConfig{ 193 Queries: config.GerritOrgRepoConfigs{ 194 config.GerritOrgRepoConfig{}, 195 }, 196 }, 197 }, 198 expect: "github", 199 }, 200 { 201 name: "explicit-github", 202 provider: "github", 203 tideConfig: config.Tide{ 204 Gerrit: &config.TideGerritConfig{ 205 Queries: config.GerritOrgRepoConfigs{ 206 config.GerritOrgRepoConfig{}, 207 }, 208 }, 209 }, 210 expect: "github", 211 }, 212 { 213 name: "explicit-gerrit", 214 provider: "gerrit", 215 tideConfig: config.Tide{ 216 TideGitHubConfig: config.TideGitHubConfig{ 217 Queries: config.TideQueries{ 218 {}, 219 }, 220 }, 221 }, 222 expect: "gerrit", 223 }, 224 { 225 name: "explicit-unsupported-provider", 226 provider: "foobar", 227 tideConfig: config.Tide{ 228 TideGitHubConfig: config.TideGitHubConfig{ 229 Queries: config.TideQueries{ 230 {}, 231 }, 232 }, 233 Gerrit: &config.TideGerritConfig{ 234 Queries: config.GerritOrgRepoConfigs{ 235 config.GerritOrgRepoConfig{}, 236 }, 237 }, 238 }, 239 expect: "", 240 }, 241 } 242 243 for _, tc := range tests { 244 tc := tc 245 t.Run(tc.name, func(t *testing.T) { 246 if want, got := tc.expect, provider(tc.provider, tc.tideConfig); want != got { 247 t.Errorf("Wrong provider. Want: %s, got: %s", want, got) 248 } 249 }) 250 } 251 }