diff options
author | Aleksander Mistewicz <a.mistewicz@samsung.com> | 2017-09-08 14:52:11 +0200 |
---|---|---|
committer | Aleksander Mistewicz <a.mistewicz@samsung.com> | 2018-03-02 15:10:53 +0100 |
commit | aa9ba4874e92fae53fd22f064d3c485af2fadaba (patch) | |
tree | 0913f877b72b299763647d44a645ec3d68be29f9 | |
parent | 9e0e786b0a4bd6ef41a01d3c3d43dfd39c9f207f (diff) | |
download | boruta-aa9ba4874e92fae53fd22f064d3c485af2fadaba.tar.gz boruta-aa9ba4874e92fae53fd22f064d3c485af2fadaba.tar.bz2 boruta-aa9ba4874e92fae53fd22f064d3c485af2fadaba.zip |
Add Dryad configuration package
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>
-rw-r--r-- | dryad/conf/conf.go | 75 | ||||
-rw-r--r-- | dryad/conf/conf_suite_test.go | 29 | ||||
-rw-r--r-- | dryad/conf/conf_test.go | 68 |
3 files changed, 172 insertions, 0 deletions
diff --git a/dryad/conf/conf.go b/dryad/conf/conf.go new file mode 100644 index 0000000..f2bf265 --- /dev/null +++ b/dryad/conf/conf.go @@ -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 index 0000000..cb1aa15 --- /dev/null +++ b/dryad/conf/conf_suite_test.go @@ -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 index 0000000..9a2d369 --- /dev/null +++ b/dryad/conf/conf_test.go @@ -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)) + }) +}) |