talm

Форк
0
/
mount_test.go 
120 строк · 3.0 Кб
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 mount_test
6

7
import (
8
	"log"
9
	"os"
10
	"os/exec"
11
	"path/filepath"
12
	"testing"
13

14
	"github.com/siderolabs/go-blockdevice/blockdevice/loopback"
15
	"github.com/stretchr/testify/suite"
16
	"golang.org/x/sys/unix"
17

18
	"github.com/aenix-io/talm/internal/pkg/mount"
19
	"github.com/siderolabs/talos/pkg/makefs"
20
)
21

22
// Some tests in this package cannot be run under buildkit, as buildkit doesn't propagate partition devices
23
// like /dev/loopXpY into the sandbox. To run the tests on your local computer, do the following:
24
//
25
//  go test -exec sudo -v --count 1 github.com/aenix-io/talm/internal/pkg/mount
26

27
type manifestSuite struct {
28
	suite.Suite
29

30
	disk           *os.File
31
	loopbackDevice *os.File
32
}
33

34
const (
35
	diskSize = 4 * 1024 * 1024 * 1024 // 4 GiB
36
)
37

38
func TestManifestSuite(t *testing.T) {
39
	suite.Run(t, new(manifestSuite))
40
}
41

42
func (suite *manifestSuite) SetupTest() {
43
	suite.skipIfNotRoot()
44

45
	var err error
46

47
	suite.disk, err = os.CreateTemp("", "talos")
48
	suite.Require().NoError(err)
49

50
	suite.Require().NoError(suite.disk.Truncate(diskSize))
51

52
	suite.loopbackDevice, err = loopback.NextLoopDevice()
53
	suite.Require().NoError(err)
54

55
	suite.T().Logf("Using %s", suite.loopbackDevice.Name())
56

57
	suite.Require().NoError(loopback.Loop(suite.loopbackDevice, suite.disk))
58

59
	suite.Require().NoError(loopback.LoopSetReadWrite(suite.loopbackDevice))
60
}
61

62
func (suite *manifestSuite) TearDownTest() {
63
	if suite.loopbackDevice != nil {
64
		suite.Assert().NoError(loopback.Unloop(suite.loopbackDevice))
65
	}
66

67
	if suite.disk != nil {
68
		suite.Assert().NoError(os.Remove(suite.disk.Name()))
69
		suite.Assert().NoError(suite.disk.Close())
70
	}
71
}
72

73
func (suite *manifestSuite) skipIfNotRoot() {
74
	if os.Getuid() != 0 {
75
		suite.T().Skip("can't run the test as non-root")
76
	}
77
}
78

79
func (suite *manifestSuite) skipUnderBuildkit() {
80
	hostname, _ := os.Hostname() //nolint:errcheck
81

82
	if hostname == "buildkitsandbox" {
83
		suite.T().Skip("test not supported under buildkit as partition devices are not propagated from /dev")
84
	}
85
}
86

87
func (suite *manifestSuite) TestCleanCorrupedXFSFileSystem() {
88
	suite.skipUnderBuildkit()
89

90
	tempDir := suite.T().TempDir()
91

92
	mountDir := filepath.Join(tempDir, "var")
93

94
	suite.Assert().NoError(os.MkdirAll(mountDir, 0o700))
95
	suite.Require().NoError(makefs.XFS(suite.loopbackDevice.Name()))
96

97
	logger := log.New(os.Stderr, "", log.LstdFlags)
98

99
	mountpoint := mount.NewMountPoint(suite.loopbackDevice.Name(), mountDir, "xfs", unix.MS_NOATIME, "", mount.WithLogger(logger))
100

101
	suite.Assert().NoError(mountpoint.Mount())
102

103
	defer func() {
104
		suite.Assert().NoError(mountpoint.Unmount())
105
	}()
106

107
	suite.Assert().NoError(mountpoint.Unmount())
108

109
	// // now corrupt the disk
110
	cmd := exec.Command("xfs_db", []string{
111
		"-x",
112
		"-c blockget",
113
		"-c blocktrash -s 512109 -n 1000",
114
		suite.loopbackDevice.Name(),
115
	}...)
116

117
	suite.Assert().NoError(cmd.Run())
118

119
	suite.Assert().NoError(mountpoint.Mount())
120
}
121

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

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

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

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