Skip to content

Commit

Permalink
Merge pull request #259 from bobrik/ivan/pci-lint-fix
Browse files Browse the repository at this point in the history
Fix new linter issues in pci decoders
  • Loading branch information
bobrik authored Sep 1, 2023
2 parents 89b3fa2 + 7df8d7a commit 6d07a15
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 189 deletions.
6 changes: 3 additions & 3 deletions decoder/pci_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type PCIClass struct{}

// Decode transforms PCI class id into a name
func (d *PCIClass) Decode(in []byte, conf config.Decoder) ([]byte, error) {
func (d *PCIClass) Decode(in []byte, _ config.Decoder) ([]byte, error) {
if pci == nil {
return []byte(missingPciIdsText), nil
}
Expand All @@ -25,7 +25,7 @@ func (d *PCIClass) Decode(in []byte, conf config.Decoder) ([]byte, error) {

if device, ok := pci.Classes[key]; ok {
return []byte(device.Name), nil
} else {
return []byte(fmt.Sprintf("unknown pci class: 0x%s", key)), nil
}

return []byte(fmt.Sprintf("unknown pci class: 0x%s", key)), nil
}
48 changes: 4 additions & 44 deletions decoder/pci_class_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
package decoder

import (
"bytes"
"testing"

"github.com/cloudflare/ebpf_exporter/v2/config"
)

func TestPCIClassDecoderMissing(t *testing.T) {
if pci != nil {
t.Skip("PCI DB is available")
}

cases := [][]byte{
testPCIMissing(t, &PCIClass{}, [][]byte{
[]byte("1"),
[]byte("2"),
[]byte("6"),
}

for _, c := range cases {
d := &PCIClass{}

out, err := d.Decode(c, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %v", c, err)
}

if !bytes.Equal(out, []byte(missingPciIdsText)) {
t.Errorf("Expected %q, got %s", missingPciIdsText, out)
}
}
})
}

func TestPCIClassDecoderPresent(t *testing.T) {
if pci == nil {
t.Skip("PCI DB is not available")
}

cases := []struct {
in []byte
out []byte
}{
testPCIPresent(t, &PCIClass{}, []pciCase{
{
in: []byte("1"),
out: []byte("Mass storage controller"),
Expand All @@ -65,18 +38,5 @@ func TestPCIClassDecoderPresent(t *testing.T) {
in: []byte("253"),
out: []byte("unknown pci class: 0xfd"),
},
}

for _, c := range cases {
d := &PCIClass{}

out, err := d.Decode(c.in, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %v", c.in, err)
}

if !bytes.Equal(out, c.out) {
t.Errorf("Expected %q, got %q", c.out, out)
}
}
})
}
8 changes: 4 additions & 4 deletions decoder/pci_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/cloudflare/ebpf_exporter/v2/config"
)

// PCIDevice2 is a decoder that transforms PCI device id into a name
// PCIDevice is a decoder that transforms PCI device id into a name
type PCIDevice struct{}

// Decode transforms PCI device id into a name
func (d *PCIDevice) Decode(in []byte, conf config.Decoder) ([]byte, error) {
func (d *PCIDevice) Decode(in []byte, _ config.Decoder) ([]byte, error) {
if pci == nil {
return []byte(missingPciIdsText), nil
}
Expand All @@ -25,7 +25,7 @@ func (d *PCIDevice) Decode(in []byte, conf config.Decoder) ([]byte, error) {

if device, ok := pci.Products[key]; ok {
return []byte(device.Name), nil
} else {
return []byte(fmt.Sprintf("unknown pci device: 0x%s", key)), nil
}

return []byte(fmt.Sprintf("unknown pci device: 0x%s", key)), nil
}
48 changes: 4 additions & 44 deletions decoder/pci_device_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
package decoder

import (
"bytes"
"testing"

"github.com/cloudflare/ebpf_exporter/v2/config"
)

func TestPCIDeviceDecoderMissing(t *testing.T) {
if pci != nil {
t.Skip("PCI DB is available")
}

cases := [][]byte{
testPCIMissing(t, &PCIDevice{}, [][]byte{
[]byte("2156269568"), // 0x80861000
[]byte("268596191"), // 0x100273df
[]byte("282994436"), // 0x10de2704
}

for _, c := range cases {
d := &PCIDevice{}

out, err := d.Decode(c, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %v", c, err)
}

if !bytes.Equal(out, []byte(missingPciIdsText)) {
t.Errorf("Expected %q, got %s", missingPciIdsText, out)
}
}
})
}

func TestPCIDeviceDecoderPresent(t *testing.T) {
if pci == nil {
t.Skip("PCI DB is not available")
}

cases := []struct {
in []byte
out []byte
}{
testPCIPresent(t, &PCIDevice{}, []pciCase{
{
in: []byte("2156269568"), // 0x80861000
out: []byte("82542 Gigabit Ethernet Controller (Fiber)"),
Expand Down Expand Up @@ -69,18 +42,5 @@ func TestPCIDeviceDecoderPresent(t *testing.T) {
in: []byte("3735928559"), // 0xdeadbeef
out: []byte("unknown pci device: 0xdeadbeef"),
},
}

for _, c := range cases {
d := &PCIDevice{}

out, err := d.Decode(c.in, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %v", c.in, err)
}

if !bytes.Equal(out, c.out) {
t.Errorf("Expected %q, got %q", c.out, out)
}
}
})
}
6 changes: 3 additions & 3 deletions decoder/pci_subclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type PCISubClass struct{}

// Decode transforms PCI class id into a name
func (d *PCISubClass) Decode(in []byte, conf config.Decoder) ([]byte, error) {
func (d *PCISubClass) Decode(in []byte, _ config.Decoder) ([]byte, error) {
if pci == nil {
return []byte(missingPciIdsText), nil
}
Expand All @@ -32,7 +32,7 @@ func (d *PCISubClass) Decode(in []byte, conf config.Decoder) ([]byte, error) {
}

return []byte(fmt.Sprintf("unknown pci subclass: 0x%s (class 0x%s)", subclassID, classID)), nil
} else {
return []byte(fmt.Sprintf("unknown pci class: 0x%s", classID)), nil
}

return []byte(fmt.Sprintf("unknown pci class: 0x%s", classID)), nil
}
48 changes: 4 additions & 44 deletions decoder/pci_subclass_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
package decoder

import (
"bytes"
"testing"

"github.com/cloudflare/ebpf_exporter/v2/config"
)

func TestPCISubClassDecoderMissing(t *testing.T) {
if pci != nil {
t.Skip("PCI DB is available")
}

cases := [][]byte{
testPCIMissing(t, &PCISubClass{}, [][]byte{
[]byte("5"),
[]byte("264"),
[]byte("512"),
}

for _, c := range cases {
d := &PCISubClass{}

out, err := d.Decode(c, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %v", c, err)
}

if !bytes.Equal(out, []byte(missingPciIdsText)) {
t.Errorf("Expected %q, got %s", missingPciIdsText, out)
}
}
})
}

func TestPCISubClassDecoderPresent(t *testing.T) {
if pci == nil {
t.Skip("PCI DB is not available")
}

cases := []struct {
in []byte
out []byte
}{
testPCIPresent(t, &PCISubClass{}, []pciCase{
{
in: []byte("5"), // 0x0005
out: []byte("Image coprocessor"),
Expand Down Expand Up @@ -81,18 +54,5 @@ func TestPCISubClassDecoderPresent(t *testing.T) {
in: []byte("3"), // 0x0003
out: []byte("unknown pci subclass: 0x03 (class 0x00)"),
},
}

for _, c := range cases {
d := &PCISubClass{}

out, err := d.Decode(c.in, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %v", c.in, err)
}

if !bytes.Equal(out, c.out) {
t.Errorf("Expected %q, got %q", c.out, out)
}
}
})
}
47 changes: 47 additions & 0 deletions decoder/pci_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package decoder

import (
"bytes"
"testing"

"github.com/cloudflare/ebpf_exporter/v2/config"
)

func testPCIMissing(t *testing.T, d Decoder, cases [][]byte) {
if pci != nil {
t.Skip("PCI DB is available")
}

for _, c := range cases {
out, err := d.Decode(c, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %v", c, err)
}

if !bytes.Equal(out, []byte(missingPciIdsText)) {
t.Errorf("Expected %q, got %s", missingPciIdsText, out)
}
}
}

type pciCase struct {
in []byte
out []byte
}

func testPCIPresent(t *testing.T, d Decoder, cases []pciCase) {
if pci == nil {
t.Skip("PCI DB is not available")
}

for _, c := range cases {
out, err := d.Decode(c.in, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %v", c.in, err)
}

if !bytes.Equal(out, c.out) {
t.Errorf("Expected %q, got %q", c.out, out)
}
}
}
6 changes: 3 additions & 3 deletions decoder/pci_vendor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type PCIVendor struct{}

// Decode transforms PCI vendor id into a name
func (d *PCIVendor) Decode(in []byte, conf config.Decoder) ([]byte, error) {
func (d *PCIVendor) Decode(in []byte, _ config.Decoder) ([]byte, error) {
if pci == nil {
return []byte(missingPciIdsText), nil
}
Expand All @@ -25,7 +25,7 @@ func (d *PCIVendor) Decode(in []byte, conf config.Decoder) ([]byte, error) {

if vendor, ok := pci.Vendors[key]; ok {
return []byte(vendor.Name), nil
} else {
return []byte(fmt.Sprintf("unknown pci vendor: 0x%s", key)), nil
}

return []byte(fmt.Sprintf("unknown pci vendor: 0x%s", key)), nil
}
Loading

0 comments on commit 6d07a15

Please sign in to comment.