sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/cmd/admission/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 prowflagutil "sigs.k8s.io/prow/pkg/flagutil" 25 ) 26 27 func TestOptions(t *testing.T) { 28 cases := []struct { 29 name string 30 args []string 31 expected *options 32 }{{ 33 name: "forgot cert", 34 args: []string{"--tls-private-key-file=k"}, 35 }, 36 { 37 name: "forgot private key", 38 args: []string{"--tls-cert-file=c"}, 39 }, 40 { 41 name: "works with both private/pub", 42 args: []string{"--tls-cert-file=c", "--tls-private-key-file=k"}, 43 expected: &options{ 44 cert: "c", 45 privateKey: "k", 46 instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(), 47 }, 48 }, 49 { 50 name: "defaults error", 51 }} 52 for _, tc := range cases { 53 t.Run(tc.name, func(t *testing.T) { 54 flags := flag.NewFlagSet(tc.name, flag.ContinueOnError) 55 var actual options 56 switch err := actual.parse(flags, tc.args); { 57 case tc.expected == nil: 58 if err == nil { 59 t.Error("failed to receive an error") 60 } 61 case err != nil: 62 t.Errorf("unexpected error: %v", err) 63 case !reflect.DeepEqual(&actual, tc.expected): 64 t.Errorf("actual %#v != expected %#v", actual, *tc.expected) 65 } 66 }) 67 } 68 }