Repository: prometheus/procfs
Pull request #646 (2024-05-31 17:54:08)

enable gofmt and goimports linter
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>


Patch:
diff --git a/.golangci.yml b/.golangci.yml
index 126df9e..d7476d5 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -3,6 +3,8 @@ linters:
   enable:
   - errcheck
   - godot
+  - gofmt
+  - goimports
   - gosimple
   - govet
   - ineffassign
@@ -18,5 +20,7 @@ linter-settings:
     exclude:
     # Ignore "See: URL"
     - 'See:'
+  goimports:
+      local-prefixes: github.com/prometheus/procfs
   misspell:
     locale: US
diff --git a/sysfs/class_thermal.go b/sysfs/class_thermal.go
index ab9048f..10f4a9a 100644
--- a/sysfs/class_thermal.go
+++ b/sysfs/class_thermal.go
@@ -23,8 +23,9 @@ import (
 	"strings"
 	"syscall"
 
-	"github.com/prometheus/procfs/internal/util"
 	fsp "io/fs"
+
+	"github.com/prometheus/procfs/internal/util"
 )
 
 // ClassThermalZoneStats contains info from files in /sys/class/thermal/thermal_zone<zone>
diff --git a/sysfs/system_cpu.go b/sysfs/system_cpu.go
index 5b1bec8..a68c6d9 100644
--- a/sysfs/system_cpu.go
+++ b/sysfs/system_cpu.go
@@ -132,6 +132,16 @@ func (c CPU) ThermalThrottle() (*CPUThermalThrottle, error) {
 	return t, nil
 }
 
+// Online returns the online status of a CPU from `/sys/devices/system/cpu/cpuN/online`.
+func (c CPU) Online() (bool, error) {
+	cpuPath := filepath.Join(string(c), "online")
+	str, err := util.SysReadFile(cpuPath)
+	if err != nil {
+		return false, err
+	}
+	return str == "1", nil
+}
+
 func parseCPUThermalThrottle(cpuPath string) (*CPUThermalThrottle, error) {
 	t := CPUThermalThrottle{}
 	var err error


Test patch:
diff --git a/mdstat_test.go b/mdstat_test.go
index adeb2bb..7a577ed 100644
--- a/mdstat_test.go
+++ b/mdstat_test.go
@@ -13,8 +13,11 @@
 
 package procfs
 
-import "testing"
-import "github.com/google/go-cmp/cmp"
+import (
+	"testing"
+
+	"github.com/google/go-cmp/cmp"
+)
 
 func TestFS_MDStat(t *testing.T) {
 	fs := getProcFixtures(t)
diff --git a/net_conntrackstat_test.go b/net_conntrackstat_test.go
index b19222a..67aa18d 100644
--- a/net_conntrackstat_test.go
+++ b/net_conntrackstat_test.go
@@ -34,7 +34,7 @@ func TestParseConntrackStat(t *testing.T) {
 	}
 
 	want := []ConntrackStatEntry{
-		ConntrackStatEntry{
+		{
 			Entries:       33,
 			Found:         0,
 			Invalid:       3,
@@ -45,7 +45,7 @@ func TestParseConntrackStat(t *testing.T) {
 			EarlyDrop:     0,
 			SearchRestart: 0,
 		},
-		ConntrackStatEntry{
+		{
 			Entries:       33,
 			Found:         0,
 			Invalid:       2,
@@ -56,7 +56,7 @@ func TestParseConntrackStat(t *testing.T) {
 			EarlyDrop:     0,
 			SearchRestart: 2,
 		},
-		ConntrackStatEntry{
+		{
 			Entries:       33,
 			Found:         0,
 			Invalid:       1,
@@ -67,7 +67,7 @@ func TestParseConntrackStat(t *testing.T) {
 			EarlyDrop:     0,
 			SearchRestart: 1,
 		},
-		ConntrackStatEntry{
+		{
 			Entries:       33,
 			Found:         0,
 			Invalid:       47,
@@ -96,7 +96,7 @@ func TestParseOldConntrackStat(t *testing.T) {
 	}
 
 	want := []ConntrackStatEntry{
-		ConntrackStatEntry{
+		{
 			Entries:       43,
 			Searched:      202143,
 			Found:         48658538,
diff --git a/net_udp_test.go b/net_udp_test.go
index 6576161..0db53bf 100644
--- a/net_udp_test.go
+++ b/net_udp_test.go
@@ -30,7 +30,7 @@ func Test_newNetUDP(t *testing.T) {
 			name: "udp file found, no error should come up",
 			file: "testdata/fixtures/proc/net/udp",
 			want: []*netIPSocketLine{
-				&netIPSocketLine{
+				{
 					Sl:        0,
 					LocalAddr: net.IP{10, 0, 0, 5},
 					LocalPort: 22,
@@ -43,7 +43,7 @@ func Test_newNetUDP(t *testing.T) {
 					Inode:     2740,
 					Drops:     intToU64(100),
 				},
-				&netIPSocketLine{
+				{
 					Sl:        1,
 					LocalAddr: net.IP{0, 0, 0, 0},
 					LocalPort: 22,
@@ -56,7 +56,7 @@ func Test_newNetUDP(t *testing.T) {
 					Inode:     2740,
 					Drops:     intToU64(100),
 				},
-				&netIPSocketLine{
+				{
 					Sl:        2,
 					LocalAddr: net.IP{0, 0, 0, 0},
 					LocalPort: 22,
@@ -76,7 +76,7 @@ func Test_newNetUDP(t *testing.T) {
 			name: "udp6 file found, no error should come up",
 			file: "testdata/fixtures/proc/net/udp6",
 			want: []*netIPSocketLine{
-				&netIPSocketLine{
+				{
 					Sl:        1315,
 					LocalAddr: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
 					LocalPort: 5355,
@@ -89,7 +89,7 @@ func Test_newNetUDP(t *testing.T) {
 					Inode:     21040,
 					Drops:     intToU64(0),
 				},
-				&netIPSocketLine{
+				{
 					Sl:        6073,
 					LocalAddr: net.IP{254, 128, 0, 0, 0, 0, 0, 0, 86, 225, 173, 255, 254, 124, 102, 9},
 					LocalPort: 51073,
diff --git a/proc_test.go b/proc_test.go
index 239e35b..ecf889a 100644
--- a/proc_test.go
+++ b/proc_test.go
@@ -257,9 +257,9 @@ func TestFileDescriptorsInfo(t *testing.T) {
 	sort.Sort(fdinfos)
 	var want = ProcFDInfos{
 		ProcFDInfo{FD: "0", Pos: "0", Flags: "02004000", MntID: "13", InotifyInfos: []InotifyInfo{
-			InotifyInfo{WD: "3", Ino: "1", Sdev: "34", Mask: "fce"},
-			InotifyInfo{WD: "2", Ino: "1300016", Sdev: "fd00002", Mask: "fce"},
-			InotifyInfo{WD: "1", Ino: "2e0001", Sdev: "fd00000", Mask: "fce"},
+			{WD: "3", Ino: "1", Sdev: "34", Mask: "fce"},
+			{WD: "2", Ino: "1300016", Sdev: "fd00002", Mask: "fce"},
+			{WD: "1", Ino: "2e0001", Sdev: "fd00000", Mask: "fce"},
 		}},
 		ProcFDInfo{FD: "1", Pos: "0", Flags: "02004002", MntID: "13", InotifyInfos: nil},
 		ProcFDInfo{FD: "10", Pos: "0", Flags: "02004002", MntID: "9", InotifyInfos: nil},
diff --git a/sysfs/system_cpu_test.go b/sysfs/system_cpu_test.go
index 577c118..a39c570 100644
--- a/sysfs/system_cpu_test.go
+++ b/sysfs/system_cpu_test.go
@@ -18,6 +18,7 @@ package sysfs
 
 import (
 	"errors"
+	"os"
 	"reflect"
 	"testing"
 )
@@ -63,6 +64,34 @@ func TestCPUTopology(t *testing.T) {
 	}
 }
 
+func TestCPUOnline(t *testing.T) {
+	fs, err := NewFS(sysTestFixtures)
+	if err != nil {
+		t.Fatal(err)
+	}
+	cpus, err := fs.CPUs()
+	if err != nil {
+		t.Fatal(err)
+	}
+	if want, have := 3, len(cpus); want != have {
+		t.Errorf("incorrect number of CPUs, have %v, want %v", want, have)
+	}
+	cpu0Online, err := cpus[0].Online()
+	if err != nil {
+		t.Fatal(err)
+	}
+	if want, have := true, cpu0Online; want != have {
+		t.Errorf("incorrect online status, have %v, want %v", want, have)
+	}
+	cpu1Online, err := cpus[1].Online()
+	if err != nil && !errors.Is(err, os.ErrNotExist) {
+		t.Fatal(err)
+	}
+	if want, have := false, cpu1Online; want != have {
+		t.Errorf("incorrect online status, have %v, want %v", want, have)
+	}
+}
+
 func TestCPUThermalThrottle(t *testing.T) {
 	fs, err := NewFS(sysTestFixtures)
 	if err != nil {
diff --git a/testdata/fixtures.ttar b/testdata/fixtures.ttar
index a998b27..61e879e 100644
--- a/testdata/fixtures.ttar
+++ b/testdata/fixtures.ttar
@@ -13441,6 +13441,11 @@ Mode: 775
 Path: fixtures/sys/devices/system/cpu/cpu0/cpufreq
 SymlinkTo: ../cpufreq/policy0
 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+Path: fixtures/sys/devices/system/cpu/cpu0/online
+Lines: 1
+1EOF
+Mode: 644
+# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Directory: fixtures/sys/devices/system/cpu/cpu0/thermal_throttle
 Mode: 755
 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -