github.com/abayer/test-infra@v0.0.5/prow/cmd/branchprotector/request_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 "reflect" 21 "testing" 22 23 branchprotection "k8s.io/test-infra/prow/config" 24 "k8s.io/test-infra/prow/github" 25 ) 26 27 func TestMakeBool(t *testing.T) { 28 yes := true 29 no := false 30 cases := []struct { 31 input *bool 32 expected bool 33 }{ 34 { 35 input: nil, 36 expected: false, 37 }, 38 { 39 input: &no, 40 expected: false, 41 }, 42 { 43 input: &yes, 44 expected: true, 45 }, 46 } 47 for _, tc := range cases { 48 if actual := makeBool(tc.input); actual != tc.expected { 49 t.Errorf("%v: actual %v != expected %t", tc.input, actual, tc.expected) 50 } 51 } 52 } 53 54 func TestMakeReviews(t *testing.T) { 55 zero := 0 56 three := 3 57 one := 1 58 yes := true 59 cases := []struct { 60 name string 61 input *branchprotection.ReviewPolicy 62 expected *github.RequiredPullRequestReviews 63 }{ 64 { 65 name: "nil returns nil", 66 }, 67 { 68 name: "nil apporvals returns nil", 69 input: &branchprotection.ReviewPolicy{ 70 Approvals: nil, 71 }, 72 }, 73 { 74 name: "0 approvals returns nil", 75 input: &branchprotection.ReviewPolicy{ 76 Approvals: &zero, 77 }, 78 }, 79 { 80 name: "approvals set", 81 input: &branchprotection.ReviewPolicy{ 82 Approvals: &three, 83 }, 84 expected: &github.RequiredPullRequestReviews{ 85 RequiredApprovingReviewCount: 3, 86 }, 87 }, 88 { 89 name: "set all", 90 input: &branchprotection.ReviewPolicy{ 91 Approvals: &one, 92 RequireOwners: &yes, 93 DismissStale: &yes, 94 DismissalRestrictions: &branchprotection.Restrictions{ 95 Users: []string{"fred", "jane"}, 96 Teams: []string{"megacorp", "startup"}, 97 }, 98 }, 99 expected: &github.RequiredPullRequestReviews{ 100 RequiredApprovingReviewCount: 1, 101 RequireCodeOwnerReviews: true, 102 DismissStaleReviews: true, 103 DismissalRestrictions: github.Restrictions{ 104 Teams: &[]string{"megacorp", "startup"}, 105 Users: &[]string{"fred", "jane"}, 106 }, 107 }, 108 }, 109 } 110 111 for _, tc := range cases { 112 actual := makeReviews(tc.input) 113 if !reflect.DeepEqual(actual, tc.expected) { 114 t.Errorf("%s: actual %v != expected %v", tc.name, actual, tc.expected) 115 } 116 } 117 } 118 119 func TestMakeRequest(t *testing.T) { 120 yes := true 121 cases := []struct { 122 name string 123 policy branchprotection.Policy 124 expected github.BranchProtectionRequest 125 }{ 126 { 127 name: "Empty works", 128 }, 129 { 130 name: "teams != nil => users != nil", 131 policy: branchprotection.Policy{ 132 Restrictions: &branchprotection.Restrictions{ 133 Teams: []string{"hello"}, 134 }, 135 }, 136 expected: github.BranchProtectionRequest{ 137 Restrictions: &github.Restrictions{ 138 Teams: &[]string{"hello"}, 139 Users: &[]string{}, 140 }, 141 }, 142 }, 143 { 144 name: "users != nil => teams != nil", 145 policy: branchprotection.Policy{ 146 Restrictions: &branchprotection.Restrictions{ 147 Users: []string{"there"}, 148 }, 149 }, 150 expected: github.BranchProtectionRequest{ 151 Restrictions: &github.Restrictions{ 152 Users: &[]string{"there"}, 153 Teams: &[]string{}, 154 }, 155 }, 156 }, 157 { 158 name: "Strict => Contexts != nil", 159 policy: branchprotection.Policy{ 160 RequiredStatusChecks: &branchprotection.ContextPolicy{ 161 Strict: &yes, 162 }, 163 }, 164 expected: github.BranchProtectionRequest{ 165 RequiredStatusChecks: &github.RequiredStatusChecks{ 166 Strict: true, 167 Contexts: []string{}, 168 }, 169 }, 170 }, 171 } 172 for _, tc := range cases { 173 t.Run(tc.name, func(t *testing.T) { 174 actual := makeRequest(tc.policy) 175 expected := tc.expected 176 if !reflect.DeepEqual(actual, expected) { 177 t.Errorf("actual %+v != expected %+v", actual, expected) 178 } 179 }) 180 } 181 }