Skip to content

Commit e4cb25b

Browse files
authored
Merge pull request #999 from rollandf/daemonresult
fix: check for sriov result file in systemd mode
2 parents a81bb34 + 2ea00d2 commit e4cb25b

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

pkg/daemon/daemon.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (dn *NodeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
181181
}
182182

183183
// if we are running in systemd mode we want to get the sriov result from the config-daemon that runs in systemd
184-
sriovResult, sriovResultExists, err := dn.checkSystemdStatus()
184+
sriovResult, sriovResultExists, err := dn.CheckSystemdStatus()
185185
//TODO: in the case we need to think what to do if we try to apply again or not
186186
if err != nil {
187187
reqLogger.Error(err, "failed to check systemd status unexpected error")
@@ -322,14 +322,14 @@ func (dn *NodeReconciler) checkOnNodeStateChange(desiredNodeState *sriovnetworkv
322322
return reqReboot, reqDrain, nil
323323
}
324324

325-
// checkSystemdStatus Checks the status of systemd services on the host node.
325+
// CheckSystemdStatus Checks the status of systemd services on the host node.
326326
// return the sriovResult struct a boolean if the result file exist on the node
327-
func (dn *NodeReconciler) checkSystemdStatus() (*hosttypes.SriovResult, bool, error) {
327+
func (dn *NodeReconciler) CheckSystemdStatus() (*hosttypes.SriovResult, bool, error) {
328328
if !vars.UsingSystemdMode {
329329
return nil, false, nil
330330
}
331331

332-
funcLog := log.Log.WithName("checkSystemdStatus")
332+
funcLog := log.Log.WithName("CheckSystemdStatus")
333333
serviceEnabled, err := dn.hostHelpers.IsServiceEnabled(consts.SriovServicePath)
334334
if err != nil {
335335
funcLog.Error(err, "failed to check if sriov-config service exist on host")
@@ -351,12 +351,12 @@ func (dn *NodeReconciler) checkSystemdStatus() (*hosttypes.SriovResult, bool, er
351351

352352
// check if the service exist
353353
if serviceEnabled && postNetworkServiceEnabled {
354-
exist = true
355354
sriovResult, err = dn.hostHelpers.ReadSriovResult()
356355
if err != nil {
357356
funcLog.Error(err, "failed to load sriov result file from host")
358357
return nil, false, err
359358
}
359+
exist = sriovResult != nil
360360
}
361361
return sriovResult, exist, nil
362362
}

pkg/daemon/daemon_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package daemon_test
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"sync"
78
"time"
@@ -689,6 +690,90 @@ var _ = Describe("Daemon Controller", Ordered, func() {
689690
})
690691
})
691692

693+
var _ = Describe("Daemon CheckSystemdStatus", func() {
694+
var (
695+
myMockCtrl *gomock.Controller
696+
myHostHelper *mock_helper.MockHostHelpersInterface
697+
reconciler *daemon.NodeReconciler
698+
)
699+
700+
BeforeEach(func() {
701+
myMockCtrl = gomock.NewController(GinkgoT())
702+
myHostHelper = mock_helper.NewMockHostHelpersInterface(myMockCtrl)
703+
reconciler = daemon.New(nil, myHostHelper, nil, nil, nil)
704+
705+
originalUsingSystemdMode := vars.UsingSystemdMode
706+
vars.UsingSystemdMode = true
707+
DeferCleanup(func() {
708+
vars.UsingSystemdMode = originalUsingSystemdMode
709+
})
710+
})
711+
712+
Context("when systemd services are enabled", func() {
713+
BeforeEach(func() {
714+
myHostHelper.EXPECT().IsServiceEnabled(constants.SriovServicePath).Return(true, nil)
715+
myHostHelper.EXPECT().IsServiceEnabled(constants.SriovPostNetworkServicePath).Return(true, nil)
716+
})
717+
718+
It("should return exist=true if sriov result file exists", func() {
719+
myHostHelper.EXPECT().ReadSriovResult().Return(&hostTypes.SriovResult{}, nil)
720+
result, exist, err := reconciler.CheckSystemdStatus()
721+
Expect(err).ToNot(HaveOccurred())
722+
Expect(exist).To(BeTrue())
723+
Expect(result).ToNot(BeNil())
724+
})
725+
726+
It("should return exist=false if sriov result file does not exist", func() {
727+
myHostHelper.EXPECT().ReadSriovResult().Return(nil, nil)
728+
result, exist, err := reconciler.CheckSystemdStatus()
729+
Expect(err).ToNot(HaveOccurred())
730+
Expect(exist).To(BeFalse())
731+
Expect(result).To(BeNil())
732+
})
733+
734+
It("should return error if ReadSriovResult fails", func() {
735+
myHostHelper.EXPECT().ReadSriovResult().Return(nil, fmt.Errorf("read error"))
736+
result, exist, err := reconciler.CheckSystemdStatus()
737+
Expect(err).To(HaveOccurred())
738+
Expect(exist).To(BeFalse())
739+
Expect(result).To(BeNil())
740+
})
741+
})
742+
743+
Context("when systemd services are NOT enabled", func() {
744+
It("should return exist=false if first service is disabled", func() {
745+
myHostHelper.EXPECT().IsServiceEnabled(constants.SriovServicePath).Return(false, nil)
746+
myHostHelper.EXPECT().IsServiceEnabled(constants.SriovPostNetworkServicePath).Return(true, nil)
747+
result, exist, err := reconciler.CheckSystemdStatus()
748+
Expect(err).ToNot(HaveOccurred())
749+
Expect(exist).To(BeFalse())
750+
Expect(result.SyncStatus).To(Equal(constants.SyncStatusFailed))
751+
})
752+
753+
It("should return exist=false if second service is disabled", func() {
754+
myHostHelper.EXPECT().IsServiceEnabled(constants.SriovServicePath).Return(true, nil)
755+
myHostHelper.EXPECT().IsServiceEnabled(constants.SriovPostNetworkServicePath).Return(false, nil)
756+
result, exist, err := reconciler.CheckSystemdStatus()
757+
Expect(err).ToNot(HaveOccurred())
758+
Expect(exist).To(BeFalse())
759+
Expect(result.SyncStatus).To(Equal(constants.SyncStatusFailed))
760+
})
761+
})
762+
763+
Context("when not in systemd mode", func() {
764+
BeforeEach(func() {
765+
vars.UsingSystemdMode = false
766+
})
767+
768+
It("should return exist=false and no error", func() {
769+
result, exist, err := reconciler.CheckSystemdStatus()
770+
Expect(err).ToNot(HaveOccurred())
771+
Expect(exist).To(BeFalse())
772+
Expect(result).To(BeNil())
773+
})
774+
})
775+
})
776+
692777
func patchAnnotation(nodeState *sriovnetworkv1.SriovNetworkNodeState, key, value string) {
693778
originalNodeState := nodeState.DeepCopy()
694779
nodeState.Annotations[key] = value

0 commit comments

Comments
 (0)