github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/cmd/deck/job_history_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 "net/url" 21 "testing" 22 ) 23 24 func TestJobHistURL(t *testing.T) { 25 cases := []struct { 26 name string 27 address string 28 bktName string 29 root string 30 id int64 31 expErr bool 32 }{ 33 { 34 address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e", 35 bktName: "foo-bucket", 36 root: "logs/bar-e2e", 37 id: emptyID, 38 }, 39 { 40 address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=", 41 bktName: "foo-bucket", 42 root: "logs/bar-e2e", 43 id: emptyID, 44 }, 45 { 46 address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=123456789123456789", 47 bktName: "foo-bucket", 48 root: "logs/bar-e2e", 49 id: 123456789123456789, 50 }, 51 { 52 address: "http://www.example.com/job-history/foo-bucket", 53 expErr: true, 54 }, 55 { 56 address: "http://www.example.com/job-history/foo-bucket/", 57 expErr: true, 58 }, 59 { 60 address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=-738", 61 expErr: true, 62 }, 63 { 64 address: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=nope", 65 expErr: true, 66 }, 67 } 68 for _, tc := range cases { 69 u, _ := url.Parse(tc.address) 70 bktName, root, id, err := parseJobHistURL(u) 71 if tc.expErr { 72 if err == nil && tc.expErr { 73 t.Errorf("parsing %q: expected error", tc.address) 74 } 75 continue 76 } 77 if err != nil { 78 t.Errorf("parsing %q: unexpected error: %v", tc.address, err) 79 } 80 if bktName != tc.bktName { 81 t.Errorf("parsing %q: expected bucket %s, got %s", tc.address, tc.bktName, bktName) 82 } 83 if root != tc.root { 84 t.Errorf("parsing %q: expected root %s, got %s", tc.address, tc.root, root) 85 } 86 if id != tc.id { 87 t.Errorf("parsing %q: expected id %d, got %d", tc.address, tc.id, id) 88 } 89 } 90 } 91 92 func eq(a, b []int64) bool { 93 if len(a) != len(b) { 94 return false 95 } 96 for i := 0; i < len(a); i++ { 97 if a[i] != b[i] { 98 return false 99 } 100 } 101 return true 102 } 103 104 func TestCropResults(t *testing.T) { 105 cases := []struct { 106 a []int64 107 max int64 108 exp []int64 109 p int 110 q int 111 }{ 112 { 113 a: []int64{}, 114 max: 42, 115 exp: []int64{}, 116 p: -1, 117 q: 0, 118 }, 119 { 120 a: []int64{81, 27, 9, 3, 1}, 121 max: 100, 122 exp: []int64{81, 27, 9, 3, 1}, 123 p: 0, 124 q: 4, 125 }, 126 { 127 a: []int64{81, 27, 9, 3, 1}, 128 max: 50, 129 exp: []int64{27, 9, 3, 1}, 130 p: 1, 131 q: 4, 132 }, 133 { 134 a: []int64{25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, 135 max: 23, 136 exp: []int64{23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4}, 137 p: 2, 138 q: 21, 139 }, 140 } 141 for _, tc := range cases { 142 actual, firstIndex, lastIndex := cropResults(tc.a, tc.max) 143 if !eq(actual, tc.exp) || firstIndex != tc.p || lastIndex != tc.q { 144 t.Errorf("cropResults(%v, %d) expected (%v, %d, %d), got (%v, %d, %d)", 145 tc.a, tc.max, tc.exp, tc.p, tc.q, actual, firstIndex, lastIndex) 146 } 147 } 148 } 149 150 func TestLinkID(t *testing.T) { 151 cases := []struct { 152 startAddr string 153 id int64 154 expAddr string 155 }{ 156 { 157 startAddr: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e", 158 id: -1, 159 expAddr: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=", 160 }, 161 { 162 startAddr: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e", 163 id: 23, 164 expAddr: "http://www.example.com/job-history/foo-bucket/logs/bar-e2e?buildId=23", 165 }, 166 } 167 for _, tc := range cases { 168 u, _ := url.Parse(tc.startAddr) 169 actual := linkID(u, tc.id) 170 if actual != tc.expAddr { 171 t.Errorf("adding id param %d expected %s, got %s", tc.id, tc.expAddr, actual) 172 } 173 again, _ := url.Parse(tc.startAddr) 174 if again.String() != u.String() { 175 t.Errorf("linkID incorrectly mutated URL (expected %s, got %s)", u.String(), again.String()) 176 } 177 } 178 }