talos

Форк
0
/
archiver_test.go 
88 строк · 1.9 Кб
1
// This Source Code Form is subject to the terms of the Mozilla Public
2
// License, v. 2.0. If a copy of the MPL was not distributed with this
3
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4

5
package archiver_test
6

7
import (
8
	"bytes"
9
	"os"
10
	"path/filepath"
11

12
	"github.com/stretchr/testify/suite"
13
)
14

15
type CommonSuite struct {
16
	suite.Suite
17

18
	tmpDir string
19
}
20

21
var filesFixture = []struct {
22
	Path     string
23
	Mode     os.FileMode
24
	Contents []byte
25
	Size     int
26
}{
27
	{
28
		Path:     "/etc/hostname",
29
		Mode:     0o644,
30
		Contents: []byte("localhost"),
31
	},
32
	{
33
		Path:     "/etc/certs/ca.crt",
34
		Mode:     0o600,
35
		Contents: []byte("-- CA PEM CERT -- VERY SECRET"),
36
	},
37
	{
38
		Path: "/dev/random",
39
		Mode: 0o600 | os.ModeDevice | os.ModeCharDevice,
40
	},
41
	{
42
		Path:     "/usr/bin/cp",
43
		Mode:     0o755,
44
		Contents: []byte("ELF EXECUTABLE IIRC"),
45
	},
46
	{
47
		Path:     "/usr/bin/mv",
48
		Mode:     0o644 | os.ModeSymlink,
49
		Contents: []byte("/usr/bin/cp"),
50
	},
51
	{
52
		Path:     "/lib/dynalib.so",
53
		Mode:     0o644,
54
		Contents: []byte("SOME LIBRARY OUT THERE"),
55
		Size:     20 * 1024,
56
	},
57
}
58

59
func (suite *CommonSuite) SetupSuite() {
60
	suite.tmpDir = suite.T().TempDir()
61

62
	for _, file := range filesFixture {
63
		suite.Require().NoError(os.MkdirAll(filepath.Join(suite.tmpDir, filepath.Dir(file.Path)), 0o777))
64

65
		if file.Mode&os.ModeSymlink != 0 {
66
			suite.Require().NoError(os.Symlink(string(file.Contents), filepath.Join(suite.tmpDir, file.Path)))
67

68
			continue
69
		}
70

71
		f, err := os.OpenFile(filepath.Join(suite.tmpDir, file.Path), os.O_CREATE|os.O_WRONLY, file.Mode)
72
		suite.Require().NoError(err)
73

74
		var contents []byte
75

76
		if file.Size > 0 {
77
			contents = bytes.Repeat(file.Contents, file.Size/len(file.Contents))
78
			contents = append(contents, file.Contents[:file.Size-file.Size/len(file.Contents)*len(file.Contents)]...)
79
		} else {
80
			contents = file.Contents
81
		}
82

83
		_, err = f.Write(contents)
84
		suite.Require().NoError(err)
85

86
		suite.Require().NoError(f.Close())
87
	}
88
}
89

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.