Skip to content

Commit

Permalink
pillar/assignableadapters: clear error strings
Browse files Browse the repository at this point in the history
for parentassigngrp cycles:

1. create two usb adapters that have each other as parentassigngrp
2. change the parentassignrp of one of the adapters

now the cycle should be gone, but
as the error string for both adapters has been set, it was
not cleared

Signed-off-by: Christoph Ostarek <christoph@zededa.com>
  • Loading branch information
christoph-zededa committed Sep 12, 2024
1 parent ec49506 commit fa8b1a1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pkg/pillar/types/assignableadapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ func (aa *AssignableAdapters) LookupIoBundleLogicallabel(label string) *IoBundle
// LookupIoBundleGroup returns an empty slice if not found
// Returns pointers into aa
func (aa *AssignableAdapters) LookupIoBundleGroup(group string) []*IoBundle {

var list []*IoBundle
if group == "" {
return list
Expand All @@ -430,7 +429,6 @@ func (aa *AssignableAdapters) LookupIoBundleGroup(group string) []*IoBundle {
// a member phylabel, logicallabel, or a group
// Returns pointers into aa
func (aa *AssignableAdapters) LookupIoBundleAny(name string) []*IoBundle {

list := aa.LookupIoBundleGroup(name)
if len(list) != 0 {
return list
Expand Down Expand Up @@ -464,24 +462,39 @@ func (aa *AssignableAdapters) LookupIoBundleIfName(ifname string) *IoBundle {
func (aa *AssignableAdapters) CheckParentAssigngrp() bool {
assigngrp2parent := make(map[string]string)

ownParentError := "IOBundle cannot be it's own parent"
parentAssigngrpMismatchError := "IOBundle with parentassigngrp mismatch found"
emptyAssigngrpWithParentError := "IOBundle with empty assigngrp cannot have a parent"
cycleDetectedError := "Cycle detected, please check provided parentassigngrp/assigngrp"

var cycleDetectedAssigngrp string
for i := range aa.IoBundleList {
ioBundle := &aa.IoBundleList[i]

for _, errStr := range []string{
ownParentError,
parentAssigngrpMismatchError,
emptyAssigngrpWithParentError,
cycleDetectedError,
} {
if ioBundle.Error == errStr {
ioBundle.Error = ""
}
}
if ioBundle.AssignmentGroup == ioBundle.ParentAssignmentGroup && ioBundle.AssignmentGroup != "" {
ioBundle.Error = "IOBundle cannot be it's own parent"
ioBundle.Error = ownParentError
ioBundle.ErrorTime = time.Now()
return true
}
parentassigngrp, ok := assigngrp2parent[ioBundle.AssignmentGroup]
if ok && parentassigngrp != ioBundle.ParentAssignmentGroup {
ioBundle.Error = "IOBundle with parentassigngrp mismatch found"
ioBundle.Error = parentAssigngrpMismatchError
ioBundle.ErrorTime = time.Now()
return true
}

if ioBundle.AssignmentGroup == "" && ioBundle.ParentAssignmentGroup != "" {
ioBundle.Error = "IOBundle with empty assigngrp cannot have a parent"
ioBundle.Error = emptyAssigngrpWithParentError
ioBundle.ErrorTime = time.Now()
return true
}
Expand Down Expand Up @@ -516,7 +529,7 @@ func (aa *AssignableAdapters) CheckParentAssigngrp() bool {
for i := range aa.IoBundleList {
ioBundle := &aa.IoBundleList[i]
if ioBundle.AssignmentGroup == cycleDetectedAssigngrp {
ioBundle.Error = "Cycle detected, please check provided parentassigngrp/assigngrp"
ioBundle.Error = cycleDetectedError
ioBundle.ErrorTime = time.Now()
}
}
Expand Down
43 changes: 43 additions & 0 deletions pkg/pillar/types/assignableadapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,49 @@ func alternativeCheckBadUSBBundlesImpl(bundles []IoBundle) {
}
}

func TestClearingCycleErrors(t *testing.T) {
t.Parallel()

aa := AssignableAdapters{}
bundles := make([]IoBundle, 2)

bundles[0].Phylabel = "usb1"
bundles[1].Phylabel = "usb2"

bundles[0].UsbAddr = "1:1"
bundles[1].UsbAddr = "1:2"

bundles[0].AssignmentGroup = "a1"
bundles[1].AssignmentGroup = "a2"

bundles[0].ParentAssignmentGroup = "a2"
bundles[1].ParentAssignmentGroup = "a1"

aa.IoBundleList = bundles

aa.CheckParentAssigngrp()

errFound := func() bool {
found := false
for _, ioBundle := range aa.IoBundleList {
if ioBundle.Error != "" {
found = true
}
}
return found
}

if !errFound() {
t.Fatalf("no error found although there is a cycle: %+v", aa.IoBundleList)
}

aa.IoBundleList[1].ParentAssignmentGroup = "p2"
aa.CheckParentAssigngrp()
if errFound() {
t.Fatalf("error found although there is no cycle anymore: %+v", aa.IoBundleList)
}
}

func TestClearingUSBCollision(t *testing.T) {
t.Parallel()
aa := AssignableAdapters{}
Expand Down

0 comments on commit fa8b1a1

Please sign in to comment.