Skip to content

Commit

Permalink
wip: Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clarsonneur committed Feb 5, 2019
1 parent 464bbfa commit 114757f
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 50 deletions.
25 changes: 19 additions & 6 deletions creds/forj_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -35,4 +38,14 @@ func (v *ForjValue) AddResource(key, value string) {
}

v.resource[key] = value
}
}

// 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)
}
95 changes: 95 additions & 0 deletions creds/forj_value_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
8 changes: 5 additions & 3 deletions creds/objects_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
129 changes: 129 additions & 0 deletions creds/objects_value_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
28 changes: 15 additions & 13 deletions creds/secure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 (
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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())
}

Expand All @@ -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())
}

Expand Down
Loading

0 comments on commit 114757f

Please sign in to comment.