@@ -11,10 +11,12 @@ import (
1111 "strings"
1212 "testing"
1313
14+ "github.com/google/go-cmp/cmp"
1415 "github.com/hashicorp/terraform/internal/e2e"
1516 "github.com/hashicorp/terraform/internal/getproviders"
1617)
1718
19+ // Tests using `terraform workspace` commands in combination with pluggable state storage.
1820func TestPrimary_stateStore_workspaceCmd (t * testing.T ) {
1921 if ! canRunGoBuild {
2022 // We're running in a separate-build-then-run context, so we can't
@@ -119,3 +121,79 @@ func TestPrimary_stateStore_workspaceCmd(t *testing.T) {
119121 t .Errorf ("unexpected output, expected %q, but got:\n %s" , expectedMsg , stdout )
120122 }
121123}
124+
125+ // Tests using `terraform state` subcommands in combination with pluggable state storage:
126+ // > `terraform state show`
127+ // > `terraform state list`
128+ func TestPrimary_stateStore_stateCmds (t * testing.T ) {
129+
130+ if ! canRunGoBuild {
131+ // We're running in a separate-build-then-run context, so we can't
132+ // currently execute this test which depends on being able to build
133+ // new executable at runtime.
134+ //
135+ // (See the comment on canRunGoBuild's declaration for more information.)
136+ t .Skip ("can't run without building a new provider executable" )
137+ }
138+
139+ t .Setenv (e2e .TestExperimentFlag , "true" )
140+ tfBin := e2e .GoBuild ("github.com/hashicorp/terraform" , "terraform" )
141+
142+ fixturePath := filepath .Join ("testdata" , "initialized-directory-with-state-store-fs" )
143+ tf := e2e .NewBinary (t , tfBin , fixturePath )
144+
145+ workspaceDirName := "states" // see test fixture value for workspace_dir
146+
147+ // In order to test integration with PSS we need a provider plugin implementing a state store.
148+ // Here will build the simple6 (built with protocol v6) provider, which implements PSS.
149+ simple6Provider := filepath .Join (tf .WorkDir (), "terraform-provider-simple6" )
150+ simple6ProviderExe := e2e .GoBuild ("github.com/hashicorp/terraform/internal/provider-simple-v6/main" , simple6Provider )
151+
152+ // Move the provider binaries into the correct directory .terraform/providers/ directory
153+ // that will contain provider binaries in an initialized working directory.
154+ platform := getproviders .CurrentPlatform .String ()
155+ if err := os .MkdirAll (tf .Path (".terraform/providers/registry.terraform.io/hashicorp/simple6/0.0.1/" , platform ), os .ModePerm ); err != nil {
156+ t .Fatal (err )
157+ }
158+ if err := os .Rename (simple6ProviderExe , tf .Path (".terraform/providers/registry.terraform.io/hashicorp/simple6/0.0.1/" , platform , "terraform-provider-simple6" )); err != nil {
159+ t .Fatal (err )
160+ }
161+
162+ // Assert that the test starts with the default state present from test fixtures
163+ defaultStateId := "default"
164+ fi , err := os .Stat (path .Join (tf .WorkDir (), workspaceDirName , defaultStateId , "terraform.tfstate" ))
165+ if err != nil {
166+ t .Fatalf ("failed to open default workspace's state file: %s" , err )
167+ }
168+ if fi .Size () == 0 {
169+ t .Fatal ("default workspace's state file should not have size 0 bytes" )
170+ }
171+
172+ //// List State: terraform state list
173+ expectedResourceAddr := "terraform_data.my-data"
174+ stdout , stderr , err := tf .Run ("state" , "list" , "-no-color" )
175+ if err != nil {
176+ t .Fatalf ("unexpected error: %s\n stderr:\n %s" , err , stderr )
177+ }
178+ expectedMsg := expectedResourceAddr + "\n " // This is the only resource instance in the test fixture state
179+ if stdout != expectedMsg {
180+ t .Errorf ("unexpected output, expected %q, but got:\n %s" , expectedMsg , stdout )
181+ }
182+
183+ //// Show State: terraform state show
184+ stdout , stderr , err = tf .Run ("state" , "show" , expectedResourceAddr , "-no-color" )
185+ if err != nil {
186+ t .Fatalf ("unexpected error: %s\n stderr:\n %s" , err , stderr )
187+ }
188+ // show displays the state for the specified resource
189+ expectedMsg = `# terraform_data.my-data:
190+ resource "terraform_data" "my-data" {
191+ id = "d71fb368-2ba1-fb4c-5bd9-6a2b7f05d60c"
192+ input = "hello world"
193+ output = "hello world"
194+ }
195+ `
196+ if diff := cmp .Diff (stdout , expectedMsg ); diff != "" {
197+ t .Errorf ("wrong result, diff:\n %s" , diff )
198+ }
199+ }
0 commit comments