github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/experiment/bootstrap/paths_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 "path/filepath" 21 "reflect" 22 "testing" 23 ) 24 25 func TestCIPaths(t *testing.T) { 26 testCases := []struct { 27 Name string 28 Base string 29 Job string 30 Build string 31 Expected *Paths 32 }{ 33 { 34 Name: "normal", 35 Base: "/some/foo/base", 36 Job: "some-foo-job", 37 Build: "1337", 38 Expected: &Paths{ 39 Artifacts: filepath.Join("/some/foo/base", "some-foo-job", "1337", "artifacts"), 40 BuildLog: filepath.Join("/some/foo/base", "some-foo-job", "1337", "build-log.txt"), 41 Finished: filepath.Join("/some/foo/base", "some-foo-job", "1337", "finished.json"), 42 Latest: filepath.Join("/some/foo/base", "some-foo-job", "latest-build.txt"), 43 ResultCache: filepath.Join("/some/foo/base", "some-foo-job", "jobResultsCache.json"), 44 Started: filepath.Join("/some/foo/base", "some-foo-job", "1337", "started.json"), 45 }, 46 }, 47 } 48 for _, test := range testCases { 49 res := CIPaths(test.Base, test.Job, test.Build) 50 if !reflect.DeepEqual(res, test.Expected) { 51 t.Errorf("Paths did not match expected for test: %#v", test.Name) 52 t.Errorf("%#v", res) 53 t.Errorf("%#v", test.Expected) 54 } 55 } 56 } 57 58 func TestPRPaths(t *testing.T) { 59 // create some Repos values for use in the test cases below 60 reposEmtpy := Repos{} 61 reposK8sIO, err := ParseRepos([]string{"k8s.io/kubernetes=master:42e2ca8c18c93ba25eb0e5bd02ecba2eaa05e871,52057:b4f639f57ae0a89cdf1b43d1810b617c76f4b1b3,2001:03a564a5309ea84065fb203f628b50c382b65a50"}) 62 if err != nil { 63 t.Errorf("got unexpected error parsing test repos: %v", err) 64 } 65 reposK8sIOTestInfra, err := ParseRepos([]string{"k8s.io/test-infra=master:42e2ca8c18c93ba25eb0e5bd02ecba2eaa05e871,52057:b4f639f57ae0a89cdf1b43d1810b617c76f4b1b3"}) 66 if err != nil { 67 t.Errorf("got unexpected error parsing test repos: %v", err) 68 } 69 reposKubernetes, err := ParseRepos([]string{"kubernetes/test-infra=master:0efa7e1b,2001:8b376c6c"}) 70 if err != nil { 71 t.Errorf("got unexpected error parsing test repos: %v", err) 72 } 73 reposGithub, err := ParseRepos([]string{"github.com/foo/bar=master:0efa7e1b,2001:8b376c6c"}) 74 if err != nil { 75 t.Errorf("got unexpected error parsing test repos: %v", err) 76 } 77 reposOther, err := ParseRepos([]string{"example.com/foo/bar=master:0efa7e1b,2001:8b376c6c"}) 78 if err != nil { 79 t.Errorf("got unexpected error parsing test repos: %v", err) 80 } 81 // assert some known expected values and the expected failure for len(repos) == 0 82 testCases := []struct { 83 Name string 84 Base string 85 Repos Repos 86 Job string 87 Build string 88 Expected *Paths 89 ExpectErr bool 90 }{ 91 { 92 Name: "normal-k8s.io/kubernetes", 93 Base: "/base", 94 Job: "some-job", 95 Repos: reposK8sIO, 96 Build: "1337", 97 Expected: &Paths{ 98 Artifacts: filepath.Join("/base", "pull", "batch", "some-job", "1337", "artifacts"), 99 BuildLog: filepath.Join("/base", "pull", "batch", "some-job", "1337", "build-log.txt"), 100 PRPath: filepath.Join("/base", "pull", "batch", "some-job", "1337"), 101 PRBuildLink: filepath.Join("/base", "directory", "some-job", "1337.txt"), 102 PRLatest: filepath.Join("/base", "pull", "batch", "some-job", "latest-build.txt"), 103 PRResultCache: filepath.Join("/base", "pull", "batch", "some-job", "jobResultsCache.json"), 104 ResultCache: filepath.Join("/base", "directory", "some-job", "jobResultsCache.json"), 105 Started: filepath.Join("/base", "pull", "batch", "some-job", "1337", "started.json"), 106 Finished: filepath.Join("/base", "pull", "batch", "some-job", "1337", "finished.json"), 107 Latest: filepath.Join("/base", "directory", "some-job", "latest-build.txt"), 108 }, 109 ExpectErr: false, 110 }, 111 { 112 Name: "normal-k8s.io/test-infra", 113 Base: "/base", 114 Job: "some-job", 115 Repos: reposK8sIOTestInfra, 116 Build: "1337", 117 Expected: &Paths{ 118 Artifacts: filepath.Join("/base", "pull", "test-infra", "52057", "some-job", "1337", "artifacts"), 119 BuildLog: filepath.Join("/base", "pull", "test-infra", "52057", "some-job", "1337", "build-log.txt"), 120 PRPath: filepath.Join("/base", "pull", "test-infra", "52057", "some-job", "1337"), 121 PRBuildLink: filepath.Join("/base", "directory", "some-job", "1337.txt"), 122 PRLatest: filepath.Join("/base", "pull", "test-infra", "52057", "some-job", "latest-build.txt"), 123 PRResultCache: filepath.Join("/base", "pull", "test-infra", "52057", "some-job", "jobResultsCache.json"), 124 ResultCache: filepath.Join("/base", "directory", "some-job", "jobResultsCache.json"), 125 Started: filepath.Join("/base", "pull", "test-infra", "52057", "some-job", "1337", "started.json"), 126 Finished: filepath.Join("/base", "pull", "test-infra", "52057", "some-job", "1337", "finished.json"), 127 Latest: filepath.Join("/base", "directory", "some-job", "latest-build.txt"), 128 }, 129 ExpectErr: false, 130 }, 131 { 132 Name: "normal-kubernetes/test-infra", 133 Base: "/base", 134 Job: "some-job", 135 Repos: reposKubernetes, 136 Build: "1337", 137 Expected: &Paths{ 138 Artifacts: filepath.Join("/base", "pull", "test-infra", "2001", "some-job", "1337", "artifacts"), 139 BuildLog: filepath.Join("/base", "pull", "test-infra", "2001", "some-job", "1337", "build-log.txt"), 140 PRPath: filepath.Join("/base", "pull", "test-infra", "2001", "some-job", "1337"), 141 PRBuildLink: filepath.Join("/base", "directory", "some-job", "1337.txt"), 142 PRLatest: filepath.Join("/base", "pull", "test-infra", "2001", "some-job", "latest-build.txt"), 143 PRResultCache: filepath.Join("/base", "pull", "test-infra", "2001", "some-job", "jobResultsCache.json"), 144 ResultCache: filepath.Join("/base", "directory", "some-job", "jobResultsCache.json"), 145 Started: filepath.Join("/base", "pull", "test-infra", "2001", "some-job", "1337", "started.json"), 146 Finished: filepath.Join("/base", "pull", "test-infra", "2001", "some-job", "1337", "finished.json"), 147 Latest: filepath.Join("/base", "directory", "some-job", "latest-build.txt"), 148 }, 149 ExpectErr: false, 150 }, 151 { 152 Name: "normal-github", 153 Base: "/base", 154 Job: "some-job", 155 Repos: reposGithub, 156 Build: "1337", 157 Expected: &Paths{ 158 Artifacts: filepath.Join("/base", "pull", "foo_bar", "2001", "some-job", "1337", "artifacts"), 159 BuildLog: filepath.Join("/base", "pull", "foo_bar", "2001", "some-job", "1337", "build-log.txt"), 160 PRPath: filepath.Join("/base", "pull", "foo_bar", "2001", "some-job", "1337"), 161 PRBuildLink: filepath.Join("/base", "directory", "some-job", "1337.txt"), 162 PRLatest: filepath.Join("/base", "pull", "foo_bar", "2001", "some-job", "latest-build.txt"), 163 PRResultCache: filepath.Join("/base", "pull", "foo_bar", "2001", "some-job", "jobResultsCache.json"), 164 ResultCache: filepath.Join("/base", "directory", "some-job", "jobResultsCache.json"), 165 Started: filepath.Join("/base", "pull", "foo_bar", "2001", "some-job", "1337", "started.json"), 166 Finished: filepath.Join("/base", "pull", "foo_bar", "2001", "some-job", "1337", "finished.json"), 167 Latest: filepath.Join("/base", "directory", "some-job", "latest-build.txt"), 168 }, 169 ExpectErr: false, 170 }, 171 { 172 Name: "normal-other", 173 Base: "/base", 174 Job: "some-job", 175 Repos: reposOther, 176 Build: "1337", 177 Expected: &Paths{ 178 Artifacts: filepath.Join("/base", "pull", "example.com_foo_bar", "2001", "some-job", "1337", "artifacts"), 179 BuildLog: filepath.Join("/base", "pull", "example.com_foo_bar", "2001", "some-job", "1337", "build-log.txt"), 180 PRPath: filepath.Join("/base", "pull", "example.com_foo_bar", "2001", "some-job", "1337"), 181 PRBuildLink: filepath.Join("/base", "directory", "some-job", "1337.txt"), 182 PRLatest: filepath.Join("/base", "pull", "example.com_foo_bar", "2001", "some-job", "latest-build.txt"), 183 PRResultCache: filepath.Join("/base", "pull", "example.com_foo_bar", "2001", "some-job", "jobResultsCache.json"), 184 ResultCache: filepath.Join("/base", "/directory/some-job", "jobResultsCache.json"), 185 Started: filepath.Join("/base", "pull", "example.com_foo_bar", "2001", "some-job", "1337", "started.json"), 186 Finished: filepath.Join("/base", "pull", "example.com_foo_bar", "2001", "some-job", "1337", "finished.json"), 187 Latest: filepath.Join("/base", "/directory/some-job", "latest-build.txt"), 188 }, 189 ExpectErr: false, 190 }, 191 { 192 Name: "expect-failure (no repos)", 193 Base: "/some/foo/base", 194 Job: "some-foo-job", 195 Repos: reposEmtpy, 196 Build: "1337", 197 Expected: nil, 198 ExpectErr: true, 199 }, 200 } 201 for _, test := range testCases { 202 res, err := PRPaths(test.Base, test.Repos, test.Job, test.Build) 203 if test.ExpectErr && err == nil { 204 t.Errorf("err == nil and error expected for test %#v", test.Name) 205 } else if err != nil && !test.ExpectErr { 206 t.Errorf("Got error and did not expect one for test %#v, %v", test.Name, err) 207 } else if !reflect.DeepEqual(res, test.Expected) { 208 t.Errorf("Paths did not match expected for test: %#v", test.Name) 209 t.Errorf("%#v", res) 210 t.Errorf("%#v", test.Expected) 211 } 212 } 213 } 214 215 func TestGubernatorBuildURL(t *testing.T) { 216 // test with and without gs:// 217 tests := []struct { 218 Name string 219 Paths *Paths 220 Expected string 221 }{ 222 { 223 Name: "with gs://", 224 Paths: CIPaths("gs://foo", "bar", "baz"), 225 Expected: "https://gubernator.k8s.io/build/foo/bar/baz", 226 }, 227 { 228 Name: "without gs://", 229 Paths: CIPaths("/foo", "bar", "baz"), 230 Expected: "/foo/bar/baz", 231 }, 232 } 233 for _, test := range tests { 234 res := GubernatorBuildURL(test.Paths) 235 if res != test.Expected { 236 t.Errorf("result did not match expected for test case %#v", test.Name) 237 t.Errorf("%#v != %#v", res, test.Expected) 238 } 239 } 240 }