-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool_test.go
139 lines (131 loc) · 3.05 KB
/
pool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2017, Boise State University All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"net/http"
"os"
"regexp"
"testing"
"time"
)
func init() {
f, _ := os.Create(os.DevNull)
log.SetOutput(f)
}
func TestImageMatch(t *testing.T) {
match := regexp.MustCompile(jupyterNotebookImageMatch)
tests := []struct {
s string
m bool
}{
{"ksshannon/geo-notebook", true},
{"ksshannon/geo-notebook:latest", true},
{"ksshannon/geo-notebook:sometag", true},
{"ksshannon/notanotebook", false},
{"ksshannon/geo-notebook:two:tags", false},
{"notanotebook", false},
{"notanotebook:invalid", false},
{"jupyter/tmpnb:latest", false},
{"jupyter/configurable-http-proxy:latest", false},
}
for _, test := range tests {
if match.MatchString(test.s) != test.m {
t.Errorf("missed match: %v", test)
}
}
}
const (
skipDocker = "skipping docker dependent test"
testNotebook = "jupyter/minimal-notebook"
)
func TestNewNotebook(t *testing.T) {
if testing.Short() {
t.Skip(skipDocker)
}
p, err := newNotebookPool(".*", 2, time.Minute*2, false, "")
if err != nil {
t.Fatal(err)
}
p.stopCollector()
nb, err := p.newNotebook(testNotebook, false, "", "")
if err != nil {
t.Error(err)
}
time.Sleep(time.Second * 10)
if len(p.activeNotebooks()) != 1 {
t.Fatal("failed to create a notebook")
}
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/?token=", nb.port))
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
p.stopAndKillContainer(nb.id)
}
func TestCollection(t *testing.T) {
if testing.Short() {
t.Skip(skipDocker)
}
p, err := newNotebookPool(".*", 2, time.Second*5, false, "")
if err != nil {
t.Fatal(err)
}
// Stop the collector, then restart it with an aggressive rate
p.stopCollector()
p.startCollector(time.Second)
nb, err := p.newNotebook(testNotebook, false, "", "")
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Second * 10)
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/?token=", nb.port))
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if err == nil && resp.StatusCode == 200 {
t.Errorf("container should be dead")
}
n := len(p.activeNotebooks())
if n != 0 {
t.Errorf("pool not drained (%d)", n)
}
p.stopAndKillContainer(nb.id)
}
func TestZombies(t *testing.T) {
if testing.Short() {
t.Skip(skipDocker)
}
p, err := newNotebookPool(".*", 2, time.Minute*2, false, "")
if err != nil {
t.Fatal(err)
}
p.stopCollector()
nb, err := p.newNotebook(testNotebook, false, "", "")
if err != nil {
t.Error(err)
}
if len(p.containerMap) != 1 {
t.Fatal("failed to create container")
}
// manually remove the container from the container map, and drop the port
p.portSet.Drop(nb.port)
p.Lock()
delete(p.containerMap, nb.key)
p.Unlock()
c, err := p.zombieContainers()
if len(c) != 1 {
t.Error("failed to locate zombie")
}
err = p.killZombieContainers()
if err != nil {
t.Error(err)
}
c, err = p.zombieContainers()
if len(c) != 0 {
t.Error("failed to kill zombie")
}
}