github.com/Kevinklinger/open_terraform@v0.11.12-beta1/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  		"object_name": keyName,
    36  	}).(*Backend)
    37  
    38  	createMantaFolder(t, b.storageClient, directory)
    39  	defer deleteMantaFolder(t, b.storageClient, directory)
    40  
    41  	backend.TestBackendStates(t, b)
    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  		"object_name": keyName,
    53  	}).(*Backend)
    54  
    55  	b2 := backend.TestBackendConfig(t, New(), map[string]interface{}{
    56  		"path":        directory,
    57  		"object_name": keyName,
    58  	}).(*Backend)
    59  
    60  	createMantaFolder(t, b1.storageClient, directory)
    61  	defer deleteMantaFolder(t, b1.storageClient, directory)
    62  
    63  	backend.TestBackendStateLocks(t, b1, b2)
    64  	backend.TestBackendStateForceUnlock(t, b1, b2)
    65  }
    66  
    67  func createMantaFolder(t *testing.T, mantaClient *storage.StorageClient, directoryName string) {
    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  		if obj.Type == "directory" {
    92  			ojs, err := mantaClient.Dir().List(context.Background(), &storage.ListDirectoryInput{
    93  				DirectoryName: path.Join(mantaDefaultRootStore, directoryName, obj.Name),
    94  			})
    95  			if err != nil {
    96  				t.Fatal("failed to retrieve directory listing")
    97  			}
    98  			for _, oj := range ojs.Entries {
    99  				err := mantaClient.Objects().Delete(context.Background(), &storage.DeleteObjectInput{
   100  					ObjectPath: path.Join(mantaDefaultRootStore, directoryName, obj.Name, oj.Name),
   101  				})
   102  				if err != nil {
   103  					t.Fatal(err)
   104  				}
   105  			}
   106  		}
   107  
   108  		err := mantaClient.Objects().Delete(context.Background(), &storage.DeleteObjectInput{
   109  			ObjectPath: path.Join(mantaDefaultRootStore, directoryName, obj.Name),
   110  		})
   111  		if err != nil {
   112  			t.Fatal(err)
   113  		}
   114  	}
   115  
   116  	err = mantaClient.Dir().Delete(context.Background(), &storage.DeleteDirectoryInput{
   117  		DirectoryName: path.Join(mantaDefaultRootStore, directoryName),
   118  	})
   119  	if err != nil {
   120  		t.Fatal("failed to delete manta directory")
   121  	}
   122  }