Beispiel #1
0
func (c *CLI) setupRateLimiter() {
	if c.c.String("rate-limit-redis-url") == "" {
		c.rateLimiter = ratelimit.NewNullRateLimiter()
		return
	}
	c.rateLimiter = ratelimit.NewRateLimiter(
		c.c.String("rate-limit-redis-url"),
		c.c.String("rate-limit-prefix"))
}
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 TestNewInstanceCleaner(t *testing.T) {
	log := logrus.New()
	rl := ratelimit.NewNullRateLimiter()
	cutoffTime := time.Now().Add(-1 * time.Hour)

	ic := newInstanceCleaner(nil, log, rl, 10, time.Second,
		cutoffTime, "foo-project",
		[]string{"name eq ^test.*"}, true)

	assert.NotNil(t, ic)
	assert.NotNil(t, ic.log)
	assert.Equal(t, "foo-project", ic.projectID)
	assert.Equal(t, []string{"name eq ^test.*"}, ic.filters)
	assert.True(t, ic.noop)
	assert.Equal(t, cutoffTime, ic.CutoffTime)
}
func TestInstanceCleaner_Run(t *testing.T) {
	mux := http.NewServeMux()
	mux.HandleFunc(
		"/foo-project/aggregated/instances",
		func(w http.ResponseWriter, req *http.Request) {
			body := map[string]interface{}{
				"items": map[string]interface{}{
					"zones/us-central1-a": map[string]interface{}{
						"instances": []interface{}{
							map[string]string{
								"name":              "test-vm-0",
								"status":            "RUNNING",
								"creationTimestamp": time.Now().Format(time.RFC3339),
								"zone":              "zones/us-central1-a",
							},
							map[string]string{
								"name":              "test-vm-1",
								"status":            "TERMINATED",
								"creationTimestamp": "2016-01-02T07:11:12.999-07:00",
								"zone":              "zones/us-central1-a",
							},
							map[string]string{
								"name":              "test-vm-2",
								"status":            "RUNNING",
								"creationTimestamp": time.Now().Add(-8 * time.Hour).Format(time.RFC3339),
								"zone":              "zones/us-central1-a",
							},
						},
					},
				},
			}
			err := json.NewEncoder(w).Encode(body)
			assert.Nil(t, err)
		})
	mux.HandleFunc(
		"/foo-project/zones/us-central1-a/instances/test-vm-1",
		func(w http.ResponseWriter, req *http.Request) {
			assert.Equal(t, req.Method, "DELETE")
			fmt.Fprintf(w, `{}`)
		})
	mux.HandleFunc(
		"/foo-project/zones/us-central1-a/instances/test-vm-2",
		func(w http.ResponseWriter, req *http.Request) {
			assert.Equal(t, req.Method, "DELETE")
			fmt.Fprintf(w, `{}`)
		})
	mux.HandleFunc("/",
		func(w http.ResponseWriter, req *http.Request) {
			t.Errorf("Unhandled URL: %s %v", req.Method, req.URL)
		})

	srv := httptest.NewServer(mux)

	defer srv.Close()

	cs, err := compute.New(&http.Client{})
	assert.Nil(t, err)
	cs.BasePath = srv.URL

	log := logrus.New()
	log.Level = logrus.FatalLevel
	if os.Getenv("GCLOUD_CLEANUP_TEST_DEBUG") != "" {
		log.Level = logrus.DebugLevel
	}
	rl := ratelimit.NewNullRateLimiter()
	cutoffTime := time.Now().Add(-1 * time.Hour)

	ic := newInstanceCleaner(cs, log, rl, 10, time.Second,
		cutoffTime, "foo-project",
		[]string{"name eq ^test.*"}, false)

	err = ic.Run()
	assert.Nil(t, err)
}
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)
}