sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/clonerefs/parse_test.go (about) 1 /* 2 Copyright 2017 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 "reflect" 21 "testing" 22 23 "k8s.io/apimachinery/pkg/util/diff" 24 prowapi "sigs.k8s.io/prow/pkg/apis/prowjobs/v1" 25 ) 26 27 func TestParseRefs(t *testing.T) { 28 var testCases = []struct { 29 name string 30 value string 31 expected *prowapi.Refs 32 expectErr bool 33 }{ 34 { 35 name: "base branch only", 36 value: "org,repo=branch", 37 expected: &prowapi.Refs{ 38 Org: "org", 39 Repo: "repo", 40 BaseRef: "branch", 41 }, 42 expectErr: false, 43 }, 44 { 45 name: "base branch and sha", 46 value: "org,repo=branch:sha", 47 expected: &prowapi.Refs{ 48 Org: "org", 49 Repo: "repo", 50 BaseRef: "branch", 51 BaseSHA: "sha", 52 }, 53 expectErr: false, 54 }, 55 { 56 name: "base branch and pr number only", 57 value: "org,repo=branch,1", 58 expected: &prowapi.Refs{ 59 Org: "org", 60 Repo: "repo", 61 BaseRef: "branch", 62 Pulls: []prowapi.Pull{{Number: 1}}, 63 }, 64 expectErr: false, 65 }, 66 { 67 name: "base branch and pr number and sha", 68 value: "org,repo=branch,1:sha", 69 expected: &prowapi.Refs{ 70 Org: "org", 71 Repo: "repo", 72 BaseRef: "branch", 73 Pulls: []prowapi.Pull{{Number: 1, SHA: "sha"}}, 74 }, 75 expectErr: false, 76 }, 77 { 78 name: "base branch, sha, pr number and sha", 79 value: "org,repo=branch:sha,1:pull-sha", 80 expected: &prowapi.Refs{ 81 Org: "org", 82 Repo: "repo", 83 BaseRef: "branch", 84 BaseSHA: "sha", 85 Pulls: []prowapi.Pull{{Number: 1, SHA: "pull-sha"}}, 86 }, 87 expectErr: false, 88 }, 89 { 90 name: "base branch and multiple prs", 91 value: "org,repo=branch,1,2,3", 92 expected: &prowapi.Refs{ 93 Org: "org", 94 Repo: "repo", 95 BaseRef: "branch", 96 Pulls: []prowapi.Pull{{Number: 1}, {Number: 2}, {Number: 3}}, 97 }, 98 expectErr: false, 99 }, 100 { 101 name: "base branch and multiple prs with shas", 102 value: "org,repo=branch:sha,1:pull-1-sha,2:pull-2-sha,3:pull-3-sha", 103 expected: &prowapi.Refs{ 104 Org: "org", 105 Repo: "repo", 106 BaseRef: "branch", 107 BaseSHA: "sha", 108 Pulls: []prowapi.Pull{{Number: 1, SHA: "pull-1-sha"}, {Number: 2, SHA: "pull-2-sha"}, {Number: 3, SHA: "pull-3-sha"}}, 109 }, 110 expectErr: false, 111 }, 112 { 113 name: "base branch and pr with refs", 114 value: "org,repo=branch:sha,1:pull-1-sha:pull-1-ref", 115 expected: &prowapi.Refs{ 116 Org: "org", 117 Repo: "repo", 118 BaseRef: "branch", 119 BaseSHA: "sha", 120 Pulls: []prowapi.Pull{{Number: 1, SHA: "pull-1-sha", Ref: "pull-1-ref"}}, 121 }, 122 expectErr: false, 123 }, 124 { 125 name: "no org or repo", 126 value: "branch:sha", 127 expectErr: true, 128 }, 129 { 130 name: "no repo", 131 value: "org=branch", 132 expectErr: true, 133 }, 134 { 135 name: "no refs", 136 value: "org,repo=", 137 expectErr: true, 138 }, 139 { 140 name: "malformed base ref", 141 value: "org,repo=branch:whatever:sha", 142 expectErr: true, 143 }, 144 { 145 name: "malformed pull ref", 146 value: "org,repo=branch:sha,1:what:so:ever", 147 expectErr: true, 148 }, 149 { 150 name: "malformed pull number", 151 value: "org,repo=branch:sha,NaN:sha", 152 expectErr: true, 153 }, 154 } 155 156 for _, testCase := range testCases { 157 actual, err := ParseRefs(testCase.value) 158 if testCase.expectErr && err == nil { 159 t.Errorf("%s: expected an error but got none", testCase.name) 160 } 161 if !testCase.expectErr && err != nil { 162 t.Errorf("%s: expected no error but got %v", testCase.name, err) 163 } 164 165 if !testCase.expectErr && !reflect.DeepEqual(actual, testCase.expected) { 166 t.Errorf("%s: incorrect refs parsed:\n%s", testCase.name, diff.ObjectReflectDiff(testCase.expected, actual)) 167 } 168 } 169 }