From 114757fc90e6d703049ab1cffe36d7ec069b25f5 Mon Sep 17 00:00:00 2001 From: Christophe Larsonneur Date: Fri, 1 Feb 2019 22:19:51 +0100 Subject: [PATCH] wip: Fix tests --- creds/forj_value.go | 25 +++++-- creds/forj_value_test.go | 95 ++++++++++++++++++++++++++ creds/objects_value.go | 8 ++- creds/objects_value_test.go | 129 ++++++++++++++++++++++++++++++++++++ creds/secure_test.go | 28 ++++---- creds/yaml_secure.go | 11 ++- creds/yaml_secure_test.go | 41 +++++++----- glide.lock | 8 +-- 8 files changed, 295 insertions(+), 50 deletions(-) create mode 100644 creds/forj_value_test.go create mode 100644 creds/objects_value_test.go diff --git a/creds/forj_value.go b/creds/forj_value.go index aaa1b978..df13504b 100644 --- a/creds/forj_value.go +++ b/creds/forj_value.go @@ -2,12 +2,12 @@ package creds // ForjjValue describe the Forjj keys value type ForjValue struct { - value string // value. - // If source == `forjj` => real value - // If source == `file` => Path the a file containing the value - // Else => address of the data, with eventually a collection of resources to help getting the data from the address given. + value string // value. + // If source == `forjj` => real value + // If source == `file` => Path the a file containing the value + // Else => address of the data, with eventually a collection of resources to help getting the data from the address given. resource map[string]string // Collection of resources to identify where is the data and how to access it - source string // Source of the data. Can be `forjj`, `file` or an external service like `plugin:vault` + source string // Source of the data. Can be `forjj`, `file` or an external service like `plugin:vault` } func NewForjValue(source, value string) (ret *ForjValue) { @@ -23,6 +23,9 @@ func (v *ForjValue) Set(source, value string) { } v.value = value v.source = source + if v.resource == nil { + v.resource = make(map[string]string) + } } // AddResource adds resources information to the data given @@ -35,4 +38,14 @@ func (v *ForjValue) AddResource(key, value string) { } v.resource[key] = value -} \ No newline at end of file +} + +// MarshalYAML encode the object in ValueStruct output +func (v ForjValue) MarshalYAML() (interface{}, error) { + return v.value, nil +} + +// UnmarshalYAML decode the flow as a ValueStruct +func (v *ForjValue) UnmarshalYAML(unmarchal func(interface{}) error) error { + return unmarchal(&v.value) +} diff --git a/creds/forj_value_test.go b/creds/forj_value_test.go new file mode 100644 index 00000000..4c43a350 --- /dev/null +++ b/creds/forj_value_test.go @@ -0,0 +1,95 @@ +package creds + +import ( + "reflect" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_NewForjValue(t *testing.T) { + t.Log("Expecting NewForjValue to properly initialized the ForjValue object.") + assert := assert.New(t) + + // ------------- call the function + v := NewForjValue("forjj", "value") + + // -------------- testing + when := "when a new ForjValue is set" + if assert.NotNilf(v, "Expected ForjValue to be returned %s", when) { + assert.Equalf("value", v.value, "Expected value to be properly set %s", when) + assert.Equalf("forjj", v.source, "Expected source to be set properly %s", when) + if assert.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + assert.Lenf(v.resource, 0, "Expected resource to be empty %s", when) + } + } +} + +func Test_ForjValue_Set(t *testing.T) { + t.Log("Expecting ForjValue.Set to properly set the ForjValue object.") + assert := assert.New(t) + + v := ForjValue{} + // ------------- call the function + v.Set("forjj", "value") + + // -------------- testing + when := "when an empty ForjValue is set" + assert.Equalf("value", v.value, "Expected value to be properly set %s", when) + assert.Equalf("forjj", v.source, "Expected source to be set properly %s", when) + if assert.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + assert.Lenf(v.resource, 0, "Expected resource to be empty %s", when) + } + + // -------------- Update context + v1 := NewForjValue("forjj", "value") + + // ------------- call the function + v1.Set("blabla", "value1") + + // -------------- testing + when = "when an existing ForjValue is set" + if assert.NotNilf(v1, "Expected ForjValue to be returned %s", when) { + assert.Equalf("value1", v1.value, "Expected value to be properly set to new value %s", when) + assert.Equalf("blabla", v1.source, "Expected source to be set properly to new value %s", when) + if assert.NotNilf(v1.resource, "Expected resource to be initialized %s", when) { + assert.Lenf(v1.resource, 0, "Expected resource to be empty %s", when) + } + } +} + +func Test_ForjValue_AddResource(t *testing.T) { + t.Log("Expecting ForjValue.AddResource to properly set the ForjValue object resource list.") + test := assert.New(t) + + v := ForjValue{} + r := map[string]string{ + "key": "value", + } + + // ------------- call the function + v.AddResource("key", "value") + + // -------------- testing + when := "when a resource is added on an empty ForjValue." + test.Emptyf(v.value, "Expected value to stay empty %s", when) + test.Emptyf(v.source, "Expected source to stay empty %s", when) + if test.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + test.Lenf(v.resource, 1, "Expected resource to be empty %s", when) + test.Truef(reflect.DeepEqual(v.resource, r), "Expected resource to be conform %s", when) + } + // -------------- Update context + r["key"] = "value2" + + // ------------- call the function + v.AddResource("key", "value2") + + // -------------- testing + when = "when a resource is updated." + test.Emptyf(v.value, "Expected value to stay empty %s", when) + test.Emptyf(v.source, "Expected source to stay empty %s", when) + if test.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + test.Lenf(v.resource, 1, "Expected resource to be empty %s", when) + test.Truef(reflect.DeepEqual(v.resource, r), "Expected resource to be conform %s", when) + } +} diff --git a/creds/objects_value.go b/creds/objects_value.go index 626fd157..043fa77b 100644 --- a/creds/objects_value.go +++ b/creds/objects_value.go @@ -26,11 +26,13 @@ func (v *ObjectsValue) Set(source string, value *goforjj.ValueStruct) { return } if v.value == nil { - v.value = value - } else { - *v.value = *value + v.value = goforjj.NewValueStruct(nil) } + *v.value = *value v.source = source + if v.resource == nil { + v.resource = make(map[string]string) + } } // GetString source andvalue of a ForjValue instance diff --git a/creds/objects_value_test.go b/creds/objects_value_test.go new file mode 100644 index 00000000..3846b7ee --- /dev/null +++ b/creds/objects_value_test.go @@ -0,0 +1,129 @@ +package creds + +import ( + "reflect" + "testing" + + "github.com/forj-oss/goforjj" + "github.com/stretchr/testify/assert" +) + +func Test_NewObjectsValue(t *testing.T) { + t.Log("Expecting NewForjValue to properly initialized the ForjValue object.") + assert := assert.New(t) + value := goforjj.NewValueStruct("value") + + // ------------- call the function + v := NewObjectsValue("forjj", value) + + // -------------- testing + when := "when a new ObjectsValue is set" + if assert.NotNilf(v, "Expected ObjectsValue to be returned %s", when) { + if assert.NotNilf(v.value, "Expected ObjectValue to have a valid ValueStruct %s", when) { + assert.Truef(v.value.Equal(value), "Expected value to be properly set %s", when) + assert.Equalf("forjj", v.source, "Expected source to be set properly %s", when) + if assert.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + assert.Lenf(v.resource, 0, "Expected resource to be empty %s", when) + } + } + } +} + +func Test_ObjectsValue_Set(t *testing.T) { + t.Log("Expecting ForjValue.Set to properly set the ForjValue object.") + assert := assert.New(t) + + v := ObjectsValue{} + value := goforjj.NewValueStruct("value") + + // ------------- call the function + v.Set("forjj", value) + + // -------------- testing + when := "when an empty ForjValue is set" + if assert.NotNilf(v.value, "Expected ObjectValue to have a valid ValueStruct %s", when) { + assert.Truef(v.value.Equal(value), "Expected value to be properly set %s", when) + assert.Equalf("forjj", v.source, "Expected source to be set properly %s", when) + if assert.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + assert.Lenf(v.resource, 0, "Expected resource to be empty %s", when) + } + } + + // -------------- Update context + value.Set("value2") + + // -------------- testing + when = "when the ValueStruct value has been updated outside" + if assert.NotNilf(v.value, "Expected ObjectValue to have a valid ValueStruct %s", when) { + assert.Falsef(v.value.Equal(value), "Expected value to be updated %s", when) + assert.Equalf("forjj", v.source, "Expected source to be set properly %s", when) + if assert.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + assert.Lenf(v.resource, 0, "Expected resource to be empty %s", when) + } + } + + // ------------- call the function + v.Set("blabla", value) + + // -------------- testing + when = "when an existing ObjectsValue is set" + if assert.NotNilf(v, "Expected ObjectsValue to be returned %s", when) { + assert.Truef(v.value.Equal(value), "Expected value to be properly set to new value %s", when) + assert.Equalf("blabla", v.source, "Expected source to be set properly to new value %s", when) + if assert.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + assert.Lenf(v.resource, 0, "Expected resource to be empty %s", when) + } + } + + // -------------- Update context + v1 := NewObjectsValue("forjj", goforjj.NewValueStruct("value")) + + // ------------- call the function + v1.Set("blabla", value) + + // -------------- testing + when = "when an existing ObjectsValue is set" + if assert.NotNilf(v1, "Expected ObjectsValue to be returned %s", when) { + assert.Truef(v.value.Equal(value), "Expected value to be properly set to new value %s", when) + assert.Equalf("blabla", v1.source, "Expected source to be set properly to new value %s", when) + if assert.NotNilf(v1.resource, "Expected resource to be initialized %s", when) { + assert.Lenf(v1.resource, 0, "Expected resource to be empty %s", when) + } + } +} + +func Test_ObjectsValue_AddResource(t *testing.T) { + t.Log("Expecting ForjValue.AddResource to properly set the ForjValue object resource list.") + test := assert.New(t) + + v := ObjectsValue{} + r := map[string]string{ + "key": "value", + } + + // ------------- call the function + v.AddResource("key", "value") + + // -------------- testing + when := "when a resource is added on an empty ForjValue." + test.Emptyf(v.value, "Expected value to stay empty %s", when) + test.Emptyf(v.source, "Expected source to stay empty %s", when) + if test.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + test.Lenf(v.resource, 1, "Expected resource to be empty %s", when) + test.Truef(reflect.DeepEqual(v.resource, r), "Expected resource to be conform %s", when) + } + // -------------- Update context + r["key"] = "value2" + + // ------------- call the function + v.AddResource("key", "value2") + + // -------------- testing + when = "when a resource is updated." + test.Emptyf(v.value, "Expected value to stay empty %s", when) + test.Emptyf(v.source, "Expected source to stay empty %s", when) + if test.NotNilf(v.resource, "Expected resource to be initialized %s", when) { + test.Lenf(v.resource, 1, "Expected resource to be empty %s", when) + test.Truef(reflect.DeepEqual(v.resource, r), "Expected resource to be conform %s", when) + } +} diff --git a/creds/secure_test.go b/creds/secure_test.go index 30df4dd7..a47ae154 100644 --- a/creds/secure_test.go +++ b/creds/secure_test.go @@ -138,9 +138,10 @@ func TestSetForjValue(t *testing.T) { source = "source" ) s.InitEnvDefaults(myPath, prod) + forjValue1 := NewForjValue("forjj", value1) // ------------- call the function - updated, err := s.SetForjValue(prod, source, key1, value1) + updated, err := s.SetForjValue(prod, source, key1, forjValue1) // -------------- testing if !updated { @@ -174,9 +175,9 @@ func TestSetObjectValue(t *testing.T) { s.InitEnvDefaults(myPath, prod) // ------------- call the function - value := new(goforjj.ValueStruct) - value.Set(value1) - updated := s.SetObjectValue(prod, source, object1, instance1, key1, value) + value := goforjj.NewValueStruct(value1) + forjValue := NewObjectsValue("forjj", value) + updated := s.SetObjectValue(prod, source, object1, instance1, key1, forjValue) // -------------- testing if !updated { @@ -193,7 +194,7 @@ func TestSetObjectValue(t *testing.T) { } func TestGetObjectInstance(t *testing.T) { - t.Log("Expecting GetObjectInstance to set properly values.") + t.Log("Expecting GetObjectInstance to get key, value properly.") s := Secure{} const ( @@ -211,7 +212,8 @@ func TestGetObjectInstance(t *testing.T) { s.InitEnvDefaults(myPath, prod) value := new(goforjj.ValueStruct) value.Set(value1) - s.SetObjectValue(prod, source, object1, instance1, key1, value) + objectsValue := NewObjectsValue("forjj", value) + s.SetObjectValue(prod, source, object1, instance1, key1, objectsValue) // ------------- call the function result := s.GetObjectInstance(object1, instance1) @@ -223,7 +225,7 @@ func TestGetObjectInstance(t *testing.T) { t.Errorf("Expected GetObjectInstance to return a map with 1 element. Got %d.", l1) } else if v, found := result[key1]; !found { t.Errorf("Expected GetObjectInstance to return a map containing '%s'. Not found.", key1) - } else if !v.Equal(value) { + } else if !v.value.Equal(value) { t.Errorf("Expected GetObjectInstance to return a map containing '%s = %s. Got '%s'", key1, value1, v.GetString()) } // ------------- call the function @@ -236,7 +238,7 @@ func TestGetObjectInstance(t *testing.T) { // --------------- Change context value.Set(value2) - s.SetObjectValue(Global, source, object1, instance1, key1, value) + s.SetObjectValue(Global, source, object1, instance1, key1, objectsValue) // ------------- call the function result = s.GetObjectInstance(object1, instance1) @@ -248,13 +250,13 @@ func TestGetObjectInstance(t *testing.T) { t.Errorf("Expected GetObjectInstance to return a map with 1 element. Got %d.", l1) } else if v, found := result[key1]; !found { t.Errorf("Expected GetObjectInstance to return a map containing '%s'. Not found.", key1) - } else if v.Equal(value) { - t.Errorf("Expected GetObjectInstance to return a map containing '%s = %s. Got '%s'", key1, value.GetString(), v.GetString()) + } else if v.value.Equal(value) { + t.Errorf("Expected GetObjectInstance to return a map containing '%s = %s. Got '%s'", key1, value1, v.GetString()) } // --------------- Change context value.Set(value1) - s.SetObjectValue(Global, source, object2, instance1, key1, value) + s.SetObjectValue(Global, source, object2, instance1, key1, objectsValue) // ------------- call the function result1 := s.GetObjectInstance(object1, instance1) @@ -267,7 +269,7 @@ func TestGetObjectInstance(t *testing.T) { t.Errorf("Expected GetObjectInstance to return a map with 1 element. Got %d.", l1) } else if v, found := result1[key1]; !found { t.Errorf("Expected GetObjectInstance to return a map containing '%s'. Not found.", key1) - } else if !v.Equal(value) { + } else if !v.value.Equal(value) { t.Errorf("Expected GetObjectInstance to return a map containing '%s = %s. Got '%s'", key1, value1, v.GetString()) } @@ -277,7 +279,7 @@ func TestGetObjectInstance(t *testing.T) { t.Errorf("Expected GetObjectInstance to return a map with 1 element. Got %d.", l1) } else if v, found := result2[key1]; !found { t.Errorf("Expected GetObjectInstance to return a map containing '%s'. Not found.", key1) - } else if !v.Equal(value) { + } else if !v.value.Equal(value) { t.Errorf("Expected GetObjectInstance to return a map containing '%s = %s. Got '%s'", key1, value1, v.GetString()) } diff --git a/creds/yaml_secure.go b/creds/yaml_secure.go index 3eff3179..07234970 100644 --- a/creds/yaml_secure.go +++ b/creds/yaml_secure.go @@ -3,14 +3,13 @@ package creds import ( "bufio" "fmt" - "forjj/sources_info" + sourcesinfo "forjj/sources_info" "io" "io/ioutil" "os" - "github.com/forj-oss/forjj-modules/trace" - "github.com/forj-oss/goforjj" - "gopkg.in/yaml.v2" + gotrace "github.com/forj-oss/forjj-modules/trace" + yaml "gopkg.in/yaml.v2" ) type yamlSecure struct { @@ -192,8 +191,8 @@ func (d *yamlSecure) get(obj_name, instance_name, key_name string) (ret *Objects if i, isFound := d.Objects[obj_name]; isFound { if k, isFound := i[instance_name]; isFound { if v, isFound := k[key_name]; isFound && v.value != nil { - ret.value = new(goforjj.ValueStruct) - *ret.value = *v.value + ret = NewObjectsValue(v.source, v.value) + ret.resource = v.resource found = true source = d.sources.Get(obj_name + "/" + instance_name + "/" + key_name) return diff --git a/creds/yaml_secure_test.go b/creds/yaml_secure_test.go index 65313cdb..06bd6d6c 100644 --- a/creds/yaml_secure_test.go +++ b/creds/yaml_secure_test.go @@ -18,8 +18,9 @@ func Test_YamlSecure_SetForjValue(t *testing.T) { source = "source" ) + forjValue1 := NewForjValue("forjj", value1) // ------------- call the function - s.SetForjValue(source, key1, value1) + s.SetForjValue(source, key1, forjValue1) // -------------- testing if s.Forj == nil { @@ -28,7 +29,7 @@ func Test_YamlSecure_SetForjValue(t *testing.T) { t.Errorf("Expected s.Forj to have 1 element. Got %d.", l) } else if v, found := s.Forj[key1]; !found { t.Errorf("Expected s.Forj[%s] to exist. Not found", key1) - } else if v != value1 { + } else if v.value != value1 { t.Errorf("Expected s.Forj[%s] to be '%s'. Got '%s'", key1, value1, v) } } @@ -50,9 +51,9 @@ func Test_YamlSecure_setObjectValue(t *testing.T) { ) // ------------- call the function - value := new(goforjj.ValueStruct) - value.Set(value1) - result := s.setObjectValue(src1, object1, instance1, key1, value) + value := goforjj.NewValueStruct(value1) + objectsValue := NewObjectsValue("forjj", value) + result := s.setObjectValue(src1, object1, instance1, key1, objectsValue) // -------------- testing if s.Objects == nil { @@ -84,7 +85,7 @@ func Test_YamlSecure_setObjectValue(t *testing.T) { } // ------------- call the function - result = s.setObjectValue(src1, object1, instance1, key1, value) + result = s.setObjectValue(src1, object1, instance1, key1, objectsValue) // -------------- testing if result { @@ -93,15 +94,16 @@ func Test_YamlSecure_setObjectValue(t *testing.T) { // ------------- call the function value.Set(value2) - result = s.setObjectValue(src1, object1, instance1, key1, value) + result = s.setObjectValue(src1, object1, instance1, key1, objectsValue) // -------------- testing - if !result { - t.Error("Expected setObjectValue to return true. got false.") + if result { + t.Error("Expected setObjectValue to return false. got true.") } // ------------- call the function - result = s.setObjectValue(src1, object2, instance1, key1, value) + objectsValue.Set("forjj", value) + result = s.setObjectValue(src1, object2, instance1, key1, objectsValue) // -------------- testing if s.Objects == nil { @@ -116,7 +118,7 @@ func Test_YamlSecure_setObjectValue(t *testing.T) { value.Set(value2) // ------------- call the function // Set a new instance, key and value - result = s.setObjectValue(src1, object2, instance2, key1, value) + result = s.setObjectValue(src1, object2, instance2, key1, objectsValue) // -------------- testing if s.Objects == nil { @@ -160,7 +162,8 @@ func Test_YamlSecure_unsetObjectValue(t *testing.T) { value := new(goforjj.ValueStruct) value.Set(value1) - result := s.setObjectValue(src1, object1, instance1, key1, value) + objectsValue := NewObjectsValue("forjj", value) + result := s.setObjectValue(src1, object1, instance1, key1, objectsValue) // ------------- call the function result = s.unsetObjectValue(src1, object1, instance1, key1) @@ -226,7 +229,8 @@ func Test_YamlSecure_get(t *testing.T) { value := new(goforjj.ValueStruct) value.Set(value1) - s.setObjectValue(src1, object1, instance1, key1, value) + objectsValue := NewObjectsValue("forjj", value) + s.setObjectValue(src1, object1, instance1, key1, objectsValue) // ------------- call the function result, found, _ := s.get(object1, instance1, key1) @@ -235,7 +239,7 @@ func Test_YamlSecure_get(t *testing.T) { t.Error("Expected result to be set. Got nil") } else if !found { t.Error("Expected to have found to be true. got false") - } else if !result.Equal(value) { + } else if !result.value.Equal(value) { t.Error("Expected result to be equal to original value. got false") } @@ -256,19 +260,20 @@ func Test_YamlSecure_get(t *testing.T) { result, found, _ = s.get(object1, instance1, key1) // -------------- testing - if result.Equal(value) { + if result.value.Equal(value) { t.Error("Expected result to NOT be equal to original value. got true.") } // ------------- Update context - value = result - result.Set(value2) + value = result.value + result.value.Set(value2) + result.Set("forjj", result.value) // ------------- call the function result, found, _ = s.get(object1, instance1, key1) // -------------- testing - if result.Equal(value) { + if result.value.Equal(value) { t.Error("Expected result to NOT be equal to original value. got true.") } diff --git a/glide.lock b/glide.lock index f1a1b15e..87ba5583 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ hash: 96c626ea3b685de3d27336771760c757e1c870bfac2f1f2b838a36b09c940d6f -updated: 2019-01-30T17:23:11.066281833Z +updated: 2019-02-01T20:52:26.709034877Z imports: - name: github.com/alecthomas/kingpin version: a328427ab7d619fe3c8d16a0da66899d03d5afae @@ -59,7 +59,7 @@ imports: - cli/tools - trace - name: github.com/forj-oss/goforjj - version: ae6f7b07d3183b8a571798e928622b5141fc99b6 + version: 7ac0faddd9bf217e2e9a4d809150da7c549a7c49 subpackages: - runcontext - name: github.com/konsorten/go-windows-terminal-sequences @@ -89,7 +89,7 @@ imports: subpackages: - assert - name: golang.org/x/crypto - version: b01c7a72566457eb1420261cdafef86638fc3861 + version: b8fe1690c61389d7d2a8074a507d1d40c5d30448 subpackages: - ssh/terminal - name: golang.org/x/net @@ -101,7 +101,7 @@ imports: - proxy - publicsuffix - name: golang.org/x/sys - version: aca44879d5644da7c5b8ec6a1115e9b6ea6c40d9 + version: afcc84fd7533758f95a6e93ae710aa945a0b7e73 subpackages: - unix - windows