github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/cmd/crier/main_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 main 18 19 import ( 20 "flag" 21 "reflect" 22 "testing" 23 24 "k8s.io/test-infra/prow/flagutil" 25 gerritclient "k8s.io/test-infra/prow/gerrit/client" 26 ) 27 28 func TestOptions(t *testing.T) { 29 cases := []struct { 30 name string 31 args []string 32 expected *options 33 }{ 34 { 35 name: "empty, reject", 36 args: []string{}, 37 }, 38 { 39 name: "gerrit only support one worker", 40 args: []string{"--gerrit-workers=99", "--gerrit-projects=foo=bar", "--cookiefile=foobar"}, 41 expected: &options{ 42 gerritWorkers: 1, 43 cookiefilePath: "foobar", 44 gerritProjects: map[string][]string{ 45 "foo": {"bar"}, 46 }, 47 }, 48 }, 49 { 50 name: "gerrit missing --gerrit-projects, reject", 51 args: []string{"--gerrit-workers=5", "--cookiefile=foobar"}, 52 }, 53 { 54 name: "gerrit missing --cookiefile", 55 args: []string{"--gerrit-workers=5", "--gerrit-projects=foo=bar"}, 56 expected: &options{ 57 gerritWorkers: 1, 58 gerritProjects: map[string][]string{ 59 "foo": {"bar"}, 60 }, 61 }, 62 }, 63 } 64 65 for _, tc := range cases { 66 flags := flag.NewFlagSet(tc.name, flag.ContinueOnError) 67 actual := options{ 68 gerritProjects: gerritclient.ProjectsFlag{}, 69 } 70 err := actual.parseArgs(flags, tc.args) 71 actual.github = flagutil.GitHubOptions{} 72 switch { 73 case err == nil && tc.expected == nil: 74 t.Errorf("%s: failed to return an error", tc.name) 75 case err != nil && tc.expected != nil: 76 t.Errorf("%s: unexpected error: %v", tc.name, err) 77 case tc.expected != nil && !reflect.DeepEqual(*tc.expected, actual): 78 t.Errorf("%s: actual %v != expected %v", tc.name, actual, *tc.expected) 79 } 80 } 81 }