github.com/hooklift/terraform@v0.11.0-beta1.0.20171117000744-6786c1361ffe/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/hashicorp/terraform/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  }
    22  
    23  func TestBackend_impl(t *testing.T) {
    24  	var _ backend.Backend = new(Backend)
    25  }
    26  
    27  func TestBackend(t *testing.T) {
    28  	testACC(t)
    29  
    30  	directory := fmt.Sprintf("terraform-remote-manta-test-%x", time.Now().Unix())
    31  	keyName := "testState"
    32  
    33  	b := backend.TestBackendConfig(t, New(), map[string]interface{}{
    34  		"path":       directory,
    35  		"objectName": keyName,
    36  	}).(*Backend)
    37  
    38  	createMantaFolder(t, b.storageClient, directory)
    39  	defer deleteMantaFolder(t, b.storageClient, directory)
    40  
    41  	backend.TestBackend(t, b, nil)
    42  }
    43  
    44  func TestBackendLocked(t *testing.T) {
    45  	testACC(t)
    46  
    47  	directory := fmt.Sprintf("terraform-remote-manta-test-%x", time.Now().Unix())
    48  	keyName := "testState"
    49  
    50  	b1 := backend.TestBackendConfig(t, New(), map[string]interface{}{
    51  		"path":       directory,
    52  		"objectName": keyName,
    53  	}).(*Backend)
    54  
    55  	b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
    56  		"path":       directory,
    57  		"objectName": keyName,
    58  	}).(*Backend)
    59  
    60  	createMantaFolder(t, b1.storageClient, directory)
    61  	defer deleteMantaFolder(t, b1.storageClient, directory)
    62  
    63  	backend.TestBackend(t, b1, b2)
    64  }
    65  
    66  func createMantaFolder(t *testing.T, mantaClient *storage.StorageClient, directoryName string) {
    67  
    68  	// Be clear about what we're doing in case the user needs to clean
    69  	// this up later.
    70  	//t.Logf("creating Manta directory %s", directoryName)
    71  	err := mantaClient.Dir().Put(context.Background(), &storage.PutDirectoryInput{
    72  		DirectoryName: path.Join(mantaDefaultRootStore, directoryName),
    73  	})
    74  	if err != nil {
    75  		t.Fatal("failed to create test Manta directory:", err)
    76  	}
    77  }
    78  
    79  func deleteMantaFolder(t *testing.T, mantaClient *storage.StorageClient, directoryName string) {
    80  	//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)"
    81  
    82  	// first we have to get rid of the env objects, or we can't delete the directory
    83  	objs, err := mantaClient.Dir().List(context.Background(), &storage.ListDirectoryInput{
    84  		DirectoryName: path.Join(mantaDefaultRootStore, directoryName),
    85  	})
    86  	if err != nil {
    87  		t.Fatal("failed to retrieve directory listing")
    88  	}
    89  
    90  	for _, obj := range objs.Entries {
    91  
    92  		if obj.Type == "directory" {
    93  			ojs, err := mantaClient.Dir().List(context.Background(), &storage.ListDirectoryInput{
    94  				DirectoryName: path.Join(mantaDefaultRootStore, directoryName, obj.Name),
    95  			})
    96  			if err != nil {
    97  				t.Fatal("failed to retrieve directory listing")
    98  			}
    99  			for _, oj := range ojs.Entries {
   100  				err := mantaClient.Objects().Delete(context.Background(), &storage.DeleteObjectInput{
   101  					ObjectPath: path.Join(mantaDefaultRootStore, directoryName, obj.Name, oj.Name),
   102  				})
   103  				if err != nil {
   104  					t.Fatal(err)
   105  				}
   106  			}
   107  		}
   108  
   109  		err := mantaClient.Objects().Delete(context.Background(), &storage.DeleteObjectInput{
   110  			ObjectPath: path.Join(mantaDefaultRootStore, directoryName, obj.Name),
   111  		})
   112  		if err != nil {
   113  			t.Fatal(err)
   114  		}
   115  	}
   116  
   117  	err = mantaClient.Dir().Delete(context.Background(), &storage.DeleteDirectoryInput{
   118  		DirectoryName: path.Join(mantaDefaultRootStore, directoryName),
   119  	})
   120  	if err != nil {
   121  		t.Fatal("failed to delete manta directory")
   122  	}
   123  }