Skip to content

Commit

Permalink
Update dep capture unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-klotho committed Dec 8, 2023
1 parent c1bf5c6 commit 2a40e92
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
3 changes: 1 addition & 2 deletions pkg/engine2/enginetesting/mock_solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func (m *MockSolution) With(key string, value interface{}) solution_context.Solu
}

func (m *MockSolution) KnowledgeBase() knowledgebase.TemplateKB {
args := m.Called()
return args.Get(0).(knowledgebase.TemplateKB)
return &m.KB
}

func (m *MockSolution) Constraints() *constraints.Constraints {
Expand Down
2 changes: 0 additions & 2 deletions pkg/engine2/operational_eval/dependency_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ func (ctx *fauxConfigContext) FieldValue(field string, resource any) (any, error
// Cannot depend on properties within lists, stop at the list itself
ref.Property = field[:bracketIdx]
}
ctx.addRef(ref)
return nil, err
}
ctx.addRef(ref)
if value != nil {
Expand Down
52 changes: 45 additions & 7 deletions pkg/engine2/operational_eval/dependency_capture_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
package operational_eval

import (
"sort"
"testing"

construct "github.com/klothoplatform/klotho/pkg/construct2"
"github.com/klothoplatform/klotho/pkg/construct2/graphtest"
"github.com/klothoplatform/klotho/pkg/engine2/enginetesting"
knowledgebase "github.com/klothoplatform/klotho/pkg/knowledge_base2"
"github.com/klothoplatform/klotho/pkg/knowledge_base2/properties"
"github.com/klothoplatform/klotho/pkg/set"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

type expectedChanges struct {
nodes []Key
edges map[Key]set.Set[Key]
}

func keysToStrings(ks []Key) []string {
ss := make([]string, 0, len(ks))
for _, k := range ks {
ss = append(ss, k.String())
}
sort.Strings(ss)
return ss
}

func (expected expectedChanges) assert(t *testing.T, actual graphChanges) {
assert := assert.New(t)

actualNodes := make([]Key, 0, len(actual.nodes))
for node := range actual.nodes {
actualNodes = append(actualNodes, node)
}
assert.ElementsMatch(expected.nodes, actualNodes)
assert.Equal(keysToStrings(expected.nodes), keysToStrings(actualNodes), "nodes")

for from, tos := range expected.edges {
actualEs := actual.edges[from]
if !assert.NotNil(actualEs, "missing edges from %s", from) {
continue
}
assert.ElementsMatch(tos.ToSlice(), actualEs.ToSlice(), "edges from %s", from)
assert.Equal(keysToStrings(tos.ToSlice()), keysToStrings(actualEs.ToSlice()), "edges from %s", from)
}
}

Expand All @@ -41,7 +54,18 @@ func Test_fauxConfigContext_ExecuteDecode(t *testing.T) {
"mock:resource1:A -> mock:resource1:B",
"mock:resource1:B -> mock:resource1:C",
)
cfgCtx := knowledgebase.DynamicValueContext{Graph: testGraph}
kb := &enginetesting.MockKB{}
kb.On("GetResourceTemplate", mock.Anything).Return(&knowledgebase.ResourceTemplate{
Properties: knowledgebase.Properties{
"Res2s": &properties.StringProperty{},
"Res4": &properties.StringProperty{},
"Name": &properties.StringProperty{},
},
}, nil)
cfgCtx := knowledgebase.DynamicValueContext{
Graph: testGraph,
KnowledgeBase: kb,
}
ref := construct.PropertyRef{
Resource: construct.ResourceId{Provider: "mock", Type: "resource1", Name: "B"},
Property: "Res4",
Expand Down Expand Up @@ -75,8 +99,14 @@ func Test_fauxConfigContext_ExecuteDecode(t *testing.T) {
name: "dep from dynamic res",
tmpl: `{{ downstream "mock:resource1" .Self | fieldValue "Res4" }}`,
want: expectedChanges{
nodes: []Key{
{GraphState: "Downstream(mock:resource1, mock:resource1:B)"},
},
edges: map[Key]set.Set[Key]{
srcKey: set.SetOf(Key{Ref: graphtest.ParseRef(t, "mock:resource1:C#Res4")}),
srcKey: set.SetOf(
Key{Ref: graphtest.ParseRef(t, "mock:resource1:C#Res4")},
Key{GraphState: "Downstream(mock:resource1, mock:resource1:B)"},
),
},
},
},
Expand All @@ -86,10 +116,18 @@ func Test_fauxConfigContext_ExecuteDecode(t *testing.T) {
{{ $a := upstream "mock:" .Self }}
mock:{{ fieldValue "Res4" $c }}:{{ fieldValue "Res2s" $a }}:{{ fieldValue "Name" $a }}`,
want: expectedChanges{
nodes: []Key{
{GraphState: "Downstream(mock:, mock:resource1:B)"},
{GraphState: "Upstream(mock:, mock:resource1:B)"},
},
edges: map[Key]set.Set[Key]{
srcKey: set.SetOf(Key{Ref: graphtest.ParseRef(t, "mock:resource1:A#Res2s")}),
srcKey: set.SetOf(Key{Ref: graphtest.ParseRef(t, "mock:resource1:A#Name")}),
srcKey: set.SetOf(Key{Ref: graphtest.ParseRef(t, "mock:resource1:C#Res4")}),
srcKey: set.SetOf(
Key{Ref: graphtest.ParseRef(t, "mock:resource1:A#Res2s")},
Key{Ref: graphtest.ParseRef(t, "mock:resource1:A#Name")},
Key{Ref: graphtest.ParseRef(t, "mock:resource1:C#Res4")},
Key{GraphState: "Downstream(mock:, mock:resource1:B)"},
Key{GraphState: "Upstream(mock:, mock:resource1:B)"},
),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/knowledge_base2/resource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ FIELDS:
for i, field := range fields {
currFieldName := strings.Split(field, "[")[0]
found := false
for _, property := range properties {
if property.Details().Name != currFieldName {
for name, property := range properties {
if name != currFieldName {
continue
}
found = true
Expand Down

0 comments on commit 2a40e92

Please sign in to comment.