github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/gcsupload/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 gcsupload 18 19 import ( 20 "testing" 21 22 "k8s.io/test-infra/prow/kube" 23 ) 24 25 func TestOptions_Validate(t *testing.T) { 26 var testCases = []struct { 27 name string 28 input Options 29 expectedErr bool 30 }{ 31 { 32 name: "minimal set ok", 33 input: Options{ 34 DryRun: true, 35 GCSConfiguration: &kube.GCSConfiguration{ 36 PathStrategy: kube.PathStrategyExplicit, 37 }, 38 }, 39 expectedErr: false, 40 }, 41 { 42 name: "push to GCS, ok", 43 input: Options{ 44 DryRun: false, 45 GcsCredentialsFile: "secrets", 46 GCSConfiguration: &kube.GCSConfiguration{ 47 Bucket: "seal", 48 PathStrategy: kube.PathStrategyExplicit, 49 }, 50 }, 51 expectedErr: false, 52 }, 53 { 54 name: "push to GCS, missing bucket", 55 input: Options{ 56 DryRun: false, 57 GcsCredentialsFile: "secrets", 58 GCSConfiguration: &kube.GCSConfiguration{ 59 PathStrategy: kube.PathStrategyExplicit, 60 }, 61 }, 62 expectedErr: true, 63 }, 64 { 65 name: "push to GCS, missing credentials", 66 input: Options{ 67 DryRun: false, 68 GCSConfiguration: &kube.GCSConfiguration{ 69 Bucket: "seal", 70 PathStrategy: kube.PathStrategyExplicit, 71 }, 72 }, 73 expectedErr: true, 74 }, 75 } 76 77 for _, testCase := range testCases { 78 err := testCase.input.Validate() 79 if testCase.expectedErr && err == nil { 80 t.Errorf("%s: expected an error but got none", testCase.name) 81 } 82 if !testCase.expectedErr && err != nil { 83 t.Errorf("%s: expected no error but got one: %v", testCase.name, err) 84 } 85 } 86 } 87 88 func TestValidatePathOptions(t *testing.T) { 89 var testCases = []struct { 90 name string 91 strategy string 92 org string 93 repo string 94 expectedErr bool 95 }{ 96 { 97 name: "invalid strategy", 98 strategy: "whatever", 99 expectedErr: true, 100 }, 101 { 102 name: "explicit strategy, no defaults", 103 strategy: kube.PathStrategyExplicit, 104 expectedErr: false, 105 }, 106 { 107 name: "legacy strategy, no defaults", 108 strategy: "legacy", 109 expectedErr: true, 110 }, 111 { 112 name: "legacy strategy, no default repo", 113 strategy: "legacy", 114 org: "org", 115 expectedErr: true, 116 }, 117 { 118 name: "legacy strategy, no default org", 119 strategy: "legacy", 120 repo: "repo", 121 expectedErr: true, 122 }, 123 { 124 name: "legacy strategy, with defaults", 125 strategy: "legacy", 126 org: "org", 127 repo: "repo", 128 expectedErr: false, 129 }, 130 { 131 name: "single strategy, no defaults", 132 strategy: "single", 133 expectedErr: true, 134 }, 135 { 136 name: "single strategy, no default repo", 137 strategy: "single", 138 org: "org", 139 expectedErr: true, 140 }, 141 { 142 name: "single strategy, no default org", 143 strategy: "single", 144 repo: "repo", 145 expectedErr: true, 146 }, 147 { 148 name: "single strategy, with defaults", 149 strategy: "single", 150 org: "org", 151 repo: "repo", 152 expectedErr: false, 153 }, 154 } 155 156 for _, testCase := range testCases { 157 o := Options{ 158 DryRun: true, 159 GCSConfiguration: &kube.GCSConfiguration{ 160 PathStrategy: testCase.strategy, 161 DefaultOrg: testCase.org, 162 DefaultRepo: testCase.repo, 163 }, 164 } 165 err := o.Validate() 166 if err != nil && !testCase.expectedErr { 167 t.Errorf("%s: expected no err but got %v", testCase.name, err) 168 } 169 if err == nil && testCase.expectedErr { 170 t.Errorf("%s: expected err but got none", testCase.name) 171 } 172 } 173 }