This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
file_test.go
178 lines (158 loc) · 6.16 KB
/
file_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
// Copyright 2019 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package purger
import (
"io/ioutil"
"os"
"path/filepath"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/dm/pkg/streamer"
"github.com/pingcap/dm/pkg/utils"
)
func (t *testPurgerSuite) TestPurgeRelayFilesBeforeFile(c *C) {
// UUID mismatch
safeRelay := &streamer.RelayLogInfo{
UUID: "not-found-uuid",
}
files, err := getRelayFilesBeforeFile("", t.uuids, safeRelay)
c.Assert(err, NotNil)
c.Assert(files, IsNil)
// create relay log dir
baseDir, err := ioutil.TempDir("", "test_get_relay_files_before_file")
c.Assert(err, IsNil)
defer os.RemoveAll(baseDir)
// empty relay log dirs
safeRelay = &streamer.RelayLogInfo{
UUID: t.uuids[len(t.uuids)-1],
}
files, err = getRelayFilesBeforeFile(baseDir, t.uuids, safeRelay)
c.Assert(err, NotNil)
c.Assert(files, IsNil)
// create relay log files
relayDirsPath, relayFilesPath, _ := t.genRelayLogFiles(c, baseDir, -1, -1)
// no older
safeRelay = &streamer.RelayLogInfo{
UUID: t.uuids[0],
Filename: t.relayFiles[0][0],
}
files, err = getRelayFilesBeforeFile(baseDir, t.uuids, safeRelay)
c.Assert(err, IsNil)
c.Assert(files, DeepEquals, []*subRelayFiles{})
// only relay files in first sub dir
safeRelay = &streamer.RelayLogInfo{
UUID: t.uuids[1],
Filename: t.relayFiles[1][0],
}
files, err = getRelayFilesBeforeFile(baseDir, t.uuids, safeRelay)
c.Assert(err, IsNil)
c.Assert(len(files), Equals, 1)
c.Assert(files[0].dir, Equals, relayDirsPath[0])
c.Assert(files[0].files, DeepEquals, relayFilesPath[0])
c.Assert(files[0].hasAll, IsTrue)
// relay files in first sub dir, and some in second sub dir
safeRelay = &streamer.RelayLogInfo{
UUID: t.uuids[1],
Filename: t.relayFiles[1][len(t.relayFiles[1])-1],
}
files, err = getRelayFilesBeforeFile(baseDir, t.uuids, safeRelay)
c.Assert(err, IsNil)
c.Assert(len(files), Equals, 2)
c.Assert(files[0].dir, Equals, relayDirsPath[0])
c.Assert(files[0].files, DeepEquals, relayFilesPath[0])
c.Assert(files[0].hasAll, IsTrue)
c.Assert(files[1].dir, Equals, relayDirsPath[1])
c.Assert(files[1].files, DeepEquals, relayFilesPath[1][:len(t.relayFiles[1])-1])
c.Assert(files[1].hasAll, IsFalse)
// relay files in first and second sub dir, and some in third sub dir
safeRelay = &streamer.RelayLogInfo{
UUID: t.uuids[2],
Filename: t.relayFiles[2][1],
}
files, err = getRelayFilesBeforeFile(baseDir, t.uuids, safeRelay)
c.Assert(err, IsNil)
c.Assert(len(files), Equals, 3)
c.Assert(files[0].dir, Equals, relayDirsPath[0])
c.Assert(files[0].files, DeepEquals, relayFilesPath[0])
c.Assert(files[0].hasAll, IsTrue)
c.Assert(files[1].dir, Equals, relayDirsPath[1])
c.Assert(files[1].files, DeepEquals, relayFilesPath[1])
c.Assert(files[1].hasAll, IsTrue)
c.Assert(files[2].dir, Equals, relayDirsPath[2])
c.Assert(files[2].files, DeepEquals, relayFilesPath[2][:1])
c.Assert(files[2].hasAll, IsFalse)
// write a fake meta file to first sub dir
fakeMeta := filepath.Join(relayDirsPath[0], utils.MetaFilename)
ioutil.WriteFile(fakeMeta, []byte{}, 0666)
// purge all relay log files in first and second sub dir, and some in third sub dir
err = purgeRelayFilesBeforeFile(baseDir, t.uuids, safeRelay)
c.Assert(err, IsNil)
c.Assert(utils.IsDirExists(relayDirsPath[0]), IsFalse)
c.Assert(utils.IsDirExists(relayDirsPath[1]), IsFalse)
c.Assert(utils.IsDirExists(relayDirsPath[2]), IsTrue)
c.Assert(utils.IsFileExists(relayFilesPath[2][0]), IsFalse)
c.Assert(utils.IsFileExists(relayFilesPath[2][1]), IsTrue)
c.Assert(utils.IsFileExists(relayFilesPath[2][2]), IsTrue)
}
func (t *testPurgerSuite) TestPurgeRelayFilesBeforeFileAndTime(c *C) {
// create relay log dir
baseDir, err := ioutil.TempDir("", "test_get_relay_files_before_file_and_time")
c.Assert(err, IsNil)
defer os.RemoveAll(baseDir)
// empty relay log dirs
safeRelay := &streamer.RelayLogInfo{
UUID: t.uuids[len(t.uuids)-1],
}
files, err := getRelayFilesBeforeFileAndTime(baseDir, t.uuids, safeRelay, time.Now())
c.Assert(err, NotNil)
c.Assert(files, IsNil)
// create relay log files
relayDirsPath, relayFilesPath, safeTime := t.genRelayLogFiles(c, baseDir, 1, 1)
// safeRelay older than safeTime
safeRelay = &streamer.RelayLogInfo{
UUID: t.uuids[1],
Filename: t.relayFiles[1][0],
}
files, err = getRelayFilesBeforeFileAndTime(baseDir, t.uuids, safeRelay, safeTime)
c.Assert(err, IsNil)
c.Assert(len(files), Equals, 1)
c.Assert(files[0].dir, Equals, relayDirsPath[0])
c.Assert(files[0].files, DeepEquals, relayFilesPath[0])
c.Assert(files[0].hasAll, IsTrue)
// safeRelay newer than safeTime
safeRelay = &streamer.RelayLogInfo{
UUID: t.uuids[2],
Filename: t.relayFiles[2][0],
}
files, err = getRelayFilesBeforeFileAndTime(baseDir, t.uuids, safeRelay, safeTime)
c.Assert(err, IsNil)
c.Assert(len(files), Equals, 2)
c.Assert(files[0].dir, Equals, relayDirsPath[0])
c.Assert(files[0].files, DeepEquals, relayFilesPath[0])
c.Assert(files[0].hasAll, IsTrue)
c.Assert(files[1].dir, Equals, relayDirsPath[1])
c.Assert(files[1].files, DeepEquals, relayFilesPath[1][:2])
c.Assert(files[1].hasAll, IsFalse)
// write a fake meta file to first sub dir
fakeMeta := filepath.Join(relayDirsPath[0], utils.MetaFilename)
ioutil.WriteFile(fakeMeta, []byte{}, 0666)
// purge all relay log files in first and second sub dir, and some in third sub dir
err = purgeRelayFilesBeforeFileAndTime(baseDir, t.uuids, safeRelay, safeTime)
c.Assert(err, IsNil)
c.Assert(utils.IsDirExists(relayDirsPath[0]), IsFalse)
c.Assert(utils.IsDirExists(relayDirsPath[1]), IsTrue)
c.Assert(utils.IsDirExists(relayDirsPath[2]), IsTrue)
c.Assert(utils.IsFileExists(relayFilesPath[1][0]), IsFalse)
c.Assert(utils.IsFileExists(relayFilesPath[1][1]), IsFalse)
c.Assert(utils.IsFileExists(relayFilesPath[1][2]), IsTrue)
}