Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit 8c6470f

Browse files
pinglesJoseph-Irving
authored andcommitted
Testing: Add leaktest (#192)
* add leaktest
1 parent ef2cadd commit 8c6470f

File tree

13 files changed

+329
-9
lines changed

13 files changed

+329
-9
lines changed

Gopkg.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,7 @@
9696
[prune]
9797
go-tests = true
9898
unused-packages = true
99+
100+
[[constraint]]
101+
name = "github.com/fortytw2/leaktest"
102+
version = "1.3.0"

pkg/aws/metadata/handler_credentials_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package metadata
33
import (
44
"context"
55
"encoding/json"
6+
"github.com/fortytw2/leaktest"
67
"github.com/gorilla/mux"
78
"github.com/uswitch/kiam/pkg/aws/sts"
89
"github.com/uswitch/kiam/pkg/server"
@@ -22,6 +23,7 @@ func init() {
2223
func TestReturnsCredentials(t *testing.T) {
2324
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
2425
defer cancel()
26+
defer leaktest.Check(t)()
2527

2628
r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials/role", nil)
2729
rr := httptest.NewRecorder()
@@ -60,6 +62,7 @@ func TestReturnsCredentials(t *testing.T) {
6062
func TestReturnsErrorWithNoPod(t *testing.T) {
6163
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
6264
defer cancel()
65+
defer leaktest.Check(t)()
6366

6467
r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials/role", nil)
6568
rr := httptest.NewRecorder()
@@ -82,6 +85,7 @@ func TestReturnsErrorWithNoPod(t *testing.T) {
8285
func TestReturnsCredentialsWithRetryAfterError(t *testing.T) {
8386
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
8487
defer cancel()
88+
defer leaktest.Check(t)()
8589

8690
r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials/role", nil)
8791
rr := httptest.NewRecorder()

pkg/aws/metadata/handler_proxy_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ package metadata
1515

1616
import (
1717
"context"
18+
"github.com/fortytw2/leaktest"
19+
"github.com/gorilla/mux"
20+
"github.com/prometheus/client_golang/prometheus"
1821
"net/http"
1922
"net/http/httptest"
2023
"regexp"
2124
"strings"
2225
"testing"
2326
"time"
24-
25-
"github.com/gorilla/mux"
26-
"github.com/prometheus/client_golang/prometheus"
2727
)
2828

2929
func performRequest(allowed, path string) (int, *httptest.ResponseRecorder) {
@@ -48,6 +48,8 @@ func performRequest(allowed, path string) (int, *httptest.ResponseRecorder) {
4848
}
4949

5050
func TestProxyDefaultBlacklistingRoot(t *testing.T) {
51+
defer leaktest.Check(t)()
52+
5153
hits, rr := performRequest("", "/")
5254

5355
if hits != 0 {
@@ -77,6 +79,8 @@ func readPrometheusSimpleCounterValue(name string) float64 {
7779
}
7880

7981
func TestProxyFiltering(t *testing.T) {
82+
defer leaktest.Check(t)()
83+
8084
requestsInitial := readPrometheusCounterValue("kiam_metadata_responses_total", "handler", "proxy")
8185
blockedInitial := readPrometheusSimpleCounterValue("kiam_metadata_proxy_requests_blocked_total")
8286
hits, rr := performRequest("foo.*", "/bar")
@@ -92,16 +96,18 @@ func TestProxyFiltering(t *testing.T) {
9296
}
9397

9498
responses := readPrometheusCounterValue("kiam_metadata_responses_total", "handler", "proxy")
95-
if responses - requestsInitial != 1 {
99+
if responses-requestsInitial != 1 {
96100
t.Error("expected responses_total to be 1, was", responses)
97101
}
98102
blocked := readPrometheusSimpleCounterValue("kiam_metadata_proxy_requests_blocked_total")
99-
if blocked - blockedInitial != 1 {
103+
if blocked-blockedInitial != 1 {
100104
t.Error("expected blocked total to be 1, was", blocked)
101105
}
102106
}
103107

104108
func TestProxyFilteringSubpath(t *testing.T) {
109+
defer leaktest.Check(t)()
110+
105111
hits, rr := performRequest("foo.*", "/bar/baz")
106112

107113
if hits != 0 {
@@ -116,6 +122,8 @@ func TestProxyFilteringSubpath(t *testing.T) {
116122
}
117123

118124
func TestProxyWhitelisting(t *testing.T) {
125+
defer leaktest.Check(t)()
126+
119127
hits, rr := performRequest("foo.*", "/foo")
120128

121129
if hits != 1 {

pkg/aws/metadata/handler_role_name_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package metadata
33
import (
44
"context"
55
"fmt"
6+
"github.com/fortytw2/leaktest"
67
"github.com/gorilla/mux"
78
"github.com/prometheus/client_golang/prometheus"
89
"github.com/uswitch/kiam/pkg/server"
@@ -14,6 +15,8 @@ import (
1415
)
1516

1617
func TestRedirectsToCanonicalPath(t *testing.T) {
18+
defer leaktest.Check(t)()
19+
1720
r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials", nil)
1821
rr := httptest.NewRecorder()
1922

@@ -48,6 +51,8 @@ func readPrometheusCounterValue(name, labelName, labelValue string) float64 {
4851
}
4952

5053
func TestIncrementsPrometheusCounter(t *testing.T) {
54+
defer leaktest.Check(t)()
55+
5156
r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials/", nil)
5257
rr := httptest.NewRecorder()
5358

@@ -68,6 +73,8 @@ func TestIncrementsPrometheusCounter(t *testing.T) {
6873
}
6974

7075
func TestReturnRoleWhenClientResponds(t *testing.T) {
76+
defer leaktest.Check(t)()
77+
7178
r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials/", nil)
7279
rr := httptest.NewRecorder()
7380
handler := newRoleHandler(st.NewStubClient().WithRoles(st.GetRoleResult{"foo_role", nil}), getBlankClientIP)
@@ -87,6 +94,8 @@ func TestReturnRoleWhenClientResponds(t *testing.T) {
8794
}
8895

8996
func TestReturnRoleWhenRetryingFollowingError(t *testing.T) {
97+
defer leaktest.Check(t)()
98+
9099
r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials/", nil)
91100
rr := httptest.NewRecorder()
92101
handler := newRoleHandler(st.NewStubClient().WithRoles(st.GetRoleResult{"", fmt.Errorf("unexpected error")}, st.GetRoleResult{"foo_role", nil}), getBlankClientIP)
@@ -106,6 +115,8 @@ func TestReturnRoleWhenRetryingFollowingError(t *testing.T) {
106115
}
107116

108117
func TestReturnsEmptyRoleWhenClientSucceedsWithEmptyRole(t *testing.T) {
118+
defer leaktest.Check(t)()
119+
109120
r, _ := http.NewRequest("GET", "/latest/meta-data/iam/security-credentials/", nil)
110121
rr := httptest.NewRecorder()
111122
handler := newRoleHandler(st.NewStubClient().WithRoles(st.GetRoleResult{"", nil}), getBlankClientIP)
@@ -120,6 +131,8 @@ func TestReturnsEmptyRoleWhenClientSucceedsWithEmptyRole(t *testing.T) {
120131
}
121132

122133
func TestReturnErrorWhenPodNotFoundWithinTimeout(t *testing.T) {
134+
defer leaktest.Check(t)()
135+
123136
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
124137
defer cancel()
125138

pkg/future/future_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ package future
1515

1616
import (
1717
"context"
18+
"github.com/fortytw2/leaktest"
1819
"testing"
1920
"time"
2021
)
2122

2223
func TestReturnsValue(t *testing.T) {
24+
defer leaktest.Check(t)()
25+
2326
f := New(func() (interface{}, error) {
2427
return "hello", nil
2528
})
@@ -37,6 +40,8 @@ func TestReturnsValue(t *testing.T) {
3740
}
3841

3942
func TestCancelsWhenBlocked(t *testing.T) {
43+
defer leaktest.Check(t)()
44+
4045
f := New(func() (interface{}, error) {
4146
time.Sleep(1 * time.Second)
4247
return "bar", nil

pkg/k8s/pod_cache_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ package k8s
1616
import (
1717
"context"
1818
"fmt"
19-
"testing"
20-
"time"
21-
19+
"github.com/fortytw2/leaktest"
2220
"github.com/uswitch/kiam/pkg/statsd"
2321
"github.com/uswitch/kiam/pkg/testutil"
2422
kt "k8s.io/client-go/tools/cache/testing"
23+
"testing"
24+
"time"
2525
)
2626

2727
func init() {
@@ -31,6 +31,8 @@ func init() {
3131
const bufferSize = 10
3232

3333
func TestFindsRunningPod(t *testing.T) {
34+
defer leaktest.Check(t)()
35+
3436
ctx, cancel := context.WithCancel(context.Background())
3537
defer cancel()
3638

@@ -39,6 +41,7 @@ func TestFindsRunningPod(t *testing.T) {
3941
source.Add(testutil.NewPodWithRole("ns", "name", "192.168.0.1", "Failed", "failed_role"))
4042
source.Add(testutil.NewPodWithRole("ns", "name", "192.168.0.1", "Running", "running_role"))
4143
c.Run(ctx)
44+
defer source.Shutdown()
4245

4346
found, _ := c.GetPodByIP("192.168.0.1")
4447
if found == nil {
@@ -50,6 +53,8 @@ func TestFindsRunningPod(t *testing.T) {
5053
}
5154

5255
func TestFindRoleActive(t *testing.T) {
56+
defer leaktest.Check(t)()
57+
5358
ctx, cancel := context.WithCancel(context.Background())
5459
defer cancel()
5560

@@ -59,6 +64,7 @@ func TestFindRoleActive(t *testing.T) {
5964
source.Modify(testutil.NewPodWithRole("ns", "name", "192.168.0.1", "Failed", "running_role"))
6065
source.Modify(testutil.NewPodWithRole("ns", "name", "192.168.0.1", "Running", "running_role"))
6166
c.Run(ctx)
67+
defer source.Shutdown()
6268

6369
active, _ := c.IsActivePodsForRole("failed_role")
6470
if active {

pkg/prefetch/manager_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package prefetch
1515

1616
import (
1717
"context"
18+
"github.com/fortytw2/leaktest"
1819
"github.com/uswitch/kiam/pkg/aws/sts"
1920
kt "github.com/uswitch/kiam/pkg/k8s/testing"
2021
"github.com/uswitch/kiam/pkg/statsd"
@@ -28,6 +29,8 @@ func init() {
2829
}
2930

3031
func TestPrefetchRunningPods(t *testing.T) {
32+
defer leaktest.Check(t)()
33+
3134
ctx, cancel := context.WithCancel(context.Background())
3235
defer cancel()
3336

pkg/server/server_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/aws/aws-sdk-go/aws/awserr"
7+
"github.com/fortytw2/leaktest"
78
"github.com/uswitch/kiam/pkg/aws/sts"
89
"github.com/uswitch/kiam/pkg/k8s"
910
"github.com/uswitch/kiam/pkg/statsd"
@@ -37,7 +38,11 @@ func TestErrorSimplification(t *testing.T) {
3738
}
3839

3940
func TestReturnsErrorWhenPodNotFound(t *testing.T) {
41+
defer leaktest.Check(t)()
42+
4043
source := kt.NewFakeControllerSource()
44+
defer source.Shutdown()
45+
4146
podCache := k8s.NewPodCache(source, time.Second, defaultBuffer)
4247
server := &KiamServer{pods: podCache}
4348

@@ -49,10 +54,13 @@ func TestReturnsErrorWhenPodNotFound(t *testing.T) {
4954
}
5055

5156
func TestReturnsPolicyErrorWhenForbidden(t *testing.T) {
57+
defer leaktest.Check(t)()
58+
5259
ctx, cancel := context.WithCancel(context.Background())
5360
defer cancel()
5461

5562
source := kt.NewFakeControllerSource()
63+
defer source.Shutdown()
5664
source.Add(testutil.NewPodWithRole("ns", "name", "192.168.0.1", "Running", "running_role"))
5765

5866
podCache := k8s.NewPodCache(source, time.Second, defaultBuffer)
@@ -67,10 +75,13 @@ func TestReturnsPolicyErrorWhenForbidden(t *testing.T) {
6775
}
6876

6977
func TestReturnsCredentials(t *testing.T) {
78+
defer leaktest.Check(t)()
79+
7080
ctx, cancel := context.WithCancel(context.Background())
7181
defer cancel()
7282

7383
source := kt.NewFakeControllerSource()
84+
defer source.Shutdown()
7485
source.Add(testutil.NewPodWithRole("ns", "name", "192.168.0.1", "Running", "running_role"))
7586

7687
podCache := k8s.NewPodCache(source, time.Second, defaultBuffer)

vendor/github.com/fortytw2/leaktest/.travis.yml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)