-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk_usage_test.go
57 lines (46 loc) · 1.43 KB
/
disk_usage_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
package healthcheck
import (
"encoding/json"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestDiskUsage(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
var (
w = httptest.NewRecorder()
r = httptest.NewRequest("GET", "/ignored", nil)
handler = DiskUsage("tmp", "/")
)
handler(w, r)
require.Equal(t, 200, w.Code)
var resp []DiskUsageResponse
err := json.Unmarshal(w.Body.Bytes(), &resp)
got := resp[0]
require.NoError(t, err)
require.Equal(t, "/tmp", got.Dir)
require.Equal(t, "tmp", got.PvcName)
require.NotZero(t, got.AllBytes)
require.NotZero(t, got.FreeBytes)
require.True(t, got.AllBytes >= got.FreeBytes, "free bytes should not be more than all bytes")
require.NotContains(t, w.Body.String(), "error")
})
t.Run("statfs error", func(t *testing.T) {
const pvc = "this-directory-had-better-not-be-present-in-any-sort-of-test-environment"
var (
w = httptest.NewRecorder()
r = httptest.NewRequest("GET", "/ignored", nil)
handler = DiskUsage(pvc, "/")
)
handler(w, r)
require.Equal(t, 500, w.Code)
var resp []DiskUsageResponse
err := json.Unmarshal(w.Body.Bytes(), &resp)
got := resp[0]
require.NoError(t, err)
require.Equal(t, "/"+pvc, got.Dir)
require.Equal(t, "no such file or directory", got.Error)
require.NotContains(t, w.Body.String(), "all_bytes")
require.NotContains(t, w.Body.String(), "free_bytes")
})
}