Skip to content

Commit 2d92119

Browse files
author
Julien Pivotto
committed
Split test to its file
Signed-off-by: Julien Pivotto <[email protected]>
1 parent 000e2df commit 2d92119

File tree

2 files changed

+85
-65
lines changed

2 files changed

+85
-65
lines changed

web/tls_config_test.go

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

1818
import (
19-
"context"
2019
"crypto/tls"
2120
"crypto/x509"
2221
"errors"
@@ -586,67 +585,3 @@ func TestUsers(t *testing.T) {
586585
t.Run(testInputs.Name, testInputs.Test)
587586
}
588587
}
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-
}

web/users_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2021 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+
"context"
18+
"net/http"
19+
"sync"
20+
"testing"
21+
)
22+
23+
// TestBasicAuthCache validates that the cache is working by calling a password
24+
// protected endpoint multiple times.
25+
func TestBasicAuthCache(t *testing.T) {
26+
server := &http.Server{
27+
Addr: port,
28+
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
29+
w.Write([]byte("Hello World!"))
30+
}),
31+
}
32+
33+
done := make(chan struct{})
34+
t.Cleanup(func() {
35+
if err := server.Shutdown(context.Background()); err != nil {
36+
t.Fatal(err)
37+
}
38+
<-done
39+
})
40+
41+
go func() {
42+
ListenAndServe(server, "testdata/tls_config_users_noTLS.good.yml", testlogger)
43+
close(done)
44+
}()
45+
46+
login := func(username, password string, code int) {
47+
client := &http.Client{}
48+
req, err := http.NewRequest("GET", "http://localhost"+port, nil)
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
req.SetBasicAuth(username, password)
53+
r, err := client.Do(req)
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
if r.StatusCode != code {
58+
t.Fatalf("bad return code, expected %d, got %d", code, r.StatusCode)
59+
}
60+
}
61+
62+
// Initial logins, checking that it just works.
63+
login("alice", "alice123", 200)
64+
login("alice", "alice1234", 401)
65+
66+
var (
67+
start = make(chan struct{})
68+
wg sync.WaitGroup
69+
)
70+
wg.Add(300)
71+
for i := 0; i < 150; i++ {
72+
go func() {
73+
<-start
74+
login("alice", "alice123", 200)
75+
wg.Done()
76+
}()
77+
go func() {
78+
<-start
79+
login("alice", "alice1234", 401)
80+
wg.Done()
81+
}()
82+
}
83+
close(start)
84+
wg.Wait()
85+
}

0 commit comments

Comments
 (0)