github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/gerrit/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  	"time"
    24  
    25  	"k8s.io/apimachinery/pkg/util/sets"
    26  
    27  	"sigs.k8s.io/prow/pkg/flagutil"
    28  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    29  )
    30  
    31  func TestFlags(t *testing.T) {
    32  	cases := []struct {
    33  		name     string
    34  		args     map[string]string
    35  		del      sets.Set[string]
    36  		expected func(*options)
    37  		err      bool
    38  	}{
    39  		{
    40  			name: "minimal flags work",
    41  		},
    42  		{
    43  			name: "expicitly set --dry-run=false",
    44  			args: map[string]string{
    45  				"--dry-run": "false",
    46  			},
    47  			expected: func(o *options) {
    48  				o.dryRun = false
    49  			},
    50  		},
    51  		{
    52  			name: "explicitly set --dry-run=true",
    53  			args: map[string]string{
    54  				"--dry-run": "true",
    55  			},
    56  			expected: func(o *options) {
    57  				o.dryRun = true
    58  			},
    59  		},
    60  		{
    61  			name:     "dry run defaults to false",
    62  			del:      sets.New[string]("--dry-run"),
    63  			expected: func(o *options) {},
    64  		},
    65  		{
    66  			name: "gcs credentials are set",
    67  			args: map[string]string{
    68  				"--gcs-credentials-file": "/creds",
    69  			},
    70  			expected: func(o *options) {
    71  				o.storage.GCSCredentialsFile = "/creds"
    72  			},
    73  		},
    74  		{
    75  			name: "s3 credentials are set",
    76  			args: map[string]string{
    77  				"--s3-credentials-file": "/creds",
    78  			},
    79  			expected: func(o *options) {
    80  				o.storage.S3CredentialsFile = "/creds"
    81  			},
    82  		},
    83  	}
    84  
    85  	for _, tc := range cases {
    86  		t.Run(tc.name, func(t *testing.T) {
    87  			expected := &options{
    88  				lastSyncFallback: "gs://path",
    89  				config: configflagutil.ConfigOptions{
    90  					ConfigPathFlagName:                    "config-path",
    91  					JobConfigPathFlagName:                 "job-config-path",
    92  					ConfigPath:                            "yo",
    93  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    94  					InRepoConfigCacheSize:                 200,
    95  				},
    96  				dryRun:                   false,
    97  				instrumentationOptions:   flagutil.DefaultInstrumentationOptions(),
    98  				changeWorkerPoolSize:     1,
    99  				pushGatewayInterval:      time.Minute,
   100  				instanceConcurrencyLimit: 5,
   101  			}
   102  			if tc.expected != nil {
   103  				tc.expected(expected)
   104  			}
   105  			argMap := map[string]string{
   106  				"--last-sync-fallback": "gs://path",
   107  				"--config-path":        "yo",
   108  				"--dry-run":            "false",
   109  			}
   110  			for k, v := range tc.args {
   111  				argMap[k] = v
   112  			}
   113  			for k := range tc.del {
   114  				delete(argMap, k)
   115  			}
   116  
   117  			var args []string
   118  			for k, v := range argMap {
   119  				args = append(args, k+"="+v)
   120  			}
   121  			fs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
   122  			actual := gatherOptions(fs, args...)
   123  			switch err := actual.validate(); {
   124  			case err != nil:
   125  				if !tc.err {
   126  					t.Errorf("unexpected error: %v", err)
   127  				}
   128  			case tc.err:
   129  				t.Errorf("failed to receive expected error")
   130  			case !reflect.DeepEqual(*expected, actual):
   131  				t.Errorf("%#v != expected %#v", actual, *expected)
   132  			}
   133  		})
   134  	}
   135  }