Commit Graph

22553 Commits

Author SHA1 Message Date
Martin Atkins 6bbedeb88b core: Fix TestContext2Apply_countDecreaseToOneX
The "provider" metadata for a resource is now always populated, whereas
before we populated it only if it was not default.
2018-10-16 19:14:11 -07:00
Martin Atkins 81b5244584 core: Fix TestContext2Apply_provisionerDestroyModule
We now proactively prune empty modules from the state, so the expected
state for this test is completely empty, rather than an empty child
module.
2018-10-16 19:14:11 -07:00
Martin Atkins f439422b6f core: Fix panic in TestContext2Apply_multiVarCountDec 2018-10-16 19:14:11 -07:00
Martin Atkins cf903b9bec core: testDiffFn must populate old value for "type"
Previously testDiffFn was just assuming that the prior value for "type"
was always the empty string, but that doesn't hold if a mocked object is
updated in-place with a previously-populated value for type.

This wasn't a problem before because the old values in the diff were
largely just for presentation to the user, but we do now verify that the
old values match what we're applying to as an extra safety check and so
we must populate the old value properly.

This fix is verified by TestContext2Apply_Provisioner_Diff.
2018-10-16 19:14:11 -07:00
Martin Atkins e0cbdd0c4a core: Fix TestContext2Apply_error
An error during create now causes the object (if non-null) to be marked as
tainted in the state, so our expected final state must reflect that.
2018-10-16 19:14:11 -07:00
James Bardin 3627ce5a50 more plan tests 2018-10-16 19:14:11 -07:00
James Bardin 9d8ca4515e unknown collections in a flatmap
Unknown collections in a flatmap also need to be converted to unknown
values.
2018-10-16 19:14:11 -07:00
Martin Atkins 78b8400766 core: Fix TestContext2Apply_unknownAttribute
Now that we're marking errored creates as tainted in the state, the
object created by this test will get marked as tainted due to the error
about it containing an unknown value even after apply.
2018-10-16 19:14:11 -07:00
Martin Atkins c89f1fea63 core: Fix TestContext2Apply_vars
Since this test sets its own special schema for aws_instance, its expected
output must now be adjusted to only expect values that conform to that
schema. The extraneous attributes like "type" which testDiffFn produces
are no longer visible unless declared in schema.

We also need to now declare "id" as a computed attribute in order for our
state stringer shim to properly populate the formerly-special "ID".
2018-10-16 19:14:11 -07:00
Martin Atkins d574a86d41 core: Improve fail output for TestContext2Apply_destroyComputed
More detailed diagnostics log makes it easier to see what's failing.
2018-10-16 19:14:11 -07:00
Martin Atkins 14524600b1 core: Make TestContext2Apply_createBeforeDestroy fail helpful 2018-10-16 19:14:11 -07:00
Martin Atkins 7b3441f1c1 core: Fix TestContext2Apply_resourceDependsOnModuleDestroy
We can't use diff attributes to differentiate instances during destroy
because destroy diffs don't have any attributes set in the old API.

Also, our new state management code correctly prunes out the empty
module.child before returning, so our expected output is updated to not
expect that to still be present.
2018-10-16 19:14:11 -07:00
Martin Atkins 56965c2be7 core: Don't panic when refreshing or applying deposed objects 2018-10-16 19:14:11 -07:00
Martin Atkins e9e11955a8 core: EvalDiff must handle Create/Replace as a special case
When we re-run EvalDiff during apply, we may have already completed the
destroy leg of a replace operation, leaving us in a different situation
than we were when we made the original planned change.

Therefore as a special case we will allow a create to turn back into a
replace if there was an earlier diff that requested that.
2018-10-16 19:14:11 -07:00
Martin Atkins dea74f9048 core: use testApplyFn in TestContext2Apply_countDecreaseToOneCorrupted
Without testApplyFn, the test can't perform the actions it's trying to
perform.
2018-10-16 19:14:11 -07:00
Martin Atkins a90d6cc599 core: EvalDiff handling of tainted objects
If the prior object is tainted, we behave as if it doesn't exist at all
for most of our logic here but then at the end turn it into a synthetic
replace operation going from the old object to the new object, similarly
to how we'd behave if given an argument change that "requires
replacement".
2018-10-16 19:14:11 -07:00
Martin Atkins 31c412b44d core: Generate "provider bug" warnings only if no errors from provider
We can be more relaxed about our rules that a create musn't return null
or a destroy must return null if the provider also itself indicated an
error. In that case, it's expected that the return value is describing a
partial result, and so we'll just store it and move on.
2018-10-16 19:14:11 -07:00
James Bardin f63bba78c9 updating context plan test 2018-10-16 19:14:11 -07:00
James Bardin b738cdb19f make Changes.Empty() not count NoOps
This make the plan `Empty` concept more closely match the legacy diff
behavior.

Remove old unknown field from plan_test
2018-10-16 19:14:11 -07:00
Martin Atkins 103c160a47 core: Remove TestContext2Apply_Provisioner_ConnInfo
The mechanism for a provider to pre-populate parts of the connection
config for subsequent provisioners is no longer present in the new
protocol, since it was rarely used, poorly documented, and for many
resource types had no obvious good defaults.
2018-10-16 19:14:11 -07:00
Martin Atkins ccd1b1df53 core: EvalApply must run to completion even if provider produces errors
Although we have a special case where a result of the wrong type will bail
early, we must keep that set of diagnostics separate so that we can still
run to completion when there are _already_ diagnostics present (from the
provider's response) but the return value _is_ type-conforming.

This fix is verified by TestContext2Apply_provisionerCreateFail.
2018-10-16 19:14:11 -07:00
Martin Atkins 9eb32c4536 core: Reinstaint instance tainting, but without mutating objects
Our previous mechanism for dealing with tainting relied on directly
mutating the InstanceState object to mark it as such. In our new state
models we consider the instance objects to be immutable by convention, and
so we frequently copy them. As a result, the taint flagging was no longer
making it all the way through the apply evaluation process.

Here we now implement tainting as a separate step in the evaluation
process, creating a copy of the object with a tainted status if there were
any errors during creation.

This introduces a new behavior where any provider-level errors during
creation will also cause an instance to be marked as tainted if any object
is returned at all. Create-time errors _normally_ result in no object at
all, but the provider might return an object if the failure occurred at
a subsequent step of a multi-step creation process and so left behind a
remote object that needs to be cleaned up on a future run.
2018-10-16 19:14:11 -07:00
Martin Atkins 77e50894d5 core: Don't create self-references in state
Since StateReferences was implemented on NodeAbstractResource rather than
NodeAbstractResourceInstance it wasn't properly detecting references to
the same instance as self-references.

Now that we are using "seen" to filter out duplicates we can also simplify
how we handle these self-references by just pretending we saw them before
we even start the loop.

This change is confirmed by
TestContext2Apply_provisionerMultiSelfRefSingle
2018-10-16 19:14:11 -07:00
Martin Atkins 9afb0c6c0c core: EvalApplyProvisioners correct handling of errors
The error handling here is a bit tricky due to the ability for users to
opt out of aborting on error. It's important that we keep straight the
distinction between applyDiags and diags so we can tell the difference
between the errors from _this_ provisioner and the errors for the entire
run so far.
2018-10-16 19:14:11 -07:00
Martin Atkins 4581769ba1 core: Fail better in TestContext2Apply_provisionerDestroyFailContinue
"bad" is an unhelpful test assertion failure message.
2018-10-16 19:14:11 -07:00
Martin Atkins b6e31be09c core: Rewrite EvalApplyProvisioners for new provisioner API 2018-10-16 19:14:11 -07:00
Martin Atkins 859b384558 core: Legacy ApplyFn handling for MockProvisioner 2018-10-16 19:14:11 -07:00
Martin Atkins d48f3600fe states: In Module.testString, use incrementing ids for deposed
In our old world we always used 1-based indices into a slice of deposed
objects. The new models instead use a map keyed by pseudorandom strings,
so that deposed objects will have a consistent identity across multiple
operations.

However, having that pseudo-random string in our test comparison output
is not helpful, since such strings can never match hard-coded expectation
strings. Therefore for the purposes of generating this test comparison
output we'll revert back to using 1-based indexes.

This should avoid problems for tests that only create one deposed object
per instance, but those which create more than one will need to do some
more work since the _ordering_ of these objects in the output is still
pseudorandom as a result of it coming from a map rather than a slice.
2018-10-16 19:14:11 -07:00
Martin Atkins 425d5cd191 core: fix TestContext2Apply_varsEnv
The expected output string for this test is assuming a couple computed
attributes that were not declared in schema. This didn't matter before
because the provider output was not previously subject to schema-based
interpretation, but now our shims to the old provider API rely on the
schema to convert the returned data and so any unexpected attributes are
filtered.
2018-10-16 19:14:11 -07:00
Martin Atkins 5802953ab6 core: Improve realism of map representation in testDiffFn
The diffs created by testDiffFn use the flatmap package directly, rather
than running the diff-generation logic in helper/schema. It turns out that
flatmap itself can generate the k+".#" keys used to indicate the length
of a list, but only helper/schema itself knew how to generate the
corresponding k+".%" keys used for maps.

Rather than modifying the now-deprecated flatmap code directly (and risk
breaking assumptions in shims elsewhere), here we just synthesize the
extra required map element within the testDiffFn implementation, which
then in turn allows the MockProvider diffFn shim to correctly recognize
it as a map and convert it into a real cty.Map value to return.
2018-10-16 19:14:11 -07:00
Martin Atkins bd6d3a638a core: Simplify output refs to module call refs in StateReferences
As previously mentioned in a comment here, versions of Terraform prior to
0.12 would store references to module outputs as just references to the
module as a whole in the state. It's not really clear why, but we wanted
to preserve this behavior for 0.12.

The previous implementation actually failed to do so, in spite of the
comment, so this commit fixes it to actually do what the comment
originally claimed.

In a later release we might remove this special case and just depend
directly on outputs where possible, since that'd allow us to produce a
more precise dependency graph for destroy actions, but when we do that
we'll first need to confirm that there isn't a good reason for the
original exception here.
2018-10-16 19:14:11 -07:00
Martin Atkins 83066cd57f states: Support non-string primitives in state string representation 2018-10-16 19:14:11 -07:00
Martin Atkins ace46e9669 core: EvalWriteOutput discard unknown values before writing state
The state only deals in wholly-known values, so here we null out any
unknowns for storage in state. This is okay because we subsequently write
the original, possibly-unknown value into the plan and the expression
evaluator will prefer to use this if present, allowing the unknown values
to properly propagate into other expressions in the calling module.
2018-10-16 19:14:11 -07:00
Martin Atkins a709b9f07a core: Partially fix TestContext2Apply_provisionerDestroyFail tests
These need their output strings updated for the new behavior that all
resource instances recorded in state have a provider configuration
associated, whereas before we only did it for non-default ones.
2018-10-16 19:14:11 -07:00
Martin Atkins d13a932dac core: Fix TestContext2Apply_scaleInMultivarRef
We have no "val" attribute defined in the schema, so we'll use "value"
here instead.
2018-10-16 19:14:11 -07:00
Martin Atkins 5f344c9590 core: Fix TestContext2Apply_idAttr
The "num" attribute is marked as being a number in the provider schema, so
we are now required to make it actually convertible to number.
2018-10-16 19:14:11 -07:00
Martin Atkins 934dd8f710 core: fix panic in TestContext2Apply_resourceDependsOnModuleDestroy
This panic is likely caused by a bug, since "ami" should always be set,
but we'll fix the panic first to allow the tests to run to completion.
2018-10-16 19:14:11 -07:00
Martin Atkins 64eb5f732c core: NodeApplyableResource only depends on count and for_each
Only the count and for_each expressions are evaluated by this node type,
so it doesn't need to declare dependencies for any other refs in the
configuration body. Using this more refined set of dependencies means
we can avoid graph cycles in the case where one instance of a resource
refers to another instance of the same resource.

We'll still get cycles if count or for_each self-reference, but that's
forbidden anyway (caught during validate) and makes sense because those
two are whole-resource-level config rather than per-instance config.
2018-10-16 19:14:11 -07:00
Martin Atkins d2c134a80e core: go fmt updates for context_apply_test.go 2018-10-16 19:14:11 -07:00
Martin Atkins 5d3f642585 core: Fix TestContext2Apply_destroyNestedModule
Our string representation of state for testing now returns "<no state>"
when empty, rather than the empty string.
2018-10-16 19:14:11 -07:00
Martin Atkins 50b8053476 core: make providers available during the "plan destroy" walk
We now use providers for schema-related actions during this walk, so we
need to initialize them in a similar way as we do for other walks.
2018-10-16 19:14:11 -07:00
Martin Atkins 86d2716679 core: Normalize results from NodeAbstractResource.StateReferences
The underlying References function includes duplicates and returns refs
in the order they appeared in source (approximately), but after we reduce
to just the raw addresses it's better to dedupe and return in a
predictable order.
2018-10-16 19:14:11 -07:00
Martin Atkins b648e3fc84 core: fix TestContext2Apply_resourceDependsOnModuleInModule
An earlier update to make this not use info.HumanId selected the wrong
fake "ami" name in the branch here.

Also, the error message for this failure was terrible. :(
2018-10-16 19:14:11 -07:00
Martin Atkins d104e450d8 core: testProviderSchema aws_instance must include "unknown" attribute
This is computed in the special case where compute = "unknown" in order
to force inclusion of an unknown value into the ultimate result, which
is invalid.

This fixes TestContext2Apply_unknownAttribute, which is intending to test
this error handling behavior.
2018-10-16 19:14:11 -07:00
Martin Atkins e3f2c2c03f core: fix TestContext2Apply_resourceDependsOnModuleGrandchild
Some changes made to the test mocks and fixtures have changed the expected
output of this test to include some additional attributes.
2018-10-16 19:14:11 -07:00
Martin Atkins 5faf027ea7 states: In State.String, use colon suffix only after all module names 2018-10-16 19:14:11 -07:00
Martin Atkins f561c9c226 core: Populate Dependencies of ResourceInstanceObject during apply
Previously we kept the dependencies one level higher on the resource
instance itself, which meant that updating it was handled in a different
EvalNode, but now we consider these to be dependencies of the object
itself (derived from the configuration that was current at the time it
was created), so we must handle this during EvalApply.

The subtle difference here is that if an object is moved to "deposed"
during a create_before_destroy replace then it will retain the
dependencies it had on its last apply, rather than them being replaced
by the dependencies of the newly-created object.
2018-10-16 19:14:11 -07:00
Martin Atkins 55222869bd core: EvalRefresh should not mutate the state object it is given
We now treat states.ResourceInstanceObject values as immutable once
constructed, preferring to replace them completely rather than update them
in-place to avoid weird race conditions.

Therefore EvalRefresh must copy the state it is given before mutating the
Value field of it to reflect the updated value from the provider.
2018-10-16 19:14:11 -07:00
Martin Atkins 60718efc8e states: DeepCopy for ResourceInstanceObject
Also a fix for not actually deep-copying "Private", since when this was
originally written it was a cty.Value but then later became a []byte.
2018-10-16 19:14:11 -07:00
Martin Atkins 3b7a814c51 core: Partially fix TestContext2Apply_resourceDependsOnModule
Some earlier updates to it changed some things in our expected state
string. This doesn't fully fix it since there seems to still be a bug
related to recording dependencies.
2018-10-16 19:14:11 -07:00