Skip to content

Commit

Permalink
allow use of different filename formats for calnex firmware upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
PDeanCalnex committed Dec 3, 2024
1 parent 05732e9 commit 61feb60
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
4 changes: 3 additions & 1 deletion calnex/firmware/firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func (up CalnexUpgrader) ShouldUpgrade(target string, api *calnexAPI.API, fw FW,
if err != nil {
return false, err
}
calnexVersion, err := version.NewVersion(strings.ToLower(cv.Firmware))
//remove major version 2.17.0 -> 17.0 as this is hadware revision related
//calnexVersion, err := version.NewVersion(strings.ToLower(cv.Firmware))
calnexVersion, err := version.NewVersion(strings.ToLower(strings.SplitN(cv.Firmware, ".", 2)[1]))
if err != nil {
return false, err
}
Expand Down
26 changes: 25 additions & 1 deletion calnex/firmware/ossfirmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package firmware

import (
"fmt"
"path/filepath"
"regexp"
"strings"

version "github.com/hashicorp/go-version"
Expand All @@ -31,12 +33,34 @@ type OSSFW struct {

// NewOSSFW returns initialized version of OSSFW
func NewOSSFW(source string) (*OSSFW, error) {
var vs string

fw := &OSSFW{
filepath: source,
}

basename := filepath.Base(fw.filepath)
vs := strings.ReplaceAll(strings.TrimSuffix(basename, filepath.Ext(basename)), "sentinel_fw_v", "")
// Extract version from filename
// sentinel_fw_v2.13.1.0.5583D-20210924.tar -> 13.1.0.5583
// calnex_combined_fw_R21.0.0.9705-20241111.tar -> 21.0.0.9705

re := regexp.MustCompile(`(sentry_fw_|sentinel_fw_|calnex_combined_fw_)(v[0-9]\.|R)(.*)\.tar`)

matches := re.FindStringSubmatch(basename)

if len(matches) == 4 {
vs = matches[3]
} else {
return nil, fmt.Errorf("invalid filename, expected prefix sentinel_fw_, sentry_fw_, or calnex_combined_fw_ followed by 'v2.' or 'R': %s", basename)
}
v, err := version.NewVersion(strings.ToLower(vs))
if err != nil {
return nil, err
}
//expect major, minor, patch, build
if len(v.Segments()) != 4 {
return nil, fmt.Errorf("invalid version format, expected 4 segments: %v", v.Segments())
}
fw.version = v
return fw, err
}
Expand Down
83 changes: 80 additions & 3 deletions calnex/firmware/ossfirmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestOSSFW(t *testing.T) {
expectedFilePath := "/tmp/sentinel_fw_v2.13.1.0.5583D-20210924.tar"
expectedVersion, _ := version.NewVersion("2.13.1.0.5583d-20210924")
func checkOSSFW(t *testing.T, expectedFilePath string, expectedVersion *version.Version) {
fw, err := NewOSSFW(expectedFilePath)
require.NoError(t, err)

Expand All @@ -37,3 +35,82 @@ func TestOSSFW(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expectedVersion, v)
}

func checkErrorOSSFW(t *testing.T, expectedFilePath string) {
fw, err := NewOSSFW(expectedFilePath)
require.Nil(t, fw)
require.Error(t, err)
}

func TestOSSFW(t *testing.T) {
//all files are expected to produce the same version output
//development/beta build versions (D after build number)
expectedVersiondev, _ := version.NewVersion("13.1.0.5583d-20210924")

// Test case for sentinel_fw
expectedFilePathSentineldev := "/tmp/sentinel_fw_v2.13.1.0.5583D-20210924.tar"
checkOSSFW(t, expectedFilePathSentineldev, expectedVersiondev)

// Test case for sentry
expectedFilePathSentrydev := "/tmp/sentry_fw_v2.13.1.0.5583D-20210924.tar"
checkOSSFW(t, expectedFilePathSentrydev, expectedVersiondev)

// Test case for calnex_combined
expectedFilePathCalnexdev := "/tmp/calnex_combined_fw_R13.1.0.5583D-20210924.tar"
checkOSSFW(t, expectedFilePathCalnexdev, expectedVersiondev)

//full release versions (no D)
expectedVersion, _ := version.NewVersion("13.1.0.5583-20210924")

// Test case for sentinel_fw
expectedFilePathSentinel := "/tmp/sentinel_fw_v2.13.1.0.5583-20210924.tar"
checkOSSFW(t, expectedFilePathSentinel, expectedVersion)

// Test case for sentry
expectedFilePathSentry := "/tmp/sentry_fw_v2.13.1.0.5583-20210924.tar"
checkOSSFW(t, expectedFilePathSentry, expectedVersion)

// Test case for calnex_combined
expectedFilePathCalnex := "/tmp/calnex_combined_fw_R13.1.0.5583-20210924.tar"
checkOSSFW(t, expectedFilePathCalnex, expectedVersion)

// Check errors
expectedFilePathMissingStart := "/tmp/R13.1.0.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathMissingStart)

//missing v2./R
expectedFilePathMissingSecond := "/tmp/sentinel_fw_13.1.0.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathMissingSecond)

//misspelt sintinel
expectedFilePathBadStart := "/tmp/sentrinel_fw_R13.1.0.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathBadStart)

//both v2. and R
expectedFilePathBadSecond := "/tmp/sentinel_fw_Rv2.13.1.0.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathBadSecond)

//.zip not .tar
expectedFilePathBadExtension := "/tmp/sentinel_fw_R13.1.0.5583-20210924.zip"
checkErrorOSSFW(t, expectedFilePathBadExtension)

//HW version not 0-9
expectedFilePathBadHWVersion := "/tmp/sentinel_fw_v10.13.1.0.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathBadHWVersion)

//malformed versions
expectedFilePathBadBuild1 := "/tmp/sentinel_fw_v2.13.1.0.D5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathBadBuild1)

expectedFilePathBadBuild2 := "/tmp/sentinel_fw_v2.13.1D.0.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathBadBuild2)

expectedFilePathBadBuild3 := "/tmp/sentinel_fw_v2.13D.1.0.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathBadBuild3)

expectedFilePathBadBuild4 := "/tmp/sentinel_fw_v2.13.1..10.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathBadBuild4)

expectedFilePathBadBuild5 := "/tmp/sentinel_fw_v2.13.1.1.10.5583-20210924.tar"
checkErrorOSSFW(t, expectedFilePathBadBuild5)
}

0 comments on commit 61feb60

Please sign in to comment.