Add shutdown to test environment

Ensures test http server is fully closed before moving on with test.

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
Derek McGowan 2016-08-26 16:20:54 -07:00
parent 38ee36eee4
commit 798b7331ca
2 changed files with 38 additions and 12 deletions

View file

@ -43,6 +43,7 @@ var headerConfig = http.Header{
// 200 OK response. // 200 OK response.
func TestCheckAPI(t *testing.T) { func TestCheckAPI(t *testing.T) {
env := newTestEnv(t, false) env := newTestEnv(t, false)
defer env.Shutdown()
baseURL, err := env.builder.BuildBaseURL() baseURL, err := env.builder.BuildBaseURL()
if err != nil { if err != nil {
t.Fatalf("unexpected error building base url: %v", err) t.Fatalf("unexpected error building base url: %v", err)
@ -74,6 +75,7 @@ func TestCheckAPI(t *testing.T) {
func TestCatalogAPI(t *testing.T) { func TestCatalogAPI(t *testing.T) {
chunkLen := 2 chunkLen := 2
env := newTestEnv(t, false) env := newTestEnv(t, false)
defer env.Shutdown()
values := url.Values{ values := url.Values{
"last": []string{""}, "last": []string{""},
@ -229,6 +231,7 @@ func TestURLPrefix(t *testing.T) {
config.HTTP.Headers = headerConfig config.HTTP.Headers = headerConfig
env := newTestEnvWithConfig(t, &config) env := newTestEnvWithConfig(t, &config)
defer env.Shutdown()
baseURL, err := env.builder.BuildBaseURL() baseURL, err := env.builder.BuildBaseURL()
if err != nil { if err != nil {
@ -276,20 +279,23 @@ func makeBlobArgs(t *testing.T) blobArgs {
// TestBlobAPI conducts a full test of the of the blob api. // TestBlobAPI conducts a full test of the of the blob api.
func TestBlobAPI(t *testing.T) { func TestBlobAPI(t *testing.T) {
deleteEnabled := false deleteEnabled := false
env := newTestEnv(t, deleteEnabled) env1 := newTestEnv(t, deleteEnabled)
defer env1.Shutdown()
args := makeBlobArgs(t) args := makeBlobArgs(t)
testBlobAPI(t, env, args) testBlobAPI(t, env1, args)
deleteEnabled = true deleteEnabled = true
env = newTestEnv(t, deleteEnabled) env2 := newTestEnv(t, deleteEnabled)
defer env2.Shutdown()
args = makeBlobArgs(t) args = makeBlobArgs(t)
testBlobAPI(t, env, args) testBlobAPI(t, env2, args)
} }
func TestBlobDelete(t *testing.T) { func TestBlobDelete(t *testing.T) {
deleteEnabled := true deleteEnabled := true
env := newTestEnv(t, deleteEnabled) env := newTestEnv(t, deleteEnabled)
defer env.Shutdown()
args := makeBlobArgs(t) args := makeBlobArgs(t)
env = testBlobAPI(t, env, args) env = testBlobAPI(t, env, args)
@ -308,6 +314,7 @@ func TestRelativeURL(t *testing.T) {
config.HTTP.Headers = headerConfig config.HTTP.Headers = headerConfig
config.HTTP.RelativeURLs = false config.HTTP.RelativeURLs = false
env := newTestEnvWithConfig(t, &config) env := newTestEnvWithConfig(t, &config)
defer env.Shutdown()
ref, _ := reference.WithName("foo/bar") ref, _ := reference.WithName("foo/bar")
uploadURLBaseAbs, _ := startPushLayer(t, env, ref) uploadURLBaseAbs, _ := startPushLayer(t, env, ref)
@ -375,6 +382,7 @@ func TestRelativeURL(t *testing.T) {
func TestBlobDeleteDisabled(t *testing.T) { func TestBlobDeleteDisabled(t *testing.T) {
deleteEnabled := false deleteEnabled := false
env := newTestEnv(t, deleteEnabled) env := newTestEnv(t, deleteEnabled)
defer env.Shutdown()
args := makeBlobArgs(t) args := makeBlobArgs(t)
imageName := args.imageName imageName := args.imageName
@ -690,6 +698,7 @@ func testBlobDelete(t *testing.T, env *testEnv, args blobArgs) {
func TestDeleteDisabled(t *testing.T) { func TestDeleteDisabled(t *testing.T) {
env := newTestEnv(t, false) env := newTestEnv(t, false)
defer env.Shutdown()
imageName, _ := reference.ParseNamed("foo/bar") imageName, _ := reference.ParseNamed("foo/bar")
// "build" our layer file // "build" our layer file
@ -716,6 +725,7 @@ func TestDeleteDisabled(t *testing.T) {
func TestDeleteReadOnly(t *testing.T) { func TestDeleteReadOnly(t *testing.T) {
env := newTestEnv(t, true) env := newTestEnv(t, true)
defer env.Shutdown()
imageName, _ := reference.ParseNamed("foo/bar") imageName, _ := reference.ParseNamed("foo/bar")
// "build" our layer file // "build" our layer file
@ -744,6 +754,7 @@ func TestDeleteReadOnly(t *testing.T) {
func TestStartPushReadOnly(t *testing.T) { func TestStartPushReadOnly(t *testing.T) {
env := newTestEnv(t, true) env := newTestEnv(t, true)
defer env.Shutdown()
env.app.readOnly = true env.app.readOnly = true
imageName, _ := reference.ParseNamed("foo/bar") imageName, _ := reference.ParseNamed("foo/bar")
@ -788,16 +799,18 @@ func TestManifestAPI(t *testing.T) {
schema2Repo, _ := reference.ParseNamed("foo/schema2") schema2Repo, _ := reference.ParseNamed("foo/schema2")
deleteEnabled := false deleteEnabled := false
env := newTestEnv(t, deleteEnabled) env1 := newTestEnv(t, deleteEnabled)
testManifestAPISchema1(t, env, schema1Repo) defer env1.Shutdown()
schema2Args := testManifestAPISchema2(t, env, schema2Repo) testManifestAPISchema1(t, env1, schema1Repo)
testManifestAPIManifestList(t, env, schema2Args) schema2Args := testManifestAPISchema2(t, env1, schema2Repo)
testManifestAPIManifestList(t, env1, schema2Args)
deleteEnabled = true deleteEnabled = true
env = newTestEnv(t, deleteEnabled) env2 := newTestEnv(t, deleteEnabled)
testManifestAPISchema1(t, env, schema1Repo) defer env2.Shutdown()
schema2Args = testManifestAPISchema2(t, env, schema2Repo) testManifestAPISchema1(t, env2, schema1Repo)
testManifestAPIManifestList(t, env, schema2Args) schema2Args = testManifestAPISchema2(t, env2, schema2Repo)
testManifestAPIManifestList(t, env2, schema2Args)
} }
func TestManifestDelete(t *testing.T) { func TestManifestDelete(t *testing.T) {
@ -806,6 +819,7 @@ func TestManifestDelete(t *testing.T) {
deleteEnabled := true deleteEnabled := true
env := newTestEnv(t, deleteEnabled) env := newTestEnv(t, deleteEnabled)
defer env.Shutdown()
schema1Args := testManifestAPISchema1(t, env, schema1Repo) schema1Args := testManifestAPISchema1(t, env, schema1Repo)
testManifestDelete(t, env, schema1Args) testManifestDelete(t, env, schema1Args)
schema2Args := testManifestAPISchema2(t, env, schema2Repo) schema2Args := testManifestAPISchema2(t, env, schema2Repo)
@ -816,6 +830,7 @@ func TestManifestDeleteDisabled(t *testing.T) {
schema1Repo, _ := reference.ParseNamed("foo/schema1") schema1Repo, _ := reference.ParseNamed("foo/schema1")
deleteEnabled := false deleteEnabled := false
env := newTestEnv(t, deleteEnabled) env := newTestEnv(t, deleteEnabled)
defer env.Shutdown()
testManifestDeleteDisabled(t, env, schema1Repo) testManifestDeleteDisabled(t, env, schema1Repo)
} }
@ -1947,6 +1962,11 @@ func newTestEnvWithConfig(t *testing.T, config *configuration.Configuration) *te
} }
} }
func (t *testEnv) Shutdown() {
t.server.CloseClientConnections()
t.server.Close()
}
func putManifest(t *testing.T, msg, url, contentType string, v interface{}) *http.Response { func putManifest(t *testing.T, msg, url, contentType string, v interface{}) *http.Response {
var body []byte var body []byte
@ -2340,6 +2360,7 @@ func createRepository(env *testEnv, t *testing.T, imageName string, tag string)
func TestRegistryAsCacheMutationAPIs(t *testing.T) { func TestRegistryAsCacheMutationAPIs(t *testing.T) {
deleteEnabled := true deleteEnabled := true
env := newTestEnvMirror(t, deleteEnabled) env := newTestEnvMirror(t, deleteEnabled)
defer env.Shutdown()
imageName, _ := reference.ParseNamed("foo/bar") imageName, _ := reference.ParseNamed("foo/bar")
tag := "latest" tag := "latest"
@ -2398,6 +2419,7 @@ func TestRegistryAsCacheMutationAPIs(t *testing.T) {
// that implements http.ContextNotifier. // that implements http.ContextNotifier.
func TestCheckContextNotifier(t *testing.T) { func TestCheckContextNotifier(t *testing.T) {
env := newTestEnv(t, false) env := newTestEnv(t, false)
defer env.Shutdown()
// Register a new endpoint for testing // Register a new endpoint for testing
env.app.router.Handle("/unittest/{name}/", env.app.dispatcher(func(ctx *Context, r *http.Request) http.Handler { env.app.router.Handle("/unittest/{name}/", env.app.dispatcher(func(ctx *Context, r *http.Request) http.Handler {
@ -2437,6 +2459,7 @@ func TestProxyManifestGetByTag(t *testing.T) {
tag := "latest" tag := "latest"
truthEnv := newTestEnvWithConfig(t, &truthConfig) truthEnv := newTestEnvWithConfig(t, &truthConfig)
defer truthEnv.Shutdown()
// create a repository in the truth registry // create a repository in the truth registry
dgst := createRepository(truthEnv, t, imageName.Name(), tag) dgst := createRepository(truthEnv, t, imageName.Name(), tag)
@ -2451,6 +2474,7 @@ func TestProxyManifestGetByTag(t *testing.T) {
proxyConfig.HTTP.Headers = headerConfig proxyConfig.HTTP.Headers = headerConfig
proxyEnv := newTestEnvWithConfig(t, &proxyConfig) proxyEnv := newTestEnvWithConfig(t, &proxyConfig)
defer proxyEnv.Shutdown()
digestRef, _ := reference.WithDigest(imageName, dgst) digestRef, _ := reference.WithDigest(imageName, dgst)
manifestDigestURL, err := proxyEnv.builder.BuildManifestURL(digestRef) manifestDigestURL, err := proxyEnv.builder.BuildManifestURL(digestRef)

View file

@ -38,6 +38,7 @@ func TestAppDispatcher(t *testing.T) {
registry: registry, registry: registry,
} }
server := httptest.NewServer(app) server := httptest.NewServer(app)
defer server.Close()
router := v2.Router() router := v2.Router()
serverURL, err := url.Parse(server.URL) serverURL, err := url.Parse(server.URL)
@ -163,6 +164,7 @@ func TestNewApp(t *testing.T) {
app := NewApp(ctx, &config) app := NewApp(ctx, &config)
server := httptest.NewServer(app) server := httptest.NewServer(app)
defer server.Close()
builder, err := v2.NewURLBuilderFromString(server.URL, false) builder, err := v2.NewURLBuilderFromString(server.URL, false)
if err != nil { if err != nil {
t.Fatalf("error creating urlbuilder: %v", err) t.Fatalf("error creating urlbuilder: %v", err)