Skip to content

Commit 5f139e5

Browse files
author
Julien Pivotto
committed
Add tests
Signed-off-by: Julien Pivotto <[email protected]>
1 parent cec006b commit 5f139e5

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

web/cache_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2020 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package web
15+
16+
import (
17+
"fmt"
18+
"testing"
19+
)
20+
21+
// TestCacheSize validates that makeRoom function caps the size of the cache
22+
// appropriately.
23+
func TestCacheSize(t *testing.T) {
24+
cache := newCache(100)
25+
expectedSize := 0
26+
for i := 0; i < 200; i++ {
27+
cache.makeRoom()
28+
cache.cache[fmt.Sprintf("foo%d", i)] = true
29+
30+
expectedSize += 1
31+
if expectedSize > 100 {
32+
expectedSize = 90
33+
}
34+
35+
if gotSize := len(cache.cache); gotSize != expectedSize {
36+
t.Fatalf("iter %d: cache size invalid: expected %d, got %d", i, expectedSize, gotSize)
37+
}
38+
}
39+
}

web/tls_config_test.go

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package web
1717

1818
import (
19+
"context"
1920
"crypto/tls"
2021
"crypto/x509"
2122
"errors"
@@ -382,16 +383,14 @@ func (test *TestInputs) Test(t *testing.T) {
382383
w.Write([]byte("Hello World!"))
383384
}),
384385
}
385-
defer func() {
386-
server.Close()
387-
}()
386+
t.Cleanup(func() { server.Close() })
388387
go func() {
389388
defer func() {
390389
if recover() != nil {
391390
recordConnectionError(errors.New("Panic starting server"))
392391
}
393392
}()
394-
err := Listen(server, test.YAMLConfigPath, testlogger)
393+
err := ListenAndServe(server, test.YAMLConfigPath, testlogger)
395394
recordConnectionError(err)
396395
}()
397396

@@ -587,3 +586,67 @@ func TestUsers(t *testing.T) {
587586
t.Run(testInputs.Name, testInputs.Test)
588587
}
589588
}
589+
590+
// TestBasicAuthCache validates that the cache is working by calling a password
591+
// protected endpoint multiple times.
592+
func TestBasicAuthCache(t *testing.T) {
593+
server := &http.Server{
594+
Addr: port,
595+
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
596+
w.Write([]byte("Hello World!"))
597+
}),
598+
}
599+
600+
done := make(chan struct{})
601+
t.Cleanup(func() {
602+
if err := server.Shutdown(context.Background()); err != nil {
603+
t.Fatal(err)
604+
}
605+
<-done
606+
})
607+
608+
go func() {
609+
ListenAndServe(server, "testdata/tls_config_users_noTLS.good.yml", testlogger)
610+
close(done)
611+
}()
612+
613+
login := func(username, password string, code int) {
614+
client := &http.Client{}
615+
req, err := http.NewRequest("GET", "http://localhost"+port, nil)
616+
if err != nil {
617+
t.Fatal(err)
618+
}
619+
req.SetBasicAuth(username, password)
620+
r, err := client.Do(req)
621+
if err != nil {
622+
t.Fatal(err)
623+
}
624+
if r.StatusCode != code {
625+
t.Fatalf("bad return code, expected %d, got %d", code, r.StatusCode)
626+
}
627+
}
628+
629+
// Initial logins, checking that it just works.
630+
login("alice", "alice123", 200)
631+
login("alice", "alice1234", 401)
632+
633+
var (
634+
start = make(chan struct{})
635+
wg sync.WaitGroup
636+
)
637+
wg.Add(200)
638+
for i := 0; i < 100; i++ {
639+
go func() {
640+
<-start
641+
login("alice", "alice123", 200)
642+
wg.Done()
643+
}()
644+
go func() {
645+
<-start
646+
login("alice", "alice1234", 401)
647+
wg.Done()
648+
}()
649+
}
650+
close(start)
651+
wg.Wait()
652+
}

0 commit comments

Comments
 (0)