sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/cmd/mkpj/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  	prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1"
    23  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    24  	"sigs.k8s.io/prow/pkg/github"
    25  	"sigs.k8s.io/prow/pkg/github/fakegithub"
    26  )
    27  
    28  func TestOptions_Validate(t *testing.T) {
    29  	var testCases = []struct {
    30  		name        string
    31  		input       options
    32  		expectedErr bool
    33  	}{
    34  		{
    35  			name: "all ok",
    36  			input: options{
    37  				jobName: "job",
    38  				config:  configflagutil.ConfigOptions{ConfigPath: "somewhere"},
    39  			},
    40  			expectedErr: false,
    41  		},
    42  		{
    43  			name: "missing config",
    44  			input: options{
    45  				jobName: "job",
    46  			},
    47  			expectedErr: true,
    48  		},
    49  		{
    50  			name: "missing job",
    51  			input: options{
    52  				config: configflagutil.ConfigOptions{ConfigPath: "somewhere"},
    53  			},
    54  			expectedErr: true,
    55  		},
    56  	}
    57  
    58  	for _, testCase := range testCases {
    59  		err := testCase.input.Validate()
    60  		if testCase.expectedErr && err == nil {
    61  			t.Errorf("%s: expected an error but got none", testCase.name)
    62  		}
    63  		if !testCase.expectedErr && err != nil {
    64  			t.Errorf("%s: expected no error but got one: %v", testCase.name, err)
    65  		}
    66  	}
    67  }
    68  
    69  func TestDefaultPR(t *testing.T) {
    70  	author := "Bernardo Soares"
    71  	sha := "Esther Greenwood"
    72  	fakeGitHubClient := fakegithub.NewFakeClient()
    73  	fakeGitHubClient.PullRequests = map[int]*github.PullRequest{2: {
    74  		User: github.User{Login: author},
    75  		Head: github.PullRequestBranch{SHA: sha},
    76  	}}
    77  	o := &options{pullNumber: 2, githubClient: fakeGitHubClient}
    78  	pjs := &prowapi.ProwJobSpec{Refs: &prowapi.Refs{Pulls: []prowapi.Pull{{Number: 2}}}}
    79  	if err := o.defaultPR(pjs); err != nil {
    80  		t.Fatalf("Expected no err when defaulting PJ, but got %v", err)
    81  	}
    82  	if pjs.Refs.Pulls[0].Author != author {
    83  		t.Errorf("Expected author to get defaulted to %s but got %s", author, pjs.Refs.Pulls[0].Author)
    84  	}
    85  	if pjs.Refs.Pulls[0].SHA != sha {
    86  		t.Errorf("Expectged sha to get defaulted to %s but got %s", sha, pjs.Refs.Pulls[0].SHA)
    87  	}
    88  }
    89  
    90  func TestDefaultBaseRef(t *testing.T) {
    91  	testCases := []struct {
    92  		name            string
    93  		baseRef         string
    94  		expectedBaseSha string
    95  		pullNumber      int
    96  		prBaseSha       string
    97  	}{
    98  		{
    99  			name:            "Default for Presubmit",
   100  			expectedBaseSha: "Theodore Decker",
   101  			pullNumber:      2,
   102  			prBaseSha:       "Theodore Decker",
   103  		},
   104  		{
   105  			name:            "Default for Postsubmit",
   106  			baseRef:         "master",
   107  			expectedBaseSha: fakegithub.TestRef,
   108  		},
   109  	}
   110  
   111  	for _, test := range testCases {
   112  		t.Run(test.name, func(t *testing.T) {
   113  			fakeGitHubClient := fakegithub.NewFakeClient()
   114  			fakeGitHubClient.PullRequests = map[int]*github.PullRequest{2: {Base: github.PullRequestBranch{
   115  				SHA: test.prBaseSha,
   116  			}}}
   117  			o := &options{pullNumber: test.pullNumber, githubClient: fakeGitHubClient}
   118  			pjs := &prowapi.ProwJobSpec{Refs: &prowapi.Refs{BaseRef: test.baseRef}}
   119  			if err := o.defaultBaseRef(pjs); err != nil {
   120  				t.Fatalf("Error when calling defaultBaseRef: %v", err)
   121  			}
   122  			if pjs.Refs.BaseSHA != test.expectedBaseSha {
   123  				t.Errorf("Expected BaseSHA to be %s after defaulting but was %s",
   124  					test.expectedBaseSha, pjs.Refs.BaseSHA)
   125  			}
   126  		})
   127  	}
   128  }