libcni: add util function InjectConf
This commit is contained in:
parent
ee64ac74bc
commit
8cb3a9323a
@ -83,3 +83,28 @@ func LoadConf(dir, name string) (*NetworkConfig, error) {
|
|||||||
}
|
}
|
||||||
return nil, fmt.Errorf(`no net configuration with name "%s" in %s`, name, dir)
|
return nil, fmt.Errorf(`no net configuration with name "%s" in %s`, name, dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func InjectConf(original *NetworkConfig, key string, newValue interface{}) (*NetworkConfig, error) {
|
||||||
|
config := make(map[string]interface{})
|
||||||
|
err := json.Unmarshal(original.Bytes, &config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("unmarshal existing network bytes: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if key == "" {
|
||||||
|
return nil, fmt.Errorf("key value can not be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
if newValue == nil {
|
||||||
|
return nil, fmt.Errorf("newValue must be specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
config[key] = newValue
|
||||||
|
|
||||||
|
newBytes, err := json.Marshal(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ConfFromBytes(newBytes)
|
||||||
|
}
|
||||||
|
@ -29,6 +29,7 @@ var _ = Describe("Loading configuration from disk", func() {
|
|||||||
var (
|
var (
|
||||||
configDir string
|
configDir string
|
||||||
pluginConfig []byte
|
pluginConfig []byte
|
||||||
|
testNetConfig *libcni.NetworkConfig
|
||||||
)
|
)
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
@ -38,6 +39,9 @@ var _ = Describe("Loading configuration from disk", func() {
|
|||||||
|
|
||||||
pluginConfig = []byte(`{ "name": "some-plugin", "some-key": "some-value" }`)
|
pluginConfig = []byte(`{ "name": "some-plugin", "some-key": "some-value" }`)
|
||||||
Expect(ioutil.WriteFile(filepath.Join(configDir, "50-whatever.conf"), pluginConfig, 0600)).To(Succeed())
|
Expect(ioutil.WriteFile(filepath.Join(configDir, "50-whatever.conf"), pluginConfig, 0600)).To(Succeed())
|
||||||
|
|
||||||
|
testNetConfig = &libcni.NetworkConfig{Network: &types.NetConf{Name: "some-plugin"},
|
||||||
|
Bytes: []byte(`{ "name": "some-plugin" }`)}
|
||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(func() {
|
AfterEach(func() {
|
||||||
@ -107,4 +111,89 @@ var _ = Describe("Loading configuration from disk", func() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("InjectConf", func() {
|
||||||
|
Context("when function parameters are incorrect", func() {
|
||||||
|
It("returns unmarshal error", func() {
|
||||||
|
conf := &libcni.NetworkConfig{Network: &types.NetConf{Name: "some-plugin"},
|
||||||
|
Bytes: []byte(`{ cc cc cc}`)}
|
||||||
|
|
||||||
|
_, err := libcni.InjectConf(conf, "", nil)
|
||||||
|
Expect(err).To(MatchError(HavePrefix(`unmarshal existing network bytes`)))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("returns key error", func() {
|
||||||
|
_, err := libcni.InjectConf(testNetConfig, "", nil)
|
||||||
|
Expect(err).To(MatchError(HavePrefix(`key value can not be empty`)))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("returns newValue error", func() {
|
||||||
|
_, err := libcni.InjectConf(testNetConfig, "test", nil)
|
||||||
|
Expect(err).To(MatchError(HavePrefix(`newValue must be specified`)))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("when new string value added", func() {
|
||||||
|
It("adds the new key & value to the config", func() {
|
||||||
|
newPluginConfig := []byte(`{"name":"some-plugin","test":"test"}`)
|
||||||
|
|
||||||
|
resultConfig, err := libcni.InjectConf(testNetConfig, "test", "test")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
Expect(resultConfig).To(Equal(&libcni.NetworkConfig{
|
||||||
|
Network: &types.NetConf{Name: "some-plugin"},
|
||||||
|
Bytes: newPluginConfig,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("adds the new value for exiting key", func() {
|
||||||
|
newPluginConfig := []byte(`{"name":"some-plugin","test":"changedValue"}`)
|
||||||
|
|
||||||
|
resultConfig, err := libcni.InjectConf(testNetConfig, "test", "test")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
resultConfig, err = libcni.InjectConf(resultConfig, "test", "changedValue")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
Expect(resultConfig).To(Equal(&libcni.NetworkConfig{
|
||||||
|
Network: &types.NetConf{Name: "some-plugin"},
|
||||||
|
Bytes: newPluginConfig,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("adds existing key & value", func() {
|
||||||
|
newPluginConfig := []byte(`{"name":"some-plugin","test":"test"}`)
|
||||||
|
|
||||||
|
resultConfig, err := libcni.InjectConf(testNetConfig, "test", "test")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
resultConfig, err = libcni.InjectConf(resultConfig, "test", "test")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
Expect(resultConfig).To(Equal(&libcni.NetworkConfig{
|
||||||
|
Network: &types.NetConf{Name: "some-plugin"},
|
||||||
|
Bytes: newPluginConfig,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
|
||||||
|
It("adds sub-fields of NetworkConfig.Network to the config", func() {
|
||||||
|
|
||||||
|
expectedPluginConfig := []byte(`{"dns":{"domain":"local","nameservers":["server1","server2"]},"name":"some-plugin","type":"bridge"}`)
|
||||||
|
servers := []string{"server1", "server2"}
|
||||||
|
newDNS := &types.DNS{Nameservers: servers, Domain: "local"}
|
||||||
|
|
||||||
|
// inject DNS
|
||||||
|
resultConfig, err := libcni.InjectConf(testNetConfig, "dns", newDNS)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
// inject type
|
||||||
|
resultConfig, err = libcni.InjectConf(resultConfig, "type", "bridge")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
|
||||||
|
Expect(resultConfig).To(Equal(&libcni.NetworkConfig{
|
||||||
|
Network: &types.NetConf{Name: "some-plugin", Type: "bridge", DNS: types.DNS{Nameservers: servers, Domain: "local"}},
|
||||||
|
Bytes: expectedPluginConfig,
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user