sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/config/org/org_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 org 18 19 import ( 20 "encoding/json" 21 "reflect" 22 "testing" 23 24 "k8s.io/apimachinery/pkg/util/diff" 25 ) 26 27 func TestPrivacy(t *testing.T) { 28 get := func(v Privacy) *Privacy { 29 return &v 30 } 31 cases := []struct { 32 input string 33 expected *Privacy 34 }{ 35 { 36 "secret", 37 get(Secret), 38 }, 39 { 40 "closed", 41 get(Closed), 42 }, 43 { 44 "", 45 nil, 46 }, 47 { 48 "unknown", 49 nil, 50 }, 51 } 52 for _, tc := range cases { 53 var actual Privacy 54 err := json.Unmarshal([]byte("\""+tc.input+"\""), &actual) 55 switch { 56 case err == nil && tc.expected == nil: 57 t.Errorf("%s: failed to receive an error", tc.input) 58 case err != nil && tc.expected != nil: 59 t.Errorf("%s: unexpected error: %v", tc.input, err) 60 case err == nil && *tc.expected != actual: 61 t.Errorf("%s: actual %v != expected %v", tc.input, tc.expected, actual) 62 } 63 } 64 } 65 66 func TestPruneRepoDefaults(t *testing.T) { 67 empty := "" 68 nonEmpty := "string that is not empty" 69 yes := true 70 no := false 71 master := "master" 72 notMaster := "not-master" 73 testCases := []struct { 74 description string 75 repo Repo 76 expected Repo 77 }{ 78 { 79 description: "default values are pruned", 80 repo: Repo{ 81 Description: &empty, 82 HomePage: &empty, 83 Private: &no, 84 HasIssues: &yes, 85 HasProjects: &yes, 86 HasWiki: &yes, 87 AllowSquashMerge: &yes, 88 AllowMergeCommit: &yes, 89 AllowRebaseMerge: &yes, 90 DefaultBranch: &master, 91 Archived: &no, 92 }, 93 expected: Repo{HasProjects: &yes}, 94 }, 95 { 96 description: "non-default values are not pruned", 97 repo: Repo{ 98 Description: &nonEmpty, 99 HomePage: &nonEmpty, 100 Private: &yes, 101 HasIssues: &no, 102 HasProjects: &no, 103 HasWiki: &no, 104 AllowSquashMerge: &no, 105 AllowMergeCommit: &no, 106 AllowRebaseMerge: &no, 107 DefaultBranch: ¬Master, 108 Archived: &yes, 109 }, 110 expected: Repo{Description: &nonEmpty, 111 HomePage: &nonEmpty, 112 Private: &yes, 113 HasIssues: &no, 114 HasProjects: &no, 115 HasWiki: &no, 116 AllowSquashMerge: &no, 117 AllowMergeCommit: &no, 118 AllowRebaseMerge: &no, 119 DefaultBranch: ¬Master, 120 Archived: &yes, 121 }, 122 }, 123 } 124 125 for _, tc := range testCases { 126 t.Run(tc.description, func(t *testing.T) { 127 pruned := PruneRepoDefaults(tc.repo) 128 if !reflect.DeepEqual(tc.expected, pruned) { 129 t.Errorf("%s: result differs from expected:\n", diff.ObjectReflectDiff(tc.expected, pruned)) 130 } 131 }) 132 } 133 }