golang.org/x/build@v0.0.0-20240506185731-218518f32b70/perfdata/client_test.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package perfdata 6 7 import ( 8 "bytes" 9 "context" 10 "fmt" 11 "io" 12 "net/http" 13 "net/http/httptest" 14 "reflect" 15 "testing" 16 17 "golang.org/x/build/internal/diff" 18 "golang.org/x/perf/storage/benchfmt" 19 ) 20 21 func TestQueryError(t *testing.T) { 22 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 23 http.Error(w, "invalid query", 500) 24 })) 25 defer ts.Close() 26 27 c := &Client{BaseURL: ts.URL} 28 29 s, err := c.Query(context.Background(), "invalid query") 30 if err == nil { 31 s.Close() 32 t.Error("Err = nil, want error") 33 } 34 } 35 36 func TestQuery(t *testing.T) { 37 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 38 if have, want := r.URL.RequestURI(), "/search?q=key1%3Avalue+key2%3Avalue"; have != want { 39 t.Errorf("RequestURI = %q, want %q", have, want) 40 } 41 fmt.Fprintf(w, "key: value\nBenchmarkOne 5 ns/op\nkey: value2\nBenchmarkTwo 10 ns/op\n") 42 })) 43 defer ts.Close() 44 45 c := &Client{BaseURL: ts.URL} 46 47 s, err := c.Query(context.Background(), "key1:value key2:value") 48 if err != nil { 49 t.Fatalf("Err: %v", err) 50 } 51 defer s.Close() 52 q := benchfmt.NewReader(s) 53 54 var buf bytes.Buffer 55 bp := benchfmt.NewPrinter(&buf) 56 57 for q.Next() { 58 if err := bp.Print(q.Result()); err != nil { 59 t.Fatalf("Print: %v", err) 60 } 61 } 62 if err := q.Err(); err != nil { 63 t.Fatalf("Err: %v", err) 64 } 65 want := "key: value\nBenchmarkOne 5 ns/op\nkey: value2\nBenchmarkTwo 10 ns/op\n" 66 if diff := diff.Diff("have", buf.Bytes(), "want", []byte(want)); diff != nil { 67 t.Errorf("wrong results:\n%s", diff) 68 } 69 } 70 71 func TestListUploads(t *testing.T) { 72 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 73 if have, want := r.URL.RequestURI(), "/uploads?extra_label=key1&extra_label=key2&limit=10&q=key1%3Avalue+key2%3Avalue"; have != want { 74 t.Errorf("RequestURI = %q, want %q", have, want) 75 } 76 fmt.Fprintf(w, "%s\n", `{"UploadID": "id", "Count": 100, "LabelValues": {"key1": "value"}}`) 77 })) 78 defer ts.Close() 79 80 c := &Client{BaseURL: ts.URL} 81 82 r := c.ListUploads(context.Background(), "key1:value key2:value", []string{"key1", "key2"}, 10) 83 defer r.Close() 84 85 if !r.Next() { 86 t.Errorf("Next = false, want true") 87 } 88 if have, want := r.Info(), (UploadInfo{Count: 100, UploadID: "id", LabelValues: benchfmt.Labels{"key1": "value"}}); !reflect.DeepEqual(have, want) { 89 t.Errorf("Info = %#v, want %#v", have, want) 90 } 91 if err := r.Err(); err != nil { 92 t.Fatalf("Err: %v", err) 93 } 94 } 95 96 func TestNewUpload(t *testing.T) { 97 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 98 if have, want := r.URL.RequestURI(), "/upload"; have != want { 99 t.Errorf("RequestURI = %q, want %q", have, want) 100 } 101 mr, err := r.MultipartReader() 102 if err != nil { 103 t.Error(err) 104 } 105 i := 0 106 for i = 0; ; i++ { 107 p, err := mr.NextPart() 108 if err == io.EOF { 109 break 110 } 111 name := p.FormName() 112 if name == "commit" { 113 continue 114 } 115 if name != "file" { 116 t.Errorf("unexpected field %q, want file", name) 117 } 118 if have, want := p.FileName(), fmt.Sprintf("want%d.txt", i); have != want { 119 t.Errorf("file name = %q, want %q", have, want) 120 } 121 content, _ := io.ReadAll(p) 122 if have, want := string(content), "content"; have != want { 123 t.Errorf("unexpected content %q, want %q", have, want) 124 } 125 } 126 if i != 3 { 127 t.Errorf("number of files = %d, want %d", i, 3) 128 } 129 fmt.Fprintf(w, "%s\n", `{"uploadid": "id", "fileids": ["id/1", "id/2"]}`) 130 })) 131 defer ts.Close() 132 133 c := &Client{BaseURL: ts.URL} 134 135 u := c.NewUpload(context.Background()) 136 for i := 0; i < 2; i++ { 137 w, err := u.CreateFile(fmt.Sprintf("want%d.txt", i)) 138 if err != nil { 139 t.Fatalf("CreateFile = %v", err) 140 } 141 if _, err := fmt.Fprintf(w, "content"); err != nil { 142 t.Fatalf("Write returned %v", err) 143 } 144 } 145 status, err := u.Commit() 146 if err != nil { 147 t.Errorf("Commit = %v", err) 148 } 149 if status.UploadID != "id" { 150 t.Errorf("status.UploadID = %q, want %q", status.UploadID, "id") 151 } 152 } 153 154 func TestNewUploadAbort(t *testing.T) { 155 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 156 if have, want := r.URL.RequestURI(), "/upload"; have != want { 157 t.Errorf("RequestURI = %q, want %q", have, want) 158 } 159 mr, err := r.MultipartReader() 160 if err != nil { 161 t.Error(err) 162 } 163 i := 0 164 for i = 0; ; i++ { 165 p, err := mr.NextPart() 166 if err == io.EOF { 167 break 168 } 169 name := p.FormName() 170 if name == "abort" { 171 continue 172 } 173 if name != "file" { 174 t.Errorf("unexpected field %q, want file or abort", name) 175 } 176 if have, want := p.FileName(), fmt.Sprintf("want%d.txt", i); have != want { 177 t.Errorf("file name = %q, want %q", have, want) 178 } 179 content, _ := io.ReadAll(p) 180 if have, want := string(content), "content"; have != want { 181 t.Errorf("unexpected content %q, want %q", have, want) 182 } 183 } 184 if i != 3 { 185 t.Errorf("number of files = %d, want %d", i, 3) 186 } 187 fmt.Fprintf(w, "%s\n", `{"uploadid": "id", "fileids": ["id/1", "id/2"]}`) 188 })) 189 defer ts.Close() 190 191 c := &Client{BaseURL: ts.URL} 192 193 u := c.NewUpload(context.Background()) 194 for i := 0; i < 2; i++ { 195 w, err := u.CreateFile(fmt.Sprintf("want%d.txt", i)) 196 if err != nil { 197 t.Fatalf("CreateFile = %v", err) 198 } 199 if _, err := fmt.Fprintf(w, "content"); err != nil { 200 t.Fatalf("Write returned %v", err) 201 } 202 } 203 if err := u.Abort(); err != nil { 204 t.Errorf("Abort = %v", err) 205 } 206 }