github.com/abayer/test-infra@v0.0.5/prow/cmd/mkbuild-cluster/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 25 func TestOptions(t *testing.T) { 26 cases := []struct { 27 name string 28 args []string 29 expected *options 30 }{ 31 { 32 args: []string{"--cluster=foo", "--alias=bar", "--zone=z", "--project=p"}, 33 expected: &options{ 34 cluster: "foo", 35 alias: "bar", 36 zone: "z", 37 project: "p", 38 }, 39 }, 40 { 41 name: "missing --cluster", 42 args: []string{"--alias=bar", "--zone=z", "--project=p"}, 43 }, 44 { 45 name: "missing --alias", 46 args: []string{"--cluster=foo", "--zone=z", "--project=p"}, 47 }, 48 { 49 name: "missing --zone", 50 args: []string{"--cluster=foo", "--alias=bar", "--project=p"}, 51 }, 52 { 53 name: "--missing --project", 54 args: []string{"--cluster=foo", "--alias=bar", "--zone=z"}, 55 }, 56 { 57 args: []string{ 58 "--cluster=foo", 59 "--alias=bar", 60 "--zone=z", 61 "--project=p", 62 "--account=a", 63 "--print-file", 64 "--print-entry", 65 "--get-client-cert", 66 "--change-context", 67 "--skip-check", 68 }, 69 expected: &options{ 70 cluster: "foo", 71 alias: "bar", 72 zone: "z", 73 project: "p", 74 account: "a", 75 skipCheck: true, 76 getClientCert: true, 77 changeContext: true, 78 printData: true, 79 printEntry: true, 80 }, 81 }, 82 } 83 84 for _, tc := range cases { 85 flags := flag.NewFlagSet(tc.name, flag.ContinueOnError) 86 var actual options 87 err := actual.parseArgs(flags, tc.args) 88 switch { 89 case err == nil && tc.expected == nil: 90 t.Errorf("%s: failed to return an error", tc.name) 91 case err != nil && tc.expected != nil: 92 t.Errorf("%s: unexpected error: %v", tc.name, err) 93 case tc.expected != nil && !reflect.DeepEqual(*tc.expected, actual): 94 t.Errorf("%s: actual %#v != expected %#v", tc.name, actual, *tc.expected) 95 } 96 } 97 }