@@ -31,6 +31,7 @@ import (
3131 "github.com/zclconf/go-cty/cty"
3232
3333 backendInit "github.com/hashicorp/terraform/internal/backend/init"
34+ "github.com/hashicorp/terraform/internal/backend/local"
3435 backendLocal "github.com/hashicorp/terraform/internal/backend/local"
3536 backendInmem "github.com/hashicorp/terraform/internal/backend/remote-state/inmem"
3637)
@@ -2520,6 +2521,106 @@ func TestMeta_getStateStoreProviderFactory(t *testing.T) {
25202521 })
25212522}
25222523
2524+ func TestMetaBackend_prepareBackend (t * testing.T ) {
2525+ t .Run ("it returns a cloud backend from cloud backend config" , func (t * testing.T ) {
2526+ // Create a temporary working directory with cloud configuration in
2527+ td := t .TempDir ()
2528+ testCopyDir (t , testFixturePath ("cloud-config" ), td )
2529+ t .Chdir (td )
2530+
2531+ m := testMetaBackend (t , nil )
2532+
2533+ // Get the cloud config
2534+ mod , loadDiags := m .loadSingleModule (td )
2535+ if loadDiags .HasErrors () {
2536+ t .Fatalf ("unexpected error when loading test config: %s" , loadDiags .Err ())
2537+ }
2538+
2539+ // We cannot initialize a cloud backend so we instead check
2540+ // the init error is referencing HCP Terraform
2541+ _ , bDiags := m .prepareBackend (mod )
2542+ if ! bDiags .HasErrors () {
2543+ t .Fatal ("expected error but got none" )
2544+ }
2545+ wantErr := "HCP Terraform or Terraform Enterprise initialization required: please run \" terraform init\" "
2546+ if ! strings .Contains (bDiags .Err ().Error (), wantErr ) {
2547+ t .Fatalf ("expected error to contain %q, but got: %q" ,
2548+ wantErr ,
2549+ bDiags .Err ())
2550+ }
2551+ })
2552+ t .Run ("it returns a backend from backend config" , func (t * testing.T ) {
2553+ // Create a temporary working directory with backend configuration in
2554+ td := t .TempDir ()
2555+ testCopyDir (t , testFixturePath ("backend-unchanged" ), td )
2556+ t .Chdir (td )
2557+
2558+ m := testMetaBackend (t , nil )
2559+
2560+ // Get the backend config
2561+ mod , loadDiags := m .loadSingleModule (td )
2562+ if loadDiags .HasErrors () {
2563+ t .Fatalf ("unexpected error when loading test config: %s" , loadDiags .Err ())
2564+ }
2565+
2566+ b , bDiags := m .prepareBackend (mod )
2567+ if bDiags .HasErrors () {
2568+ t .Fatal ("unexpected error: " , bDiags .Err ())
2569+ }
2570+
2571+ if _ , ok := b .(* local.Local ); ! ok {
2572+ t .Fatal ("expected returned operations backend to be a Local backend" )
2573+ }
2574+ })
2575+
2576+ t .Run ("it returns a local backend when there is empty configuration" , func (t * testing.T ) {
2577+ m := testMetaBackend (t , nil )
2578+ emptyConfig := configs .NewEmptyConfig ()
2579+
2580+ b , bDiags := m .prepareBackend (emptyConfig .Module )
2581+ if bDiags .HasErrors () {
2582+ t .Fatal ("unexpected error: " , bDiags .Err ())
2583+ }
2584+
2585+ if _ , ok := b .(* local.Local ); ! ok {
2586+ t .Fatal ("expected returned operations backend to be a Local backend" )
2587+ }
2588+ })
2589+
2590+ t .Run ("it returns a state_store from state_store config" , func (t * testing.T ) {
2591+ // Create a temporary working directory with backend configuration in
2592+ td := t .TempDir ()
2593+ testCopyDir (t , testFixturePath ("state-store-unchanged" ), td )
2594+ t .Chdir (td )
2595+
2596+ m := testMetaBackend (t , nil )
2597+ m .AllowExperimentalFeatures = true
2598+ mock := testStateStoreMock (t )
2599+ m .testingOverrides = & testingOverrides {
2600+ Providers : map [addrs.Provider ]providers.Factory {
2601+ addrs .NewDefaultProvider ("test" ): providers .FactoryFixed (mock ),
2602+ },
2603+ }
2604+
2605+ // Get the backend config
2606+ mod , loadDiags := m .loadSingleModule (td )
2607+ if loadDiags .HasErrors () {
2608+ t .Fatalf ("unexpected error when loading test config: %s" , loadDiags .Err ())
2609+ }
2610+
2611+ _ , bDiags := m .prepareBackend (mod )
2612+ if ! bDiags .HasErrors () {
2613+ // TODO(SarahFrench/radeksimko): In future, we don't expect an error here!
2614+ t .Fatal ("expected errors while state_store, due to incomplete implementation" )
2615+ }
2616+ if ! strings .Contains (bDiags .Err ().Error (), "Changing a state store configuration is not implemented yet" ) {
2617+ t .Fatal ("unexpected error: " , bDiags .Err ())
2618+ }
2619+
2620+ // TODO(SarahFrench/radeksimko): Make assertion about returned operations backend.
2621+ })
2622+ }
2623+
25232624func testMetaBackend (t * testing.T , args []string ) * Meta {
25242625 var m Meta
25252626 m .Ui = new (cli.MockUi )
0 commit comments