This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathproper_test.go
115 lines (98 loc) · 2.6 KB
/
proper_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
package main
import (
"flag"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strings"
"testing"
"github.com/samalba/dockerclient"
)
const (
dockerAPIVersion = "/v1.10"
expectedDeletedContainers = 1
expectedDeletedImages = 4
)
var (
client *dockerclient.DockerClient
docker *httptest.Server
serverT *testing.T
matchContainersURL = regexp.MustCompile("^" + dockerAPIVersion + "/containers/([^/]+)")
matchImagesURL = regexp.MustCompile("^" + dockerAPIVersion + "/images/([^/]+)")
deletedContainers int
deletedImages int
)
func fixture(w http.ResponseWriter, file string) {
fh, err := os.Open("fixtures/" + file)
if err != nil {
serverT.Fatal(err)
}
defer fh.Close()
if _, err := io.Copy(w, fh); err != nil {
serverT.Fatal(err)
}
}
func handleDockerAPI(w http.ResponseWriter, r *http.Request) {
switch {
case strings.HasPrefix(r.URL.Path, dockerAPIVersion+"/containers/json"):
fixture(w, "containers.json")
return
case strings.HasPrefix(r.URL.Path, dockerAPIVersion+"/containers/"):
matches := matchContainersURL.FindStringSubmatch(r.URL.Path)
if len(matches) != 2 {
serverT.Fatal("ID not found in url ", r.URL.Path)
}
switch r.Method {
case "GET":
fixture(w, "containers/"+matches[1]+".json")
case "DELETE":
deletedContainers++
fmt.Fprintf(w, "")
}
return
case strings.HasPrefix(r.URL.Path, dockerAPIVersion+"/images/json"):
fixture(w, "images.json")
return
case strings.HasPrefix(r.URL.Path, dockerAPIVersion+"/images"):
matches := matchImagesURL.FindStringSubmatch(r.URL.Path)
if len(matches) != 2 {
serverT.Fatal("ID not found in url ", r.URL.Path)
}
deletedImages++
return
}
serverT.Fatalf("Unexpected request: %s", r.URL.Path)
return
}
func TestExpired(t *testing.T) {
serverT = t
flag.Set("u", "true")
docker = httptest.NewServer(http.HandlerFunc(handleDockerAPI))
defer docker.Close()
client, err := dockerclient.NewDockerClient(docker.URL, nil)
if err != nil {
t.Fatal(err)
}
expiredContainers, err := getExpiredContainers(client)
if err != nil {
t.Fatal(err)
}
if len(expiredContainers) != expectedDeletedContainers {
t.Fatalf("Expected to delete %d container but deleted %d", expectedDeletedContainers, deletedContainers)
}
}
func TestExpiredFail(t *testing.T) {
flag.Set("u", "false")
docker = httptest.NewServer(http.HandlerFunc(handleDockerAPI))
defer docker.Close()
client, err := dockerclient.NewDockerClient(docker.URL, nil)
if err != nil {
t.Fatal(err)
}
if _, err := getExpiredContainers(client); err == nil {
t.Fatal("Expected cleanup to fail because missing FinishedAt field")
}
}