forked from realvnc-labs/tacoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.go
More file actions
197 lines (159 loc) · 4.31 KB
/
Copy pathfs.go
File metadata and controls
197 lines (159 loc) · 4.31 KB
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
package apptest
import (
"context"
"fmt"
"net/url"
"os"
"time"
"github.com/cloudradar-monitoring/tacoscript/utils"
)
func DeleteFiles(files []string) error {
errs := &utils.Errors{
Errs: []error{},
}
for _, file := range files {
errs.Add(DeleteFileIfExists(file))
}
return errs.ToError()
}
func DeleteFileIfExists(filePath string) error {
fileExists, err := utils.FileExists(filePath)
if err != nil {
return err
}
if !fileExists {
return nil
}
return os.Remove(filePath)
}
type FileExpectation struct {
ShouldExist bool
ExpectedMode os.FileMode
FilePath string
ExpectedContent string
ExpectedUser string
ExpectedGroup string
ExpectedEncoding string
}
func AssertFileMatchesExpectation(fe *FileExpectation) (isExpectationMatched bool, nonMatchedReason string, err error) {
fileExists, err := utils.FileExists(fe.FilePath)
if err != nil {
return false, "", err
}
if fe.ShouldExist && !fileExists {
return false, fmt.Sprintf("file '%s' doesn't exist but it should", fe.FilePath), nil
}
if !fe.ShouldExist && fileExists {
return false, fmt.Sprintf("file '%s' exists but it shouldn't", fe.FilePath), nil
}
if !fe.ShouldExist && !fileExists {
return true, "", nil
}
fileContentsBytes, err := os.ReadFile(fe.FilePath)
if err != nil {
return false, "", err
}
fileContents := ""
if fe.ExpectedEncoding != "" {
fileContents, err = utils.Decode(fe.ExpectedEncoding, fileContentsBytes)
if err != nil {
return false, "", err
}
} else {
fileContents = string(fileContentsBytes)
}
if fe.ExpectedContent != fileContents {
return false,
fmt.Sprintf("file Contents '%s' at '%s' didn't match the expected one '%s'",
fileContents,
fe.FilePath,
fe.ExpectedContent,
), nil
}
return AssertFileMatchesExpectationOS(fe.FilePath, fe)
}
type ChownInput struct {
TargetFilePath string
UserName string
GroupName string
}
type FsManagerMock struct {
FileExistsInputPath []string
FileExistsErrToReturn error
FileExistsExistsToReturn bool
ChownInputs []ChownInput
ChownErrorToReturn error
StatInputName []string
StatOutputFileInfo os.FileInfo
StatOutputError error
}
func (fmm *FsManagerMock) FileExists(filePath string) (bool, error) {
fmm.FileExistsInputPath = append(fmm.FileExistsInputPath, filePath)
return fmm.FileExistsExistsToReturn, fmm.FileExistsErrToReturn
}
func (fmm *FsManagerMock) Remove(filePath string) error {
return nil
}
func (fmm *FsManagerMock) DownloadFile(ctx context.Context, targetLocation string, sourceURL *url.URL, skipTLSCheck bool) error {
return nil
}
func (fmm *FsManagerMock) MoveFile(sourceFilePath, targetFilePath string) error {
return nil
}
func (fmm *FsManagerMock) CopyLocalFile(sourceFilePath, targetFilePath string, mode os.FileMode) error {
return nil
}
func (fmm *FsManagerMock) WriteFile(name, contents string, mode os.FileMode) error {
return nil
}
func (fmm *FsManagerMock) ReadFile(filePath string) (content string, err error) {
return "", nil
}
func (fmm *FsManagerMock) CreateDirPathIfNeeded(targetFilePath string, mode os.FileMode) error {
return nil
}
func (fmm *FsManagerMock) Chmod(targetFilePath string, mode os.FileMode) error {
return nil
}
func (fmm *FsManagerMock) ReadEncodedFile(encodingName, fileName string) (contentsUtf8 string, err error) {
return
}
func (fmm *FsManagerMock) Chown(targetFilePath, userName, groupName string) error {
fmm.ChownInputs = append(fmm.ChownInputs, ChownInput{
TargetFilePath: targetFilePath,
UserName: userName,
GroupName: groupName,
})
return fmm.ChownErrorToReturn
}
func (fmm *FsManagerMock) Stat(name string) (os.FileInfo, error) {
fmm.StatInputName = append(fmm.StatInputName, name)
return fmm.StatOutputFileInfo, fmm.StatOutputError
}
// FakeFile implements FileLike and also os.FileInfo.
type FakeFile struct {
Nam string
Contents string
FileMode os.FileMode
}
func (f *FakeFile) Name() string {
return f.Nam
}
func (f *FakeFile) Size() int64 {
return int64(len(f.Contents))
}
func (f *FakeFile) Mode() os.FileMode {
return f.FileMode
}
func (f *FakeFile) ModTime() time.Time {
return time.Time{}
}
func (f *FakeFile) Stat() (os.FileInfo, error) {
return f, nil
}
func (f *FakeFile) IsDir() bool {
return false
}
func (f *FakeFile) Sys() interface{} {
return nil
}