sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/crier/reporters/gcs/util/util_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 util
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  
    25  	prowv1 "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    26  	"sigs.k8s.io/prow/pkg/config"
    27  )
    28  
    29  type fca struct {
    30  	c config.Config
    31  }
    32  
    33  func (ca fca) Config() *config.Config {
    34  	return &ca.c
    35  }
    36  
    37  func TestGetJobDestination(t *testing.T) {
    38  	standardGcsConfig := &prowv1.GCSConfiguration{
    39  		Bucket:       "kubernetes-jenkins",
    40  		PathPrefix:   "some-prefix",
    41  		PathStrategy: prowv1.PathStrategyLegacy,
    42  		DefaultOrg:   "kubernetes",
    43  		DefaultRepo:  "kubernetes",
    44  	}
    45  	standardRefs := &prowv1.Refs{
    46  		Org:   "kubernetes",
    47  		Repo:  "test-infra",
    48  		Pulls: []prowv1.Pull{{Number: 12345}},
    49  	}
    50  	tests := []struct {
    51  		name              string
    52  		defaultGcsConfigs map[string]*prowv1.GCSConfiguration
    53  		prowjobGcsConfig  *prowv1.GCSConfiguration
    54  		prowjobType       prowv1.ProwJobType
    55  		prowjobRefs       *prowv1.Refs
    56  		buildID           string
    57  		expectBucket      string
    58  		expectDir         string // tip: this will always end in "my-little-job/[buildID]"
    59  		expectErr         bool
    60  	}{
    61  		{
    62  			name:              "decorated prowjob uses inline config when default is empty",
    63  			defaultGcsConfigs: nil,
    64  			prowjobGcsConfig:  standardGcsConfig,
    65  			prowjobType:       prowv1.PeriodicJob,
    66  			buildID:           "123",
    67  			expectBucket:      "kubernetes-jenkins",
    68  			expectDir:         "some-prefix/logs/my-little-job/123",
    69  		},
    70  		{
    71  			name: "decorated prowjob uses inline config over default config",
    72  			defaultGcsConfigs: map[string]*prowv1.GCSConfiguration{
    73  				"*": {
    74  					Bucket:       "the-wrong-bucket",
    75  					PathPrefix:   "",
    76  					PathStrategy: prowv1.PathStrategyLegacy,
    77  					DefaultOrg:   "kubernetes",
    78  					DefaultRepo:  "kubernetes",
    79  				},
    80  			},
    81  			prowjobGcsConfig: standardGcsConfig,
    82  			prowjobType:      prowv1.PeriodicJob,
    83  			buildID:          "123",
    84  			expectBucket:     "kubernetes-jenkins",
    85  			expectDir:        "some-prefix/logs/my-little-job/123",
    86  		},
    87  		{
    88  			name:              "undecorated prowjob falls back to default config",
    89  			defaultGcsConfigs: map[string]*prowv1.GCSConfiguration{"*": standardGcsConfig},
    90  			prowjobGcsConfig:  nil,
    91  			prowjobType:       prowv1.PeriodicJob,
    92  			buildID:           "123",
    93  			expectBucket:      "kubernetes-jenkins",
    94  			expectDir:         "some-prefix/logs/my-little-job/123",
    95  		},
    96  		{
    97  			name:              "undecorated prowjob with no default config is an error",
    98  			defaultGcsConfigs: nil,
    99  			prowjobGcsConfig:  nil,
   100  			prowjobType:       prowv1.PeriodicJob,
   101  			buildID:           "123",
   102  			expectErr:         true,
   103  		},
   104  		{
   105  			name: "undecorated prowjob uses the correct org config",
   106  			defaultGcsConfigs: map[string]*prowv1.GCSConfiguration{
   107  				"*": {
   108  					Bucket:       "the-wrong-bucket",
   109  					PathPrefix:   "",
   110  					PathStrategy: prowv1.PathStrategyLegacy,
   111  					DefaultOrg:   "the-wrong-org",
   112  					DefaultRepo:  "the-wrong-repo",
   113  				},
   114  				"kubernetes": standardGcsConfig,
   115  			},
   116  			prowjobGcsConfig: nil,
   117  			prowjobType:      prowv1.PeriodicJob,
   118  			prowjobRefs:      standardRefs,
   119  			buildID:          "123",
   120  			expectBucket:     "kubernetes-jenkins",
   121  			expectDir:        "some-prefix/logs/my-little-job/123",
   122  		},
   123  		{
   124  			name:             "prowjob type is respected",
   125  			prowjobGcsConfig: standardGcsConfig,
   126  			prowjobRefs:      standardRefs,
   127  			prowjobType:      prowv1.PresubmitJob,
   128  			buildID:          "123",
   129  			expectBucket:     "kubernetes-jenkins",
   130  			expectDir:        "some-prefix/pr-logs/pull/test-infra/12345/my-little-job/123",
   131  		},
   132  		{
   133  			name:              "reporting a prowjob with no BuildID is an error",
   134  			defaultGcsConfigs: nil,
   135  			prowjobGcsConfig:  standardGcsConfig,
   136  			prowjobType:       prowv1.PeriodicJob,
   137  			expectErr:         true,
   138  		},
   139  	}
   140  
   141  	for _, tc := range tests {
   142  		t.Run(tc.name, func(t *testing.T) {
   143  			pj := &prowv1.ProwJob{
   144  				Spec: prowv1.ProwJobSpec{
   145  					Type:             tc.prowjobType,
   146  					Refs:             tc.prowjobRefs,
   147  					Agent:            prowv1.KubernetesAgent,
   148  					Job:              "my-little-job",
   149  					DecorationConfig: &prowv1.DecorationConfig{GCSConfiguration: tc.prowjobGcsConfig},
   150  				},
   151  				Status: prowv1.ProwJobStatus{
   152  					State:     prowv1.TriggeredState,
   153  					StartTime: metav1.Time{Time: time.Date(2010, 10, 10, 18, 30, 0, 0, time.UTC)},
   154  					PodName:   "some-pod",
   155  					BuildID:   tc.buildID,
   156  				},
   157  			}
   158  			decorationConfigs := map[string]*prowv1.DecorationConfig{}
   159  			for k, v := range tc.defaultGcsConfigs {
   160  				decorationConfigs[k] = &prowv1.DecorationConfig{GCSConfiguration: v}
   161  			}
   162  			cfg := fca{c: config.Config{
   163  				ProwConfig: config.ProwConfig{
   164  					Plank: config.Plank{
   165  						DefaultDecorationConfigs: config.DefaultDecorationMapToSliceTesting(decorationConfigs),
   166  					},
   167  				},
   168  			}}.Config
   169  
   170  			bucket, dir, err := GetJobDestination(cfg, pj)
   171  			if err != nil {
   172  				if !tc.expectErr {
   173  					t.Fatalf("Unexpected error: %v", err)
   174  				}
   175  			} else if tc.expectErr {
   176  				t.Fatalf("Expected an error, but didn't get one; instead got gs://%q/%q", bucket, dir)
   177  			}
   178  			if bucket != tc.expectBucket {
   179  				t.Errorf("Expected bucket %q, but got %q", tc.expectBucket, bucket)
   180  			}
   181  			if dir != tc.expectDir {
   182  				t.Errorf("Expected dir %q, but got %q", tc.expectDir, dir)
   183  			}
   184  		})
   185  	}
   186  }