github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/spyglass/lenses/common/common_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 common 18 19 import ( 20 "testing" 21 22 prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1" 23 "sigs.k8s.io/prow/pkg/config" 24 "sigs.k8s.io/prow/pkg/io/providers" 25 ) 26 27 // fakeProwJobFetcher is used to fetch ProwJobs in tests 28 type fakeProwJobFetcher struct { 29 prowJob prowapi.ProwJob 30 } 31 32 func (j *fakeProwJobFetcher) GetProwJob(job, id string) (prowapi.ProwJob, error) { 33 return j.prowJob, nil 34 } 35 36 func TestProwToGCS(t *testing.T) { 37 type args struct { 38 fetcher ProwJobFetcher 39 config config.Getter 40 prowKey string 41 } 42 tests := []struct { 43 name string 44 args args 45 wantStorageProvider string 46 wantGCSKey string 47 wantErr bool 48 }{ 49 { 50 name: "legacy gs bucket, gcs status url and deprecated gcs job url prefix", 51 args: args{ 52 fetcher: &fakeProwJobFetcher{ 53 prowJob: prowapi.ProwJob{ 54 Spec: prowapi.ProwJobSpec{ 55 DecorationConfig: &prowapi.DecorationConfig{ 56 GCSConfiguration: &prowapi.GCSConfiguration{ 57 Bucket: "kubernetes-jenkins", 58 DefaultOrg: "kubernetes", 59 DefaultRepo: "kubernetes", 60 PathStrategy: prowapi.PathStrategyLegacy, 61 }, 62 }, 63 }, 64 Status: prowapi.ProwJobStatus{ 65 URL: "https://prow.k8s.io/view/gcs/kubernetes-jenkins/logs/ci-benchmark-microbenchmarks/1258197944759226371", 66 }, 67 }, 68 }, 69 config: func() *config.Config { 70 return &config.Config{ 71 ProwConfig: config.ProwConfig{ 72 Plank: config.Plank{ 73 JobURLPrefixConfig: map[string]string{"*": "https://prow.k8s.io/view/gcs/"}, 74 }, 75 }, 76 } 77 }, 78 prowKey: "ci-benchmark-microbenchmarks/1258197944759226371", 79 }, 80 wantStorageProvider: providers.GS, 81 wantGCSKey: "kubernetes-jenkins/logs/ci-benchmark-microbenchmarks/1258197944759226371", 82 }, 83 { 84 name: "gs bucket, gcs status url and deprecated gcs job url prefix", 85 args: args{ 86 fetcher: &fakeProwJobFetcher{ 87 prowJob: prowapi.ProwJob{ 88 Spec: prowapi.ProwJobSpec{ 89 DecorationConfig: &prowapi.DecorationConfig{ 90 GCSConfiguration: &prowapi.GCSConfiguration{ 91 Bucket: "gs://kubernetes-jenkins", 92 DefaultOrg: "kubernetes", 93 DefaultRepo: "kubernetes", 94 PathStrategy: prowapi.PathStrategyLegacy, 95 }, 96 }, 97 }, 98 Status: prowapi.ProwJobStatus{ 99 URL: "https://prow.k8s.io/view/gcs/kubernetes-jenkins/logs/ci-benchmark-microbenchmarks/1258197944759226371", 100 }, 101 }, 102 }, 103 config: func() *config.Config { 104 return &config.Config{ 105 ProwConfig: config.ProwConfig{ 106 Plank: config.Plank{ 107 JobURLPrefixConfig: map[string]string{"*": "https://prow.k8s.io/view/gcs/"}, 108 }, 109 }, 110 } 111 }, 112 prowKey: "ci-benchmark-microbenchmarks/1258197944759226371", 113 }, 114 wantStorageProvider: providers.GS, 115 wantGCSKey: "kubernetes-jenkins/logs/ci-benchmark-microbenchmarks/1258197944759226371", 116 }, 117 { 118 name: "gs bucket, gs status url and new job url prefix format", 119 args: args{ 120 fetcher: &fakeProwJobFetcher{ 121 prowJob: prowapi.ProwJob{ 122 Spec: prowapi.ProwJobSpec{ 123 DecorationConfig: &prowapi.DecorationConfig{ 124 GCSConfiguration: &prowapi.GCSConfiguration{ 125 Bucket: "gs://kubernetes-jenkins", 126 DefaultOrg: "kubernetes", 127 DefaultRepo: "kubernetes", 128 PathStrategy: prowapi.PathStrategyLegacy, 129 }, 130 }, 131 }, 132 Status: prowapi.ProwJobStatus{ 133 URL: "https://prow.k8s.io/view/gs/kubernetes-jenkins/logs/ci-benchmark-microbenchmarks/1258197944759226371", 134 }, 135 }, 136 }, 137 config: func() *config.Config { 138 return &config.Config{ 139 ProwConfig: config.ProwConfig{ 140 Plank: config.Plank{ 141 JobURLPrefixConfig: map[string]string{"*": "https://prow.k8s.io/view/"}, 142 }, 143 }, 144 } 145 }, 146 prowKey: "ci-benchmark-microbenchmarks/1258197944759226371", 147 }, 148 wantStorageProvider: providers.GS, 149 wantGCSKey: "kubernetes-jenkins/logs/ci-benchmark-microbenchmarks/1258197944759226371", 150 }, 151 { 152 name: "s3 bucket, s3 status url and new job url prefix format", 153 args: args{ 154 fetcher: &fakeProwJobFetcher{ 155 prowJob: prowapi.ProwJob{ 156 Spec: prowapi.ProwJobSpec{ 157 DecorationConfig: &prowapi.DecorationConfig{ 158 GCSConfiguration: &prowapi.GCSConfiguration{ 159 Bucket: "s3://kubernetes-jenkins", 160 DefaultOrg: "kubernetes", 161 DefaultRepo: "kubernetes", 162 PathStrategy: prowapi.PathStrategyLegacy, 163 }, 164 }, 165 }, 166 Status: prowapi.ProwJobStatus{ 167 URL: "https://prow.k8s.io/view/s3/kubernetes-jenkins/logs/ci-benchmark-microbenchmarks/1258197944759226371", 168 }, 169 }, 170 }, 171 config: func() *config.Config { 172 return &config.Config{ 173 ProwConfig: config.ProwConfig{ 174 Plank: config.Plank{ 175 JobURLPrefixConfig: map[string]string{"*": "https://prow.k8s.io/view/"}, 176 }, 177 }, 178 } 179 }, 180 prowKey: "ci-benchmark-microbenchmarks/1258197944759226371", 181 }, 182 wantStorageProvider: providers.S3, 183 wantGCSKey: "kubernetes-jenkins/logs/ci-benchmark-microbenchmarks/1258197944759226371", 184 }, 185 } 186 for _, tt := range tests { 187 t.Run(tt.name, func(t *testing.T) { 188 gotStorageProvider, gotGCSKey, err := ProwToGCS(tt.args.fetcher, tt.args.config, tt.args.prowKey) 189 if (err != nil) != tt.wantErr { 190 t.Errorf("ProwToGCS() error = %v, wantErr %v", err, tt.wantErr) 191 return 192 } 193 if gotStorageProvider != tt.wantStorageProvider { 194 t.Errorf("ProwToGCS() gotStorageProvider = %v, wantStorageProvider %v", gotStorageProvider, tt.wantStorageProvider) 195 } 196 if gotGCSKey != tt.wantGCSKey { 197 t.Errorf("ProwToGCS() gotGCSKey = %v, wantGCSKey %v", gotGCSKey, tt.wantGCSKey) 198 } 199 }) 200 } 201 }