sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/gcsupload/options_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 gcsupload
    18  
    19  import (
    20  	"testing"
    21  
    22  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    23  	"sigs.k8s.io/prow/pkg/flagutil"
    24  )
    25  
    26  func TestOptions_Validate(t *testing.T) {
    27  	var testCases = []struct {
    28  		name        string
    29  		input       Options
    30  		expectedErr bool
    31  	}{
    32  		{
    33  			name: "minimal set ok",
    34  			input: Options{
    35  				DryRun: true,
    36  				GCSConfiguration: &prowapi.GCSConfiguration{
    37  					PathStrategy: prowapi.PathStrategyExplicit,
    38  				},
    39  			},
    40  			expectedErr: false,
    41  		},
    42  		{
    43  			name: "push to GCS, ok",
    44  			input: Options{
    45  				DryRun: false,
    46  				StorageClientOptions: flagutil.StorageClientOptions{
    47  					GCSCredentialsFile: "secrets",
    48  				},
    49  				GCSConfiguration: &prowapi.GCSConfiguration{
    50  					Bucket:       "seal",
    51  					PathStrategy: prowapi.PathStrategyExplicit,
    52  				},
    53  			},
    54  			expectedErr: false,
    55  		},
    56  		{
    57  			name: "push to GCS, missing bucket",
    58  			input: Options{
    59  				DryRun: false,
    60  				StorageClientOptions: flagutil.StorageClientOptions{
    61  					GCSCredentialsFile: "secrets",
    62  				},
    63  				GCSConfiguration: &prowapi.GCSConfiguration{
    64  					PathStrategy: prowapi.PathStrategyExplicit,
    65  				},
    66  			},
    67  			expectedErr: true,
    68  		},
    69  	}
    70  
    71  	for _, testCase := range testCases {
    72  		err := testCase.input.Validate()
    73  		if testCase.expectedErr && err == nil {
    74  			t.Errorf("%s: expected an error but got none", testCase.name)
    75  		}
    76  		if !testCase.expectedErr && err != nil {
    77  			t.Errorf("%s: expected no error but got one: %v", testCase.name, err)
    78  		}
    79  	}
    80  }
    81  
    82  func TestValidatePathOptions(t *testing.T) {
    83  	var testCases = []struct {
    84  		name        string
    85  		strategy    string
    86  		org         string
    87  		repo        string
    88  		expectedErr bool
    89  	}{
    90  		{
    91  			name:        "invalid strategy",
    92  			strategy:    "whatever",
    93  			expectedErr: true,
    94  		},
    95  		{
    96  			name:        "explicit strategy, no defaults",
    97  			strategy:    prowapi.PathStrategyExplicit,
    98  			expectedErr: false,
    99  		},
   100  		{
   101  			name:        "legacy strategy, no defaults",
   102  			strategy:    "legacy",
   103  			expectedErr: true,
   104  		},
   105  		{
   106  			name:        "legacy strategy, no default repo",
   107  			strategy:    "legacy",
   108  			org:         "org",
   109  			expectedErr: true,
   110  		},
   111  		{
   112  			name:        "legacy strategy, no default org",
   113  			strategy:    "legacy",
   114  			repo:        "repo",
   115  			expectedErr: true,
   116  		},
   117  		{
   118  			name:        "legacy strategy, with defaults",
   119  			strategy:    "legacy",
   120  			org:         "org",
   121  			repo:        "repo",
   122  			expectedErr: false,
   123  		},
   124  		{
   125  			name:        "single strategy, no defaults",
   126  			strategy:    "single",
   127  			expectedErr: true,
   128  		},
   129  		{
   130  			name:        "single strategy, no default repo",
   131  			strategy:    "single",
   132  			org:         "org",
   133  			expectedErr: true,
   134  		},
   135  		{
   136  			name:        "single strategy, no default org",
   137  			strategy:    "single",
   138  			repo:        "repo",
   139  			expectedErr: true,
   140  		},
   141  		{
   142  			name:        "single strategy, with defaults",
   143  			strategy:    "single",
   144  			org:         "org",
   145  			repo:        "repo",
   146  			expectedErr: false,
   147  		},
   148  	}
   149  
   150  	for _, testCase := range testCases {
   151  		o := Options{
   152  			DryRun: true,
   153  			GCSConfiguration: &prowapi.GCSConfiguration{
   154  				PathStrategy: testCase.strategy,
   155  				DefaultOrg:   testCase.org,
   156  				DefaultRepo:  testCase.repo,
   157  			},
   158  		}
   159  		err := o.Validate()
   160  		if err != nil && !testCase.expectedErr {
   161  			t.Errorf("%s: expected no err but got %v", testCase.name, err)
   162  		}
   163  		if err == nil && testCase.expectedErr {
   164  			t.Errorf("%s: expected err but got none", testCase.name)
   165  		}
   166  	}
   167  }