diff --git a/decoder/pci_class.go b/decoder/pci_class.go index 0c2fe74a..484a3541 100644 --- a/decoder/pci_class.go +++ b/decoder/pci_class.go @@ -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 } @@ -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 } diff --git a/decoder/pci_class_test.go b/decoder/pci_class_test.go index 0078c50e..bd06be33 100644 --- a/decoder/pci_class_test.go +++ b/decoder/pci_class_test.go @@ -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"), @@ -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) - } - } + }) } diff --git a/decoder/pci_device.go b/decoder/pci_device.go index 7d2aca3a..b7147169 100644 --- a/decoder/pci_device.go +++ b/decoder/pci_device.go @@ -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 } @@ -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 } diff --git a/decoder/pci_device_test.go b/decoder/pci_device_test.go index f31955a3..1ee0353d 100644 --- a/decoder/pci_device_test.go +++ b/decoder/pci_device_test.go @@ -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)"), @@ -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) - } - } + }) } diff --git a/decoder/pci_subclass.go b/decoder/pci_subclass.go index 654e68ea..1a815be4 100644 --- a/decoder/pci_subclass.go +++ b/decoder/pci_subclass.go @@ -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 } @@ -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 } diff --git a/decoder/pci_subclass_test.go b/decoder/pci_subclass_test.go index 349a2137..2c006bfc 100644 --- a/decoder/pci_subclass_test.go +++ b/decoder/pci_subclass_test.go @@ -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"), @@ -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) - } - } + }) } diff --git a/decoder/pci_test.go b/decoder/pci_test.go new file mode 100644 index 00000000..c4c3de71 --- /dev/null +++ b/decoder/pci_test.go @@ -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) + } + } +} diff --git a/decoder/pci_vendor.go b/decoder/pci_vendor.go index 5554b7da..26bf95a7 100644 --- a/decoder/pci_vendor.go +++ b/decoder/pci_vendor.go @@ -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 } @@ -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 } diff --git a/decoder/pci_vendor_test.go b/decoder/pci_vendor_test.go index 8bad695c..56727460 100644 --- a/decoder/pci_vendor_test.go +++ b/decoder/pci_vendor_test.go @@ -1,46 +1,19 @@ package decoder import ( - "bytes" "testing" - - "github.com/cloudflare/ebpf_exporter/v2/config" ) func TestPCIVendorDecoderMissing(t *testing.T) { - if pci != nil { - t.Skip("PCI DB is available") - } - - cases := [][]byte{ + testPCIMissing(t, &PCIVendor{}, [][]byte{ []byte("32902"), // 0x8086 []byte("4098"), // 0x1002 []byte("4318"), // 0x10de - } - - for _, c := range cases { - d := &PCIVendor{} - - 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 TestPCIVendorDecoderPresent(t *testing.T) { - if pci == nil { - t.Skip("PCI DB is not available") - } - - cases := []struct { - in []byte - out []byte - }{ + testPCIPresent(t, &PCIVendor{}, []pciCase{ { in: []byte("32902"), // 0x8086 out: []byte("Intel Corporation"), @@ -69,18 +42,5 @@ func TestPCIVendorDecoderPresent(t *testing.T) { in: []byte("48879"), // 0xbeef out: []byte("unknown pci vendor: 0xbeef"), }, - } - - for _, c := range cases { - d := &PCIVendor{} - - 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) - } - } + }) }