github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/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  	"testing"
    21  
    22  	"k8s.io/test-infra/prow/flagutil"
    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 ok with jenkins token",
    33  			input: options{
    34  				jenkinsURL:       "https://example.com",
    35  				jenkinsTokenFile: "secret",
    36  				github:           flagutil.GitHubOptions{TokenPath: "token"},
    37  			},
    38  			expectedErr: false,
    39  		},
    40  		{
    41  			name: "minimal ok with jenkins bearer token",
    42  			input: options{
    43  				jenkinsURL:             "https://example.com",
    44  				jenkinsBearerTokenFile: "secret",
    45  				github:                 flagutil.GitHubOptions{TokenPath: "token"},
    46  			},
    47  			expectedErr: false,
    48  		},
    49  		{
    50  			name: "both jenkins tokens failure",
    51  			input: options{
    52  				jenkinsURL:             "https://example.com",
    53  				jenkinsTokenFile:       "secret",
    54  				jenkinsBearerTokenFile: "other",
    55  				github:                 flagutil.GitHubOptions{TokenPath: "token"},
    56  			},
    57  			expectedErr: true,
    58  		},
    59  		{
    60  			name: "all certificate files given",
    61  			input: options{
    62  				jenkinsURL:       "https://example.com",
    63  				jenkinsTokenFile: "secret",
    64  				certFile:         "cert",
    65  				keyFile:          "key",
    66  				caCertFile:       "cacert",
    67  				github:           flagutil.GitHubOptions{TokenPath: "token"},
    68  			},
    69  			expectedErr: false,
    70  		},
    71  		{
    72  			name: "missing cacert",
    73  			input: options{
    74  				jenkinsURL:       "https://example.com",
    75  				jenkinsTokenFile: "secret",
    76  				certFile:         "cert",
    77  				keyFile:          "key",
    78  				github:           flagutil.GitHubOptions{TokenPath: "token"},
    79  			},
    80  			expectedErr: true,
    81  		},
    82  		{
    83  			name: "missing cert",
    84  			input: options{
    85  				jenkinsURL:       "https://example.com",
    86  				jenkinsTokenFile: "secret",
    87  				keyFile:          "key",
    88  				caCertFile:       "cacert",
    89  				github:           flagutil.GitHubOptions{TokenPath: "token"},
    90  			},
    91  			expectedErr: true,
    92  		},
    93  		{
    94  			name: "missing key",
    95  			input: options{
    96  				jenkinsURL:       "https://example.com",
    97  				jenkinsTokenFile: "secret",
    98  				certFile:         "cert",
    99  				caCertFile:       "cacert",
   100  				github:           flagutil.GitHubOptions{TokenPath: "token"},
   101  			},
   102  			expectedErr: true,
   103  		},
   104  	}
   105  
   106  	for _, testCase := range testCases {
   107  		err := testCase.input.Validate()
   108  		if testCase.expectedErr && err == nil {
   109  			t.Errorf("%s: expected an error but got none", testCase.name)
   110  		}
   111  		if !testCase.expectedErr && err != nil {
   112  			t.Errorf("%s: expected no error but got one: %v", testCase.name, err)
   113  		}
   114  	}
   115  }