Skip to content
Snippets Groups Projects
Commit 57664949 authored by Tigran Avakyan's avatar Tigran Avakyan
Browse files

Added all tests for conf params object

parent 907cb7ba
Branches
Tags
No related merge requests found
---
name: "name"
username: "username"
password: "password"
addresses:
- "127.0.0.1:80"
- "127.0.0.1:80"
- "127.0.0.1:80"
...
...@@ -13,11 +13,7 @@ import ( ...@@ -13,11 +13,7 @@ import (
"testing" "testing"
) )
// This test checks that unmarshalling the db.yaml file var ExpectedDB = DB{
// is equal to the expected DB object.
func TestDB_UnmarshallingFileEqualsExpected(t *testing.T) {
expected := DB{
Name: "name", Name: "name",
Username: "username", Username: "username",
Password: "password", Password: "password",
...@@ -28,16 +24,21 @@ func TestDB_UnmarshallingFileEqualsExpected(t *testing.T) { ...@@ -28,16 +24,21 @@ func TestDB_UnmarshallingFileEqualsExpected(t *testing.T) {
}, },
} }
buf, _ := ioutil.ReadFile("./db.yaml") // This test checks that unmarshalling the params.yaml file
actual := DB{} // has the expected DB object.
func TestDB_UnmarshallingFileEqualsExpected(t *testing.T) {
buf, _ := ioutil.ReadFile("./params.yaml")
actual := Params{}
err := yaml.Unmarshal(buf, &actual) err := yaml.Unmarshal(buf, &actual)
if err != nil { if err != nil {
t.Errorf("Unable to decode into struct, %v", err) t.Errorf("Unable to decode into struct, %v", err)
} }
if !reflect.DeepEqual(expected, actual) { if !reflect.DeepEqual(ExpectedDB, actual.Database) {
t.Errorf("DB object did not match expected values") t.Errorf("DB object did not match ExpectedDB values")
} }
} }
...@@ -8,6 +8,7 @@ package conf ...@@ -8,6 +8,7 @@ package conf
import ( import (
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/large" "gitlab.com/elixxir/crypto/large"
"strings" "strings"
...@@ -19,6 +20,22 @@ type Groups struct { ...@@ -19,6 +20,22 @@ type Groups struct {
E2E *cyclic.Group E2E *cyclic.Group
} }
// NewGroups creates a groups object from
// a viper config.
// TODO: This is a hack and UnmarshalYAML
// should likely be used, but wasn't working
// with viper. Perhaps a missing decoder option?
func NewGroups(vip *viper.Viper) Groups {
cmix := vip.GetStringMapString("groups.cmix")
e2e := vip.GetStringMapString("groups.e2e")
return Groups{
CMix: toGroup(cmix),
E2E: toGroup(e2e),
}
}
// TODO: field names start with a capital by convention // TODO: field names start with a capital by convention
// but perhaps we should override to force a consistent scheme // but perhaps we should override to force a consistent scheme
// See yaml package documentation for more info. // See yaml package documentation for more info.
......
---
cmix:
prime: "17"
smallprime: "11"
generator: "4"
e2e:
prime: "0x11"
smallprime: "0x0B"
generator: "0x04"
...
...@@ -14,35 +14,35 @@ import ( ...@@ -14,35 +14,35 @@ import (
"testing" "testing"
) )
// This test checks that unmarshalling the groups.yaml file var prime = large.NewInt(int64(17))
// is equal to the expected groups object. var smallPrime = large.NewInt(int64(11))
func TestGroups_UnmarshallingFileEqualsExpected(t *testing.T) { var generator = large.NewInt(int64(4))
prime := large.NewInt(int64(17)) var cmix = cyclic.NewGroup(prime, generator, smallPrime)
smallPrime := large.NewInt(int64(11)) var e2e = cyclic.NewGroup(prime, generator, smallPrime)
generator := large.NewInt(int64(4))
cmix := cyclic.NewGroup(prime, generator, smallPrime) var ExpectedGroups = Groups{
e2e := cyclic.NewGroup(prime, generator, smallPrime)
expected := Groups{
CMix: cmix, CMix: cmix,
E2E: e2e, E2E: e2e,
} }
actual := Groups{} // This test checks that unmarshalling the groups.yaml file
buf, _ := ioutil.ReadFile("./Groups.yaml") // is equal to the expected groups object.
func TestGroups_UnmarshallingFileEqualsExpected(t *testing.T) {
actual := Params{}
buf, _ := ioutil.ReadFile("./params.yaml")
err := yaml.Unmarshal(buf, &actual) err := yaml.Unmarshal(buf, &actual)
if err != nil { if err != nil {
t.Errorf("Unable to decode into struct, %v", err) t.Errorf("Unable to decode into struct, %v", err)
} }
if actual.E2E.GetFingerprint() != expected.E2E.GetFingerprint() { if actual.Groups.E2E.GetFingerprint() != ExpectedGroups.E2E.GetFingerprint() {
t.Errorf("Groups object did not match expected values for E2E") t.Errorf("Groups object did not match expected values for E2E")
} }
if actual.CMix.GetFingerprint() != expected.CMix.GetFingerprint() { if actual.Groups.CMix.GetFingerprint() != ExpectedGroups.CMix.GetFingerprint() {
t.Errorf("Groups object did not match expected values for E2E") t.Errorf("Groups object did not match expected values for CMIX")
} }
} }
...@@ -6,15 +6,35 @@ ...@@ -6,15 +6,35 @@
package conf package conf
import "github.com/spf13/viper"
// This object is used by the server instance. // This object is used by the server instance.
// A viper (or any yaml based) configuration // A viper (or any yaml based) configuration
// can be unmarshalled into this object. // can be unmarshalled into this object.
// For viper just use Unmarshal(&params). // For viper just use Unmarshal(&params).
// Note not all fields are in the YAML, ie NodeID
// but all fields must be in the viper object
type Params struct { type Params struct {
Database DB Database DB
Groups Groups Groups Groups
Paths Paths Paths Paths
Servers []string Servers []string
NodeID int `yaml:"nodeId"` NodeID uint64
SkipReg bool `yaml:"skipReg"` SkipReg bool `yaml:"skipReg"`
} }
// NewParams returns a params object if it is able to
// unmarshal the viper config, otherwise it returns
// an error.
func NewParams(vip *viper.Viper) (*Params, error) {
params := Params{}
err := vip.Unmarshal(&params)
if err != nil {
return nil, err
}
params.Groups = NewGroups(vip)
return &params, nil
}
--- ---
# Example yaml params file used for testing params object # Example yaml params file used for testing
# the params object and its dependencies
database: database:
name: "name" name: "name"
username: "username" username: "username"
...@@ -25,6 +26,5 @@ servers: ...@@ -25,6 +26,5 @@ servers:
- "127.0.0.1:80" - "127.0.0.1:80"
- "127.0.0.1:80" - "127.0.0.1:80"
- "127.0.0.1:80" - "127.0.0.1:80"
nodeId: 100
skipReg: true skipReg: true
... ...
\ No newline at end of file
...@@ -7,29 +7,64 @@ ...@@ -7,29 +7,64 @@
package conf package conf
import ( import (
"gopkg.in/yaml.v2" "github.com/spf13/viper"
"io/ioutil" "reflect"
"testing" "testing"
) )
// This test checks that unmarshalling the params.yaml file func TestNewParams_ReturnsParamsWhenGivenValidViper(t *testing.T) {
// is equal to the expected Params object.
func TestParams_UnmarshallingFileEqualsExpected(t *testing.T) {
buf, _ := ioutil.ReadFile("./params.yaml") expectedParams := Params{
actual := Params{} Database: ExpectedDB,
Groups: ExpectedGroups,
Paths: ExpectedPaths,
Servers: []string{"127.0.0.1:80", "127.0.0.1:80", "127.0.0.1:80"},
SkipReg: true,
NodeID: uint64(100),
}
vip := viper.New()
vip.AddConfigPath(".")
vip.SetConfigFile("params.yaml")
vip.Set("NodeID", uint64(100))
err := vip.ReadInConfig()
if err != nil {
t.Errorf("Failed to read in params.yaml into viper")
}
params, err := NewParams(vip)
err := yaml.Unmarshal(buf, &actual)
if err != nil { if err != nil {
t.Errorf("Unable to decode into struct, %v", err) t.Errorf("Failed in unmarshaling from viper object")
}
if !reflect.DeepEqual(expectedParams.Servers, params.Servers) {
t.Errorf("Servers value does not match expected value")
}
if !reflect.DeepEqual(expectedParams.NodeID, params.NodeID) {
t.Errorf("NodeId value does not match expected value")
}
if !reflect.DeepEqual(expectedParams.SkipReg, params.SkipReg) {
t.Errorf("SkipReg value does not match expected value")
}
if params.Groups.E2E.GetFingerprint() != ExpectedGroups.E2E.GetFingerprint() {
t.Errorf("E2E object did not match expected values for E2E")
}
if params.Groups.CMix.GetFingerprint() != ExpectedGroups.CMix.GetFingerprint() {
t.Errorf("CMIX object did not match expected values for CMIX")
} }
if actual.NodeID != 100 { if !reflect.DeepEqual(expectedParams.Paths, params.Paths) {
t.Errorf("Params object did not match expected value for NodeID") t.Errorf("Paths value does not match expected value")
} }
if actual.SkipReg != true { if !reflect.DeepEqual(expectedParams.Database, params.Database) {
t.Errorf("Params object did not match expected value for SkipReg") t.Errorf("v value does not match expected value")
} }
} }
---
cert: "~/.elixxir/cert.crt"
key: "~/.elixxir/key.pem"
log: "~/.elixxir/server.log"
...
\ No newline at end of file
...@@ -13,26 +13,26 @@ import ( ...@@ -13,26 +13,26 @@ import (
"testing" "testing"
) )
// This test checks that unmarshalling the path.yaml file var ExpectedPaths = Paths{
// is equal to the expected Paths object.
func TestPaths_UnmarshallingFileEqualsExpected(t *testing.T) {
expected := Paths{
Cert: "~/.elixxir/cert.crt", Cert: "~/.elixxir/cert.crt",
Key: "~/.elixxir/key.pem", Key: "~/.elixxir/key.pem",
Log: "~/.elixxir/server.log", Log: "~/.elixxir/server.log",
} }
buf, _ := ioutil.ReadFile("./paths.yaml") // This test checks that unmarshalling the path.yaml file
actual := Paths{} // is equal to the expected Paths object.
func TestPaths_UnmarshallingFileEqualsExpected(t *testing.T) {
buf, _ := ioutil.ReadFile("./params.yaml")
actual := Params{}
err := yaml.Unmarshal(buf, &actual) err := yaml.Unmarshal(buf, &actual)
if err != nil { if err != nil {
t.Errorf("Unable to decode into struct, %v", err) t.Errorf("Unable to decode into struct, %v", err)
} }
if !reflect.DeepEqual(expected, actual) { if !reflect.DeepEqual(ExpectedPaths, actual.Paths) {
t.Errorf("Paths object did not match expected values") t.Errorf("Paths object did not match ExpectedPaths values")
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment