sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/sidecar/options_test.go (about)

     1  /*
     2  Copyright 2020 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 sidecar
    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  	"sigs.k8s.io/prow/pkg/pod-utils/wrapper"
    27  )
    28  
    29  func TestOptions_LoadConfig(t *testing.T) {
    30  	type args struct {
    31  		config string
    32  	}
    33  	tests := []struct {
    34  		name        string
    35  		args        args
    36  		wantOptions *Options
    37  		wantErr     bool
    38  	}{
    39  		{
    40  			name: "Parse GCS options",
    41  			args: args{
    42  				config: `
    43  {
    44    "gcs_options": {
    45  	"items": [
    46        "/logs/artifacts"
    47      ],
    48      "bucket": "prow-artifacts",
    49      "path_strategy": "explicit",
    50      "gcs_credentials_file": "/secrets/gcs/service-account.json",
    51      "dry_run": false
    52    },
    53    "entries": [
    54      {
    55        "args": [
    56          "sh",
    57          "-c",
    58          "echo test"
    59        ],
    60        "process_log": "/logs/process-log.txt",
    61        "marker_file": "/logs/marker-file.txt",
    62        "metadata_file": "/logs/artifacts/metadata.json"
    63      }
    64    ]
    65  }
    66  				`,
    67  			},
    68  			wantOptions: &Options{
    69  				GcsOptions: &gcsupload.Options{
    70  					GCSConfiguration: &prowapi.GCSConfiguration{
    71  						Bucket:       "prow-artifacts",
    72  						PathStrategy: "explicit",
    73  					},
    74  					StorageClientOptions: flagutil.StorageClientOptions{
    75  						GCSCredentialsFile: "/secrets/gcs/service-account.json",
    76  					},
    77  					DryRun: false,
    78  					Items:  []string{"/logs/artifacts"},
    79  				},
    80  				Entries: []wrapper.Options{
    81  					{
    82  						Args:         []string{"sh", "-c", "echo test"},
    83  						ProcessLog:   "/logs/process-log.txt",
    84  						MarkerFile:   "/logs/marker-file.txt",
    85  						MetadataFile: "/logs/artifacts/metadata.json",
    86  					},
    87  				},
    88  			},
    89  			wantErr: false,
    90  		},
    91  		{
    92  			name: "Parse S3 storage options",
    93  			args: args{
    94  				config: `
    95  {
    96    "gcs_options": {
    97  	"items": [
    98        "/logs/artifacts"
    99      ],
   100      "bucket": "s3://prow-artifacts",
   101      "path_strategy": "explicit",
   102      "s3_credentials_file": "/secrets/s3-storage/service-account.json",
   103      "dry_run": false
   104    },
   105    "entries": [
   106      {
   107        "args": [
   108          "sh",
   109          "-c",
   110          "echo test"
   111        ],
   112        "process_log": "/logs/process-log.txt",
   113        "marker_file": "/logs/marker-file.txt",
   114        "metadata_file": "/logs/artifacts/metadata.json"
   115      }
   116    ]
   117  }
   118  `,
   119  			},
   120  			wantOptions: &Options{
   121  				GcsOptions: &gcsupload.Options{
   122  					GCSConfiguration: &prowapi.GCSConfiguration{
   123  						Bucket:       "s3://prow-artifacts",
   124  						PathStrategy: "explicit",
   125  					},
   126  					StorageClientOptions: flagutil.StorageClientOptions{
   127  						S3CredentialsFile: "/secrets/s3-storage/service-account.json",
   128  					},
   129  					DryRun: false,
   130  					Items:  []string{"/logs/artifacts"},
   131  				},
   132  				Entries: []wrapper.Options{
   133  					{
   134  						Args:         []string{"sh", "-c", "echo test"},
   135  						ProcessLog:   "/logs/process-log.txt",
   136  						MarkerFile:   "/logs/marker-file.txt",
   137  						MetadataFile: "/logs/artifacts/metadata.json",
   138  					},
   139  				},
   140  			},
   141  			wantErr: false,
   142  		},
   143  	}
   144  	for _, tt := range tests {
   145  		t.Run(tt.name, func(t *testing.T) {
   146  			gotOptions := NewOptions()
   147  			if err := gotOptions.LoadConfig(tt.args.config); (err != nil) != tt.wantErr {
   148  				t.Errorf("LoadConfig() error = %v, wantErr %v", err, tt.wantErr)
   149  			}
   150  
   151  			if !reflect.DeepEqual(tt.wantOptions, gotOptions) {
   152  				t.Errorf("%s: expected options to equal: %#v, but got: %#v", tt.name, tt.wantOptions, gotOptions)
   153  			}
   154  		})
   155  	}
   156  }