Skip to content

Commit c53bca9

Browse files
committed
source: add OSRelease.IDLike support
Read the ID_LIKE field and expose in OSRelease.
1 parent 4d57ffa commit c53bca9

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

bib/internal/source/source.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path"
7+
"strings"
78

89
"github.com/sirupsen/logrus"
910

@@ -16,6 +17,7 @@ type OSRelease struct {
1617
VersionID string
1718
Name string
1819
VariantID string
20+
IDLike []string
1921
}
2022

2123
type Info struct {
@@ -69,6 +71,10 @@ func LoadInfo(root string) (*Info, error) {
6971
if err != nil {
7072
logrus.Debugf("cannot read UEFI vendor: %v, setting it to none", err)
7173
}
74+
var idLike []string
75+
if osrelease["ID_LIKE"] != "" {
76+
idLike = strings.Split(osrelease["ID_LIKE"], " ")
77+
}
7278

7379
return &Info{
7480
OSRelease: OSRelease{
@@ -77,6 +83,7 @@ func LoadInfo(root string) (*Info, error) {
7783
Name: osrelease["NAME"],
7884
PlatformID: osrelease["PLATFORM_ID"],
7985
VariantID: osrelease["VARIANT_ID"],
86+
IDLike: idLike,
8087
},
8188

8289
UEFIVendor: vendor,

bib/internal/source/source_test.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package source
33
import (
44
"os"
55
"path"
6+
"strings"
67
"testing"
78

89
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011
)
1112

12-
func writeOSRelease(root, id, versionID, name, platformID, variantID string) error {
13+
func writeOSRelease(root, id, versionID, name, platformID, variantID, idLike string) error {
1314
err := os.MkdirAll(path.Join(root, "etc"), 0755)
1415
if err != nil {
1516
return err
@@ -31,6 +32,9 @@ func writeOSRelease(root, id, versionID, name, platformID, variantID string) err
3132
if variantID != "" {
3233
buf += "VARIANT_ID=" + variantID + "\n"
3334
}
35+
if idLike != "" {
36+
buf += "ID_LIKE=" + idLike + "\n"
37+
}
3438

3539
return os.WriteFile(path.Join(root, "etc/os-release"), []byte(buf), 0644)
3640
}
@@ -52,21 +56,23 @@ func TestLoadInfo(t *testing.T) {
5256
uefiVendor string
5357
platformID string
5458
variantID string
59+
idLike string
5560
errorStr string
5661
}{
57-
{"happy", "fedora", "40", "Fedora Linux", "fedora", "platform:f40", "coreos", ""},
58-
{"happy-no-uefi", "fedora", "40", "Fedora Linux", "", "platform:f40", "coreos", ""},
59-
{"happy-no-variant_id", "fedora", "40", "Fedora Linux", "", "platform:f40", "", ""},
60-
{"sad-no-id", "", "40", "Fedora Linux", "fedora", "platform:f40", "", "missing ID in os-release"},
61-
{"sad-no-id", "fedora", "", "Fedora Linux", "fedora", "platform:f40", "", "missing VERSION_ID in os-release"},
62-
{"sad-no-id", "fedora", "40", "", "fedora", "platform:f40", "", "missing NAME in os-release"},
63-
{"sad-no-id", "fedora", "40", "Fedora Linux", "fedora", "", "", "missing PLATFORM_ID in os-release"},
62+
{"happy", "fedora", "40", "Fedora Linux", "fedora", "platform:f40", "coreos", "", ""},
63+
{"happy-no-uefi", "fedora", "40", "Fedora Linux", "", "platform:f40", "coreos", "", ""},
64+
{"happy-no-variant_id", "fedora", "40", "Fedora Linux", "", "platform:f40", "", "", ""},
65+
{"happy-with-id-like", "centos", "9", "CentOS Stream", "", "platform:el9", "", "rhel fedora", ""},
66+
{"sad-no-id", "", "40", "Fedora Linux", "fedora", "platform:f40", "", "", "missing ID in os-release"},
67+
{"sad-no-id", "fedora", "", "Fedora Linux", "fedora", "platform:f40", "", "", "missing VERSION_ID in os-release"},
68+
{"sad-no-id", "fedora", "40", "", "fedora", "platform:f40", "", "", "missing NAME in os-release"},
69+
{"sad-no-id", "fedora", "40", "Fedora Linux", "fedora", "", "", "", "missing PLATFORM_ID in os-release"},
6470
}
6571

6672
for _, c := range cases {
6773
t.Run(c.desc, func(t *testing.T) {
6874
root := t.TempDir()
69-
require.NoError(t, writeOSRelease(root, c.id, c.versionID, c.name, c.platformID, c.variantID))
75+
require.NoError(t, writeOSRelease(root, c.id, c.versionID, c.name, c.platformID, c.variantID, c.idLike))
7076
if c.uefiVendor != "" {
7177
require.NoError(t, createBootupdEFI(root, c.uefiVendor))
7278

@@ -85,7 +91,12 @@ func TestLoadInfo(t *testing.T) {
8591
assert.Equal(t, c.uefiVendor, info.UEFIVendor)
8692
assert.Equal(t, c.platformID, info.OSRelease.PlatformID)
8793
assert.Equal(t, c.variantID, info.OSRelease.VariantID)
88-
94+
if c.idLike == "" {
95+
assert.Equal(t, len(info.OSRelease.IDLike), 0)
96+
} else {
97+
expected := strings.Split(c.idLike, " ")
98+
assert.Equal(t, expected, info.OSRelease.IDLike)
99+
}
89100
})
90101
}
91102
}

0 commit comments

Comments
 (0)