Skip to content

Commit

Permalink
Fix TestWireless from dpcmanager package
Browse files Browse the repository at this point in the history
TestWireless may fail if reading of DNS content from inside of the test
overlaps with updates made by DPCManager.
This can be prevented by reading published DNS (via pubsub) as opposed
to accessing DNS directly in memory.
Additionally, the test should wait for RadioSilence.ChangeInProgress
to turn false before checking values of other RadioSilence attributes.

Signed-off-by: Milan Lenco <milan@zededa.com>
  • Loading branch information
milan-zededa authored and eriknordmark committed May 25, 2022
1 parent fffc58c commit ec68fa7
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions pkg/pillar/dpcmanager/dpcmanager_test.go
Expand Up @@ -180,13 +180,13 @@ func itemIsCreatedCb(itemRef dg.ItemRef) func() bool {

func dnsKeyCb() func() string {
return func() string {
return dpcManager.GetDNS().DPCKey
return getDNS().DPCKey
}
}

func testingInProgressCb() func() bool {
return func() bool {
return dpcManager.GetDNS().Testing
return getDNS().Testing
}
}

Expand Down Expand Up @@ -225,6 +225,14 @@ func dpcStateCb(idx int) func() types.DPCState {
}
}

func getDNS() types.DeviceNetworkStatus {
dnsObj, err := pubDNS.Get("global")
if err != nil {
return types.DeviceNetworkStatus{}
}
return dnsObj.(types.DeviceNetworkStatus)
}

func getDPC(idx int) types.DevicePortConfig {
_, dpcList := getDPCList()
if idx < 0 || idx >= len(dpcList) {
Expand All @@ -243,7 +251,7 @@ func getDPCList() (currentIndex int, list []types.DevicePortConfig) {
}

func wirelessStatusFromDNS(wType types.WirelessType) types.WirelessStatus {
for _, port := range dpcManager.GetDNS().Ports {
for _, port := range getDNS().Ports {
if port.WirelessStatus.WType == wType {
return port.WirelessStatus
}
Expand All @@ -258,6 +266,12 @@ func wwanOpModeCb(expMode types.WwanOpMode) func() bool {
}
}

func rsChangeInProgressCb() func() bool {
return func() bool {
return getDNS().RadioSilence.ChangeInProgress
}
}

func macAddress(macAddr string) net.HardwareAddr {
mac, err := net.ParseMAC(macAddr)
if err != nil {
Expand Down Expand Up @@ -698,7 +712,7 @@ func TestSingleDPC(test *testing.T) {
t.Expect(dpcList[0].Key).To(Equal("zedagent"))
t.Expect(dpcList[0].LastSucceeded.After(dpcList[0].LastFailed)).To(BeTrue())
t.Expect(dpcList[0].LastError).To(BeEmpty())
dns := dpcManager.GetDNS()
dns := getDNS()
t.Expect(dns.CurrentIndex).To(Equal(0))
t.Expect(dns.State).To(Equal(types.DPCStateSuccess))

Expand Down Expand Up @@ -1093,7 +1107,7 @@ func TestWireless(test *testing.T) {
wwan0 = mockWwan0() // with IP
networkMonitor.AddOrUpdateInterface(wwan0)
t.Eventually(func() bool {
ports := dpcManager.GetDNS().Ports
ports := getDNS().Ports
return len(ports) == 2 && len(ports[1].AddrInfoList) == 1 &&
ports[1].AddrInfoList[0].Addr.String() == "15.123.87.20"
}).Should(BeTrue())
Expand Down Expand Up @@ -1200,10 +1214,10 @@ func TestWireless(test *testing.T) {
ConfigError: "Error from upper layers",
})
t.Eventually(func() bool {
rs := dpcManager.GetDNS().RadioSilence
rs := getDNS().RadioSilence
return rs.ConfigError == "Error from upper layers"
}).Should(BeTrue())
rs := dpcManager.GetDNS().RadioSilence
rs := getDNS().RadioSilence
t.Expect(rs.ChangeRequestedAt.Equal(rsImposedAt)).To(BeTrue())
t.Expect(rs.ConfigError).To(Equal("Error from upper layers"))
t.Expect(rs.Imposed).To(BeFalse())
Expand All @@ -1218,6 +1232,7 @@ func TestWireless(test *testing.T) {
ChangeInProgress: true,
ChangeRequestedAt: rsImposedAt,
})
t.Eventually(rsChangeInProgressCb()).Should(BeTrue())
expectedWwanConfig.RadioSilence = true
_, wwanCfgHash, err = generic.MarshalWwanConfig(expectedWwanConfig)
t.Expect(err).To(BeNil())
Expand All @@ -1227,7 +1242,8 @@ func TestWireless(test *testing.T) {
wwan0Status.Networks[0].ConfigError = ""
wwanWatcher.UpdateStatus(wwan0Status)
t.Eventually(wwanOpModeCb(types.WwanOpModeRadioOff)).Should(BeTrue())
rs = dpcManager.GetDNS().RadioSilence
t.Eventually(rsChangeInProgressCb()).Should(BeFalse())
rs = getDNS().RadioSilence
t.Expect(rs.ChangeRequestedAt.Equal(rsImposedAt)).To(BeTrue())
t.Expect(rs.ConfigError).To(BeEmpty())
t.Expect(rs.Imposed).To(BeTrue())
Expand All @@ -1241,6 +1257,7 @@ func TestWireless(test *testing.T) {
ChangeInProgress: true,
ChangeRequestedAt: rsLiftedAt,
})
t.Eventually(rsChangeInProgressCb()).Should(BeTrue())
expectedWwanConfig.RadioSilence = false
_, wwanCfgHash, err = generic.MarshalWwanConfig(expectedWwanConfig)
t.Expect(err).To(BeNil())
Expand All @@ -1250,7 +1267,8 @@ func TestWireless(test *testing.T) {
wwan0Status.Networks[0].ConfigError = ""
wwanWatcher.UpdateStatus(wwan0Status)
t.Eventually(wwanOpModeCb(types.WwanOpModeConnected)).Should(BeTrue())
rs = dpcManager.GetDNS().RadioSilence
t.Eventually(rsChangeInProgressCb()).Should(BeFalse())
rs = getDNS().RadioSilence
t.Expect(rs.ChangeRequestedAt.Equal(rsLiftedAt)).To(BeTrue())
t.Expect(rs.ConfigError).To(BeEmpty())
t.Expect(rs.Imposed).To(BeFalse())
Expand All @@ -1264,6 +1282,7 @@ func TestWireless(test *testing.T) {
ChangeInProgress: true,
ChangeRequestedAt: rsImposedAt,
})
t.Eventually(rsChangeInProgressCb()).Should(BeTrue())
expectedWwanConfig.RadioSilence = true
_, wwanCfgHash, err = generic.MarshalWwanConfig(expectedWwanConfig)
t.Expect(err).To(BeNil())
Expand All @@ -1273,7 +1292,8 @@ func TestWireless(test *testing.T) {
wwan0Status.Networks[0].ConfigError = "failed to impose RS"
wwanWatcher.UpdateStatus(wwan0Status)
t.Eventually(wwanOpModeCb(types.WwanOpModeOnline)).Should(BeTrue())
rs = dpcManager.GetDNS().RadioSilence
t.Eventually(rsChangeInProgressCb()).Should(BeFalse())
rs = getDNS().RadioSilence
t.Expect(rs.ChangeRequestedAt.Equal(rsImposedAt)).To(BeTrue())
t.Expect(rs.ConfigError).To(Equal("mock-wwan0: failed to impose RS"))
t.Expect(rs.Imposed).To(BeFalse())
Expand Down Expand Up @@ -1454,7 +1474,7 @@ func TestDPCWithReleasedAndRenamedInterface(test *testing.T) {
eth1Dhcpcd := dg.Reference(generic.Dhcpcd{AdapterIfName: "eth1"})
t.Consistently(itemIsCreatedCb(eth0Dhcpcd)).Should(BeFalse())
t.Consistently(itemIsCreatedCb(eth1Dhcpcd)).Should(BeFalse())
dns := dpcManager.GetDNS()
dns := getDNS()
t.Expect(dns.Ports).To(HaveLen(2))
t.Expect(dns.Ports[0].Up).To(BeFalse())
t.Expect(dns.Ports[1].Up).To(BeFalse())
Expand Down Expand Up @@ -1656,7 +1676,7 @@ func TestVlansAndBonds(test *testing.T) {
t.Expect(getDPC(0).State).To(Equal(types.DPCStateSuccess))
// Eventually both VLAN sub-interfaces are reported as functional.
t.Eventually(func() bool {
dns := dpcManager.GetDNS()
dns := getDNS()
return len(dns.Ports) == 5 &&
dns.Ports[3].LastError == "" &&
dns.Ports[4].LastError == ""
Expand Down Expand Up @@ -1818,7 +1838,7 @@ func TestTransientDNSError(test *testing.T) {
t.Expect(dpcEth0).ToNot(BeNil())
t.Expect(dpcEth0.HasError()).To(BeTrue())
t.Expect(dpcEth0.LastError).To(Equal("interface eth0: no DNS server available"))
dns := dpcManager.GetDNS()
dns := getDNS()
dnsEth0 := dns.GetPortByIfName("eth0")
t.Expect(dnsEth0).ToNot(BeNil())
t.Expect(dnsEth0.HasError()).To(BeTrue())
Expand All @@ -1833,7 +1853,7 @@ func TestTransientDNSError(test *testing.T) {
t.Expect(dpcEth0).ToNot(BeNil())
t.Expect(dpcEth0.HasError()).To(BeFalse())
t.Expect(dpcEth0.LastError).To(BeEmpty())
dns = dpcManager.GetDNS()
dns = getDNS()
dnsEth0 = dns.GetPortByIfName("eth0")
t.Expect(dnsEth0).ToNot(BeNil())
t.Expect(dnsEth0.HasError()).To(BeFalse())
Expand Down

0 comments on commit ec68fa7

Please sign in to comment.