This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crash_recovery_test.go
180 lines (151 loc) · 5.49 KB
/
crash_recovery_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 2017 Google Inc.
//
// 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 apidAnalytics
import (
"compress/gzip"
"encoding/json"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var _ = Describe("test crashRecoveryNeeded(), ", func() {
Context("directories in recovered folder", func() {
It("should return true", func() {
dirName := "t~e~20160108536000~recoveredTS~20160101222612.123"
dirPath := filepath.Join(localAnalyticsRecoveredDir, dirName)
err := os.Mkdir(dirPath, os.ModePerm)
Expect(err).ShouldNot(HaveOccurred())
needed := crashRecoveryNeeded()
Expect(needed).To(BeTrue())
err = os.Remove(dirPath)
Expect(err).ShouldNot(HaveOccurred())
})
})
Context("directories in tmp folder", func() {
It("should return true", func() {
d := "t~e~20160112630000"
dirPath := filepath.Join(localAnalyticsTempDir, d)
fp := filepath.Join(dirPath, "fakefile.txt.gz")
err := os.Mkdir(dirPath, os.ModePerm)
Expect(err).ShouldNot(HaveOccurred())
_, err = os.Create(fp)
Expect(err).ShouldNot(HaveOccurred())
needed := crashRecoveryNeeded()
Expect(needed).To(BeTrue())
// moves file to recovered dir
dirs, _ := ioutil.ReadDir(localAnalyticsRecoveredDir)
for _, dir := range dirs {
if strings.Contains(dir.Name(), d) {
Expect(dir.Name()).To(
ContainSubstring(d + recoveredTS))
err = os.RemoveAll(localAnalyticsRecoveredDir +
"/" + dir.Name())
Expect(err).ShouldNot(HaveOccurred())
}
}
})
})
})
var _ = Describe("test performRecovery(), ", func() {
It("should move all recovered directories to staging", func() {
dirName := "t~e~20160101545000~recoveredTS~20160101222612.123"
dirPath := filepath.Join(localAnalyticsRecoveredDir, dirName)
err := os.Mkdir(dirPath, os.ModePerm)
Expect(err).ShouldNot(HaveOccurred())
performRecovery()
newPath := filepath.Join(localAnalyticsStagingDir, dirName)
Expect(newPath).To(BeADirectory())
err = os.Remove(newPath)
Expect(err).ShouldNot(HaveOccurred())
})
})
var _ = Describe("test recoverDirectory(), ", func() {
It("should recover file and move folder to staging", func() {
dirName := "t~e~20160101535000~recoveredTS~20160101222612.123"
dirPath := filepath.Join(localAnalyticsRecoveredDir, dirName)
fp := filepath.Join(dirPath, "fakefile.txt.gz")
err := os.Mkdir(dirPath, os.ModePerm)
Expect(err).ShouldNot(HaveOccurred())
recoveredFile, err := os.OpenFile(fp,
os.O_WRONLY|os.O_CREATE, os.ModePerm)
Expect(err).ShouldNot(HaveOccurred())
gw := gzip.NewWriter(recoveredFile)
// write some content to file
var records = []byte(`{
"response_status_code": 200,
"client_id":"testapikey",
"client_received_start_timestamp": 1486406248277,
"client_received_end_timestamp": 1486406248290
}`)
gw.Write(records)
gw.Close()
recoveredFile.Close()
recoverDirectory(dirName)
stagingPath := filepath.Join(localAnalyticsStagingDir, dirName)
Expect(dirPath).ToNot(BeADirectory())
Expect(stagingPath).To(BeADirectory())
err = os.RemoveAll(stagingPath)
Expect(err).ShouldNot(HaveOccurred())
})
})
var _ = Describe("test recoverFile(), ", func() {
It("should create a recovered file and delete parital file", func() {
dirName := "t~e~20160101530000~recoveredTS~20160101222612.123"
dirPath := filepath.Join(localAnalyticsRecoveredDir, dirName)
fp := filepath.Join(dirPath, "fakefile.txt.gz")
err := os.Mkdir(dirPath, os.ModePerm)
Expect(err).ShouldNot(HaveOccurred())
recoveredFile, err := os.OpenFile(fp,
os.O_WRONLY|os.O_CREATE, os.ModePerm)
Expect(err).ShouldNot(HaveOccurred())
gw := gzip.NewWriter(recoveredFile)
// write some content to file
var records = []byte(`{
"response_status_code": 200,
"client_id":"testapikey",
"client_received_start_timestamp": 1486406248277,
"client_received_end_timestamp": 1486406248290
}`)
gw.Write(records)
gw.Close()
recoveredFile.Close()
recoverFile("_20160101222612.123", dirName, "fakefile.txt.gz")
recoveredFileName := "fakefile_recovered_20160101222612.123.txt.gz"
recoveredFilePath := filepath.Join(dirPath, recoveredFileName)
Expect(recoveredFilePath).To(BeAnExistingFile())
Expect(fp).ToNot(BeAnExistingFile())
// Verify file was written to properly
f, err := os.Open(recoveredFilePath)
defer f.Close()
gzReader, err := gzip.NewReader(f)
defer gzReader.Close()
Expect(err).ToNot((HaveOccurred()))
var record map[string]interface{}
decoder := json.NewDecoder(gzReader) // Decode payload to JSON data
decoder.UseNumber()
err = decoder.Decode(&record)
Expect(err).ToNot((HaveOccurred()))
Expect(record["client_id"]).To(Equal("testapikey"))
Expect(record["response_status_code"]).To(Equal(json.Number("200")))
Expect(record["client_received_start_timestamp"]).
To(Equal(json.Number("1486406248277")))
Expect(record["client_received_end_timestamp"]).
To(Equal(json.Number("1486406248290")))
err = os.RemoveAll(dirPath)
Expect(err).ShouldNot(HaveOccurred())
})
})