github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/cmd/generic-autobumper/helper_test.go (about) 1 /* 2 Copyright 2019 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 23 // TestImageAndTagFromName tests ImageFromName() and TagFromName(). 24 func TestImageAndTagFromName(t *testing.T) { 25 cases := []struct { 26 name string 27 imageName string 28 expectedImage string 29 expectedTag string 30 }{ 31 { 32 name: "empty", 33 imageName: "", 34 expectedImage: "", 35 expectedTag: "", 36 }, 37 { 38 name: "basically works", 39 imageName: "gcr.io/k8s-prow/test:tag1", 40 expectedImage: "gcr.io/k8s-prow/test", 41 expectedTag: "tag1", 42 }, 43 { 44 name: "just an image", 45 imageName: "gcr.io/k8s-prow/test", 46 expectedImage: "", 47 expectedTag: "", 48 }, 49 } 50 51 for _, tc := range cases { 52 t.Run(tc.name, func(t *testing.T) { 53 if actual := imageFromName(tc.imageName); actual != tc.expectedImage { 54 t.Errorf("imageFromName(%q) got %q, want %q", tc.imageName, actual, tc.expectedImage) 55 } 56 if actual := tagFromName(tc.imageName); actual != tc.expectedTag { 57 t.Errorf("tagFromName(%q) got %q, want %q", tc.imageName, actual, tc.expectedTag) 58 } 59 }) 60 } 61 } 62 63 func TestCommitToRef(t *testing.T) { 64 cases := []struct { 65 name string 66 commit string 67 expected string 68 }{ 69 { 70 name: "basically works", 71 }, 72 { 73 name: "just tag works", 74 commit: "v0.0.30", 75 expected: "v0.0.30", 76 }, 77 { 78 name: "just commit works", 79 commit: "deadbeef", 80 expected: "deadbeef", 81 }, 82 { 83 name: "commits past tag works", 84 commit: "v0.0.30-14-gdeadbeef", 85 expected: "deadbeef", 86 }, 87 } 88 89 for _, tc := range cases { 90 t.Run(tc.name, func(t *testing.T) { 91 if actual, expected := commitToRef(tc.commit), tc.expected; actual != tc.expected { 92 t.Errorf("commitToRef(%q) got %q want %q", tc.commit, actual, expected) 93 } 94 }) 95 } 96 } 97 98 func TestIsUnderPath(t *testing.T) { 99 cases := []struct { 100 description string 101 paths []string 102 file string 103 expected bool 104 }{ 105 { 106 description: "file is under the direct path", 107 paths: []string{"config/prow/"}, 108 file: "config/prow/config.yaml", 109 expected: true, 110 }, 111 { 112 description: "file is under the indirect path", 113 paths: []string{"config/prow-staging/"}, 114 file: "config/prow-staging/jobs/config.yaml", 115 expected: true, 116 }, 117 { 118 description: "file is under one path but not others", 119 paths: []string{"config/prow/", "config/prow-staging/"}, 120 file: "config/prow-staging/jobs/whatever-repo/whatever-file", 121 expected: true, 122 }, 123 { 124 description: "file is not under the path but having the same prefix", 125 paths: []string{"config/prow/"}, 126 file: "config/prow-staging/config.yaml", 127 expected: false, 128 }, 129 } 130 131 for _, tc := range cases { 132 t.Run(tc.description, func(t *testing.T) { 133 actual := isUnderPath(tc.file, tc.paths) 134 if actual != tc.expected { 135 t.Errorf("expected to be %t but actual is %t", tc.expected, actual) 136 } 137 }) 138 } 139 }