Skip to content

Commit

Permalink
stacks: early exit on invalid for_each values for components
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed Apr 26, 2024
1 parent 0bb1e90 commit 1a99da2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
9 changes: 8 additions & 1 deletion internal/stacks/stackruntime/internal/stackeval/component.go
Expand Up @@ -159,7 +159,14 @@ func (c *Component) CheckInstances(ctx context.Context, phase EvalPhase) (map[ad
ctx, c.instances.For(phase), c.main,
func(ctx context.Context) (map[addrs.InstanceKey]*ComponentInstance, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
forEachVal := c.ForEachValue(ctx, phase)
forEachVal, forEachValueDiags := c.CheckForEachValue(ctx, phase)

// We can not create an instance map if the for each vaulu is not valid.
// We don't want to report on forEachValueDiags here because we
// already do this in checkValid.
if forEachValueDiags.HasErrors() {
return nil, diags
}

ret := instancesMap(forEachVal, func(ik addrs.InstanceKey, rd instances.RepetitionData) *ComponentInstance {
return newComponentInstance(c, ik, rd)
Expand Down
25 changes: 11 additions & 14 deletions internal/stacks/stackruntime/internal/stackeval/component_test.go
Expand Up @@ -192,10 +192,10 @@ func TestComponentCheckInstances(t *testing.T) {
// When the for_each expression is invalid, CheckInstances should
// return a single instance with dynamic values in the repetition data.
// We don't distinguish between invalid and unknown for_each values.
gotInsts, diags := component.CheckInstances(ctx, InspectPhase)
assertNoDiags(t, diags)
if got, want := len(gotInsts), 1; got != want {
t.Fatalf("wrong number of instances %d; want %d\n%#v", got, want, gotInsts)
gotInsts, _ := component.CheckInstances(ctx, InspectPhase)

if gotInsts != nil {
t.Fatalf("unexpected instances\ngot: %#v\nwant: nil", gotInsts)
}
})
subtestInPromisingTask(t, "unknown", func(ctx context.Context, t *testing.T) {
Expand Down Expand Up @@ -389,16 +389,13 @@ func TestComponentResultValue(t *testing.T) {
component := getComponent(ctx, t, main)
got := component.ResultValue(ctx, InspectPhase)
// When the for_each expression is unknown, the result value
// is unknown too so we can use it as a placeholder for partial
// downstream checking.
want := cty.DynamicVal
// FIXME: the cmp transformer ctydebug.CmpOptions seems to find
// this particular pair of values troubling, causing it to get
// into an infinite recursion. For now we'll just use RawEquals,
// at the expense of a less helpful failure message. This seems
// to be a bug in upstream ctydebug.
if !want.RawEquals(got) {
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", got, want)
// is an instance map with the wildcard key and an empty object
want := cty.ObjectVal(map[string]cty.Value{
"*": cty.EmptyObjectVal,
})

if diff := cmp.Diff(want, got, ctydebug.CmpOptions); diff != "" {
t.Fatalf("wrong result\n%s", diff)
}
})
})
Expand Down

0 comments on commit 1a99da2

Please sign in to comment.