Skip to content

Commit 28e61ce

Browse files
committed
test: Add command-level test for state identities showing integration with pluggable state storage code.
1 parent 3ff0518 commit 28e61ce

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

internal/command/state_identities_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
package command
55

66
import (
7+
"bytes"
78
"encoding/json"
89
"os"
910
"path/filepath"
1011
"reflect"
1112
"testing"
1213

1314
"github.com/hashicorp/cli"
15+
"github.com/hashicorp/terraform/internal/addrs"
16+
"github.com/hashicorp/terraform/internal/providers"
17+
"github.com/hashicorp/terraform/internal/states/statefile"
1418
)
1519

1620
func TestStateIdentities(t *testing.T) {
@@ -423,3 +427,73 @@ func TestStateIdentities_modules(t *testing.T) {
423427
})
424428

425429
}
430+
431+
func TestStateIdentities_stateStore(t *testing.T) {
432+
// We need configuration present to force pluggable state storage to be used
433+
td := t.TempDir()
434+
testCopyDir(t, testFixturePath("state-identities-state-store"), td)
435+
t.Chdir(td)
436+
437+
// Get a state file, that contains identity information,as bytes
438+
state := testStateWithIdentity()
439+
var stateBuf bytes.Buffer
440+
if err := statefile.Write(statefile.New(state, "", 1), &stateBuf); err != nil {
441+
t.Fatalf("error during test setup: %s", err)
442+
}
443+
stateBytes := stateBuf.Bytes()
444+
445+
// Create a mock that contains a persisted "default" state that uses the bytes from above.
446+
mockProvider := mockPluggableStateStorageProvider(t)
447+
mockProvider.MockStates = map[string]interface{}{
448+
"default": stateBytes,
449+
}
450+
mockProviderAddress := addrs.NewDefaultProvider("test")
451+
providerSource, close := newMockProviderSource(t, map[string][]string{
452+
"hashicorp/test": {"1.0.0"},
453+
})
454+
defer close()
455+
456+
ui := cli.NewMockUi()
457+
c := &StateIdentitiesCommand{
458+
Meta: Meta{
459+
AllowExperimentalFeatures: true,
460+
testingOverrides: &testingOverrides{
461+
Providers: map[addrs.Provider]providers.Factory{
462+
mockProviderAddress: providers.FactoryFixed(mockProvider),
463+
},
464+
},
465+
ProviderSource: providerSource,
466+
Ui: ui,
467+
},
468+
}
469+
470+
args := []string{"-json"}
471+
if code := c.Run(args); code != 0 {
472+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
473+
}
474+
475+
// Test that outputs were displayed
476+
expected := `{
477+
"test_instance.bar": {
478+
"id": "my-bar-id"
479+
},
480+
"test_instance.foo": {
481+
"id": "my-foo-id"
482+
}
483+
}
484+
`
485+
actual := ui.OutputWriter.String()
486+
487+
// Normalize JSON strings
488+
var expectedJSON, actualJSON map[string]interface{}
489+
if err := json.Unmarshal([]byte(expected), &expectedJSON); err != nil {
490+
t.Fatalf("Failed to unmarshal expected JSON: %s", err)
491+
}
492+
if err := json.Unmarshal([]byte(actual), &actualJSON); err != nil {
493+
t.Fatalf("Failed to unmarshal actual JSON: %s", err)
494+
}
495+
496+
if !reflect.DeepEqual(expectedJSON, actualJSON) {
497+
t.Fatalf("Expected:\n%q\n\nTo equal: %q", expected, actual)
498+
}
499+
}

internal/command/testdata/state-identities-state-store/.terraform.lock.hcl

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": 3,
3+
"serial": 0,
4+
"lineage": "666f9301-7e65-4b19-ae23-71184bb19b03",
5+
"state_store": {
6+
"type": "test_store",
7+
"config": {
8+
"value": "foobar"
9+
},
10+
"provider": {
11+
"version": "1.2.3",
12+
"source": "registry.terraform.io/hashicorp/test",
13+
"config": {
14+
"region": null
15+
}
16+
},
17+
"hash": 4158988729
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
terraform {
2+
required_providers {
3+
test = {
4+
source = "registry.terraform.io/hashicorp/test"
5+
}
6+
}
7+
8+
state_store "test_store" {
9+
provider "test" {}
10+
11+
value = "foobar"
12+
}
13+
}

0 commit comments

Comments
 (0)