github.com/hugorut/terraform@v1.1.3/src/backend/remote-state/manta/backend_test.go (about)

     1  package manta
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/hugorut/terraform/src/backend"
    12  	"github.com/joyent/triton-go/storage"
    13  )
    14  
    15  func testACC(t *testing.T) {
    16  	skip := os.Getenv("TF_ACC") == "" && os.Getenv("TF_MANTA_TEST") == ""
    17  	if skip {
    18  		t.Log("Manta backend tests require setting TF_ACC or TF_MANTA_TEST")
    19  		t.Skip()
    20  	}
    21  	skip = os.Getenv("TRITON_ACCOUNT") == "" && os.Getenv("SDC_ACCOUNT") == ""
    22  	if skip {
    23  		t.Fatal("Manta backend tests require setting TRITON_ACCOUNT or SDC_ACCOUNT")
    24  	}
    25  	skip = os.Getenv("TRITON_KEY_ID") == "" && os.Getenv("SDC_KEY_ID") == ""
    26  	if skip {
    27  		t.Fatal("Manta backend tests require setting TRITON_KEY_ID or SDC_KEY_ID")
    28  	}
    29  }
    30  
    31  func TestBackend_impl(t *testing.T) {
    32  	var _ backend.Backend = new(Backend)
    33  }
    34  
    35  func TestBackend(t *testing.T) {
    36  	testACC(t)
    37  
    38  	directory := fmt.Sprintf("terraform-remote-manta-test-%x", time.Now().Unix())
    39  	keyName := "testState"
    40  
    41  	b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
    42  		"path":        directory,
    43  		"object_name": keyName,
    44  	})).(*Backend)
    45  
    46  	createMantaFolder(t, b.storageClient, directory)
    47  	defer deleteMantaFolder(t, b.storageClient, directory)
    48  
    49  	backend.TestBackendStates(t, b)
    50  }
    51  
    52  func TestBackendLocked(t *testing.T) {
    53  	testACC(t)
    54  
    55  	directory := fmt.Sprintf("terraform-remote-manta-test-%x", time.Now().Unix())
    56  	keyName := "testState"
    57  
    58  	b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
    59  		"path":        directory,
    60  		"object_name": keyName,
    61  	})).(*Backend)
    62  
    63  	b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
    64  		"path":        directory,
    65  		"object_name": keyName,
    66  	})).(*Backend)
    67  
    68  	createMantaFolder(t, b1.storageClient, directory)
    69  	defer deleteMantaFolder(t, b1.storageClient, directory)
    70  
    71  	backend.TestBackendStateLocks(t, b1, b2)
    72  	backend.TestBackendStateForceUnlock(t, b1, b2)
    73  }
    74  
    75  func createMantaFolder(t *testing.T, mantaClient *storage.StorageClient, directoryName string) {
    76  	// Be clear about what we're doing in case the user needs to clean
    77  	// this up later.
    78  	//t.Logf("creating Manta directory %s", directoryName)
    79  	err := mantaClient.Dir().Put(context.Background(), &storage.PutDirectoryInput{
    80  		DirectoryName: path.Join(mantaDefaultRootStore, directoryName),
    81  	})
    82  	if err != nil {
    83  		t.Fatal("failed to create test Manta directory:", err)
    84  	}
    85  }
    86  
    87  func deleteMantaFolder(t *testing.T, mantaClient *storage.StorageClient, directoryName string) {
    88  	//warning := "WARNING: Failed to delete the test Manta directory. It may have been left in your Manta account and may incur storage charges. (error was %s)"
    89  
    90  	// first we have to get rid of the env objects, or we can't delete the directory
    91  	objs, err := mantaClient.Dir().List(context.Background(), &storage.ListDirectoryInput{
    92  		DirectoryName: path.Join(mantaDefaultRootStore, directoryName),
    93  	})
    94  	if err != nil {
    95  		t.Fatal("failed to retrieve directory listing")
    96  	}
    97  
    98  	for _, obj := range objs.Entries {
    99  		if obj.Type == "directory" {
   100  			ojs, err := mantaClient.Dir().List(context.Background(), &storage.ListDirectoryInput{
   101  				DirectoryName: path.Join(mantaDefaultRootStore, directoryName, obj.Name),
   102  			})
   103  			if err != nil {
   104  				t.Fatal("failed to retrieve directory listing")
   105  			}
   106  			for _, oj := range ojs.Entries {
   107  				err := mantaClient.Objects().Delete(context.Background(), &storage.DeleteObjectInput{
   108  					ObjectPath: path.Join(mantaDefaultRootStore, directoryName, obj.Name, oj.Name),
   109  				})
   110  				if err != nil {
   111  					t.Fatal(err)
   112  				}
   113  			}
   114  		}
   115  
   116  		err := mantaClient.Objects().Delete(context.Background(), &storage.DeleteObjectInput{
   117  			ObjectPath: path.Join(mantaDefaultRootStore, directoryName, obj.Name),
   118  		})
   119  		if err != nil {
   120  			t.Fatal(err)
   121  		}
   122  	}
   123  
   124  	err = mantaClient.Dir().Delete(context.Background(), &storage.DeleteDirectoryInput{
   125  		DirectoryName: path.Join(mantaDefaultRootStore, directoryName),
   126  	})
   127  	if err != nil {
   128  		t.Fatal("failed to delete manta directory")
   129  	}
   130  }