github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/cmd/tot/main_test.go (about) 1 /* 2 Copyright 2016 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 "fmt" 21 "io/ioutil" 22 "net/http" 23 "net/http/httptest" 24 "os" 25 "reflect" 26 "strings" 27 "testing" 28 ) 29 30 func expectEqual(t *testing.T, msg string, have interface{}, want interface{}) { 31 if !reflect.DeepEqual(have, want) { 32 t.Errorf("bad %s: got %v, wanted %v", 33 msg, have, want) 34 } 35 } 36 37 func makeStore(t *testing.T) *store { 38 tmp, err := ioutil.TempFile("", "tot_test_") 39 if err != nil { 40 t.Fatal(err) 41 } 42 os.Remove(tmp.Name()) // json decoding an empty file throws an error 43 44 store, err := newStore(tmp.Name()) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 return store 50 } 51 52 func TestVend(t *testing.T) { 53 store := makeStore(t) 54 defer os.Remove(store.storagePath) 55 56 expectEqual(t, "empty vend", store.vend("a"), 1) 57 expectEqual(t, "second vend", store.vend("a"), 2) 58 expectEqual(t, "third vend", store.vend("a"), 3) 59 expectEqual(t, "second empty", store.vend("b"), 1) 60 61 store2, err := newStore(store.storagePath) 62 if err != nil { 63 t.Fatal(err) 64 } 65 expectEqual(t, "fourth vend, different instance", store2.vend("a"), 4) 66 } 67 68 func TestSet(t *testing.T) { 69 store := makeStore(t) 70 defer os.Remove(store.storagePath) 71 72 store.set("foo", 300) 73 expectEqual(t, "peek", store.peek("foo"), 300) 74 store.set("foo2", 300) 75 expectEqual(t, "vend", store.vend("foo2"), 301) 76 expectEqual(t, "vend", store.vend("foo2"), 302) 77 } 78 79 func expectResponse(t *testing.T, handler http.Handler, req *http.Request, msg, value string) { 80 rr := httptest.NewRecorder() 81 82 handler.ServeHTTP(rr, req) 83 84 expectEqual(t, "http status OK", rr.Code, 200) 85 86 expectEqual(t, msg, rr.Body.String(), value) 87 } 88 89 func TestHandler(t *testing.T) { 90 store := makeStore(t) 91 defer os.Remove(store.storagePath) 92 93 handler := http.HandlerFunc(store.handle) 94 95 req, err := http.NewRequest("GET", "/vend/foo", nil) 96 if err != nil { 97 t.Fatal(err) 98 } 99 100 expectResponse(t, handler, req, "http vend", "1") 101 expectResponse(t, handler, req, "http vend", "2") 102 103 req, err = http.NewRequest("HEAD", "/vend/foo", nil) 104 if err != nil { 105 t.Fatal(err) 106 } 107 108 expectResponse(t, handler, req, "http peek", "2") 109 110 req, err = http.NewRequest("POST", "/vend/bar", strings.NewReader("40")) 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 expectResponse(t, handler, req, "http post", "") 116 117 req, err = http.NewRequest("HEAD", "/vend/bar", nil) 118 if err != nil { 119 t.Fatal(err) 120 } 121 122 expectResponse(t, handler, req, "http vend", "40") 123 } 124 125 type mapHandler map[string]string 126 127 func (h mapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 128 fmt.Fprintf(w, "%s", h[r.URL.Path]) 129 } 130 131 func TestFallback(t *testing.T) { 132 serv := httptest.NewServer(mapHandler(map[string]string{ 133 "/logs/foo/latest-build.txt": "200", 134 "/logs/bar/latest-build.txt": "\t300 \n", 135 "/logs/baz/latest-build.txt": "asdf", 136 })) 137 defer serv.Close() 138 139 store := makeStore(t) 140 defer os.Remove(store.storagePath) 141 store.fallbackFunc = fallbackHandler{serv.URL + "/logs/%s/latest-build.txt"}.get 142 143 expectEqual(t, "vend foo 1", store.vend("foo"), 201) 144 expectEqual(t, "vend foo 2", store.vend("foo"), 202) 145 146 expectEqual(t, "vend bar", store.vend("bar"), 301) 147 expectEqual(t, "vend baz", store.vend("baz"), 1) 148 expectEqual(t, "vend quux", store.vend("quux"), 1) 149 }