Skip to content

Commit

Permalink
distro: use the new function in GetHostDistroName
Browse files Browse the repository at this point in the history
The function now works even without /etc/os-release (which is rare
but possible). The function now also no longer panics if ID or
VERSION_ID aren't set. Some tests were added to cover everything.
  • Loading branch information
ondrejbudai authored and achilleas-k committed Apr 10, 2024
1 parent 26899f8 commit ad45a75
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
17 changes: 11 additions & 6 deletions pkg/distro/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ import (
"strings"
)

// variable so that it can be overridden in tests
var getHostDistroNameTree = "/"

// GetHostDistroName returns the name of the host distribution, such as
// "fedora-32" or "rhel-8.2". It does so by reading the /etc/os-release file.
func GetHostDistroName() (string, error) {
f, err := os.Open("/etc/os-release")
osrelease, err := ReadOSReleaseFromTree(getHostDistroNameTree)
if err != nil {
return "", err
return "", fmt.Errorf("cannot get the host distro name: %w", err)
}
defer f.Close()
osrelease, err := readOSRelease(f)
if err != nil {
return "", err

if _, ok := osrelease["ID"]; !ok {
return "", errors.New("cannot get the host distro name: missing ID field in os-release")
}
if _, ok := osrelease["VERSION_ID"]; !ok {
return "", errors.New("cannot get the host distro name: missing VERSION_ID field in os-release")
}

name := osrelease["ID"] + "-" + osrelease["VERSION_ID"]
Expand Down
40 changes: 40 additions & 0 deletions pkg/distro/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,46 @@ import (
"github.com/stretchr/testify/require"
)

func TestGetHostDistroName(t *testing.T) {
backup := getHostDistroNameTree
defer func() { getHostDistroNameTree = backup }()
getHostDistroNameTree = t.TempDir()

require.NoError(t, os.MkdirAll(path.Join(getHostDistroNameTree, "etc"), 0755))
require.NoError(t,
os.WriteFile(path.Join(getHostDistroNameTree, "etc/os-release"), []byte("ID=toucanOS\nVERSION_ID=42\n"), 0600),
)

name, err := GetHostDistroName()
require.NoError(t, err)
require.Equal(t, "toucanOS-42", name)
}
func TestGetHostDistroNameUnhappy(t *testing.T) {
backup := getHostDistroNameTree
defer func() { getHostDistroNameTree = backup }()
getHostDistroNameTree = t.TempDir()

require.NoError(t, os.MkdirAll(path.Join(getHostDistroNameTree, "etc"), 0755))

// no file at all
_, err := GetHostDistroName()
require.ErrorContains(t, err, "cannot get the host distro name: failed to read os-release")

// missing ID
require.NoError(t,
os.WriteFile(path.Join(getHostDistroNameTree, "etc/os-release"), []byte("VERSION_ID=toucanOS\n"), 0600),
)
_, err = GetHostDistroName()
require.ErrorContains(t, err, "cannot get the host distro name: missing ID field")

// missing VERSION_ID
require.NoError(t,
os.WriteFile(path.Join(getHostDistroNameTree, "etc/os-release"), []byte("ID=42\n"), 0600),
)
_, err = GetHostDistroName()
require.ErrorContains(t, err, "cannot get the host distro name: missing VERSION_ID field")
}

func TestOSRelease(t *testing.T) {
var cases = []struct {
Input string
Expand Down

0 comments on commit ad45a75

Please sign in to comment.