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

     1  /*
     2  Copyright 2017 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  	"github.com/google/go-cmp/cmp"
    25  	"k8s.io/apimachinery/pkg/util/sets"
    26  	"sigs.k8s.io/prow/pkg/flagutil"
    27  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    28  	pluginsflagutil "sigs.k8s.io/prow/pkg/flagutil/plugins"
    29  )
    30  
    31  func newSetStringsFlagForTest(vals ...string) flagutil.Strings {
    32  	ss := flagutil.NewStrings()
    33  	for _, v := range vals {
    34  		ss.Set(v)
    35  	}
    36  	return ss
    37  }
    38  
    39  func TestGatherOptions(t *testing.T) {
    40  	cases := []struct {
    41  		name        string
    42  		args        []string
    43  		expected    func(*options)
    44  		expectedErr error
    45  	}{
    46  		{
    47  			name: "minimal flags work",
    48  		},
    49  		{
    50  			name: "gcs-credentials-file sets the GCS credentials on the storage client",
    51  			args: []string{
    52  				"-gcs-credentials-file=/creds",
    53  			},
    54  			expected: func(o *options) {
    55  				o.storage = flagutil.StorageClientOptions{
    56  					GCSCredentialsFile: "/creds",
    57  				}
    58  			},
    59  		},
    60  		{
    61  			name: "s3-credentials-file sets the S3 credentials on the storage client",
    62  			args: []string{
    63  				"-s3-credentials-file=/creds",
    64  			},
    65  			expected: func(o *options) {
    66  				o.storage = flagutil.StorageClientOptions{
    67  					S3CredentialsFile: "/creds",
    68  				}
    69  			},
    70  		},
    71  		{
    72  			name: "support denylist",
    73  			args: []string{
    74  				"-denylist=a",
    75  				"-denylist=b",
    76  			},
    77  			expected: func(o *options) {
    78  				o.addedPresubmitDenylist = newSetStringsFlagForTest("a", "b")
    79  			},
    80  		},
    81  	}
    82  
    83  	for _, tc := range cases {
    84  		t.Run(tc.name, func(t *testing.T) {
    85  			expected := &options{
    86  				dryRun: true,
    87  				config: configflagutil.ConfigOptions{
    88  					ConfigPath:                            "yo",
    89  					ConfigPathFlagName:                    "config-path",
    90  					JobConfigPathFlagName:                 "job-config-path",
    91  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    92  					InRepoConfigCacheSize:                 200,
    93  				},
    94  				pluginsConfig: pluginsflagutil.PluginOptions{
    95  					PluginConfigPath:                         "/etc/plugins/plugins.yaml",
    96  					PluginConfigPathDefault:                  "/etc/plugins/plugins.yaml",
    97  					SupplementalPluginsConfigsFileNameSuffix: "_pluginconfig.yaml",
    98  				},
    99  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
   100  			}
   101  			expectedfs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
   102  			expected.github.AddCustomizedFlags(expectedfs, flagutil.ThrottlerDefaults(300, 100))
   103  			if tc.expected != nil {
   104  				tc.expected(expected)
   105  			}
   106  
   107  			args := append(tc.args,
   108  				"--config-path=yo")
   109  			fs := flag.NewFlagSet("fake-flags", flag.PanicOnError)
   110  			actual := gatherOptions(fs, args...)
   111  			switch err := actual.Validate(); {
   112  			case err == nil && tc.expectedErr != nil:
   113  				t.Errorf("Expect err, got nil")
   114  			case err != nil && tc.expectedErr == nil:
   115  				t.Errorf("Expect no error, got: %v", err)
   116  			case err != nil && err.Error() != tc.expectedErr.Error():
   117  				t.Errorf("Expect error: %v\ngot:\n%v", err, tc.expectedErr)
   118  			case !reflect.DeepEqual(*expected, actual):
   119  				t.Errorf("actual differs from expected: %s", cmp.Diff(actual, *expected, cmp.Exporter(func(_ reflect.Type) bool { return true })))
   120  			}
   121  		})
   122  	}
   123  }
   124  
   125  func TestGetDenyList(t *testing.T) {
   126  	tests := []struct {
   127  		name string
   128  		o    options
   129  		want sets.Set[string]
   130  	}{
   131  		{
   132  			name: "black list only",
   133  			o: options{
   134  				addedPresubmitDenylist: newSetStringsFlagForTest("a", "b"),
   135  			},
   136  			want: sets.New[string]("a", "b"),
   137  		},
   138  		{
   139  			name: "deny list only",
   140  			o: options{
   141  				addedPresubmitDenylist: newSetStringsFlagForTest("c", "d"),
   142  			},
   143  			want: sets.New[string]("c", "d"),
   144  		},
   145  	}
   146  
   147  	for _, tc := range tests {
   148  		t.Run(tc.name, func(t *testing.T) {
   149  			got := tc.o.getDenyList()
   150  			if diff := cmp.Diff(tc.want, got); diff != "" {
   151  				t.Fatalf("Want(-), got(+):\n%s", diff)
   152  			}
   153  		})
   154  	}
   155  }