-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcheckpoint_test.go
180 lines (154 loc) · 5.32 KB
/
checkpoint_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright © 2018 Jeff Coffler <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"math/rand"
"os"
"path"
"testing"
"time"
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var randSrc = rand.NewSource(time.Now().UnixNano())
func randomStringBytes(n int) string {
// Generate a string of bytes as recommended in Stack Overflow:
//
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, randSrc.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = randSrc.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func TestCheckPointRead_NotFound(t *testing.T) {
globalLockDir = os.TempDir()
cmdConfig = "SomeCheckpointFileThatShouldNotExist"
checkpoint, iteration := readCheckpoint()
if checkpoint != checkpointNone {
t.Errorf("Checkpoint is incorrect, got '%d', expected '%d'.", checkpoint, checkpointNone)
}
if iteration != 0 {
t.Errorf("iteration is incorrect, got '%d', expected '%d'.", iteration, 0)
}
}
func TestCheckpointRead_KnownGoodFile(t *testing.T) {
// Use random name for the checkpoint file
globalLockDir = os.TempDir()
cmdConfig = "checkpoint-file-" + randomStringBytes(6)
filename := path.Join(path.Join(globalLockDir, cmdConfig+"_checkpoint.yaml"))
// Write out the checkpoint file ourselves (isolate testing to readCheckpoint())
file, err := os.Create(filename)
if err != nil {
t.Errorf("Error creating YAML file: %s: %s.", filename, err)
}
defer func() {
file.Close()
os.Remove(filename)
}()
_, err = file.WriteString(fmt.Sprintf("Operation: %d\nIteration: %d\n", checkpointPrune, 42))
if err != nil {
t.Errorf("Error writing to checkpoint file: %s.", err)
}
file.Close()
// Now test reading the checkpoint file
checkpoint, iteration := readCheckpoint()
if checkpoint != checkpointPrune {
t.Errorf("Incorrect checkpoint read, got '%d', expected '%d'.", checkpoint, checkpointPrune)
}
if iteration != 42 {
t.Errorf("Incorrect iteration read, got '%d', expected '%d'.", iteration, 42)
}
}
func TestCheckpointRead_KnownBadFile(t *testing.T) {
// Use random name for the checkpoint file
globalLockDir = os.TempDir()
cmdConfig = "checkpoint-file-" + randomStringBytes(6)
filename := path.Join(path.Join(globalLockDir, cmdConfig+"_checkpoint.yaml"))
// Write out the checkpoint file ourselves (isolate testing to readCheckpoint())
file, err := os.Create(filename)
if err != nil {
t.Errorf("Error creating YAML file: %s: %s.", filename, err)
}
defer func() {
file.Close()
os.Remove(filename)
}()
// Write out a known-invalid checkpoint file
_, err = file.WriteString(fmt.Sprintf("Operation: %d\nIteration: %d\n", 42, 84))
if err != nil {
t.Errorf("Error writing to checkpoint file: %s.", err)
}
file.Close()
// Now test reading the checkpoint file
checkpoint, iteration := readCheckpoint()
if checkpoint != checkpointNone {
t.Errorf("Incorrect checkpoint read, got '%d', expected '%d'.", checkpoint, checkpointNone)
}
if iteration != 0 {
t.Errorf("Incorrect iteration read, got '%d', expected '%d'.", iteration, 0)
}
}
func TestCheckpointWrite(t *testing.T) {
testEntries := []struct {
checkpointExpected int
iterationExpected int
valid bool
}{
{checkpointBackup, 10, true},
{checkpointCopy, 2, true},
{checkpointPrune, 1, true},
{checkpointCheck, 3, true},
{10, 5, false},
{checkpointCheck + 1, 6, false},
{-1, 7, false},
}
for _, entry := range testEntries {
// Use random name for the checkpoint file
globalLockDir = os.TempDir()
cmdConfig = "checkpoint-file-" + randomStringBytes(6)
// Write the checkpoint file out, re-read it
writeCheckpoint(entry.checkpointExpected, entry.iterationExpected)
defer removeCheckpoint()
checkpoint, iteration := readCheckpoint()
// If this is an invalid entry, change what we expect
if !entry.valid {
entry.checkpointExpected = 0
entry.iterationExpected = 0
}
if checkpoint != entry.checkpointExpected {
t.Errorf("Incorrect checkpoint read, got '%d', expected '%d'.", checkpoint, entry.checkpointExpected)
}
if iteration != entry.iterationExpected {
t.Errorf("Incorrect iteration read, got '%d', expected '%d'.", iteration, entry.iterationExpected)
}
if err := removeCheckpoint(); err != nil {
t.Errorf("Error removing checkpoint file: %s", err)
}
}
}