libcni: add util function InjectConf
authorLukasz Zajaczkowski <Lukasz.Zajaczkowski@ts.fujitsu.com>
Fri, 5 Aug 2016 08:45:58 +0000 (10:45 +0200)
committerLukasz Zajaczkowski <Lukasz.Zajaczkowski@ts.fujitsu.com>
Mon, 8 Aug 2016 11:21:25 +0000 (13:21 +0200)
libcni/conf.go
libcni/conf_test.go

index 029769e..984b159 100644 (file)
@@ -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)
 }
+
+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)
+}
index 9460aca..acbbfc0 100644 (file)
@@ -27,8 +27,9 @@ import (
 
 var _ = Describe("Loading configuration from disk", func() {
        var (
-               configDir    string
-               pluginConfig []byte
+               configDir     string
+               pluginConfig  []byte
+               testNetConfig *libcni.NetworkConfig
        )
 
        BeforeEach(func() {
@@ -38,6 +39,9 @@ var _ = Describe("Loading configuration from disk", func() {
 
                pluginConfig = []byte(`{ "name": "some-plugin", "some-key": "some-value" }`)
                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() {
@@ -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,
+                               }))
+                       })
+               })
+       })
 })