forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_test.go
106 lines (90 loc) · 2.71 KB
/
fs_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
package checkup
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
func TestFS(t *testing.T) {
results := []Result{{Title: "Testing"}}
resultsBytes := []byte(`[{"title":"Testing"}]` + "\n")
dir, err := ioutil.TempDir("", "checkup")
if err != nil {
t.Fatalf("Cannot create temporary directory: %v", err)
}
defer os.RemoveAll(dir)
specimen := FS{
Dir: dir,
}
if err := specimen.Store(results); err != nil {
t.Fatalf("Expected no error from Store(), got: %v", err)
}
// Make sure index has been created
index, err := specimen.readIndex()
if err != nil {
t.Fatalf("Cannot read index: %v", err)
}
if len(index) != 1 {
t.Fatalf("Expected length of index to be 1, but got %v", len(index))
}
var (
name string
nsec int64
)
for name, nsec = range index {
}
// Make sure index has timestamp of check
ts := time.Unix(0, nsec)
if time.Since(ts) > 1*time.Second {
t.Errorf("Timestamp of check is %s but expected something very recent", ts)
}
// Make sure check file bytes are correct
checkfile := filepath.Join(specimen.Dir, name)
b, err := ioutil.ReadFile(checkfile)
if err != nil {
t.Fatalf("Expected no error reading body, got: %v", err)
}
if bytes.Compare(b, resultsBytes) != 0 {
t.Errorf("Contents of file are wrong\nExpected %s\nGot %s", resultsBytes, b)
}
// Test the StorageReader interface
index, err = specimen.GetIndex()
if err != nil {
t.Fatalf("StoreReader: cannot read index: %v", err)
}
if len(index) != 1 {
t.Fatalf("StoreReader: expected length of index to be 1, but got %v", len(index))
}
var indexKey string
for k := range index {
indexKey = k // Get first (and unique) key
break
}
testResults, err := specimen.Fetch(indexKey)
if err != nil {
t.Fatalf("StoreReader: cannot fetch contents for '%s': %v", indexKey, err)
}
if len(testResults) != 1 {
t.Fatalf("StoreReader: expected length of []Result to be 1, but got %v", len(testResults))
}
if testResults[0].Title != results[0].Title {
t.Fatalf("StoreReader: expected test result title to be '%s', but got '%s'", results[0].Title, testResults[0].Title)
}
// Make sure check file is not deleted after maintain with CheckExpiry == 0
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, err := os.Stat(checkfile); err != nil {
t.Fatalf("Expected not error calling Stat() on checkfile, got: %v", err)
}
// Make sure checkfile is deleted after maintain with CheckExpiry > 0
specimen.CheckExpiry = 1 * time.Nanosecond
if err := specimen.Maintain(); err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if _, err := os.Stat(checkfile); !os.IsNotExist(err) {
t.Fatalf("Expected checkfile to be deleted, but Stat() returned error: %v", err)
}
}