Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix empty linux kernel platform information #22141

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/util/kernel/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package kernel

import (
gopsutilhost "github.com/shirou/gopsutil/v3/host"
gopsutilhost "github.com/DataDog/gopsutil/host"

"github.com/DataDog/datadog-agent/pkg/util/funcs"
)
Expand All @@ -31,7 +31,9 @@ var PlatformVersion = funcs.Memoize(func() (string, error) {
return pi.version, err
})

var platformInformation = funcs.Memoize(func() (platformInfo, error) {
var platformInformation = funcs.Memoize(getPlatformInformation)

func getPlatformInformation() (platformInfo, error) {
platform, family, version, err := gopsutilhost.PlatformInformation()
return platformInfo{platform, family, version}, err
})
}
46 changes: 46 additions & 0 deletions pkg/util/kernel/platform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

//go:build linux

package kernel

import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestPlatform(t *testing.T) {
osr, err := os.Open("/etc/os-release")
if err != nil && errors.Is(err, fs.ErrNotExist) {
t.Skip("/etc/os-release does not exist")
}
require.NoError(t, err)
t.Cleanup(func() { _ = osr.Close() })

tmp := t.TempDir()
t.Setenv("HOST_ETC", tmp)
require.NoError(t, os.Mkdir(filepath.Join(tmp, "redhat-release"), 0755))

// copy /etc/os-release to <tmpdir>/os-release
dosr, err := os.Create(filepath.Join(tmp, "os-release"))
require.NoError(t, err)
t.Cleanup(func() { _ = dosr.Close() })
_, err = io.Copy(dosr, osr)
require.NoError(t, err)
_ = dosr.Close()

pi, err := getPlatformInformation()
require.NoError(t, err)
require.NotEmpty(t, pi.platform, "platform")
require.NotEmpty(t, pi.family, "family")
require.NotEmpty(t, pi.version, "version")
}