2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2021-02-11 20:52:10 -05:00
|
|
|
package views
|
2017-03-01 17:16:22 -05:00
|
|
|
|
|
|
|
|
import (
|
2024-10-30 08:44:02 -04:00
|
|
|
"errors"
|
2017-03-01 17:16:22 -05:00
|
|
|
"fmt"
|
2017-07-13 16:01:19 -04:00
|
|
|
"regexp"
|
2017-03-01 17:16:22 -05:00
|
|
|
"testing"
|
2017-03-13 16:09:25 -04:00
|
|
|
"time"
|
|
|
|
|
|
2021-02-16 11:33:03 -05:00
|
|
|
"strings"
|
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
2021-05-17 15:00:50 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 15:07:38 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/command/arguments"
|
2021-05-17 15:33:17 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
2021-05-17 13:40:40 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/providers"
|
2021-05-17 15:43:35 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2021-02-11 20:52:10 -05:00
|
|
|
"github.com/hashicorp/terraform/internal/terminal"
|
2021-05-17 15:46:19 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/terraform"
|
2017-03-01 17:16:22 -05:00
|
|
|
)
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
func testUiHookResourceID(addr addrs.AbsResourceInstance) terraform.HookResourceIdentity {
|
|
|
|
|
return terraform.HookResourceIdentity{
|
|
|
|
|
Addr: addr,
|
|
|
|
|
ProviderAddr: addrs.Provider{
|
|
|
|
|
Type: "test",
|
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
|
Hostname: "example.com",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 11:33:03 -05:00
|
|
|
// Test the PreApply hook for creating a new resource
|
|
|
|
|
func TestUiHookPreApply_create(t *testing.T) {
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2021-02-16 11:33:03 -05:00
|
|
|
h.resources = map[string]uiResourceState{
|
|
|
|
|
"test_instance.foo": {
|
|
|
|
|
Op: uiResourceCreate,
|
|
|
|
|
Start: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
priorState := cty.NullVal(cty.Object(map[string]cty.Type{
|
|
|
|
|
"id": cty.String,
|
|
|
|
|
"bar": cty.List(cty.String),
|
|
|
|
|
}))
|
|
|
|
|
plannedNewState := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
"bar": cty.ListVal([]cty.Value{
|
|
|
|
|
cty.StringVal("baz"),
|
|
|
|
|
}),
|
|
|
|
|
})
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PreApply(testUiHookResourceID(addr), addrs.NotDeposed, plans.Create, priorState, plannedNewState)
|
2021-02-16 11:33:03 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stop the background writer
|
|
|
|
|
uiState := h.resources[addr.String()]
|
|
|
|
|
close(uiState.DoneCh)
|
|
|
|
|
<-uiState.done
|
|
|
|
|
|
|
|
|
|
expectedOutput := "test_instance.foo: Creating...\n"
|
|
|
|
|
result := done(t)
|
|
|
|
|
output := result.Stdout()
|
|
|
|
|
if output != expectedOutput {
|
|
|
|
|
t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedErrOutput := ""
|
|
|
|
|
errOutput := result.Stderr()
|
|
|
|
|
if errOutput != expectedErrOutput {
|
|
|
|
|
t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test the PreApply hook's use of a periodic timer to display "still working"
|
|
|
|
|
// log lines
|
2017-03-13 16:09:25 -04:00
|
|
|
func TestUiHookPreApply_periodicTimer(t *testing.T) {
|
2025-08-18 10:39:49 -04:00
|
|
|
syncTest, streams, done := streamableSyncTest(t)
|
|
|
|
|
syncTest(t, func(t *testing.T) {
|
|
|
|
|
view := NewView(streams)
|
|
|
|
|
h := NewUiHook(view)
|
|
|
|
|
h.periodicUiTimer = 1 * time.Second
|
|
|
|
|
h.resources = map[string]uiResourceState{
|
|
|
|
|
"test_instance.foo": {
|
|
|
|
|
Op: uiResourceModify,
|
|
|
|
|
Start: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
}
|
2017-03-13 16:09:25 -04:00
|
|
|
|
2025-08-18 10:39:49 -04:00
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
|
2025-08-18 10:39:49 -04:00
|
|
|
priorState := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
"bar": cty.ListValEmpty(cty.String),
|
|
|
|
|
})
|
|
|
|
|
plannedNewState := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
"bar": cty.ListVal([]cty.Value{
|
|
|
|
|
cty.StringVal("baz"),
|
|
|
|
|
}),
|
|
|
|
|
})
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
|
2025-08-18 10:39:49 -04:00
|
|
|
action, err := h.PreApply(testUiHookResourceID(addr), addrs.NotDeposed, plans.Update, priorState, plannedNewState)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
2017-03-13 16:09:25 -04:00
|
|
|
|
2025-08-18 10:39:49 -04:00
|
|
|
time.Sleep(3005 * time.Millisecond)
|
2017-03-13 16:09:25 -04:00
|
|
|
|
2025-08-18 10:39:49 -04:00
|
|
|
// stop the background writer
|
|
|
|
|
uiState := h.resources[addr.String()]
|
|
|
|
|
close(uiState.DoneCh)
|
|
|
|
|
<-uiState.done
|
2017-03-17 11:04:27 -04:00
|
|
|
|
2025-08-18 10:39:49 -04:00
|
|
|
expectedOutput := `test_instance.foo: Modifying... [id=test]
|
2025-03-05 05:40:57 -05:00
|
|
|
test_instance.foo: Still modifying... [id=test, 00m01s elapsed]
|
|
|
|
|
test_instance.foo: Still modifying... [id=test, 00m02s elapsed]
|
|
|
|
|
test_instance.foo: Still modifying... [id=test, 00m03s elapsed]
|
2017-03-13 16:09:25 -04:00
|
|
|
`
|
2025-08-18 10:39:49 -04:00
|
|
|
result := done(t)
|
|
|
|
|
output := result.Stdout()
|
|
|
|
|
if output != expectedOutput {
|
|
|
|
|
t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedErrOutput := ""
|
|
|
|
|
errOutput := result.Stderr()
|
|
|
|
|
if errOutput != expectedErrOutput {
|
|
|
|
|
t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput)
|
|
|
|
|
}
|
|
|
|
|
})
|
2017-03-13 16:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
2021-02-16 11:33:03 -05:00
|
|
|
// Test the PreApply hook's destroy path, including passing a deposed key as
|
|
|
|
|
// the gen argument.
|
2017-03-13 16:09:25 -04:00
|
|
|
func TestUiHookPreApply_destroy(t *testing.T) {
|
2021-02-11 20:52:10 -05:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2017-03-13 16:09:25 -04:00
|
|
|
h.resources = map[string]uiResourceState{
|
2021-02-16 11:33:03 -05:00
|
|
|
"test_instance.foo": {
|
2017-03-13 16:09:25 -04:00
|
|
|
Op: uiResourceDestroy,
|
|
|
|
|
Start: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
addr := addrs.Resource{
|
2021-02-16 11:33:03 -05:00
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
2018-10-13 13:47:46 -04:00
|
|
|
priorState := cty.ObjectVal(map[string]cty.Value{
|
2021-02-16 11:33:03 -05:00
|
|
|
"id": cty.StringVal("abc123"),
|
|
|
|
|
"verbs": cty.ListVal([]cty.Value{
|
|
|
|
|
cty.StringVal("boop"),
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
}),
|
|
|
|
|
})
|
2018-10-13 13:47:46 -04:00
|
|
|
plannedNewState := cty.NullVal(cty.Object(map[string]cty.Type{
|
|
|
|
|
"id": cty.String,
|
2021-02-16 11:33:03 -05:00
|
|
|
"verbs": cty.List(cty.String),
|
2018-10-13 13:47:46 -04:00
|
|
|
}))
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
|
2021-02-16 11:33:03 -05:00
|
|
|
key := states.NewDeposedKey()
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PreApply(testUiHookResourceID(addr), key, plans.Delete, priorState, plannedNewState)
|
2017-03-13 16:09:25 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
// stop the background writer
|
|
|
|
|
uiState := h.resources[addr.String()]
|
|
|
|
|
close(uiState.DoneCh)
|
|
|
|
|
<-uiState.done
|
|
|
|
|
|
2021-02-11 20:52:10 -05:00
|
|
|
result := done(t)
|
2021-05-12 18:18:25 -04:00
|
|
|
expectedOutput := fmt.Sprintf("test_instance.foo (deposed object %s): Destroying... [id=abc123]\n", key)
|
2021-02-11 20:52:10 -05:00
|
|
|
output := result.Stdout()
|
2017-03-13 16:09:25 -04:00
|
|
|
if output != expectedOutput {
|
|
|
|
|
t.Fatalf("Output didn't match.\nExpected: %q\nGiven: %q", expectedOutput, output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedErrOutput := ""
|
2021-02-11 20:52:10 -05:00
|
|
|
errOutput := result.Stderr()
|
2017-03-13 16:09:25 -04:00
|
|
|
if errOutput != expectedErrOutput {
|
|
|
|
|
t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 11:33:03 -05:00
|
|
|
// Verify that colorize is called on format strings, not user input, by adding
|
|
|
|
|
// valid color codes as resource names and IDs.
|
|
|
|
|
func TestUiHookPostApply_colorInterpolation(t *testing.T) {
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
|
|
|
|
view.Configure(&arguments.View{NoColor: false})
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2021-02-16 11:33:03 -05:00
|
|
|
h.resources = map[string]uiResourceState{
|
|
|
|
|
"test_instance.foo[\"[red]\"]": {
|
|
|
|
|
Op: uiResourceCreate,
|
|
|
|
|
Start: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.StringKey("[red]")).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
newState := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("[blue]"),
|
|
|
|
|
})
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PostApply(testUiHookResourceID(addr), addrs.NotDeposed, newState, nil)
|
2021-02-16 11:33:03 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
reset := "\x1b[0m"
|
|
|
|
|
bold := "\x1b[1m"
|
|
|
|
|
wantPrefix := reset + bold + `test_instance.foo["[red]"]: Creation complete after`
|
|
|
|
|
wantSuffix := "[id=[blue]]" + reset + "\n"
|
|
|
|
|
output := result.Stdout()
|
|
|
|
|
|
|
|
|
|
if !strings.HasPrefix(output, wantPrefix) {
|
|
|
|
|
t.Fatalf("wrong output prefix\n got: %#v\nwant: %#v", output, wantPrefix)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !strings.HasSuffix(output, wantSuffix) {
|
|
|
|
|
t.Fatalf("wrong output suffix\n got: %#v\nwant: %#v", output, wantSuffix)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedErrOutput := ""
|
|
|
|
|
errOutput := result.Stderr()
|
|
|
|
|
if errOutput != expectedErrOutput {
|
|
|
|
|
t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that the PostApply hook renders a total time.
|
2017-03-13 16:09:25 -04:00
|
|
|
func TestUiHookPostApply_emptyState(t *testing.T) {
|
2021-02-11 20:52:10 -05:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2017-03-13 16:09:25 -04:00
|
|
|
h.resources = map[string]uiResourceState{
|
2021-02-16 11:33:03 -05:00
|
|
|
"data.google_compute_zones.available": {
|
2017-03-13 16:09:25 -04:00
|
|
|
Op: uiResourceDestroy,
|
|
|
|
|
Start: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 17:24:45 -04:00
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
|
Type: "google_compute_zones",
|
|
|
|
|
Name: "available",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
newState := cty.NullVal(cty.Object(map[string]cty.Type{
|
|
|
|
|
"id": cty.String,
|
|
|
|
|
"names": cty.List(cty.String),
|
|
|
|
|
}))
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PostApply(testUiHookResourceID(addr), addrs.NotDeposed, newState, nil)
|
2017-03-13 16:09:25 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
2021-02-11 20:52:10 -05:00
|
|
|
result := done(t)
|
2017-03-13 16:09:25 -04:00
|
|
|
|
2019-01-31 07:25:57 -05:00
|
|
|
expectedRegexp := "^data.google_compute_zones.available: Destruction complete after -?[a-z0-9µ.]+\n$"
|
2021-02-11 20:52:10 -05:00
|
|
|
output := result.Stdout()
|
2017-07-13 16:01:19 -04:00
|
|
|
if matched, _ := regexp.MatchString(expectedRegexp, output); !matched {
|
|
|
|
|
t.Fatalf("Output didn't match regexp.\nExpected: %q\nGiven: %q", expectedRegexp, output)
|
2017-03-13 16:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expectedErrOutput := ""
|
2021-02-11 20:52:10 -05:00
|
|
|
errOutput := result.Stderr()
|
2017-03-13 16:09:25 -04:00
|
|
|
if errOutput != expectedErrOutput {
|
|
|
|
|
t.Fatalf("Error output didn't match.\nExpected: %q\nGiven: %q", expectedErrOutput, errOutput)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 22:16:01 -05:00
|
|
|
func TestUiHookPreProvisionInstanceStep(t *testing.T) {
|
2021-02-16 11:33:03 -05:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PreProvisionInstanceStep(testUiHookResourceID(addr), "local-exec")
|
2021-02-16 11:33:03 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
if got, want := result.Stdout(), "test_instance.foo: Provisioning with 'local-exec'...\n"; got != want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test ProvisionOutput, including lots of edge cases for the output
|
|
|
|
|
// whitespace/line ending logic.
|
2024-02-27 22:16:01 -05:00
|
|
|
func TestUiHookProvisionOutput(t *testing.T) {
|
2021-02-16 11:33:03 -05:00
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
|
provisioner string
|
|
|
|
|
input string
|
|
|
|
|
wantOutput string
|
|
|
|
|
}{
|
|
|
|
|
"single line": {
|
|
|
|
|
"local-exec",
|
|
|
|
|
"foo\n",
|
|
|
|
|
"test_instance.foo (local-exec): foo\n",
|
|
|
|
|
},
|
|
|
|
|
"multiple lines": {
|
|
|
|
|
"x",
|
|
|
|
|
`foo
|
|
|
|
|
bar
|
|
|
|
|
baz
|
|
|
|
|
`,
|
|
|
|
|
`test_instance.foo (x): foo
|
|
|
|
|
test_instance.foo (x): bar
|
|
|
|
|
test_instance.foo (x): baz
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
"trailing whitespace": {
|
|
|
|
|
"x",
|
|
|
|
|
"foo \nbar\n",
|
|
|
|
|
"test_instance.foo (x): foo\ntest_instance.foo (x): bar\n",
|
|
|
|
|
},
|
|
|
|
|
"blank lines": {
|
|
|
|
|
"x",
|
|
|
|
|
"foo\n\nbar\n\n\nbaz\n",
|
|
|
|
|
`test_instance.foo (x): foo
|
|
|
|
|
test_instance.foo (x): bar
|
|
|
|
|
test_instance.foo (x): baz
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
"no final newline": {
|
|
|
|
|
"x",
|
|
|
|
|
`foo
|
|
|
|
|
bar`,
|
|
|
|
|
`test_instance.foo (x): foo
|
|
|
|
|
test_instance.foo (x): bar
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
"CR, no LF": {
|
|
|
|
|
"MacOS 9?",
|
|
|
|
|
"foo\rbar\r",
|
|
|
|
|
`test_instance.foo (MacOS 9?): foo
|
|
|
|
|
test_instance.foo (MacOS 9?): bar
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
"CRLF": {
|
|
|
|
|
"winrm",
|
|
|
|
|
"foo\r\nbar\r\n",
|
|
|
|
|
`test_instance.foo (winrm): foo
|
|
|
|
|
test_instance.foo (winrm): bar
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
h.ProvisionOutput(testUiHookResourceID(addr), tc.provisioner, tc.input)
|
2021-02-16 11:33:03 -05:00
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
if got := result.Stdout(); got != tc.wantOutput {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, tc.wantOutput)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test the PreRefresh hook in the normal path where the resource exists with
|
|
|
|
|
// an ID key and value in the state.
|
2024-02-27 22:16:01 -05:00
|
|
|
func TestUiHookPreRefresh(t *testing.T) {
|
2021-02-16 11:33:03 -05:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
priorState := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
"bar": cty.ListValEmpty(cty.String),
|
|
|
|
|
})
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PreRefresh(testUiHookResourceID(addr), addrs.NotDeposed, priorState)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
if got, want := result.Stdout(), "test_instance.foo: Refreshing state... [id=test]\n"; got != want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test that PreRefresh still works if no ID key and value can be determined
|
|
|
|
|
// from state.
|
2024-02-27 22:16:01 -05:00
|
|
|
func TestUiHookPreRefresh_noID(t *testing.T) {
|
2021-02-16 11:33:03 -05:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
priorState := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"bar": cty.ListValEmpty(cty.String),
|
|
|
|
|
})
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PreRefresh(testUiHookResourceID(addr), addrs.NotDeposed, priorState)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
if got, want := result.Stdout(), "test_instance.foo: Refreshing state...\n"; got != want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test the very simple PreImportState hook.
|
2024-02-27 22:16:01 -05:00
|
|
|
func TestUiHookPreImportState(t *testing.T) {
|
2021-02-16 11:33:03 -05:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PreImportState(testUiHookResourceID(addr), "test")
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
if got, want := result.Stdout(), "test_instance.foo: Importing from ID \"test\"...\n"; got != want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-16 09:24:24 -04:00
|
|
|
func TestUiHookPreApplyImport(t *testing.T) {
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
|
importingSrc plans.ImportingSrc
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
"id": {
|
|
|
|
|
importingSrc: plans.ImportingSrc{
|
|
|
|
|
ID: "test",
|
|
|
|
|
},
|
|
|
|
|
want: "test_instance.foo: Importing... [id=test]\n",
|
|
|
|
|
},
|
|
|
|
|
"identity": {
|
|
|
|
|
importingSrc: plans.ImportingSrc{
|
|
|
|
|
Identity: mustNewDynamicValue(
|
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
}),
|
|
|
|
|
cty.Object(map[string]cty.Type{
|
|
|
|
|
"id": cty.String,
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
want: "test_instance.foo: Importing... [identity=id=test]\n",
|
|
|
|
|
},
|
|
|
|
|
"identity type error": {
|
|
|
|
|
importingSrc: plans.ImportingSrc{
|
|
|
|
|
Identity: mustNewDynamicValue(
|
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
}),
|
|
|
|
|
cty.DynamicPseudoType,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
want: "test_instance.foo: Importing... [identity=(type error)]\n",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
|
|
|
|
h := NewUiHook(view)
|
|
|
|
|
|
|
|
|
|
action, err := h.PreApplyImport(testUiHookResourceID(addr), tc.importingSrc)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
got := result.Stdout()
|
|
|
|
|
|
|
|
|
|
if got != tc.want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, tc.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUiHookPostApplyImport(t *testing.T) {
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
|
importingSrc plans.ImportingSrc
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
"id": {
|
|
|
|
|
importingSrc: plans.ImportingSrc{
|
|
|
|
|
ID: "test",
|
|
|
|
|
},
|
|
|
|
|
want: "test_instance.foo: Import complete [id=test]\n",
|
|
|
|
|
},
|
|
|
|
|
"identity": {
|
|
|
|
|
importingSrc: plans.ImportingSrc{
|
|
|
|
|
Identity: mustNewDynamicValue(
|
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
}),
|
|
|
|
|
cty.Object(map[string]cty.Type{
|
|
|
|
|
"id": cty.String,
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
want: "test_instance.foo: Import complete [identity=id=test]\n",
|
|
|
|
|
},
|
|
|
|
|
"identity type error": {
|
|
|
|
|
importingSrc: plans.ImportingSrc{
|
|
|
|
|
Identity: mustNewDynamicValue(
|
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
}),
|
|
|
|
|
cty.DynamicPseudoType,
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
want: "test_instance.foo: Import complete [identity=(type error)]\n",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
|
|
|
|
h := NewUiHook(view)
|
|
|
|
|
|
|
|
|
|
action, err := h.PostApplyImport(testUiHookResourceID(addr), tc.importingSrc)
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
got := result.Stdout()
|
|
|
|
|
|
|
|
|
|
if got != tc.want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, tc.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 11:33:03 -05:00
|
|
|
// Test the PostImportState UI hook. Again, this hook behaviour seems odd to
|
|
|
|
|
// me (see below), so please don't consider these tests as justification for
|
|
|
|
|
// keeping this behaviour.
|
2024-02-27 22:16:01 -05:00
|
|
|
func TestUiHookPostImportState(t *testing.T) {
|
2021-02-16 11:33:03 -05:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
2023-05-10 05:00:45 -04:00
|
|
|
h := NewUiHook(view)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
// The "Prepared [...] for import" lines display the type name of each of
|
|
|
|
|
// the imported resources passed to the hook. I'm not sure how it's
|
|
|
|
|
// possible for an import to result in a different resource type name than
|
|
|
|
|
// the target address, but the hook works like this so we're covering it.
|
|
|
|
|
imported := []providers.ImportedResource{
|
|
|
|
|
{
|
|
|
|
|
TypeName: "test_some_instance",
|
|
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
TypeName: "test_other_instance",
|
|
|
|
|
State: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"id": cty.StringVal("test"),
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
Refactor terraform.Hook to use a resource-identifying wrapper struct
The terraform.Hook interface lets other areas of code perform streaming
reactions to various events, generally in the service of some UI somewhere.
Nearly all of the methods on this interface take an `addrs.AbsResourceInstance`
as their first argument, to identify the resource that's being operated on.
However, that addrs struct doesn't necessarily contain everything you might want
in order to uniquely and usefully identify a resource. It has the module
instance and resource instance addresses, but it lacks the provider source
address, which can affect how the consuming UI should display the resource's
events. (For example, Terraform Cloud wants reliable info about who maintains a
given provider, what cloud provider it operates on, and where to find its
documentation.)
Instead of polluting `addrs.AbsResourceInstance` with extra information that
isn't relevant to other call sites, let's change the first argument of each Hook
method to be a wrapper struct defined in the package that owns the Hook
interface, and add the provider address to that wrapper as a sibling of the
resource address. This causes a big noisy commit today, but should streamline
future updates to the UI-facing "identity" of a resource; existing callers can
ignore any new fields they're uninterested in, or exploit new info as needed.
Other than making new information available for future edits to Hook
implementing types, this commit should have no effect on existing behavior.
2024-02-27 20:42:17 -05:00
|
|
|
action, err := h.PostImportState(testUiHookResourceID(addr), imported)
|
2021-02-16 11:33:03 -05:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
want := `test_instance.foo: Import prepared!
|
|
|
|
|
Prepared test_some_instance for import
|
|
|
|
|
Prepared test_other_instance for import
|
|
|
|
|
`
|
|
|
|
|
if got := result.Stdout(); got != want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-30 08:44:02 -04:00
|
|
|
func TestUiHookEphemeralOp(t *testing.T) {
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
|
|
|
|
h := NewUiHook(view)
|
|
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.EphemeralResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
action, err := h.PreEphemeralOp(testUiHookResourceID(addr), plans.Close)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
action, err = h.PostEphemeralOp(testUiHookResourceID(addr), plans.Close, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
want := `ephemeral.test_instance.foo: Closing...
|
|
|
|
|
ephemeral.test_instance.foo: Closing complete after 0s
|
|
|
|
|
`
|
|
|
|
|
if got := result.Stdout(); got != want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUiHookEphemeralOp_progress(t *testing.T) {
|
2025-08-18 10:39:49 -04:00
|
|
|
syncTest, streams, done := streamableSyncTest(t)
|
|
|
|
|
syncTest(t, func(t *testing.T) {
|
|
|
|
|
view := NewView(streams)
|
|
|
|
|
h := NewUiHook(view)
|
|
|
|
|
h.periodicUiTimer = 1 * time.Second
|
|
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.EphemeralResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
action, err := h.PreEphemeralOp(testUiHookResourceID(addr), plans.Open)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
time.Sleep(2005 * time.Millisecond)
|
|
|
|
|
elapsed := time.Since(start).Round(time.Second)
|
|
|
|
|
|
|
|
|
|
action, err = h.PostEphemeralOp(testUiHookResourceID(addr), plans.Open, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := done(t)
|
|
|
|
|
stdout := result.Stdout()
|
|
|
|
|
|
|
|
|
|
// we do not test for equality because time.Sleep can take longer than declared time
|
|
|
|
|
wantPrefix := `ephemeral.test_instance.foo: Opening...
|
2025-03-05 05:40:57 -05:00
|
|
|
ephemeral.test_instance.foo: Still opening... [00m01s elapsed]
|
|
|
|
|
ephemeral.test_instance.foo: Still opening... [00m02s elapsed]`
|
2025-08-18 10:39:49 -04:00
|
|
|
if !strings.HasPrefix(stdout, wantPrefix) {
|
|
|
|
|
t.Fatalf("unexpected prefix\n got: %q\nwant: %q", stdout, wantPrefix)
|
|
|
|
|
}
|
|
|
|
|
wantSuffix := fmt.Sprintf(`ephemeral.test_instance.foo: Opening complete after %s
|
2024-12-09 05:23:55 -05:00
|
|
|
`, elapsed)
|
2025-08-18 10:39:49 -04:00
|
|
|
if !strings.HasSuffix(stdout, wantSuffix) {
|
|
|
|
|
t.Fatalf("unexpected prefix\n got: %q\nwant: %q", stdout, wantSuffix)
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-10-30 08:44:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestUiHookEphemeralOp_error(t *testing.T) {
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
view := NewView(streams)
|
|
|
|
|
h := NewUiHook(view)
|
|
|
|
|
|
|
|
|
|
addr := addrs.Resource{
|
|
|
|
|
Mode: addrs.EphemeralResourceMode,
|
|
|
|
|
Type: "test_instance",
|
|
|
|
|
Name: "foo",
|
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
|
|
|
|
|
|
|
|
|
|
action, err := h.PreEphemeralOp(testUiHookResourceID(addr), plans.Close)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
action, err = h.PostEphemeralOp(testUiHookResourceID(addr), plans.Close, errors.New("test error"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if action != terraform.HookActionContinue {
|
|
|
|
|
t.Fatalf("Expected hook to continue, given: %#v", action)
|
|
|
|
|
}
|
|
|
|
|
result := done(t)
|
|
|
|
|
|
|
|
|
|
want := `ephemeral.test_instance.foo: Closing...
|
|
|
|
|
`
|
|
|
|
|
if got := result.Stdout(); got != want {
|
|
|
|
|
t.Fatalf("unexpected output\n got: %q\nwant: %q", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 17:16:22 -05:00
|
|
|
func TestTruncateId(t *testing.T) {
|
|
|
|
|
testCases := []struct {
|
|
|
|
|
Input string
|
|
|
|
|
Expected string
|
|
|
|
|
MaxLen int
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "H...d",
|
|
|
|
|
MaxLen: 3,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "H...d",
|
|
|
|
|
MaxLen: 5,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "He...d",
|
|
|
|
|
MaxLen: 6,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "He...ld",
|
|
|
|
|
MaxLen: 7,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "Hel...ld",
|
|
|
|
|
MaxLen: 8,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "Hel...rld",
|
|
|
|
|
MaxLen: 9,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "Hell...rld",
|
|
|
|
|
MaxLen: 10,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "Hello world",
|
|
|
|
|
MaxLen: 11,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "Hello world",
|
|
|
|
|
Expected: "Hello world",
|
|
|
|
|
MaxLen: 12,
|
|
|
|
|
},
|
2018-09-09 11:21:24 -04:00
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あ...さ",
|
|
|
|
|
MaxLen: 3,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あ...さ",
|
|
|
|
|
MaxLen: 5,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あい...さ",
|
|
|
|
|
MaxLen: 6,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あい...こさ",
|
|
|
|
|
MaxLen: 7,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あいう...こさ",
|
|
|
|
|
MaxLen: 8,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あいう...けこさ",
|
|
|
|
|
MaxLen: 9,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あいうえ...けこさ",
|
|
|
|
|
MaxLen: 10,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あいうえおかきくけこさ",
|
|
|
|
|
MaxLen: 11,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Input: "あいうえおかきくけこさ",
|
|
|
|
|
Expected: "あいうえおかきくけこさ",
|
|
|
|
|
MaxLen: 12,
|
|
|
|
|
},
|
2017-03-01 17:16:22 -05:00
|
|
|
}
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
|
testName := fmt.Sprintf("%d", i)
|
|
|
|
|
t.Run(testName, func(t *testing.T) {
|
|
|
|
|
out := truncateId(tc.Input, tc.MaxLen)
|
|
|
|
|
if out != tc.Expected {
|
|
|
|
|
t.Fatalf("Expected %q to be shortened to %d as %q (given: %q)",
|
|
|
|
|
tc.Input, tc.MaxLen, tc.Expected, out)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-16 09:24:24 -04:00
|
|
|
|
|
|
|
|
func mustNewDynamicValue(val cty.Value, ty cty.Type) plans.DynamicValue {
|
|
|
|
|
ret, err := plans.NewDynamicValue(val, ty)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|