Skip to content

Commit

Permalink
Merge pull request #106 from Cray-HPE/sort-hardware
Browse files Browse the repository at this point in the history
Moved hardware sort code to standalone functions
  • Loading branch information
rsjostrand-hpe authored Aug 1, 2023
2 parents c7c138e + 2645845 commit 2d0930f
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 18 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ validate-hardware-type-schemas:

unittest: bin
GOOS=$(GOOS) GOARCH=$(GOARCH) go test -cover \
github.com/Cray-HPE/cani/internal/inventory \
github.com/Cray-HPE/cani/internal/provider/csm \
github.com/Cray-HPE/cani/internal/provider/csm/ipam \
github.com/Cray-HPE/cani/internal/provider/csm/sls \
Expand Down
20 changes: 2 additions & 18 deletions internal/domain/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sort"
"strings"

"github.com/Cray-HPE/cani/internal/inventory"
"github.com/Cray-HPE/cani/internal/provider"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -68,24 +69,7 @@ func (d *Domain) ExportCsv(ctx context.Context, writer *csv.Writer, headers []st
sort.Slice(keys, func(i, j int) bool {
hwi := inv.Hardware[keys[i]]
hwj := inv.Hardware[keys[j]]
if hwi.Type == hwj.Type {
lenj := len(hwj.LocationPath)
for i, loci := range hwi.LocationPath {
if i >= lenj {
return false
}

locj := hwj.LocationPath[i]
if loci.Ordinal == locj.Ordinal {
continue // go to the next location
}
return loci.Ordinal < locj.Ordinal
}
// This case should not be hit
// It means that the hardware type and location path are the same.
return false
}
return hwi.Type < hwj.Type
return inventory.CompareHardwareByTypeThenLocation(&hwi, &hwj)
})

normalizedHeaders, err := toNormalizedHeaders(headers)
Expand Down
55 changes: 55 additions & 0 deletions internal/inventory/model_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package inventory

// CompareHardwareByTypeThenLocation returns true if hw1 should sort before h2
// otherwise it returns false
func CompareHardwareByTypeThenLocation(hw1 *Hardware, hw2 *Hardware) bool {
if hw1.Type == hw2.Type {
return CompareLocationPath(hw1.LocationPath, hw2.LocationPath)
}
return hw1.Type < hw2.Type
}

// CompareLocationPath returns true if location1 should be before location2 when
// sorted. This func only compares the location ordinals, and thus it assumes
// that the locations are for the same type of hardware.
func CompareLocationPath(location1 LocationPath, location2 LocationPath) bool {
len2 := len(location2)
for i, loc1 := range location1 {
if i >= len2 {
return false
}

loc2 := location2[i]
if loc1.Ordinal == loc2.Ordinal {
continue // go to the next location
}
return loc1.Ordinal < loc2.Ordinal
}
// This case means that the two locations contain the same list of ordinals
return false
}
81 changes: 81 additions & 0 deletions internal/inventory/model_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package inventory

import (
"testing"

"github.com/Cray-HPE/cani/pkg/hardwaretypes"
)

func buildLocation(ordinals ...int) LocationPath {
loc := LocationPath{}
for _, ordinal := range ordinals {
loc = append(loc, LocationToken{Ordinal: ordinal})
}
return loc
}

func TestCompareHardwareByTypeThenLocation(t *testing.T) {
hw1 := Hardware{Type: hardwaretypes.Cabinet}

hw2 := Hardware{Type: hardwaretypes.Node}

v := CompareHardwareByTypeThenLocation(&hw1, &hw1)
if v {
t.Fatalf("Expected false when comparing same hardware")
}

v = CompareHardwareByTypeThenLocation(&hw1, &hw2)
if !v {
t.Fatalf("Expected true when comparing Cabinet to Node, hw1: %v, hw2: %v", hw1, hw2)
}

v = CompareHardwareByTypeThenLocation(&hw2, &hw1)
if v {
t.Fatalf("Expected false when comparing Node to Cabinet, hw1: %v, hw2: %v", hw2, hw1)
}

hw3 := Hardware{Type: hardwaretypes.Node}
hw2.LocationPath = buildLocation(1, 2, 3)
hw3.LocationPath = buildLocation(1, 2, 3)
v = CompareHardwareByTypeThenLocation(&hw2, &hw3)
if v {
t.Fatalf("Expected false when comparing same hardware with locations, hw1: %v, hw2: %v", hw2, hw3)
}

hw2.LocationPath = buildLocation(1, 2, 3)
hw3.LocationPath = buildLocation(1, 2, 4)
v = CompareHardwareByTypeThenLocation(&hw2, &hw3)
if !v {
t.Fatalf("Expected true when comparing hardware with locations, hw1: %v, hw2: %v", hw2, hw3)
}

v = CompareHardwareByTypeThenLocation(&hw3, &hw2)
if v {
t.Fatalf("Expected false when comparing hardware with locations, hw1: %v, hw2: %v", hw3, hw2)
}
}

0 comments on commit 2d0930f

Please sign in to comment.