Add Dryad configuration package
authorAleksander Mistewicz <a.mistewicz@samsung.com>
Fri, 8 Sep 2017 12:52:11 +0000 (14:52 +0200)
committerAleksander Mistewicz <a.mistewicz@samsung.com>
Fri, 2 Mar 2018 14:10:53 +0000 (15:10 +0100)
A non-standard library for configuration serialization is used.
It is licensed under MIT license. Install it with command below:
    go get "github.com/BurntSushi/toml"

Change-Id: I71321d341ac8d64c6a9ed85f2d895524eca01fac
Signed-off-by: Aleksander Mistewicz <a.mistewicz@samsung.com>
dryad/conf/conf.go [new file with mode: 0644]
dryad/conf/conf_suite_test.go [new file with mode: 0644]
dryad/conf/conf_test.go [new file with mode: 0644]

diff --git a/dryad/conf/conf.go b/dryad/conf/conf.go
new file mode 100644 (file)
index 0000000..f2bf265
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ *  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+// Package conf manages Dryad's configuration.
+package conf
+
+import (
+       "fmt"
+       "io"
+       "io/ioutil"
+
+       "github.com/BurntSushi/toml"
+)
+
+// DefaultRPCPort is a port that should be used as default parameter
+// for Dryad's RPC client and server.
+const DefaultRPCPort = 7175
+
+// NewConf returns a new instance of General configuration with default values set.
+func NewConf() *General {
+       return &General{
+               Address: fmt.Sprintf(":%d", DefaultRPCPort),
+               User: &User{
+                       Name:   "boruta-user",
+                       Groups: []string{},
+               },
+               SDcard: "/dev/sdX",
+       }
+}
+
+// User is a section in a configuration used for user manipulation.
+type User struct {
+       // Name is a username of a local account. It should be used to establish SSH session
+       // to the system Dryad is running on.
+       Name string `toml:"name"`
+       // Groups is a list of local Unix groups the username belongs to.
+       Groups []string `toml:"groups"`
+}
+
+// General is a base struct of configuration.
+type General struct {
+       // Address is used to listen for connection from Boruta.
+       Address string `toml:"listen_address"`
+       // User refers information necessary to create the user.
+       User *User `toml:"user"`
+       // SDcard is a base path to block device of sdcard.
+       SDcard string `toml:"sdcard"`
+}
+
+// Marshal writes TOML representation of g to w.
+func (g *General) Marshal(w io.Writer) error {
+       return toml.NewEncoder(w).Encode(g)
+}
+
+// Unmarshal reads TOML representation from r and parses it into g.
+func (g *General) Unmarshal(r io.Reader) error {
+       b, err := ioutil.ReadAll(r)
+       if err != nil {
+               return err
+       }
+       return toml.Unmarshal(b, g)
+}
diff --git a/dryad/conf/conf_suite_test.go b/dryad/conf/conf_suite_test.go
new file mode 100644 (file)
index 0000000..cb1aa15
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ *  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+package conf_test
+
+import (
+       . "github.com/onsi/ginkgo"
+       . "github.com/onsi/gomega"
+
+       "testing"
+)
+
+func TestConf(t *testing.T) {
+       RegisterFailHandler(Fail)
+       RunSpecs(t, "Conf Suite")
+}
diff --git a/dryad/conf/conf_test.go b/dryad/conf/conf_test.go
new file mode 100644 (file)
index 0000000..9a2d369
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ *  Copyright (c) 2017-2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+
+package conf_test
+
+import (
+       "bytes"
+       "strings"
+
+       . "git.tizen.org/tools/boruta/dryad/conf"
+
+       . "github.com/onsi/ginkgo"
+       . "github.com/onsi/gomega"
+)
+
+var _ = Describe("Conf", func() {
+       marshaled := `listen_address = ":7175"
+sdcard = "/dev/sdX"
+
+[user]
+  name = "boruta-user"
+  groups = []
+`
+       unmarshaled := &General{
+               Address: ":7175",
+               User: &User{
+                       Name:   "boruta-user",
+                       Groups: []string{},
+               },
+               SDcard: "/dev/sdX",
+       }
+       var g *General
+
+       BeforeEach(func() {
+               g = NewConf()
+       })
+
+       It("should initially have default configuration", func() {
+               Expect(g).To(Equal(unmarshaled))
+       })
+
+       It("should encode default configuration", func() {
+               var w bytes.Buffer
+               g.Marshal(&w)
+               result := w.String()
+               Expect(result).ToNot(BeEmpty())
+               Expect(result).To(Equal(marshaled))
+       })
+
+       It("should decode default configuration", func() {
+               g = new(General)
+               g.Unmarshal(strings.NewReader(marshaled))
+               Expect(g).To(Equal(unmarshaled))
+       })
+})