Skip to content

Commit

Permalink
api/v1/firmware_sets: update fixtures and tests for AttributeOperators
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Aug 14, 2023
1 parent cf5890d commit a008189
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 30 deletions.
60 changes: 57 additions & 3 deletions internal/dbtools/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ var (
FixtureDellR640CPLD *models.ComponentFirmwareVersion
FixtureDellR6515BMC *models.ComponentFirmwareVersion
FixtureDellR6515BIOS *models.ComponentFirmwareVersion
FixtureSuperMicro *models.ComponentFirmwareVersion
FixtureServerComponents models.ServerComponentSlice

// ComponentFirmwareSet fixtures
FixtureSuperMicroX11DPHTBMC *models.ComponentFirmwareVersion
FixtureFirmwareUUIDsSuperMicro []string
FixtureFirmwareSetX11DPHT *models.ComponentFirmwareSet
FixtureFirmwareSetX11DPHTAttribute *models.AttributesFirmwareSet

FixtureFirmwareUUIDsR6515 []string
FixtureFirmwareSetR6515 *models.ComponentFirmwareSet
FixtureFirmwareSetR6515Attribute *models.AttributesFirmwareSet
Expand Down Expand Up @@ -129,6 +133,10 @@ func addFixtures(t *testing.T) error {
return err
}

if err := setupFirmwareSetSupermicroX11DPHT(ctx, testDB); err != nil {
return err
}

// excluding Chuckles here since that server is deleted
FixtureServers = models.ServerSlice{FixtureNemo, FixtureDory, FixtureMarlin}
FixtureDeletedServers = models.ServerSlice{FixtureChuckles}
Expand Down Expand Up @@ -459,7 +467,9 @@ func setupFirmwareDellR6515(ctx context.Context, db *sqlx.DB) error {
}

func setupFirmwareSuperMicro(ctx context.Context, db *sqlx.DB) error {
FixtureSuperMicro = &models.ComponentFirmwareVersion{
FixtureFirmwareUUIDsSuperMicro = []string{}

FixtureSuperMicroX11DPHTBMC = &models.ComponentFirmwareVersion{
Vendor: "SuperMicro",
Model: types.StringArray{"X11DPH-T"},
Filename: "SMT_X11AST2500_173_11.bin",
Expand All @@ -470,7 +480,51 @@ func setupFirmwareSuperMicro(ctx context.Context, db *sqlx.DB) error {
RepositoryURL: "https://example-firmware-bucket.s3.amazonaws.com/firmware/supermicro/X11DPH-T/bmc/SMT_X11AST2500_173_11.bin",
}

return FixtureSuperMicro.Insert(ctx, db, boil.Infer())
if err := FixtureSuperMicroX11DPHTBMC.Insert(ctx, db, boil.Infer()); err != nil {
return err
}

FixtureFirmwareUUIDsSuperMicro = append(FixtureFirmwareUUIDsSuperMicro, FixtureSuperMicroX11DPHTBMC.ID)

return nil
}

func setupFirmwareSetSupermicroX11DPHT(ctx context.Context, db *sqlx.DB) error {
// setup firmware fixtures if they haven't been
if len(FixtureFirmwareUUIDsSuperMicro) == 0 {
if err := setupFirmwareSuperMicro(ctx, db); err != nil {
return err
}
}

FixtureFirmwareSetX11DPHT = &models.ComponentFirmwareSet{Name: "x11dph-t"}

if err := FixtureFirmwareSetX11DPHT.Insert(ctx, db, boil.Infer()); err != nil {
return err
}

FixtureFirmwareSetX11DPHTAttribute = &models.AttributesFirmwareSet{
FirmwareSetID: null.StringFrom(FixtureFirmwareSetX11DPHT.ID),
Namespace: "sh.hollow.firmware_set.labels",
Data: types.JSON([]byte(`{"vendor": "supermicro", "model": "x11dph-t"}`)),
}

if err := FixtureFirmwareSetX11DPHT.AddFirmwareSetAttributesFirmwareSets(ctx, db, true, FixtureFirmwareSetX11DPHTAttribute); err != nil {
return err
}

for _, firmwareID := range FixtureFirmwareUUIDsSuperMicro {
m := &models.ComponentFirmwareSetMap{
FirmwareSetID: FixtureFirmwareSetX11DPHT.ID,
FirmwareID: firmwareID,
}

if err := m.Insert(ctx, db, boil.Infer()); err != nil {
return err
}
}

return nil
}

func setupFirmwareSetR6515(ctx context.Context, db *sqlx.DB) error {
Expand Down
129 changes: 103 additions & 26 deletions pkg/api/v1/router_firmware_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

"go.hollow.sh/serverservice/internal/dbtools"
Expand Down Expand Up @@ -404,7 +405,8 @@ func TestIntegrationServerComponentFirmwareSetGet(t *testing.T) {
// assert firmware set attributes
assert.Equal(t, 1, len(got.Attributes))
assert.Equal(t, dbtools.FixtureFirmwareSetR640Attribute.Namespace, got.Attributes[0].Namespace)
assertAttributesEqual(t, []byte(dbtools.FixtureFirmwareSetR640Attribute.Data), got.Attributes[0].Data)
assertAttributesContains(t, got.Attributes, []byte(dbtools.FixtureFirmwareSetR640Attribute.Data))
assertAttributesContains(t, got.Attributes, []byte(dbtools.FixtureFirmwareSetX11DPHTAttribute.Data))

// assert component firmware
assert.Equal(t, 2, len(got.ComponentFirmware))
Expand All @@ -413,7 +415,17 @@ func TestIntegrationServerComponentFirmwareSetGet(t *testing.T) {
}
}

func assertAttributesEqual(t *testing.T, a, b []byte) {
func assertAttributesContains(t *testing.T, attrs []serverservice.Attributes, a []byte) bool {
for _, attr := range attrs {
if assertAttributesEqual(t, a, attr.Data) {
return true
}
}

return false
}

func assertAttributesEqual(t *testing.T, a, b []byte) bool {
t.Helper()

// unmarshal fixture attribute data
Expand All @@ -428,8 +440,7 @@ func assertAttributesEqual(t *testing.T, a, b []byte) {
t.Fatal(err)
}

// assert fixture data
assert.Equal(t, aData, bData)
return maps.Equal(aData, bData)
}

func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
Expand All @@ -449,9 +460,8 @@ func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
testCases := []struct {
testName string
params *serverservice.ComponentFirmwareSetListParams
expectedFirmwareSetAttribute *models.AttributesFirmwareSet
expectedFirmwareModel []string
expectedFirmwareCount int
expectedFirmwareSetAttribute []*models.AttributesFirmwareSet
expectedFirmwareModels []string
expectedTotalRecordCount int
expectedPage int
expectedError bool
Expand All @@ -461,9 +471,8 @@ func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
{
"list firmware set by name - r640",
&serverservice.ComponentFirmwareSetListParams{Name: "r640"},
dbtools.FixtureFirmwareSetR640Attribute,
[]*models.AttributesFirmwareSet{dbtools.FixtureFirmwareSetR640Attribute},
[]string{"R640"},
2,
1,
1,
false,
Expand All @@ -472,9 +481,8 @@ func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
{
"list firmware set by name - r6515",
&serverservice.ComponentFirmwareSetListParams{Name: "r6515"},
dbtools.FixtureFirmwareSetR6515Attribute,
[]*models.AttributesFirmwareSet{dbtools.FixtureFirmwareSetR6515Attribute},
[]string{"R6515"},
2,
1,
1,
false,
Expand All @@ -490,8 +498,7 @@ func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
},
nil,
nil,
2,
2,
3,
2,
false,
"",
Expand All @@ -514,14 +521,42 @@ func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
},
},
},
dbtools.FixtureFirmwareSetR640Attribute,
[]*models.AttributesFirmwareSet{dbtools.FixtureFirmwareSetR640Attribute},
[]string{"R640"},
2,
1,
1,
false,
"",
},
{
"list firmware set by attribute params with OR on attribute",
&serverservice.ComponentFirmwareSetListParams{
AttributeListParams: []serverservice.AttributeListParams{
{
Namespace: "sh.hollow.firmware_set.labels",
Keys: []string{"model"},
Operator: "eq",
Value: "r640",
},
{
Namespace: "sh.hollow.firmware_set.labels",
Keys: []string{"model"},
Operator: "eq",
Value: "x11dph-t",
AttributeOperator: serverservice.AttributeLogicalOR,
},
},
},
[]*models.AttributesFirmwareSet{
dbtools.FixtureFirmwareSetR640Attribute,
dbtools.FixtureFirmwareSetX11DPHTAttribute,
},
[]string{"R640", "X11DPH-T"},
2,
1,
false,
"",
},
{
"list with incorrect firmware set Name attribute returns no records",
&serverservice.ComponentFirmwareSetListParams{
Expand All @@ -530,7 +565,6 @@ func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
nil,
nil,
0,
0,
1,
false,
"",
Expand All @@ -549,19 +583,13 @@ func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
assert.NotNil(t, resp)
assert.NotNil(t, got)

if tt.expectedFirmwareCount > 0 {
assert.Equal(t, tt.expectedFirmwareCount, len(got[0].ComponentFirmware))
}

if tt.expectedFirmwareSetAttribute != nil {
assert.Equal(t, 1, len(got[0].Attributes))
assert.Equal(t, tt.expectedFirmwareSetAttribute.Namespace, got[0].Attributes[0].Namespace)
assertAttributesEqual(t, []byte(tt.expectedFirmwareSetAttribute.Data), got[0].Attributes[0].Data)
assert.True(t, assertFirmwareSetAttributeNSEqual(t, tt.expectedFirmwareSetAttribute, got))
assert.True(t, assertContainsFirmwareSetAttributes(t, tt.expectedFirmwareSetAttribute, got))
}

if tt.expectedFirmwareModel != nil {
assert.Equal(t, tt.expectedFirmwareModel, got[0].ComponentFirmware[0].Model)
assert.Equal(t, tt.expectedFirmwareModel, got[0].ComponentFirmware[1].Model)
if tt.expectedFirmwareModels != nil {
assert.True(t, firmwareSetContainsModel(t, tt.expectedFirmwareModels, got))
}

assert.Equal(t, tt.expectedPage, resp.Page)
Expand All @@ -570,6 +598,55 @@ func TestIntegrationServerComponentFirmwareSetList(t *testing.T) {
}
}

func assertContainsFirmwareSetAttributes(t *testing.T, fwSetModelAttrs []*models.AttributesFirmwareSet, fwSets []serverservice.ComponentFirmwareSet) bool {
t.Helper()

expected := len(fwSetModelAttrs)

var got int

for _, fwSetModelAttr := range fwSetModelAttrs {
for _, fwSet := range fwSets {
if assertAttributesContains(t, fwSet.Attributes, fwSetModelAttr.Data) {
got++
}
}
}

return expected == got
}

func assertFirmwareSetAttributeNSEqual(t *testing.T, fwSetModelAttrs []*models.AttributesFirmwareSet, fwSets []serverservice.ComponentFirmwareSet) bool {
for _, fwSetModelAttr := range fwSetModelAttrs {
for _, fwSet := range fwSets {
for _, attr := range fwSet.Attributes {
if fwSetModelAttr.Namespace != attr.Namespace {
t.Errorf("attr namespace %s != %s", fwSetModelAttr.Namespace, attr.Namespace)
return false
}
}
}
}

return true
}

func firmwareSetContainsModel(t *testing.T, models []string, set []serverservice.ComponentFirmwareSet) bool {
t.Helper()

for _, model := range models {
for _, f := range set {
for _, firmware := range f.ComponentFirmware {
if slices.Contains(firmware.Model, model) {
return true
}
}
}
}

return false
}

func TestIntegrationServerComponentFirmwareSetDelete(t *testing.T) {
s := serverTest(t)

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/router_firmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestIntegrationFirmwareList(t *testing.T) {
&serverservice.ComponentFirmwareVersionListParams{
Model: []string{"X11DPH-T"},
},
[]string{dbtools.FixtureSuperMicro.ID},
[]string{dbtools.FixtureSuperMicroX11DPHTBMC.ID},
false,
"",
},
Expand Down

0 comments on commit a008189

Please sign in to comment.