Skip to content

Commit

Permalink
utils/nvme: Explicitly test parsing fna and sanicap values
Browse files Browse the repository at this point in the history
Better to test explicitly for all expected cases than implicitly/by
proxy from the other tests.
  • Loading branch information
mmlb committed Apr 22, 2024
1 parent aeb30a4 commit c9942dc
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions utils/nvme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package utils

import (
"context"
"strconv"
"testing"

"github.com/bmc-toolbox/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_NvmeComponents(t *testing.T) {
Expand Down Expand Up @@ -68,3 +70,90 @@ var fixtureNvmeDeviceCapabilities = []*common.Capability{
{Name: "owr", Description: "Overwrite Sanitize Operation Supported", Enabled: false},
{Name: "ndi", Description: "No-Deallocate After Sanitize bit in Sanitize command Supported", Enabled: false},
}

func Test_NvmeParseFna(t *testing.T) {
// These are laid out so if you squint and pretend false/true are 0/1 they match the bit pattern of the int
// Its a map so order doesn't matter but I think it makes it easier to match a broken test to the code
wants := []map[string]bool{
{"cese": false, "cens": false, "fmns": false},
{"cese": false, "cens": false, "fmns": true},
{"cese": false, "cens": true, "fmns": false},
{"cese": false, "cens": true, "fmns": true},
{"cese": true, "cens": false, "fmns": false},
{"cese": true, "cens": false, "fmns": true},
{"cese": true, "cens": true, "fmns": false},
{"cese": true, "cens": true, "fmns": true},
}
for i, want := range wants {
t.Run(strconv.Itoa(i), func(t *testing.T) {
caps := parseFna(uint(i))
require.Len(t, caps, len(want))
for _, cap := range caps {
require.Equal(t, want[cap.Name], cap.Enabled)
}
})
}
}

func Test_NvmeParseSanicap(t *testing.T) {
// These are laid out so if you squint and pretend false/true are 0/1 they match the bit pattern of the int
// Its a map so order doesn't matter but I think it makes it easier to match a broken test to the code

// lower bits only
wants := []map[string]bool{
{"owr": false, "ber": false, "cer": false},
{"owr": false, "ber": false, "cer": true},
{"owr": false, "ber": true, "cer": false},
{"owr": false, "ber": true, "cer": true},
{"owr": true, "ber": false, "cer": false},
{"owr": true, "ber": false, "cer": true},
{"owr": true, "ber": true, "cer": false},
{"owr": true, "ber": true, "cer": true},
}
for i, want := range wants {
// not testing ndi yet but its being returned
// don't want to add it above to avoid noise
want["ndi"] = false
t.Run(strconv.Itoa(i), func(t *testing.T) {
caps, err := parseSanicap(uint(i))
require.NoError(t, err)
require.Len(t, caps, len(want))
for _, cap := range caps {
require.Equal(t, want[cap.Name], cap.Enabled)
}
})
}

// higher bits only
wants = []map[string]bool{
{"ndi": false},
{"ndi": true},
{"nodmmas": false, "ndi": false},
{"nodmmas": false, "ndi": true},
{"nodmmas": true, "ndi": false},
{"nodmmas": true, "ndi": true},
}
for i, want := range wants {
// not testing these now but they are being returned
// don't want to add them above to avoid noise
want["owr"] = false
want["ber"] = false
want["cer"] = false
i = (i << 29)
t.Run(strconv.Itoa(i), func(t *testing.T) {
caps, err := parseSanicap(uint(i))
require.NoError(t, err)
require.Len(t, caps, len(want))
for _, cap := range caps {
require.Equal(t, want[cap.Name], cap.Enabled)
}
})
}

i := 0b11 << 30
t.Run(strconv.Itoa(i), func(t *testing.T) {
caps, err := parseSanicap(uint(i))
require.Error(t, err)
require.Nil(t, caps)
})
}

0 comments on commit c9942dc

Please sign in to comment.