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/.
14
"github.com/siderolabs/go-blockdevice/blockdevice/loopback"
15
"github.com/stretchr/testify/suite"
16
"golang.org/x/sys/unix"
18
"github.com/aenix-io/talm/internal/pkg/mount"
19
"github.com/siderolabs/talos/pkg/makefs"
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:
25
// go test -exec sudo -v --count 1 github.com/aenix-io/talm/internal/pkg/mount
27
type manifestSuite struct {
31
loopbackDevice *os.File
35
diskSize = 4 * 1024 * 1024 * 1024 // 4 GiB
38
func TestManifestSuite(t *testing.T) {
39
suite.Run(t, new(manifestSuite))
42
func (suite *manifestSuite) SetupTest() {
47
suite.disk, err = os.CreateTemp("", "talos")
48
suite.Require().NoError(err)
50
suite.Require().NoError(suite.disk.Truncate(diskSize))
52
suite.loopbackDevice, err = loopback.NextLoopDevice()
53
suite.Require().NoError(err)
55
suite.T().Logf("Using %s", suite.loopbackDevice.Name())
57
suite.Require().NoError(loopback.Loop(suite.loopbackDevice, suite.disk))
59
suite.Require().NoError(loopback.LoopSetReadWrite(suite.loopbackDevice))
62
func (suite *manifestSuite) TearDownTest() {
63
if suite.loopbackDevice != nil {
64
suite.Assert().NoError(loopback.Unloop(suite.loopbackDevice))
67
if suite.disk != nil {
68
suite.Assert().NoError(os.Remove(suite.disk.Name()))
69
suite.Assert().NoError(suite.disk.Close())
73
func (suite *manifestSuite) skipIfNotRoot() {
75
suite.T().Skip("can't run the test as non-root")
79
func (suite *manifestSuite) skipUnderBuildkit() {
80
hostname, _ := os.Hostname() //nolint:errcheck
82
if hostname == "buildkitsandbox" {
83
suite.T().Skip("test not supported under buildkit as partition devices are not propagated from /dev")
87
func (suite *manifestSuite) TestCleanCorrupedXFSFileSystem() {
88
suite.skipUnderBuildkit()
90
tempDir := suite.T().TempDir()
92
mountDir := filepath.Join(tempDir, "var")
94
suite.Assert().NoError(os.MkdirAll(mountDir, 0o700))
95
suite.Require().NoError(makefs.XFS(suite.loopbackDevice.Name()))
97
logger := log.New(os.Stderr, "", log.LstdFlags)
99
mountpoint := mount.NewMountPoint(suite.loopbackDevice.Name(), mountDir, "xfs", unix.MS_NOATIME, "", mount.WithLogger(logger))
101
suite.Assert().NoError(mountpoint.Mount())
104
suite.Assert().NoError(mountpoint.Unmount())
107
suite.Assert().NoError(mountpoint.Unmount())
109
// // now corrupt the disk
110
cmd := exec.Command("xfs_db", []string{
113
"-c blocktrash -s 512109 -n 1000",
114
suite.loopbackDevice.Name(),
117
suite.Assert().NoError(cmd.Run())
119
suite.Assert().NoError(mountpoint.Mount())