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

     1  /*
     2  Copyright 2016 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  	"github.com/google/go-cmp/cmp"
    26  	"k8s.io/apimachinery/pkg/util/sets"
    27  
    28  	"sigs.k8s.io/prow/pkg/flagutil"
    29  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    30  	pluginsflagutil "sigs.k8s.io/prow/pkg/flagutil/plugins"
    31  )
    32  
    33  func Test_gatherOptions(t *testing.T) {
    34  	cases := []struct {
    35  		name     string
    36  		args     map[string]string
    37  		del      sets.Set[string]
    38  		expected func(*options)
    39  		err      bool
    40  	}{
    41  		{
    42  			name: "minimal flags work",
    43  		},
    44  		{
    45  			name: "explicitly set --config-path",
    46  			args: map[string]string{
    47  				"--config-path": "/random/value",
    48  			},
    49  			expected: func(o *options) {
    50  				o.config.ConfigPath = "/random/value"
    51  			},
    52  		},
    53  		{
    54  			name: "expicitly set --dry-run=false",
    55  			args: map[string]string{
    56  				"--dry-run": "false",
    57  			},
    58  			expected: func(o *options) {
    59  				o.dryRun = false
    60  			},
    61  		},
    62  		{
    63  			name: "explicitly set --plugin-config",
    64  			args: map[string]string{
    65  				"--plugin-config": "/random/value",
    66  			},
    67  			expected: func(o *options) {
    68  				o.pluginsConfig.PluginConfigPath = "/random/value"
    69  			},
    70  		},
    71  		{
    72  			name: "explicitly set --webhook-path",
    73  			args: map[string]string{
    74  				"--webhook-path": "/random/hook",
    75  			},
    76  			expected: func(o *options) {
    77  				o.webhookPath = "/random/hook"
    78  			},
    79  		},
    80  	}
    81  	for _, tc := range cases {
    82  		t.Run(tc.name, func(t *testing.T) {
    83  			expected := &options{
    84  				webhookPath: "/hook",
    85  				port:        8888,
    86  				config: configflagutil.ConfigOptions{
    87  					ConfigPath:                            "yo",
    88  					ConfigPathFlagName:                    "config-path",
    89  					JobConfigPathFlagName:                 "job-config-path",
    90  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    91  					InRepoConfigCacheSize:                 200,
    92  				},
    93  				pluginsConfig: pluginsflagutil.PluginOptions{
    94  					PluginConfigPath:                         "/etc/plugins/plugins.yaml",
    95  					PluginConfigPathDefault:                  "/etc/plugins/plugins.yaml",
    96  					SupplementalPluginsConfigsFileNameSuffix: "_pluginconfig.yaml",
    97  				},
    98  				dryRun:                 true,
    99  				gracePeriod:            180 * time.Second,
   100  				webhookSecretFile:      "/etc/webhook/hmac",
   101  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
   102  			}
   103  			expectedfs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
   104  			expected.github.AddFlags(expectedfs)
   105  			if tc.expected != nil {
   106  				tc.expected(expected)
   107  			}
   108  
   109  			argMap := map[string]string{
   110  				"--config-path": "yo",
   111  			}
   112  			for k, v := range tc.args {
   113  				argMap[k] = v
   114  			}
   115  			for k := range tc.del {
   116  				delete(argMap, k)
   117  			}
   118  
   119  			var args []string
   120  			for k, v := range argMap {
   121  				args = append(args, k+"="+v)
   122  			}
   123  			fs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
   124  			actual := gatherOptions(fs, args...)
   125  			switch err := actual.Validate(); {
   126  			case err != nil:
   127  				if !tc.err {
   128  					t.Errorf("unexpected error: %v", err)
   129  				}
   130  			case tc.err:
   131  				t.Errorf("failed to receive expected error")
   132  			case !reflect.DeepEqual(*expected, actual):
   133  				t.Errorf("actual differs from expected: %s", cmp.Diff(actual, *expected, cmp.Exporter(func(_ reflect.Type) bool { return true })))
   134  			}
   135  		})
   136  	}
   137  }