Skip to content

Commit

Permalink
tests: add yet another workaround attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Nov 4, 2021
1 parent 67cc6bd commit 8bccfb2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
12 changes: 8 additions & 4 deletions overlord/configstate/configcore/netplan.go
Expand Up @@ -33,6 +33,7 @@ package configcore
import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -165,10 +166,14 @@ func handleNetplanConfiguration(tr config.Conf, opts *fsOnlyContext) error {
// that got unset.
//
// XXX: The osutil.FileExists() is needed until LP:1946957 gets fixed
var configs []string
if osutil.FileExists(filepath.Join(dirs.GlobalRootDir, "/etc/netplan/90-snapd-conf.yaml")) {
configs = []string{"network=null"}
originHint := "90-snapd-conf"
netplanCfgPath := filepath.Join(dirs.GlobalRootDir, fmt.Sprintf("/etc/netplan/%s.yaml", originHint))
if !osutil.FileExists(netplanCfgPath) {
if err := ioutil.WriteFile(netplanCfgPath, nil, 0644); err != nil {
return err
}
}
configs := []string{"network=null"}
// and then pass the full new config in
for key := range cfg {
// We pass the new config back to netplan as json, the reason
Expand All @@ -185,7 +190,6 @@ func handleNetplanConfiguration(tr config.Conf, opts *fsOnlyContext) error {
for _, jsonNetplanConfig := range configs {
logger.Debugf("calling netplan.Set: %v", jsonNetplanConfig)

originHint := "90-snapd-conf"
var wasSet bool
if err := netplanCfgSnapshot.Call("io.netplan.Netplan.Config.Set", 0, jsonNetplanConfig, originHint).Store(&wasSet); err != nil {
return fmt.Errorf("cannot Set() config: %v", err)
Expand Down
38 changes: 3 additions & 35 deletions overlord/configstate/configcore/netplan_test.go
Expand Up @@ -98,7 +98,7 @@ func (s *netplanSuite) SetUpTest(c *C) {
restore = release.MockOnClassic(false)
s.AddCleanup(restore)

err = os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/"), 0755)
err = os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/netplan"), 0755)
c.Assert(err, IsNil)
}

Expand Down Expand Up @@ -239,40 +239,7 @@ func (s *netplanSuite) TestNetplanConnectivityCheck(c *C) {
}
}

func (s *netplanSuite) TestNetplanWriteConfigHappyNoPrevConfig(c *C) {
// export the V2 api, things work with that
s.backend.ExportApiV2()

// and everything is fine
s.fakestore.status = map[string]bool{"host1": true}
s.backend.ConfigApiTryRet = true
s.backend.ConfigApiApplyRet = true

s.state.Lock()
tr := config.NewTransaction(s.state)
s.state.Unlock()
tr.Set("core", "system.network.netplan.network.ethernets.eth0.dhcp4", true)
tr.Set("core", "system.network.netplan.network.wifi.wlan0.dhcp4", true)

err := configcore.Run(coreDev, tr)
c.Assert(err, IsNil)

c.Check(s.backend.ConfigApiSetCalls, DeepEquals, []string{
`network={"ethernets":{"eth0":{"dhcp4":true}},"renderer":"NetworkManager","version":2,"wifi":{"wlan0":{"dhcp4":true}}}/90-snapd-conf`,
})
c.Check(s.backend.ConfigApiTryCalls, Equals, 1)
c.Check(s.backend.ConfigApiApplyCalls, Equals, 1)
}

func (s *netplanSuite) TestNetplanWriteConfigHappy(c *C) {
// see https://bugs.launchpad.net/netplan/+bug/1946957 why needed
// XXX: once the above bug is fixed we can delete this test
mockNetplanFile := filepath.Join(dirs.GlobalRootDir, "/etc/netplan/90-snapd-conf.yaml")
err := os.MkdirAll(filepath.Dir(mockNetplanFile), 0755)
c.Assert(err, IsNil)
err = ioutil.WriteFile(mockNetplanFile, nil, 0644)
c.Assert(err, IsNil)

// export the V2 api, things work with that
s.backend.ExportApiV2()

Expand All @@ -287,14 +254,15 @@ func (s *netplanSuite) TestNetplanWriteConfigHappy(c *C) {
tr.Set("core", "system.network.netplan.network.ethernets.eth0.dhcp4", true)
tr.Set("core", "system.network.netplan.network.wifi.wlan0.dhcp4", true)

err = configcore.Run(coreDev, tr)
err := configcore.Run(coreDev, tr)
c.Assert(err, IsNil)

c.Check(s.backend.ConfigApiSetCalls, DeepEquals, []string{
`network=null/90-snapd-conf`,
`network={"ethernets":{"eth0":{"dhcp4":true}},"renderer":"NetworkManager","version":2,"wifi":{"wlan0":{"dhcp4":true}}}/90-snapd-conf`,
})
c.Check(s.backend.ConfigApiTryCalls, Equals, 1)
c.Check(s.backend.ConfigApiApplyCalls, Equals, 1)
}

func (s *netplanSuite) TestNetplanWriteConfigNoNetworkAfterTry(c *C) {
Expand Down
1 change: 1 addition & 0 deletions tests/core/netplan-cfg/task.yaml
Expand Up @@ -44,4 +44,5 @@ execute: |
echo "Unset the whole subtree works"
snap unset system system.network.netplan.network.bridges.br54
snap get -d system system.network.netplan | NOMATCH br54
ip link | NOMATCH br54

0 comments on commit 8bccfb2

Please sign in to comment.