/
image_cleaner_test.go
116 lines (101 loc) · 2.83 KB
/
image_cleaner_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
package gcloudcleanup
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
compute "google.golang.org/api/compute/v1"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/travis-ci/gcloud-cleanup/ratelimit"
)
func TestNewImageCleaner(t *testing.T) {
log := logrus.New()
ratelimit := ratelimit.NewNullRateLimiter()
ic := newImageCleaner(nil, log, ratelimit, 10, time.Second,
"foo-project", "http://foo.example.com",
[]string{"name eq ^travis-test.*"}, true)
assert.NotNil(t, ic)
assert.Nil(t, ic.cs)
assert.NotNil(t, ic.log)
assert.Equal(t, "foo-project", ic.projectID)
assert.Equal(t, "http://foo.example.com", ic.jobBoardURL)
assert.Equal(t, []string{"name eq ^travis-test.*"}, ic.filters)
}
func TestImageCleaner_Run(t *testing.T) {
gceMux := http.NewServeMux()
gceMux.HandleFunc(
"/foo-project/global/images",
func(w http.ResponseWriter, req *http.Request) {
body := map[string]interface{}{
"items": []interface{}{
map[string]string{
"name": "travis-test-image-0",
"status": "READY",
},
map[string]string{
"name": "travis-test-bananapants-9001",
"status": "READY",
},
map[string]string{
"name": "travis-test-bananapants-9000",
"status": "READY",
},
},
}
err := json.NewEncoder(w).Encode(body)
assert.Nil(t, err)
})
gceMux.HandleFunc("/foo-project/global/images/travis-test-image-0",
func(w http.ResponseWriter, req *http.Request) {
assert.Equal(t, req.Method, "DELETE")
fmt.Fprintf(w, `{}`)
})
gceMux.HandleFunc("/",
func(w http.ResponseWriter, req *http.Request) {
t.Errorf("Unhandled gce URL: %s %v", req.Method, req.URL)
})
gceSrv := httptest.NewServer(gceMux)
defer gceSrv.Close()
jbMux := http.NewServeMux()
jbMux.HandleFunc("/images", func(w http.ResponseWriter, req *http.Request) {
body := map[string]interface{}{
"data": []interface{}{
map[string]string{
"name": "travis-test-bananapants-9000",
},
map[string]string{
"name": "travis-test-bananapants-9001",
},
map[string]string{
"name": "travis-test-roboticshoe-1138",
},
},
}
err := json.NewEncoder(w).Encode(body)
assert.Nil(t, err)
})
jbMux.HandleFunc("/",
func(w http.ResponseWriter, req *http.Request) {
t.Errorf("Unhandled job-board URL: %s %v", req.Method, req.URL)
})
jbSrv := httptest.NewServer(jbMux)
defer jbSrv.Close()
cs, err := compute.New(&http.Client{})
assert.Nil(t, err)
cs.BasePath = gceSrv.URL
log := logrus.New()
log.Level = logrus.FatalLevel
if os.Getenv("GCLOUD_CLEANUP_TEST_DEBUG") != "" {
log.Level = logrus.DebugLevel
}
rl := ratelimit.NewNullRateLimiter()
ic := newImageCleaner(cs, log, rl, 10, time.Second,
"foo-project", jbSrv.URL,
[]string{"name eq ^travis-test.*"}, false)
err = ic.Run()
assert.Nil(t, err)
}