github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/pod-utils/gcs/upload_test.go (about) 1 /* 2 Copyright 2017 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 gcs 18 19 import ( 20 "errors" 21 "fmt" 22 "sync" 23 "testing" 24 25 "cloud.google.com/go/storage" 26 ) 27 28 func TestUploadToGcs(t *testing.T) { 29 var testCases = []struct { 30 name string 31 passingTargets int 32 failingTargets int 33 expectedErr bool 34 }{ 35 { 36 name: "all passing", 37 passingTargets: 10, 38 failingTargets: 0, 39 expectedErr: false, 40 }, 41 { 42 name: "all but one passing", 43 passingTargets: 10, 44 failingTargets: 1, 45 expectedErr: true, 46 }, 47 { 48 name: "all but one failing", 49 passingTargets: 1, 50 failingTargets: 10, 51 expectedErr: true, 52 }, 53 { 54 name: "all failing", 55 passingTargets: 0, 56 failingTargets: 10, 57 expectedErr: true, 58 }, 59 } 60 61 for _, testCase := range testCases { 62 lock := sync.Mutex{} 63 count := 0 64 65 update := func() { 66 lock.Lock() 67 defer lock.Unlock() 68 count = count + 1 69 } 70 71 fail := func(obj *storage.ObjectHandle) error { 72 update() 73 return errors.New("fail") 74 } 75 76 success := func(obj *storage.ObjectHandle) error { 77 update() 78 return nil 79 } 80 81 targets := map[string]UploadFunc{} 82 for i := 0; i < testCase.passingTargets; i++ { 83 targets[fmt.Sprintf("pass-%d", i)] = success 84 } 85 86 for i := 0; i < testCase.failingTargets; i++ { 87 targets[fmt.Sprintf("fail-%d", i)] = fail 88 } 89 90 err := Upload(&storage.BucketHandle{}, targets) 91 if err != nil && !testCase.expectedErr { 92 t.Errorf("%s: expected no error but got %v", testCase.name, err) 93 } 94 if err == nil && testCase.expectedErr { 95 t.Errorf("%s: expected an error but got none", testCase.name) 96 } 97 98 if count != (testCase.passingTargets + testCase.failingTargets) { 99 t.Errorf("%s: had %d passing and %d failing targets but only ran %d targets, not %d", testCase.name, testCase.passingTargets, testCase.failingTargets, count, testCase.passingTargets+testCase.failingTargets) 100 } 101 } 102 }