github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/pipeline/main_test.go (about)

     1  /*
     2  Copyright 2019 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  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    26  )
    27  
    28  func TestOptions(t *testing.T) {
    29  	cases := []struct {
    30  		name     string
    31  		args     []string
    32  		expected *options
    33  		err      bool
    34  	}{{
    35  		name:     "defaults don't work (set --config to prow config.yaml file)",
    36  		expected: &options{},
    37  		err:      true,
    38  	}, {
    39  		name: "only config works",
    40  		args: []string{"--config=/etc/config.yaml"},
    41  		expected: &options{
    42  			config: configflagutil.ConfigOptions{
    43  				ConfigPathFlagName:                    "config",
    44  				ConfigPath:                            "/etc/config.yaml",
    45  				JobConfigPathFlagName:                 "job-config-path",
    46  				SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    47  				InRepoConfigCacheSize:                 200,
    48  			},
    49  			instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
    50  		},
    51  	}, {
    52  		name: "parse all arguments",
    53  		args: []string{"--all-contexts=true", "--tot-url=https://tot", "--config=/etc/config.yaml"},
    54  		expected: &options{
    55  			allContexts: true,
    56  			totURL:      "https://tot",
    57  			config: configflagutil.ConfigOptions{
    58  				ConfigPathFlagName:                    "config",
    59  				ConfigPath:                            "/etc/config.yaml",
    60  				JobConfigPathFlagName:                 "job-config-path",
    61  				SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    62  				InRepoConfigCacheSize:                 200,
    63  			},
    64  			instrumentationOptions: prowflagutil.DefaultInstrumentationOptions(),
    65  		},
    66  	}}
    67  	for _, tc := range cases {
    68  		t.Run(tc.name, func(t *testing.T) {
    69  			flags := flag.NewFlagSet(tc.name, flag.ContinueOnError)
    70  			var actual options
    71  			switch err := actual.parse(flags, tc.args); {
    72  			case tc.expected == nil:
    73  				if err == nil {
    74  					t.Error("failed to receive an error")
    75  				}
    76  			case err != nil:
    77  				if !tc.err {
    78  					t.Errorf("unexpected error: %v", err)
    79  				}
    80  			case tc.err:
    81  				t.Error("failed to received expected error")
    82  			case !reflect.DeepEqual(&actual, tc.expected):
    83  				t.Errorf("actual %#v != expected %#v", actual, *tc.expected)
    84  			}
    85  		})
    86  	}
    87  }