2017-10-09 13:46:54 +02:00
package manta
import (
"context"
"fmt"
"os"
"path"
"testing"
"time"
2021-05-17 17:42:17 +02:00
"github.com/hashicorp/terraform/internal/backend"
2017-10-09 13:46:54 +02:00
"github.com/joyent/triton-go/storage"
)
func testACC ( t * testing . T ) {
skip := os . Getenv ( "TF_ACC" ) == "" && os . Getenv ( "TF_MANTA_TEST" ) == ""
if skip {
t . Log ( "Manta backend tests require setting TF_ACC or TF_MANTA_TEST" )
t . Skip ( )
}
2019-09-26 17:30:52 +02:00
skip = os . Getenv ( "TRITON_ACCOUNT" ) == "" && os . Getenv ( "SDC_ACCOUNT" ) == ""
if skip {
t . Fatal ( "Manta backend tests require setting TRITON_ACCOUNT or SDC_ACCOUNT" )
}
skip = os . Getenv ( "TRITON_KEY_ID" ) == "" && os . Getenv ( "SDC_KEY_ID" ) == ""
if skip {
t . Fatal ( "Manta backend tests require setting TRITON_KEY_ID or SDC_KEY_ID" )
}
2017-10-09 13:46:54 +02:00
}
func TestBackend_impl ( t * testing . T ) {
var _ backend . Backend = new ( Backend )
}
func TestBackend ( t * testing . T ) {
testACC ( t )
directory := fmt . Sprintf ( "terraform-remote-manta-test-%x" , time . Now ( ) . Unix ( ) )
keyName := "testState"
2018-03-21 02:43:02 +01:00
b := backend . TestBackendConfig ( t , New ( ) , backend . TestWrapConfig ( map [ string ] interface { } {
2018-10-31 16:45:03 +01:00
"path" : directory ,
"object_name" : keyName ,
2018-03-21 02:43:02 +01:00
} ) ) . ( * Backend )
2017-10-09 13:46:54 +02:00
createMantaFolder ( t , b . storageClient , directory )
defer deleteMantaFolder ( t , b . storageClient , directory )
2018-02-21 03:05:58 +01:00
backend . TestBackendStates ( t , b )
2017-10-09 13:46:54 +02:00
}
func TestBackendLocked ( t * testing . T ) {
testACC ( t )
directory := fmt . Sprintf ( "terraform-remote-manta-test-%x" , time . Now ( ) . Unix ( ) )
keyName := "testState"
2018-03-21 02:43:02 +01:00
b1 := backend . TestBackendConfig ( t , New ( ) , backend . TestWrapConfig ( map [ string ] interface { } {
2018-10-31 16:45:03 +01:00
"path" : directory ,
"object_name" : keyName ,
2018-03-21 02:43:02 +01:00
} ) ) . ( * Backend )
2017-10-09 13:46:54 +02:00
2018-03-21 02:43:02 +01:00
b2 := backend . TestBackendConfig ( t , New ( ) , backend . TestWrapConfig ( map [ string ] interface { } {
2018-10-31 16:45:03 +01:00
"path" : directory ,
"object_name" : keyName ,
2018-03-21 02:43:02 +01:00
} ) ) . ( * Backend )
2017-10-09 13:46:54 +02:00
createMantaFolder ( t , b1 . storageClient , directory )
defer deleteMantaFolder ( t , b1 . storageClient , directory )
2018-02-21 03:05:58 +01:00
backend . TestBackendStateLocks ( t , b1 , b2 )
backend . TestBackendStateForceUnlock ( t , b1 , b2 )
2017-10-09 13:46:54 +02:00
}
func createMantaFolder ( t * testing . T , mantaClient * storage . StorageClient , directoryName string ) {
// Be clear about what we're doing in case the user needs to clean
// this up later.
//t.Logf("creating Manta directory %s", directoryName)
err := mantaClient . Dir ( ) . Put ( context . Background ( ) , & storage . PutDirectoryInput {
DirectoryName : path . Join ( mantaDefaultRootStore , directoryName ) ,
} )
if err != nil {
t . Fatal ( "failed to create test Manta directory:" , err )
}
}
func deleteMantaFolder ( t * testing . T , mantaClient * storage . StorageClient , directoryName string ) {
//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)"
// first we have to get rid of the env objects, or we can't delete the directory
objs , err := mantaClient . Dir ( ) . List ( context . Background ( ) , & storage . ListDirectoryInput {
DirectoryName : path . Join ( mantaDefaultRootStore , directoryName ) ,
} )
if err != nil {
t . Fatal ( "failed to retrieve directory listing" )
}
for _ , obj := range objs . Entries {
if obj . Type == "directory" {
ojs , err := mantaClient . Dir ( ) . List ( context . Background ( ) , & storage . ListDirectoryInput {
DirectoryName : path . Join ( mantaDefaultRootStore , directoryName , obj . Name ) ,
} )
if err != nil {
t . Fatal ( "failed to retrieve directory listing" )
}
for _ , oj := range ojs . Entries {
err := mantaClient . Objects ( ) . Delete ( context . Background ( ) , & storage . DeleteObjectInput {
ObjectPath : path . Join ( mantaDefaultRootStore , directoryName , obj . Name , oj . Name ) ,
} )
if err != nil {
t . Fatal ( err )
}
}
}
err := mantaClient . Objects ( ) . Delete ( context . Background ( ) , & storage . DeleteObjectInput {
ObjectPath : path . Join ( mantaDefaultRootStore , directoryName , obj . Name ) ,
} )
if err != nil {
t . Fatal ( err )
}
}
err = mantaClient . Dir ( ) . Delete ( context . Background ( ) , & storage . DeleteDirectoryInput {
DirectoryName : path . Join ( mantaDefaultRootStore , directoryName ) ,
} )
if err != nil {
t . Fatal ( "failed to delete manta directory" )
}
}