github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/boskos/client/client_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 client 18 19 import ( 20 "errors" 21 "fmt" 22 "net/http" 23 "net/http/httptest" 24 "reflect" 25 "testing" 26 "time" 27 28 "k8s.io/test-infra/boskos/common" 29 ) 30 31 var FAKE_RES = "{\"name\": \"res\", \"type\": \"t\", \"state\": \"d\"}" 32 var FAKE_MAP = "{\"res\":\"user\"}" 33 var FAKE_METRIC = "{\"type\":\"t\",\"current\":{\"s\":1},\"owner\":{\"merlin\":1}}" 34 35 func AreErrorsEqual(got error, expect error) bool { 36 if got == nil && expect == nil { 37 return true 38 } 39 40 if got == nil || expect == nil { 41 return false 42 } 43 44 return got.Error() == expect.Error() 45 } 46 47 func TestAcquire(t *testing.T) { 48 var testcases = []struct { 49 name string 50 serverErr bool 51 expectErr error 52 }{ 53 { 54 name: "request error", 55 serverErr: true, 56 expectErr: fmt.Errorf("Status %d %s, StatusCode %d", http.StatusBadRequest, http.StatusText(http.StatusBadRequest), http.StatusBadRequest), 57 }, 58 { 59 name: "request successful", 60 expectErr: nil, 61 }, 62 } 63 64 for _, tc := range testcases { 65 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 66 if tc.serverErr { 67 http.Error(w, "", http.StatusBadRequest) 68 } else { 69 fmt.Fprint(w, FAKE_RES) 70 } 71 })) 72 defer ts.Close() 73 74 c := NewClient("user", ts.URL) 75 name, err := c.Acquire("t", "s", "d") 76 77 if !AreErrorsEqual(err, tc.expectErr) { 78 t.Errorf("Test %v, got error %v, expect error %v", tc.name, err, tc.expectErr) 79 } 80 if err == nil { 81 if name != "res" { 82 t.Errorf("Test %v, got resource name %v, expect res", tc.name, name) 83 } else if len(c.resources) != 1 { 84 t.Errorf("Test %v, resource in client: %d, expect 1", tc.name, len(c.resources)) 85 } 86 } 87 } 88 } 89 90 func TestRelease(t *testing.T) { 91 var testcases = []struct { 92 name string 93 resources []string 94 res string 95 expectErr error 96 }{ 97 { 98 name: "all - no res", 99 resources: []string{}, 100 res: "", 101 expectErr: errors.New("No holding resource"), 102 }, 103 { 104 name: "one - no res", 105 resources: []string{}, 106 res: "res", 107 expectErr: errors.New("No resource name res"), 108 }, 109 { 110 name: "one - no match", 111 resources: []string{"foo"}, 112 res: "res", 113 expectErr: errors.New("No resource name res"), 114 }, 115 { 116 name: "all - ok", 117 resources: []string{"foo"}, 118 res: "", 119 expectErr: nil, 120 }, 121 { 122 name: "one - ok", 123 resources: []string{"res"}, 124 res: "res", 125 expectErr: nil, 126 }, 127 } 128 129 for _, tc := range testcases { 130 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) 131 defer ts.Close() 132 133 c := NewClient("user", ts.URL) 134 for _, r := range tc.resources { 135 c.resources = append(c.resources, r) 136 } 137 var err error 138 if tc.res == "" { 139 err = c.ReleaseAll("d") 140 } else { 141 err = c.ReleaseOne(tc.res, "d") 142 } 143 144 if !AreErrorsEqual(err, tc.expectErr) { 145 t.Errorf("Test %v, got err %v, expect %v", tc.name, err, tc.expectErr) 146 } 147 148 if tc.expectErr == nil && len(c.resources) != 0 { 149 t.Errorf("Test %v, resource count %v, expect 0", tc.name, len(c.resources)) 150 } 151 } 152 } 153 154 func TestUpdate(t *testing.T) { 155 var testcases = []struct { 156 name string 157 resources []string 158 res string 159 expectErr error 160 }{ 161 { 162 name: "all - no res", 163 resources: []string{}, 164 res: "", 165 expectErr: errors.New("No holding resource"), 166 }, 167 { 168 name: "one - no res", 169 resources: []string{}, 170 res: "res", 171 expectErr: errors.New("No resource name res"), 172 }, 173 { 174 name: "one - no match", 175 resources: []string{"foo"}, 176 res: "res", 177 expectErr: errors.New("No resource name res"), 178 }, 179 { 180 name: "all - ok", 181 resources: []string{"foo"}, 182 res: "", 183 expectErr: nil, 184 }, 185 { 186 name: "one - ok", 187 resources: []string{"res"}, 188 res: "res", 189 expectErr: nil, 190 }, 191 } 192 193 for _, tc := range testcases { 194 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) 195 defer ts.Close() 196 197 c := NewClient("user", ts.URL) 198 for _, r := range tc.resources { 199 c.resources = append(c.resources, r) 200 } 201 var err error 202 if tc.res == "" { 203 err = c.UpdateAll("s") 204 } else { 205 err = c.UpdateOne(tc.res, "s") 206 } 207 208 if !AreErrorsEqual(err, tc.expectErr) { 209 t.Errorf("Test %v, got err %v, expect %v", tc.name, err, tc.expectErr) 210 } 211 } 212 } 213 214 func TestReset(t *testing.T) { 215 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 216 fmt.Fprint(w, FAKE_MAP) 217 })) 218 defer ts.Close() 219 220 c := NewClient("user", ts.URL) 221 rmap, err := c.Reset("t", "s", time.Minute, "d") 222 if err != nil { 223 t.Errorf("Error in reset : %v", err) 224 } else if len(rmap) != 1 { 225 t.Errorf("Resource in returned map: %d, expect 1", len(c.resources)) 226 } else if rmap["res"] != "user" { 227 t.Errorf("Owner of res: %s, expect user", rmap["res"]) 228 } 229 } 230 231 func TestMetric(t *testing.T) { 232 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 233 fmt.Fprint(w, FAKE_METRIC) 234 })) 235 defer ts.Close() 236 expectMetric := common.Metric{ 237 Type: "t", 238 Current: map[string]int{ 239 "s": 1, 240 }, 241 Owners: map[string]int{ 242 "merlin": 1, 243 }, 244 } 245 246 c := NewClient("user", ts.URL) 247 metric, err := c.Metric("t") 248 if err != nil { 249 t.Errorf("Error in reset : %v", err) 250 } else if !reflect.DeepEqual(metric, expectMetric) { 251 t.Errorf("wrong metric, got %v, want %v", metric, expectMetric) 252 } 253 }