Merge pull request #757 from maiqueb/fix-flaky-config-regen-test

flaky: fix delegate CNI conf updates unit test
This commit is contained in:
Doug Smith
2021-12-14 10:06:01 -05:00
committed by GitHub
2 changed files with 49 additions and 32 deletions

View File

@@ -155,6 +155,9 @@ func (m Manager) MonitorDelegatedPluginConfiguration(shutDown chan struct{}, don
if err := m.PersistMultusConfig(updatedConfig); err != nil {
_ = logging.Errorf("failed to persist the multus configuration: %v", err)
}
if err := m.loadPrimaryCNIConfigFromFile(); err != nil {
_ = logging.Errorf("failed to reload the updated config: %v", err)
}
case err := <-m.configWatcher.Errors:
if err == nil {

View File

@@ -16,18 +16,13 @@
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"github.com/fsnotify/fsnotify"
)
const suiteName = "Configuration Manager"
@@ -49,28 +44,34 @@ var _ = Describe(suiteName, func() {
"dns": {}
}
`
tmpDir = "/tmp"
)
var configManager *Manager
var multusConfigFilePath string
var multusConfigDir string
var defaultCniConfig string
BeforeEach(func() {
format.TruncatedDiff = false
multusConfigFilePath = fmt.Sprintf("%s/%s", tmpDir, primaryCNIPluginName)
Expect(ioutil.WriteFile(multusConfigFilePath, []byte(primaryCNIPluginTemplate), userRWPermission)).To(Succeed())
var err error
multusConfigDir, err = ioutil.TempDir("", "multus-config")
Expect(err).ToNot(HaveOccurred())
Expect(os.MkdirAll(multusConfigDir, 0755)).To(Succeed())
})
BeforeEach(func() {
defaultCniConfig = fmt.Sprintf("%s/%s", multusConfigDir, primaryCNIPluginName)
Expect(ioutil.WriteFile(defaultCniConfig, []byte(primaryCNIPluginTemplate), userRWPermission)).To(Succeed())
multusConf := NewMultusConfig(
primaryCNIName,
cniVersion,
kubeconfig)
var err error
configManager, err = NewManagerWithExplicitPrimaryCNIPlugin(*multusConf, tmpDir, primaryCNIPluginName)
configManager, err = NewManagerWithExplicitPrimaryCNIPlugin(*multusConf, multusConfigDir, primaryCNIPluginName)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(os.Remove(multusConfigFilePath)).To(Succeed())
Expect(os.RemoveAll(multusConfigDir)).To(Succeed())
})
It("Generates a configuration, based on the contents of the delegated CNI config file", func() {
@@ -80,28 +81,37 @@ var _ = Describe(suiteName, func() {
Expect(config).To(Equal(expectedResult))
})
It("Reacts to updates of the delegated CNI configuration", func() {
stopChan := make(chan struct{})
doneChan := make(chan struct{})
go func(stopChannel, doneChan chan struct{}) {
Expect(configManager.MonitorDelegatedPluginConfiguration(stopChannel, doneChan)).To(Succeed())
}(stopChan, doneChan)
Context("Updates to the delegate CNI configuration", func() {
var (
doneChannel chan struct{}
stopChannel chan struct{}
)
newCNIConfig := "{\"cniVersion\":\"0.4.0\",\"dns\":{},\"ipam\":{},\"name\":\"mycni-name\",\"type\":\"mycni\"}"
Expect(ioutil.WriteFile(multusConfigFilePath, []byte(newCNIConfig), userRWPermission)).To(Succeed())
Eventually(<-configManager.configWatcher.Events, time.Second).Should(Equal(
fsnotify.Event{
Name: multusConfigFilePath,
Op: fsnotify.Write,
}))
BeforeEach(func() {
doneChannel = make(chan struct{})
stopChannel = make(chan struct{})
go func() {
Expect(configManager.MonitorDelegatedPluginConfiguration(stopChannel, doneChannel)).To(Succeed())
}()
})
bytes, err := json.Marshal(configManager.cniConfigData)
Expect(err).NotTo(HaveOccurred())
Expect(string(bytes)).To(Equal(newCNIConfig))
AfterEach(func() {
go func() { stopChannel <- struct{}{} }()
Eventually(<-doneChannel).Should(Equal(struct{}{}))
close(doneChannel)
close(stopChannel)
})
// cleanup the watcher
go func() { stopChan <- struct{}{} }()
Eventually(<-doneChan, time.Second).Should(Equal(struct{}{}))
It("Trigger the re-generation of the Multus CNI configuration", func() {
newCNIConfig := "{\"cniVersion\":\"0.4.0\",\"dns\":{},\"ipam\":{},\"name\":\"yoyo-newnet\",\"type\":\"mycni\"}"
Expect(ioutil.WriteFile(defaultCniConfig, []byte(newCNIConfig), userRWPermission)).To(Succeed())
multusCniConfigFile := fmt.Sprintf("%s/%s", multusConfigDir, multusConfigFileName)
Eventually(func() (string, error) {
multusCniData, err := ioutil.ReadFile(multusCniConfigFile)
return string(multusCniData), err
}).Should(Equal(multusConfigFromDelegate(newCNIConfig)))
})
})
When("the user requests the name of the multus configuration to be overridden", func() {
@@ -117,3 +127,7 @@ var _ = Describe(suiteName, func() {
})
})
})
func multusConfigFromDelegate(delegateConfig string) string {
return fmt.Sprintf("{\"cniVersion\":\"0.4.0\",\"delegates\":[%s],\"kubeconfig\":\"/a/b/c/kubeconfig.kubeconfig\",\"name\":\"multus-cni-network\",\"type\":\"myCNI\"}", delegateConfig)
}