-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusher_test.go
218 lines (205 loc) · 5.07 KB
/
usher_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package usher
import (
"log"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
)
func TestProcessFile(t *testing.T) {
// Create a temporary source directory
srcDir, err := os.MkdirTemp("", "test-src")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(srcDir)
// Create a temporary destination directory
destDir, err := os.MkdirTemp("", "test-dest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(destDir)
// Create a temporary external executable script
externalScript := []byte(`#!/bin/bash
echo "external/executable/mapper/$1"
`)
externalScriptPath := filepath.Join(srcDir, "external.sh")
os.WriteFile(externalScriptPath, externalScript, 0755)
// Set up table test conditions/expectations
tests := []struct {
title string
config Config
destPrefix func(srcFile string) string
filename string
}{
{
title: "simple file mtime",
config: Config{
Globals: Globals{
SrcDir: srcDir,
DestDir: destDir,
Debug: false,
DryRun: false,
Copy: false,
},
FileMapper: NewMtimeFileMapper("2006/01/02/"),
},
destPrefix: func(srcFile string) string {
srcStat, err := os.Stat(srcFile)
if err != nil {
t.Fatal(err)
}
return srcStat.ModTime().Format("2006/01/02/")
},
filename: "mtime.txt",
},
{
title: "simple file mtime, copy true",
config: Config{
Globals: Globals{
SrcDir: srcDir,
DestDir: destDir,
Debug: false,
DryRun: false,
Copy: true,
},
FileMapper: NewMtimeFileMapper("2006/01/02/"),
},
destPrefix: func(srcFile string) string {
srcStat, err := os.Stat(srcFile)
if err != nil {
t.Fatal(err)
}
return srcStat.ModTime().Format("2006/01/02/")
},
filename: "mtime_copy_true.txt",
},
{
title: "dry run",
config: Config{
Globals: Globals{
SrcDir: srcDir,
DestDir: destDir,
Debug: false,
DryRun: true,
Copy: false,
},
FileMapper: PassThroughFileMapper,
},
destPrefix: func(srcFile string) string {
return "dry_run"
},
filename: "dry_run.txt",
},
{
title: "copy dry run",
config: Config{
Globals: Globals{
SrcDir: srcDir,
DestDir: destDir,
Debug: false,
DryRun: true,
Copy: true,
},
FileMapper: PassThroughFileMapper,
},
destPrefix: func(srcFile string) string {
return "copy_dry_run"
},
filename: "copy_dry_run.txt",
},
{
title: "date in file name",
config: Config{
Globals: Globals{
SrcDir: srcDir,
DestDir: destDir,
Debug: false,
DryRun: false,
Copy: false,
},
FileMapper: &DelegatingFileMapper{
GetFileDestPathFunc: func(relSrcFile string, absSrcFile string, baseSrcFile string,
mappedRootSrcPath string, mappedRootDestPath string) (string, error) {
// parse time from filename
fileTime, err := time.Parse("2006-01-02T150405",
strings.TrimPrefix(strings.Split(baseSrcFile, ".")[0], "prefix-"))
if err != nil {
t.Fatal(err)
}
return fileTime.Format("2006/2006-01/") + baseSrcFile, nil
},
},
},
destPrefix: func(srcFile string) string {
return "2019/2019-03"
},
filename: "prefix-2019-03-05T124500.suffix.txt",
},
{
title: "external executable",
config: Config{
Globals: Globals{
SrcDir: srcDir,
DestDir: destDir,
Debug: false,
DryRun: false,
Copy: true,
},
FileMapper: NewExternalFileMapper(externalScriptPath),
},
destPrefix: func(srcFile string) string {
return "external/executable/mapper"
},
filename: "external.txt",
},
}
for _, tc := range tests {
log.Println("Running test:", tc.title)
// Create source file
srcFile := filepath.Join(srcDir, tc.filename)
if err := os.WriteFile(srcFile, []byte("test content"), 0644); err != nil {
t.Fatal(err)
}
srcFileInfo, err := os.Stat(srcFile)
if err != nil {
t.Fatal(err)
}
srcStat, ok := srcFileInfo.Sys().(*syscall.Stat_t)
if !ok {
t.Fatal("Couldn't get stat info for ", tc.filename)
return
}
// Call the processFile function
processFile(tc.config, srcFile)
// Check if the target file exists in the destination directory
destFile := filepath.Join(tc.destPrefix(srcFile), tc.filename)
destFileInfo, err := os.Stat(filepath.Join(destDir, destFile))
if tc.config.DryRun {
if err == nil {
t.Fatal("DryRun should not have created the dest file")
}
continue
}
if err != nil {
t.Errorf("Expected file %s to exist in destination directory, but got error: %v", destFile, err)
}
destStat, ok := destFileInfo.Sys().(*syscall.Stat_t)
if !ok {
t.Fatal("Couldn't get stat info for ", destFile)
return
}
// Make sure inodes are the same/hard links are used (or inodes are different if copying)
if tc.config.Copy == true {
if srcStat.Ino == destStat.Ino {
t.Fatal("src and dest files with config copy: true should not have the same inode")
}
} else {
if srcStat.Ino != destStat.Ino {
t.Fatal("src and dest files with config copy: false should have the same inode")
}
}
}
}