github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/experiment/bootstrap/repos_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 main 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 func TestParseRepos(t *testing.T) { 25 testCases := []struct { 26 Name string 27 Repos []string 28 Expected Repos 29 ExpectErr bool 30 }{ 31 { 32 Name: "normal", 33 Repos: []string{ 34 "k8s.io/kubernetes=master:42e2ca8c18c93ba25eb0e5bd02ecba2eaa05e871,52057:b4f639f57ae0a89cdf1b43d1810b617c76f4b1b3", 35 "k8s.io/release", 36 }, 37 Expected: []Repo{ 38 { 39 Name: "k8s.io/kubernetes", 40 Branch: "", 41 Pull: "master:42e2ca8c18c93ba25eb0e5bd02ecba2eaa05e871,52057:b4f639f57ae0a89cdf1b43d1810b617c76f4b1b3", 42 }, 43 { 44 Name: "k8s.io/release", 45 Branch: "master", 46 Pull: "", 47 }, 48 }, 49 }, 50 { 51 Name: "single-commit", 52 Repos: []string{ 53 "k8s.io/kubernetes=master:42e2ca8c18c93ba25eb0e5bd02ecba2eaa05e871", 54 }, 55 Expected: []Repo{ 56 { 57 Name: "k8s.io/kubernetes", 58 Branch: "master:42e2ca8c18c93ba25eb0e5bd02ecba2eaa05e871", 59 Pull: "", 60 }, 61 }, 62 ExpectErr: false, 63 }, 64 { 65 Name: "expect-to-fail (invalid repo)", 66 Repos: []string{ 67 "k8s.io/kubernetes=master:42e2ca8c18c93ba25eb0e5bd02ecba2eaa05e871,52057:b4f63https://github.com/googlecartographer/point_cloud_viewer9f57ae0a89cdf1b43d1810b617c76f4b1b3", 68 "k8s.io/release", 69 "foobar=,=", 70 }, 71 Expected: nil, 72 ExpectErr: true, 73 }, 74 } 75 for _, test := range testCases { 76 res, err := ParseRepos(test.Repos) 77 if test.ExpectErr && err == nil { 78 t.Errorf("err == nil and error expected for test %#v", test.Name) 79 } else if err != nil && !test.ExpectErr { 80 t.Errorf("Got error and did not expect one for test %#v: %v", test.Name, err) 81 } else if !reflect.DeepEqual(res, test.Expected) { 82 t.Errorf("Repos did not match expected for test: %#v", test.Name) 83 t.Errorf("%#v", res) 84 t.Errorf("%#v", test.Expected) 85 // assert that currently Repos.Main() == Repos[0] 86 } else if len(test.Expected) > 0 && res.Main() != &res[0] { 87 t.Errorf("Expected repos.Main() to be &res[0] for all tests (test: %#v)", test.Name) 88 } 89 } 90 } 91 92 func TestRepoGitBasePath(t *testing.T) { 93 // TODO(bentheelder): use ParseRepos instead of "hand-written" Repo{}s? 94 // these are based on expected Repos from TestParseRepos 95 testCases := []struct { 96 Name string 97 Repo Repo 98 SSH bool 99 Expected string 100 }{ 101 { 102 Name: "k8s.io", 103 Repo: Repo{ 104 Name: "k8s.io/kubernetes", 105 Branch: "master", 106 Pull: "", 107 }, 108 SSH: false, 109 Expected: "https://github.com/kubernetes/kubernetes", 110 }, 111 { 112 Name: "k8s.io,ssh", 113 Repo: Repo{ 114 Name: "k8s.io/kubernetes", 115 Branch: "master", 116 Pull: "", 117 }, 118 SSH: true, 119 Expected: "git@github.com:kubernetes/kubernetes", 120 }, 121 { 122 Name: "kubernetes/test-infra", 123 Repo: Repo{ 124 Name: "github.com/kubernetes/test-infra", 125 Branch: "master", 126 Pull: "", 127 }, 128 SSH: false, 129 Expected: "https://github.com/kubernetes/test-infra", 130 }, 131 { 132 Name: "kubernetes/test-infra,ssh", 133 Repo: Repo{ 134 Name: "github.com/kubernetes/test-infra", 135 Branch: "master", 136 Pull: "", 137 }, 138 SSH: true, 139 Expected: "git@github.com:kubernetes/test-infra", 140 }, 141 } 142 for _, test := range testCases { 143 res := test.Repo.GitBasePath(test.SSH) 144 if res != test.Expected { 145 t.Errorf("result did not match expected for test case %#v", test.Name) 146 t.Errorf("%#v != %#v", res, test.Expected) 147 } 148 } 149 } 150 151 func TestRepoPullNumbers(t *testing.T) { 152 // TODO(bentheelder): use ParseRepos instead of "hand-written" Repo{}s? 153 // these are based on expected Repos from TestParseRepos 154 testCases := []struct { 155 Name string 156 Repo Repo 157 Expected []string 158 }{ 159 { 160 Name: "", 161 Repo: Repo{ 162 Name: "k8s.io/kubernetes", 163 Branch: "", 164 Pull: "master:42e2ca8c18c93ba25eb0e5bd02ecba2eaa05e871,52057:b4f639f57ae0a89cdf1b43d1810b617c76f4b1b3", 165 }, 166 Expected: []string{"52057"}, 167 }, 168 } 169 for _, test := range testCases { 170 res := test.Repo.PullNumbers() 171 if !reflect.DeepEqual(res, test.Expected) { 172 t.Errorf("result did not match expected for test case %#v", test.Name) 173 t.Errorf("%#v", res) 174 t.Errorf("%#v", test.Expected) 175 } 176 } 177 } 178 179 func TestRepos(t *testing.T) { 180 // assert that Repos.Main() is nil for empty Repos 181 emptyRepos := Repos{} 182 if emptyRepos.Main() != nil { 183 t.Errorf("Expected emptyRepos.Main() == nil") 184 } 185 // assert that currently Repos.Main() == Repos[0] 186 repos, err := ParseRepos([]string{ 187 "k8s.io/release", 188 }) 189 if err != nil { 190 t.Errorf("Expected err to be nil") 191 } 192 if repos.Main() != &repos[0] { 193 t.Errorf("Expected repos.Main() to be &repos[0]") 194 } 195 }