Skip to content

Commit

Permalink
fix: lookup filesystem labels on the actual device path
Browse files Browse the repository at this point in the history
The problem was that device path was used, which skips partition as
filesystem label is searched only on the blockdevices.

Add more tests for probe with filesystem labels.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
  • Loading branch information
smira committed Mar 28, 2022
1 parent 7b9de26 commit ec428fe
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
4 changes: 2 additions & 2 deletions .conform.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2021-11-15T13:22:12Z by kres c4d092b.
# Generated on 2022-03-28T13:15:04Z by kres 8bc4139-dirty.

This comment has been minimized.

Copy link
@rgl

rgl Mar 29, 2022

is it expected for this to start having a -dirty suffix?

This comment has been minimized.

Copy link
@smira

smira Mar 30, 2022

Author Member

yes, it's not important actually


---
policies:
Expand All @@ -10,7 +10,7 @@ policies:
gpg:
required: true
identity:
gitHubOrganization: talos-systems
gitHubOrganization: siderolabs
spellcheck:
locale: US
maximumOfOneCommit: true
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2021-11-15T13:22:12Z by kres c4d092b.
# Generated on 2022-03-28T13:15:04Z by kres 8bc4139-dirty.

# common variables

Expand All @@ -9,7 +9,7 @@ TAG := $(shell git describe --tag --always --dirty)
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
ARTIFACTS := _out
REGISTRY ?= ghcr.io
USERNAME ?= talos-systems
USERNAME ?= siderolabs
REGISTRY_AND_USERNAME ?= $(REGISTRY)/$(USERNAME)
GOFUMPT_VERSION ?= abc0db2c416aca0f60ea33c23c76665f6e7ba0b6
GO_VERSION ?= 1.17
Expand All @@ -18,7 +18,7 @@ GRPC_GO_VERSION ?= 1.1.0
GRPC_GATEWAY_VERSION ?= 2.4.0
VTPROTOBUF_VERSION ?= 81d623a9a700ede8ef765e5ab08b3aa1f5b4d5a8
TESTPKGS ?= ./...
KRES_IMAGE ?= ghcr.io/talos-systems/kres:latest
KRES_IMAGE ?= ghcr.io/siderolabs/kres:latest

# docker build settings

Expand Down
5 changes: 2 additions & 3 deletions blockdevice/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ func WithPartitionLabel(label string) SelectOption {
}
}

// WithFileSystemLabel search for a block device which has filesystem on root level
// and that filesystem is labeled as provided label.
// WithFileSystemLabel searches for a block device which has filesystem labeled with the provided label.
func WithFileSystemLabel(label string) SelectOption {
return func(device *ProbedBlockDevice) (bool, error) {
superblock, err := filesystem.Probe(device.Device().Name())
superblock, err := filesystem.Probe(device.Path)
if err != nil {
return false, err
}
Expand Down
44 changes: 38 additions & 6 deletions blockdevice/probe/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
package probe_test

import (
"errors"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/suite"

"github.com/talos-systems/go-blockdevice/blockdevice"
"github.com/talos-systems/go-blockdevice/blockdevice/partition/gpt"
"github.com/talos-systems/go-blockdevice/blockdevice/probe"
"github.com/talos-systems/go-blockdevice/blockdevice/test"
Expand All @@ -26,17 +26,25 @@ func (suite *ProbeSuite) SetupTest() {
}

func (suite *ProbeSuite) addPartition(name string, size uint64) *gpt.Partition {
g, err := gpt.New(suite.Dev)
var (
g *gpt.GPT
err error
)

g, err = gpt.Open(suite.Dev)
if errors.Is(err, gpt.ErrPartitionTableDoesNotExist) {
g, err = gpt.New(suite.Dev)
} else if err == nil {
err = g.Read()
}

suite.Require().NoError(err)

partition, err := g.Add(size, gpt.WithPartitionName(name))
suite.Require().NoError(err)

suite.Require().NoError(g.Write())

_, err = blockdevice.Open(suite.LoopbackDevice.Name())
suite.Require().NoError(err)

partPath, err := partition.Path()
suite.Require().NoError(err)

Expand Down Expand Up @@ -84,7 +92,7 @@ func (suite *ProbeSuite) TestGetDevWithFileSystemLabel() {
func (suite *ProbeSuite) TestProbeByPartitionLabel() {
size := uint64(1024 * 1024 * 256)
suite.addPartition("test", size)
suite.addPartition("test", size)
suite.addPartition("test2", size)

probed, err := probe.All(probe.WithPartitionLabel("test"))
suite.Require().NoError(err)
Expand All @@ -93,6 +101,30 @@ func (suite *ProbeSuite) TestProbeByPartitionLabel() {
suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Device().Name())
}

func (suite *ProbeSuite) TestProbeByFilesystemLabelBlockdevice() {
suite.setSystemLabel("FSLBABELBD")

probed, err := probe.All(probe.WithFileSystemLabel("FSLBABELBD"))
suite.Require().NoError(err)
suite.Require().Equal(1, len(probed))

suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Device().Name())
suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Path)
}

func (suite *ProbeSuite) TestProbeByFilesystemLabelPartition() {
size := uint64(1024 * 1024 * 256)
suite.addPartition("FOO", size)
suite.addPartition("FSLABELPART", size)

probed, err := probe.All(probe.WithFileSystemLabel("FSLABELPART"))
suite.Require().NoError(err)
suite.Require().Equal(1, len(probed))

suite.Require().Equal(suite.LoopbackDevice.Name(), probed[0].Device().Name())
suite.Require().Equal(suite.LoopbackDevice.Name()+"p2", probed[0].Path)
}

func TestProbe(t *testing.T) {
if os.Getuid() != 0 {
t.Skip("can't run the test as non-root")
Expand Down
4 changes: 2 additions & 2 deletions hack/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2021-11-15T13:22:12Z by kres c4d092b.
# Generated on 2022-03-28T13:13:16Z by kres 8bc4139-dirty.

set -e

RELEASE_TOOL_IMAGE="ghcr.io/talos-systems/release-tool:latest"
RELEASE_TOOL_IMAGE="ghcr.io/siderolabs/release-tool:latest"

function release-tool {
docker pull "${RELEASE_TOOL_IMAGE}" >/dev/null
Expand Down

0 comments on commit ec428fe

Please sign in to comment.