k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/testgrid/pkg/configurator/options/options_test.go (about)

     1  /*
     2  Copyright 2021 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 options
    18  
    19  import (
    20  	"flag"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"sigs.k8s.io/prow/pkg/flagutil"
    26  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    27  )
    28  
    29  func Test_Options(t *testing.T) {
    30  	tests := []struct {
    31  		name     string
    32  		args     []string
    33  		expected *Options
    34  	}{
    35  		{
    36  			name: "No options: fails",
    37  			args: []string{},
    38  		},
    39  		{
    40  			name: "Print Text",
    41  			args: []string{"--yaml=file.yaml", "--print-text", "--oneshot"},
    42  			expected: &Options{
    43  				Inputs:      []string{"file.yaml"},
    44  				DefaultYAML: "file.yaml",
    45  				ProwConfig: configflagutil.ConfigOptions{
    46  					ConfigPathFlagName:                    "prow-config",
    47  					JobConfigPathFlagName:                 "prow-job-config",
    48  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    49  					InRepoConfigCacheSize:                 200,
    50  				},
    51  				PrintText: true,
    52  				Oneshot:   true,
    53  			},
    54  		},
    55  		{
    56  			name: "Output to Location",
    57  			args: []string{"--yaml=file.yaml", "--output=gs://foo/bar"},
    58  			expected: &Options{
    59  				Inputs:      []string{"file.yaml"},
    60  				DefaultYAML: "file.yaml",
    61  				ProwConfig: configflagutil.ConfigOptions{
    62  					ConfigPathFlagName:                    "prow-config",
    63  					JobConfigPathFlagName:                 "prow-job-config",
    64  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    65  					InRepoConfigCacheSize:                 200,
    66  				},
    67  				Output: flagutil.NewStringsBeenSet("gs://foo/bar"),
    68  			},
    69  		},
    70  		{
    71  			name: "Output to multiple Locations",
    72  			args: []string{"--yaml=file.yaml", "--output=gs://foo/bar", "--output=./foo/bar"},
    73  			expected: &Options{
    74  				Inputs:      []string{"file.yaml"},
    75  				DefaultYAML: "file.yaml",
    76  				ProwConfig: configflagutil.ConfigOptions{
    77  					ConfigPathFlagName:                    "prow-config",
    78  					JobConfigPathFlagName:                 "prow-job-config",
    79  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    80  					InRepoConfigCacheSize:                 200,
    81  				},
    82  				Output: flagutil.NewStringsBeenSet("gs://foo/bar", "./foo/bar"),
    83  			},
    84  		},
    85  		{
    86  			name: "Many files: first set as default",
    87  			args: []string{"--yaml=first,second,third", "--validate-config-file"},
    88  			expected: &Options{
    89  				Inputs:      []string{"first", "second", "third"},
    90  				DefaultYAML: "first",
    91  				ProwConfig: configflagutil.ConfigOptions{
    92  					ConfigPathFlagName:                    "prow-config",
    93  					JobConfigPathFlagName:                 "prow-job-config",
    94  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    95  					InRepoConfigCacheSize:                 200,
    96  				},
    97  				ValidateConfigFile: true,
    98  			},
    99  		},
   100  		{
   101  			name: "--validate-config-file with output: fails",
   102  			args: []string{"--yaml=file.yaml", "--validate-config-file", "--output=/foo/bar"},
   103  		},
   104  		{
   105  			name: "Prow jobs with no root config: fails",
   106  			args: []string{"--yaml=file.yaml", "--output=/foo/bar", "--prow-job-config=/prow/jobs"},
   107  		},
   108  	}
   109  
   110  	for _, test := range tests {
   111  		t.Run(test.name, func(t *testing.T) {
   112  			flags := flag.NewFlagSet(test.name, flag.ContinueOnError)
   113  			var actual Options
   114  			err := actual.GatherOptions(flags, test.args)
   115  			switch {
   116  			case err == nil && test.expected == nil:
   117  				t.Errorf("Failed to return an error")
   118  			case err != nil && test.expected != nil:
   119  				t.Errorf("Unexpected error: %v", err)
   120  			case test.expected != nil && !reflect.DeepEqual(*test.expected, actual):
   121  				t.Errorf("Mismatched Options: diff: %s", cmp.Diff(*test.expected, actual, cmp.Exporter(func(_ reflect.Type) bool { return true })))
   122  			}
   123  		})
   124  	}
   125  }