sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/clonerefs/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 clonerefs
    18  
    19  import (
    20  	"testing"
    21  
    22  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    23  	"sigs.k8s.io/prow/pkg/github"
    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: "all ok",
    34  			input: Options{
    35  				SrcRoot: "test",
    36  				Log:     "thing",
    37  				GitRefs: []prowapi.Refs{
    38  					{
    39  						Repo: "repo1",
    40  						Org:  "org1",
    41  					},
    42  				},
    43  			},
    44  			expectedErr: false,
    45  		},
    46  		{
    47  			name: "missing src root",
    48  			input: Options{
    49  				Log: "thing",
    50  				GitRefs: []prowapi.Refs{
    51  					{
    52  						Repo: "repo1",
    53  						Org:  "org1",
    54  					},
    55  				},
    56  			},
    57  			expectedErr: true,
    58  		},
    59  		{
    60  			name: "missing Log location",
    61  			input: Options{
    62  				SrcRoot: "test",
    63  				GitRefs: []prowapi.Refs{
    64  					{
    65  						Repo: "repo1",
    66  						Org:  "org1",
    67  					},
    68  				},
    69  			},
    70  			expectedErr: true,
    71  		},
    72  		{
    73  			name: "missing refs",
    74  			input: Options{
    75  				SrcRoot: "test",
    76  				Log:     "thing",
    77  			},
    78  			expectedErr: true,
    79  		},
    80  		{
    81  			name: "separate repos",
    82  			input: Options{
    83  				SrcRoot: "test",
    84  				Log:     "thing",
    85  				GitRefs: []prowapi.Refs{
    86  					{
    87  						Repo: "repo1",
    88  						Org:  "org1",
    89  					},
    90  					{
    91  						Repo: "repo2",
    92  						Org:  "org2",
    93  					},
    94  				},
    95  			},
    96  			expectedErr: false,
    97  		},
    98  		{
    99  			name: "duplicate repos",
   100  			input: Options{
   101  				SrcRoot: "test",
   102  				Log:     "thing",
   103  				GitRefs: []prowapi.Refs{
   104  					{
   105  						Repo: "repo",
   106  						Org:  "org",
   107  					},
   108  					{
   109  						Repo: "repo",
   110  						Org:  "org",
   111  					},
   112  				},
   113  			},
   114  			expectedErr: true,
   115  		},
   116  		{
   117  			name: "specify access token file",
   118  			input: Options{
   119  				SrcRoot: "test",
   120  				Log:     "thing",
   121  				GitRefs: []prowapi.Refs{
   122  					{
   123  						Repo: "repo",
   124  						Org:  "org",
   125  					},
   126  				},
   127  				OauthTokenFile: "/tmp/token",
   128  			},
   129  			expectedErr: false,
   130  		},
   131  		{
   132  			name: "specify GitHub App ID and private key",
   133  			input: Options{
   134  				SrcRoot: "test",
   135  				Log:     "thing",
   136  				GitRefs: []prowapi.Refs{
   137  					{
   138  						Repo: "repo",
   139  						Org:  "org",
   140  					},
   141  				},
   142  				GitHubAPIEndpoints:      []string{github.DefaultAPIEndpoint},
   143  				GitHubAppID:             "123456",
   144  				GitHubAppPrivateKeyFile: "/tmp/private-key.pem",
   145  			},
   146  			expectedErr: false,
   147  		},
   148  		{
   149  			name: "specify aceess token file and GitHub App authentication",
   150  			input: Options{
   151  				SrcRoot: "test",
   152  				Log:     "thing",
   153  				GitRefs: []prowapi.Refs{
   154  					{
   155  						Repo: "repo",
   156  						Org:  "org",
   157  					},
   158  				},
   159  				OauthTokenFile:          "/tmp/token",
   160  				GitHubAPIEndpoints:      []string{github.DefaultAPIEndpoint},
   161  				GitHubAppID:             "123456",
   162  				GitHubAppPrivateKeyFile: "/tmp/private-key.pem",
   163  			},
   164  			expectedErr: true,
   165  		},
   166  		{
   167  			name: "specify GitHub App authentication but no API endpoints",
   168  			input: Options{
   169  				SrcRoot: "test",
   170  				Log:     "thing",
   171  				GitRefs: []prowapi.Refs{
   172  					{
   173  						Repo: "repo",
   174  						Org:  "org",
   175  					},
   176  				},
   177  				GitHubAppID:             "123456",
   178  				GitHubAppPrivateKeyFile: "/tmp/private-key.pem",
   179  			},
   180  			expectedErr: true,
   181  		},
   182  		{
   183  			name: "specify GitHub App ID but no private key",
   184  			input: Options{
   185  				SrcRoot: "test",
   186  				Log:     "thing",
   187  				GitRefs: []prowapi.Refs{
   188  					{
   189  						Repo: "repo",
   190  						Org:  "org",
   191  					},
   192  				},
   193  				GitHubAPIEndpoints: []string{github.DefaultAPIEndpoint},
   194  				GitHubAppID:        "123456",
   195  			},
   196  			expectedErr: true,
   197  		},
   198  		{
   199  			name: "specify GitHub App private key but no ID",
   200  			input: Options{
   201  				SrcRoot: "test",
   202  				Log:     "thing",
   203  				GitRefs: []prowapi.Refs{
   204  					{
   205  						Repo: "repo",
   206  						Org:  "org",
   207  					},
   208  				},
   209  				GitHubAPIEndpoints:      []string{github.DefaultAPIEndpoint},
   210  				GitHubAppPrivateKeyFile: "/tmp/private-key.pem",
   211  			},
   212  			expectedErr: true,
   213  		},
   214  	}
   215  
   216  	for _, testCase := range testCases {
   217  		err := testCase.input.Validate()
   218  		if testCase.expectedErr && err == nil {
   219  			t.Errorf("%s: expected an error but got none", testCase.name)
   220  		}
   221  		if !testCase.expectedErr && err != nil {
   222  			t.Errorf("%s: expected no error but got one: %v", testCase.name, err)
   223  		}
   224  	}
   225  }