github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/jenkins-operator/main_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 main 18 19 import ( 20 "flag" 21 "testing" 22 23 "sigs.k8s.io/prow/pkg/flagutil" 24 ) 25 26 func TestOptions_Validate(t *testing.T) { 27 var testCases = []struct { 28 name string 29 input options 30 expectedErr bool 31 }{ 32 { 33 name: "minimal ok with jenkins token", 34 input: options{ 35 jenkinsURL: "https://example.com", 36 jenkinsTokenFile: "secret", 37 github: flagutil.GitHubOptions{TokenPath: "token"}, 38 }, 39 expectedErr: false, 40 }, 41 { 42 name: "minimal ok with jenkins bearer token", 43 input: options{ 44 jenkinsURL: "https://example.com", 45 jenkinsBearerTokenFile: "secret", 46 github: flagutil.GitHubOptions{TokenPath: "token"}, 47 }, 48 expectedErr: false, 49 }, 50 { 51 name: "both jenkins tokens failure", 52 input: options{ 53 jenkinsURL: "https://example.com", 54 jenkinsTokenFile: "secret", 55 jenkinsBearerTokenFile: "other", 56 github: flagutil.GitHubOptions{TokenPath: "token"}, 57 }, 58 expectedErr: true, 59 }, 60 { 61 name: "all certificate files given", 62 input: options{ 63 jenkinsURL: "https://example.com", 64 jenkinsTokenFile: "secret", 65 certFile: "cert", 66 keyFile: "key", 67 caCertFile: "cacert", 68 github: flagutil.GitHubOptions{TokenPath: "token"}, 69 }, 70 expectedErr: false, 71 }, 72 { 73 name: "missing cacert", 74 input: options{ 75 jenkinsURL: "https://example.com", 76 jenkinsTokenFile: "secret", 77 certFile: "cert", 78 keyFile: "key", 79 github: flagutil.GitHubOptions{TokenPath: "token"}, 80 }, 81 expectedErr: true, 82 }, 83 { 84 name: "missing cert", 85 input: options{ 86 jenkinsURL: "https://example.com", 87 jenkinsTokenFile: "secret", 88 keyFile: "key", 89 caCertFile: "cacert", 90 github: flagutil.GitHubOptions{TokenPath: "token"}, 91 }, 92 expectedErr: true, 93 }, 94 { 95 name: "missing key", 96 input: options{ 97 jenkinsURL: "https://example.com", 98 jenkinsTokenFile: "secret", 99 certFile: "cert", 100 caCertFile: "cacert", 101 github: flagutil.GitHubOptions{TokenPath: "token"}, 102 }, 103 expectedErr: true, 104 }, 105 } 106 107 for _, testCase := range testCases { 108 testCase.input.config.AddFlags(&flag.FlagSet{}) 109 if testCase.input.config.ConfigPath == "" { 110 testCase.input.config.ConfigPath = "/etc/config/config.yaml" 111 } 112 err := testCase.input.Validate() 113 if testCase.expectedErr && err == nil { 114 t.Errorf("%s: expected an error but got none", testCase.name) 115 } 116 if !testCase.expectedErr && err != nil { 117 t.Errorf("%s: expected no error but got one: %v", testCase.name, err) 118 } 119 } 120 }