sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/cmd/crier/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  
    24  	"github.com/google/go-cmp/cmp"
    25  
    26  	"sigs.k8s.io/prow/pkg/flagutil"
    27  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    28  )
    29  
    30  func TestOptions(t *testing.T) {
    31  
    32  	var defaultGitHubOptions flagutil.GitHubOptions
    33  	defaultGitHubOptions.AddFlags(flag.NewFlagSet("", flag.ContinueOnError))
    34  
    35  	cases := []struct {
    36  		name     string
    37  		args     []string
    38  		expected *options
    39  	}{
    40  		//General
    41  		{
    42  			name: "no args, reject",
    43  			args: []string{},
    44  		},
    45  		{
    46  			name: "config-path is empty string, reject",
    47  			args: []string{"--pubsub-workers=1", "--config-path="},
    48  		},
    49  		//Gerrit Reporter
    50  		{
    51  			name: "gerrit supports multiple workers",
    52  			args: []string{"--gerrit-workers=99", "--cookiefile=foobar", "--config-path=foo"},
    53  			expected: &options{
    54  				gerritWorkers:  99,
    55  				cookiefilePath: "foobar",
    56  				config: configflagutil.ConfigOptions{
    57  					ConfigPathFlagName:                    "config-path",
    58  					JobConfigPathFlagName:                 "job-config-path",
    59  					ConfigPath:                            "foo",
    60  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    61  					InRepoConfigCacheSize:                 200,
    62  				},
    63  				github:                 defaultGitHubOptions,
    64  				k8sReportFraction:      1.0,
    65  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
    66  			},
    67  		},
    68  		{
    69  			name: "gerrit missing --cookiefile",
    70  			args: []string{"--gerrit-workers=5", "--config-path=foo"},
    71  			expected: &options{
    72  				gerritWorkers: 5,
    73  				config: configflagutil.ConfigOptions{
    74  					ConfigPathFlagName:                    "config-path",
    75  					JobConfigPathFlagName:                 "job-config-path",
    76  					ConfigPath:                            "foo",
    77  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    78  					InRepoConfigCacheSize:                 200,
    79  				},
    80  				github:                 defaultGitHubOptions,
    81  				k8sReportFraction:      1.0,
    82  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
    83  			},
    84  		},
    85  		//PubSub Reporter
    86  		{
    87  			name: "pubsub workers, sets workers",
    88  			args: []string{"--pubsub-workers=7", "--config-path=baz"},
    89  			expected: &options{
    90  				config: configflagutil.ConfigOptions{
    91  					ConfigPathFlagName:                    "config-path",
    92  					JobConfigPathFlagName:                 "job-config-path",
    93  					ConfigPath:                            "baz",
    94  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
    95  					InRepoConfigCacheSize:                 200,
    96  				},
    97  				pubsubWorkers:          7,
    98  				github:                 defaultGitHubOptions,
    99  				k8sReportFraction:      1.0,
   100  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
   101  			},
   102  		},
   103  		{
   104  			name: "pubsub workers set to negative, rejects",
   105  			args: []string{"--pubsub-workers=-3", "--config-path=foo"},
   106  		},
   107  		//Slack Reporter
   108  		{
   109  			name: "slack workers, sets workers",
   110  			args: []string{"--slack-workers=13", "--slack-token-file=/bar/baz", "--config-path=foo"},
   111  			expected: &options{
   112  				slackWorkers:   13,
   113  				slackTokenFile: "/bar/baz",
   114  				config: configflagutil.ConfigOptions{
   115  					ConfigPathFlagName:                    "config-path",
   116  					JobConfigPathFlagName:                 "job-config-path",
   117  					ConfigPath:                            "foo",
   118  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
   119  					InRepoConfigCacheSize:                 200,
   120  				},
   121  				github:                 defaultGitHubOptions,
   122  				k8sReportFraction:      1.0,
   123  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
   124  			},
   125  		},
   126  		{
   127  			name: "slack missing --slack-token, rejects",
   128  			args: []string{"--slack-workers=1", "--config-path=foo"},
   129  		},
   130  		{
   131  			name: "slack with --dry-run, sets",
   132  			args: []string{"--slack-workers=13", "--slack-token-file=/bar/baz", "--config-path=foo", "--dry-run"},
   133  			expected: &options{
   134  				slackWorkers:   13,
   135  				slackTokenFile: "/bar/baz",
   136  				config: configflagutil.ConfigOptions{
   137  					ConfigPathFlagName:                    "config-path",
   138  					JobConfigPathFlagName:                 "job-config-path",
   139  					ConfigPath:                            "foo",
   140  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
   141  					InRepoConfigCacheSize:                 200,
   142  				},
   143  				dryrun:                 true,
   144  				github:                 defaultGitHubOptions,
   145  				k8sReportFraction:      1.0,
   146  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
   147  			},
   148  		},
   149  		{
   150  			name: "k8s-gcs enables k8s-gcs",
   151  			args: []string{"--kubernetes-blob-storage-workers=3", "--config-path=foo"},
   152  			expected: &options{
   153  				k8sBlobStorageWorkers: 3,
   154  				config: configflagutil.ConfigOptions{
   155  					ConfigPathFlagName:                    "config-path",
   156  					JobConfigPathFlagName:                 "job-config-path",
   157  					ConfigPath:                            "foo",
   158  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
   159  					InRepoConfigCacheSize:                 200,
   160  				},
   161  				github:                 defaultGitHubOptions,
   162  				k8sReportFraction:      1.0,
   163  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
   164  			},
   165  		},
   166  		{
   167  			name: "k8s-gcs with report fraction sets report fraction",
   168  			args: []string{"--kubernetes-blob-storage-workers=3", "--config-path=foo", "--kubernetes-report-fraction=0.5"},
   169  			expected: &options{
   170  				k8sBlobStorageWorkers: 3,
   171  				config: configflagutil.ConfigOptions{
   172  					ConfigPathFlagName:                    "config-path",
   173  					JobConfigPathFlagName:                 "job-config-path",
   174  					ConfigPath:                            "foo",
   175  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
   176  					InRepoConfigCacheSize:                 200,
   177  				},
   178  				github:                 defaultGitHubOptions,
   179  				k8sReportFraction:      0.5,
   180  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
   181  			},
   182  		},
   183  		{
   184  			name: "k8s-gcs with too large report fraction rejects",
   185  			args: []string{"--kubernetes-blob-storage-workers=3", "--config-path=foo", "--kubernetes-report-fraction=1.5"},
   186  		},
   187  		{
   188  			name: "k8s-gcs with negative report fraction rejects",
   189  			args: []string{"--kubernetes-blob-storage-workers=3", "--config-path=foo", "--kubernetes-report-fraction=-1.2"},
   190  		},
   191  		{
   192  			name: "resultstore workers, sets workers",
   193  			args: []string{"--resultstore-workers=3", "--config-path=foo"},
   194  			expected: &options{
   195  				resultStoreWorkers: 3,
   196  				config: configflagutil.ConfigOptions{
   197  					ConfigPathFlagName:                    "config-path",
   198  					JobConfigPathFlagName:                 "job-config-path",
   199  					ConfigPath:                            "foo",
   200  					SupplementalProwConfigsFileNameSuffix: "_prowconfig.yaml",
   201  					InRepoConfigCacheSize:                 200,
   202  				},
   203  				github:                 defaultGitHubOptions,
   204  				k8sReportFraction:      1.0,
   205  				instrumentationOptions: flagutil.DefaultInstrumentationOptions(),
   206  			},
   207  		},
   208  	}
   209  
   210  	for _, tc := range cases {
   211  		t.Run(tc.name, func(t *testing.T) {
   212  			flags := flag.NewFlagSet(tc.name, flag.ContinueOnError)
   213  			var actual options
   214  			err := actual.parseArgs(flags, tc.args)
   215  			switch {
   216  			case err == nil && tc.expected == nil:
   217  				t.Fatalf("%s: failed to return an error", tc.name)
   218  			case err != nil && tc.expected != nil:
   219  				t.Fatalf("%s: unexpected error: %v", tc.name, err)
   220  			}
   221  
   222  			if tc.expected == nil {
   223  				return
   224  			}
   225  			if diff := cmp.Diff(actual, *tc.expected, cmp.Exporter(func(_ reflect.Type) bool { return true })); diff != "" {
   226  				t.Errorf("Result differs from expected: %s", diff)
   227  			}
   228  
   229  		})
   230  	}
   231  }
   232  
   233  /*
   234  The GitHubOptions object has several private fields and objects
   235  This unit testing covers only the public portions
   236  */
   237  func TestGitHubOptions(t *testing.T) {
   238  	cases := []struct {
   239  		name              string
   240  		args              []string
   241  		expectedWorkers   int
   242  		expectedTokenPath string
   243  	}{
   244  		{
   245  			name:              "github workers, only support single worker",
   246  			args:              []string{"--github-workers=5", "--github-token-path=tkpath", "--config-path=foo"},
   247  			expectedWorkers:   5,
   248  			expectedTokenPath: "tkpath",
   249  		},
   250  	}
   251  
   252  	for _, tc := range cases {
   253  		flags := flag.NewFlagSet(tc.name, flag.ContinueOnError)
   254  		actual := options{}
   255  		err := actual.parseArgs(flags, tc.args)
   256  
   257  		if err != nil {
   258  			t.Errorf("%s: unexpected error: %v", tc.name, err)
   259  		}
   260  		if actual.githubWorkers != tc.expectedWorkers {
   261  			t.Errorf("%s: worker mismatch: actual %d != expected %d",
   262  				tc.name, actual.githubWorkers, tc.expectedWorkers)
   263  		}
   264  		if actual.github.TokenPath != tc.expectedTokenPath {
   265  			t.Errorf("%s: path mismatch: actual %s != expected %s",
   266  				tc.name, actual.github.TokenPath, tc.expectedTokenPath)
   267  		}
   268  	}
   269  }