Skip to content

Commit

Permalink
fix empty platform information (#22139)
Browse files Browse the repository at this point in the history
fix empty linux kernel platform information
  • Loading branch information
brycekahle authored Jan 19, 2024
1 parent 1ccaf26 commit caa29ec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
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")
}

0 comments on commit caa29ec

Please sign in to comment.