Browse code

Hack: don't run integration tests in /var/lib/docker/unit-tests; add missing cleanups in a few tests

Solomon Hykes authored on 2013/10/17 05:10:20
Showing 4 changed files
... ...
@@ -112,6 +112,7 @@ func TestGetInfo(t *testing.T) {
112 112
 
113 113
 func TestGetEvents(t *testing.T) {
114 114
 	runtime := mkRuntime(t)
115
+	defer nuke(runtime)
115 116
 	srv := &Server{
116 117
 		runtime:   runtime,
117 118
 		events:    make([]utils.JSONMessage, 0, 64),
... ...
@@ -87,45 +87,63 @@ func init() {
87 87
 
88 88
 	NetworkBridgeIface = unitTestNetworkBridge
89 89
 
90
-	// Make it our Store root
91
-	if runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false); err != nil {
92
-		log.Fatalf("Unable to create a runtime for tests:", err)
93
-	} else {
94
-		globalRuntime = runtime
95
-	}
90
+	// Setup the base runtime, which will be duplicated for each test.
91
+	// (no tests are run directly in the base)
92
+	setupBaseImage()
96 93
 
97
-	// Cleanup any leftover container
98
-	for _, container := range globalRuntime.List() {
99
-		if err := globalRuntime.Destroy(container); err != nil {
100
-			log.Fatalf("Error destroying leftover container: %s", err)
101
-		}
94
+	// Create the "global runtime" with a long-running daemon for integration tests
95
+	spawnGlobalDaemon()
96
+	startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine()
97
+}
98
+
99
+
100
+func setupBaseImage() {
101
+	runtime, err := NewRuntimeFromDirectory(unitTestStoreBase, false)
102
+	if err != nil {
103
+		log.Fatalf("Unable to create a runtime for tests:", err)
102 104
 	}
103 105
 
104 106
 	// Create the "Server"
105 107
 	srv := &Server{
106
-		runtime:     globalRuntime,
108
+		runtime:     runtime,
107 109
 		enableCors:  false,
108 110
 		pullingPool: make(map[string]struct{}),
109 111
 		pushingPool: make(map[string]struct{}),
110 112
 	}
113
+
111 114
 	// If the unit test is not found, try to download it.
112
-	if img, err := globalRuntime.repositories.LookupImage(unitTestImageName); err != nil || img.ID != unitTestImageID {
115
+	if img, err := runtime.repositories.LookupImage(unitTestImageName); err != nil || img.ID != unitTestImageID {
113 116
 		// Retrieve the Image
114 117
 		if err := srv.ImagePull(unitTestImageName, "", os.Stdout, utils.NewStreamFormatter(false), nil, nil, true); err != nil {
115 118
 			log.Fatalf("Unable to pull the test image:", err)
116 119
 		}
117 120
 	}
121
+}
122
+
123
+
124
+func spawnGlobalDaemon() {
125
+	if globalRuntime != nil {
126
+		utils.Debugf("Global runtime already exists. Skipping.")
127
+		return
128
+	}
129
+	globalRuntime = mkRuntime(log.New(os.Stderr, "", 0))
130
+	srv := &Server{
131
+		runtime:     globalRuntime,
132
+		enableCors:  false,
133
+		pullingPool: make(map[string]struct{}),
134
+		pushingPool: make(map[string]struct{}),
135
+	}
136
+
118 137
 	// Spawn a Daemon
119 138
 	go func() {
139
+		utils.Debugf("Spawning global daemon for integration tests")
120 140
 		if err := ListenAndServe(testDaemonProto, testDaemonAddr, srv, os.Getenv("DEBUG") != ""); err != nil {
121 141
 			log.Fatalf("Unable to spawn the test daemon:", err)
122 142
 		}
123 143
 	}()
124
-
125 144
 	// Give some time to ListenAndServer to actually start
145
+	// FIXME: use inmem transports instead of tcp
126 146
 	time.Sleep(time.Second)
127
-
128
-	startFds, startGoroutines = utils.GetTotalUsedFds(), runtime.NumGoroutine()
129 147
 }
130 148
 
131 149
 // FIXME: test that ImagePull(json=true) send correct json output
... ...
@@ -351,6 +351,7 @@ func TestPools(t *testing.T) {
351 351
 
352 352
 func TestLogEvent(t *testing.T) {
353 353
 	runtime := mkRuntime(t)
354
+	defer nuke(runtime)
354 355
 	srv := &Server{
355 356
 		runtime:   runtime,
356 357
 		events:    make([]utils.JSONMessage, 0, 64),
... ...
@@ -11,7 +11,7 @@ func displayFdGoroutines(t *testing.T) {
11 11
 }
12 12
 
13 13
 func TestFinal(t *testing.T) {
14
-	cleanup(globalRuntime)
14
+	nuke(globalRuntime)
15 15
 	t.Logf("Start Fds: %d, Start Goroutines: %d", startFds, startGoroutines)
16 16
 	displayFdGoroutines(t)
17 17
 }