Skip to content

Commit

Permalink
model/component: BySlug to return BIOS, BMC components as is when found
Browse files Browse the repository at this point in the history
instead of attempting to match them by vendor, model
  • Loading branch information
joelrebel committed Nov 29, 2023
1 parent 5f8a08f commit 09b0f72
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 5 deletions.
29 changes: 24 additions & 5 deletions internal/model/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,35 @@ type Components []*Component

// BySlug returns a component that matches the slug value.
func (c Components) BySlugModel(cSlug string, cModels []string) *Component {
for idx, component := range c {
// identify components that match the slug
slugsMatch := []*Component{}
for _, component := range c {
component := component
// skip non matching component slug
if !strings.EqualFold(cSlug, component.Slug) {
continue
}

// match component model with contains
for _, findModel := range cModels {
if strings.Contains(strings.ToLower(component.Model), strings.TrimSpace(findModel)) {
return c[idx]
// since theres a single BIOS, BMC (:fingers_crossed) component on a machine
// we look for further and return the found component
if strings.EqualFold(common.SlugBIOS, cSlug) || strings.EqualFold(common.SlugBMC, cSlug) {
return component
}

slugsMatch = append(slugsMatch, component)
}

// none found
if len(slugsMatch) == 0 {
return nil
}

// multiple components identified, match component by model
for _, find := range cModels {
for _, component := range slugsMatch {
find = strings.ToLower(strings.TrimSpace(find))
if strings.Contains(strings.ToLower(component.Model), find) {
return component
}
}
}
Expand Down
114 changes: 114 additions & 0 deletions internal/model/component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package model

import (
"testing"

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

func TestComponentBySlugModel(t *testing.T) {
component1 := &Component{
Slug: "cpu",
Serial: "123",
Vendor: "Intel",
Model: "Core i7",
FirmwareInstalled: "v1.0",
}

component2 := &Component{
Slug: "gpu",
Serial: "456",
Vendor: "NVIDIA",
Model: "GeForce RTX 3080",
FirmwareInstalled: "v2.0",
}

component3 := &Component{
Slug: "cpu",
Serial: "789",
Vendor: "AMD",
Model: "Ryzen 9",
FirmwareInstalled: "v1.5",
}

biosComponent := &Component{
Slug: "bios",
Serial: "111",
Vendor: "AMI",
Model: "BIOS Model",
FirmwareInstalled: "v1.2",
}

bmcComponent := &Component{
Slug: "bmc",
Serial: "222",
Vendor: "Supermicro",
Model: "BMC Model",
FirmwareInstalled: "v3.0",
}

components := Components{component1, component2, component3, biosComponent, bmcComponent}

testCases := []struct {
name string
slug string
models []string
expected *Component
}{
{
name: "Single Match",
slug: "gpu",
models: []string{"RTX 3080"},
expected: component2,
},
{
name: "No Match",
slug: "memory",
models: []string{"DDR4"},
expected: nil,
},
{
name: "Multiple Matches",
slug: "cpu",
models: []string{"Ryzen 9"},
expected: component3,
},
{
name: "Slug Match",
slug: "cpu",
models: []string{},
expected: nil,
},
{
name: "Slug BIOS",
slug: "bios",
models: []string{},
expected: biosComponent,
},
{
name: "Slug upper case BIOS",
slug: "BIOS",
models: []string{},
expected: biosComponent,
},
{
name: "Slug BMC",
slug: "bmc",
models: []string{},
expected: bmcComponent,
},
{
name: "Slug BMC with Models",
slug: "bmc",
models: []string{"BMC Model"},
expected: bmcComponent,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := components.BySlugModel(tc.slug, tc.models)
assert.Equal(t, tc.expected, result)
})
}
}

0 comments on commit 09b0f72

Please sign in to comment.