sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/initupload/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 initupload
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    24  	"sigs.k8s.io/prow/pkg/flagutil"
    25  	"sigs.k8s.io/prow/pkg/gcsupload"
    26  )
    27  
    28  func TestOptions_Validate(t *testing.T) {
    29  	var testCases = []struct {
    30  		name        string
    31  		input       Options
    32  		expectedErr bool
    33  	}{
    34  		{
    35  			name: "minimal set ok",
    36  			input: Options{
    37  				Log: "testing",
    38  				Options: &gcsupload.Options{
    39  					DryRun: true,
    40  					GCSConfiguration: &prowapi.GCSConfiguration{
    41  						PathStrategy: prowapi.PathStrategyExplicit,
    42  					},
    43  				},
    44  			},
    45  			expectedErr: false,
    46  		},
    47  		{
    48  			name: "missing clone log",
    49  			input: Options{
    50  				Options: &gcsupload.Options{
    51  					DryRun: true,
    52  					GCSConfiguration: &prowapi.GCSConfiguration{
    53  						PathStrategy: prowapi.PathStrategyExplicit,
    54  					},
    55  				},
    56  			},
    57  			expectedErr: false,
    58  		},
    59  		{
    60  			name: "missing path strategy",
    61  			input: Options{
    62  				Options: &gcsupload.Options{
    63  					DryRun:           true,
    64  					GCSConfiguration: &prowapi.GCSConfiguration{},
    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 TestOptions_LoadConfig(t *testing.T) {
    83  	type args struct {
    84  		config string
    85  	}
    86  	tests := []struct {
    87  		name        string
    88  		args        args
    89  		wantOptions *Options
    90  		wantErr     bool
    91  	}{
    92  		{
    93  			name: "Parse GCS options",
    94  			args: args{
    95  				config: `
    96  {
    97    "bucket": "prow-artifacts",
    98    "path_strategy": "explicit",
    99    "gcs_credentials_file": "/secrets/gcs/service-account.json",
   100    "dry_run": false,
   101    "log": "/logs/clone.json"
   102  }
   103  				`,
   104  			},
   105  			wantOptions: &Options{
   106  				Options: &gcsupload.Options{
   107  					GCSConfiguration: &prowapi.GCSConfiguration{
   108  						Bucket:       "prow-artifacts",
   109  						PathStrategy: "explicit",
   110  					},
   111  					StorageClientOptions: flagutil.StorageClientOptions{
   112  						GCSCredentialsFile: "/secrets/gcs/service-account.json",
   113  					},
   114  					DryRun: false,
   115  				},
   116  				Log: "/logs/clone.json",
   117  			},
   118  			wantErr: false,
   119  		},
   120  		{
   121  			name: "Parse S3 storage options",
   122  			args: args{
   123  				config: `
   124  {
   125    "bucket": "s3://prow-artifacts",
   126    "path_strategy": "explicit",
   127    "s3_credentials_file": "/secrets/s3-storage/service-account.json",
   128    "dry_run": false,
   129    "log": "/logs/clone.json"
   130  }
   131  `,
   132  			},
   133  			wantOptions: &Options{
   134  				Options: &gcsupload.Options{
   135  					GCSConfiguration: &prowapi.GCSConfiguration{
   136  						Bucket:       "s3://prow-artifacts",
   137  						PathStrategy: "explicit",
   138  					},
   139  					StorageClientOptions: flagutil.StorageClientOptions{
   140  						S3CredentialsFile: "/secrets/s3-storage/service-account.json",
   141  					},
   142  				},
   143  				Log: "/logs/clone.json",
   144  			},
   145  			wantErr: false,
   146  		},
   147  	}
   148  	for _, tt := range tests {
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			gotOptions := NewOptions()
   151  			if err := gotOptions.LoadConfig(tt.args.config); (err != nil) != tt.wantErr {
   152  				t.Errorf("LoadConfig() error = %v, wantErr %v", err, tt.wantErr)
   153  			}
   154  
   155  			if !reflect.DeepEqual(tt.wantOptions, gotOptions) {
   156  				t.Errorf("%s: expected options to equal: %#v, but got: %#v", tt.name, tt.wantOptions, gotOptions)
   157  			}
   158  		})
   159  	}
   160  }