-
Notifications
You must be signed in to change notification settings - Fork 63
/
os_utils.go
63 lines (45 loc) · 1.29 KB
/
os_utils.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
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package helpers
import (
"encoding/json"
"os"
"path"
"path/filepath"
"testing"
"github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/stretchr/testify/require"
)
const (
filePermission = 0o700
)
func CreateDirWithErrorCheck(t testing.TB, dirName string) {
t.Helper()
err := os.MkdirAll(dirName, filePermission)
require.NoError(t, err)
}
func CreateFileWithErrorCheck(t testing.TB, dir, fileName string) *os.File {
t.Helper()
testConf, err := os.CreateTemp(dir, fileName)
require.NoError(t, err)
return testConf
}
func RemoveFileWithErrorCheck(t testing.TB, fileName string) {
t.Helper()
err := os.Remove(fileName)
require.NoError(t, err)
}
func CreateCacheFiles(t testing.TB, cachePath string, cacheData map[string]*v1.FileMeta) {
t.Helper()
cache, err := json.MarshalIndent(cacheData, "", " ")
require.NoError(t, err)
err = os.MkdirAll(path.Dir(cachePath), os.ModePerm)
require.NoError(t, err)
for _, file := range cacheData {
CreateFileWithErrorCheck(t, filepath.Dir(file.GetName()), filepath.Base(file.GetName()))
}
err = os.WriteFile(cachePath, cache, os.ModePerm)
require.NoError(t, err)
}