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

@@ -35,7 +35,7 @@ const (
// Manager monitors the configuration of the primary CNI plugin, and // Manager monitors the configuration of the primary CNI plugin, and
// regenerates multus configuration whenever it gets updated. // regenerates multus configuration whenever it gets updated.
type Manager struct { type Manager struct {
cniConfigData map[string]interface{} cniConfigData map[string]interface{}
configWatcher *fsnotify.Watcher configWatcher *fsnotify.Watcher
multusConfig *MultusConf multusConfig *MultusConf
multusConfigDir string multusConfigDir string
@@ -155,6 +155,9 @@ func (m Manager) MonitorDelegatedPluginConfiguration(shutDown chan struct{}, don
if err := m.PersistMultusConfig(updatedConfig); err != nil { if err := m.PersistMultusConfig(updatedConfig); err != nil {
_ = logging.Errorf("failed to persist the multus configuration: %v", err) _ = 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: case err := <-m.configWatcher.Errors:
if err == nil { if err == nil {

View File

@@ -16,18 +16,13 @@
package config package config
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"testing" "testing"
"time"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
"github.com/fsnotify/fsnotify"
) )
const suiteName = "Configuration Manager" const suiteName = "Configuration Manager"
@@ -49,28 +44,34 @@ var _ = Describe(suiteName, func() {
"dns": {} "dns": {}
} }
` `
tmpDir = "/tmp"
) )
var configManager *Manager var configManager *Manager
var multusConfigFilePath string var multusConfigDir string
var defaultCniConfig string
BeforeEach(func() { BeforeEach(func() {
format.TruncatedDiff = false var err error
multusConfigFilePath = fmt.Sprintf("%s/%s", tmpDir, primaryCNIPluginName) multusConfigDir, err = ioutil.TempDir("", "multus-config")
Expect(ioutil.WriteFile(multusConfigFilePath, []byte(primaryCNIPluginTemplate), userRWPermission)).To(Succeed()) 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( multusConf := NewMultusConfig(
primaryCNIName, primaryCNIName,
cniVersion, cniVersion,
kubeconfig) kubeconfig)
var err error var err error
configManager, err = NewManagerWithExplicitPrimaryCNIPlugin(*multusConf, tmpDir, primaryCNIPluginName) configManager, err = NewManagerWithExplicitPrimaryCNIPlugin(*multusConf, multusConfigDir, primaryCNIPluginName)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
}) })
AfterEach(func() { 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() { 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)) Expect(config).To(Equal(expectedResult))
}) })
It("Reacts to updates of the delegated CNI configuration", func() { Context("Updates to the delegate CNI configuration", func() {
stopChan := make(chan struct{}) var (
doneChan := make(chan struct{}) doneChannel chan struct{}
go func(stopChannel, doneChan chan struct{}) { stopChannel chan struct{}
Expect(configManager.MonitorDelegatedPluginConfiguration(stopChannel, doneChan)).To(Succeed()) )
}(stopChan, doneChan)
newCNIConfig := "{\"cniVersion\":\"0.4.0\",\"dns\":{},\"ipam\":{},\"name\":\"mycni-name\",\"type\":\"mycni\"}" BeforeEach(func() {
Expect(ioutil.WriteFile(multusConfigFilePath, []byte(newCNIConfig), userRWPermission)).To(Succeed()) doneChannel = make(chan struct{})
Eventually(<-configManager.configWatcher.Events, time.Second).Should(Equal( stopChannel = make(chan struct{})
fsnotify.Event{ go func() {
Name: multusConfigFilePath, Expect(configManager.MonitorDelegatedPluginConfiguration(stopChannel, doneChannel)).To(Succeed())
Op: fsnotify.Write, }()
})) })
bytes, err := json.Marshal(configManager.cniConfigData) AfterEach(func() {
Expect(err).NotTo(HaveOccurred()) go func() { stopChannel <- struct{}{} }()
Expect(string(bytes)).To(Equal(newCNIConfig)) Eventually(<-doneChannel).Should(Equal(struct{}{}))
close(doneChannel)
close(stopChannel)
})
// cleanup the watcher It("Trigger the re-generation of the Multus CNI configuration", func() {
go func() { stopChan <- struct{}{} }() newCNIConfig := "{\"cniVersion\":\"0.4.0\",\"dns\":{},\"ipam\":{},\"name\":\"yoyo-newnet\",\"type\":\"mycni\"}"
Eventually(<-doneChan, time.Second).Should(Equal(struct{}{})) 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() { 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)
}