github.com/tompao/terraform@v0.6.10-0.20180215233341-e41b29d0961b/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 // Be clear about what we're doing in case the user needs to clean 68 // this up later. 69 //t.Logf("creating Manta directory %s", directoryName) 70 err := mantaClient.Dir().Put(context.Background(), &storage.PutDirectoryInput{ 71 DirectoryName: path.Join(mantaDefaultRootStore, directoryName), 72 }) 73 if err != nil { 74 t.Fatal("failed to create test Manta directory:", err) 75 } 76 } 77 78 func deleteMantaFolder(t *testing.T, mantaClient *storage.StorageClient, directoryName string) { 79 //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)" 80 81 // first we have to get rid of the env objects, or we can't delete the directory 82 objs, err := mantaClient.Dir().List(context.Background(), &storage.ListDirectoryInput{ 83 DirectoryName: path.Join(mantaDefaultRootStore, directoryName), 84 }) 85 if err != nil { 86 t.Fatal("failed to retrieve directory listing") 87 } 88 89 for _, obj := range objs.Entries { 90 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 }