github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/backend/remote-state/cos/backend_test.go (about) 1 package cos 2 3 import ( 4 "crypto/md5" 5 "fmt" 6 "os" 7 "testing" 8 "time" 9 10 "github.com/hashicorp/terraform/backend" 11 "github.com/hashicorp/terraform/state/remote" 12 "github.com/likexian/gokit/assert" 13 ) 14 15 const ( 16 defaultPrefix = "" 17 defaultKey = "terraform.tfstate" 18 ) 19 20 // Testing Thanks to GCS 21 22 func TestStateFile(t *testing.T) { 23 t.Parallel() 24 25 cases := []struct { 26 prefix string 27 stateName string 28 key string 29 wantStateFile string 30 wantLockFile string 31 }{ 32 {"", "default", "default.tfstate", "default.tfstate", "default.tfstate.tflock"}, 33 {"", "default", "test.tfstate", "test.tfstate", "test.tfstate.tflock"}, 34 {"", "dev", "test.tfstate", "dev/test.tfstate", "dev/test.tfstate.tflock"}, 35 {"terraform/test", "default", "default.tfstate", "terraform/test/default.tfstate", "terraform/test/default.tfstate.tflock"}, 36 {"terraform/test", "default", "test.tfstate", "terraform/test/test.tfstate", "terraform/test/test.tfstate.tflock"}, 37 {"terraform/test", "dev", "test.tfstate", "terraform/test/dev/test.tfstate", "terraform/test/dev/test.tfstate.tflock"}, 38 } 39 40 for _, c := range cases { 41 b := &Backend{ 42 prefix: c.prefix, 43 key: c.key, 44 } 45 assert.Equal(t, b.stateFile(c.stateName), c.wantStateFile) 46 assert.Equal(t, b.lockFile(c.stateName), c.wantLockFile) 47 } 48 } 49 50 func TestRemoteClient(t *testing.T) { 51 t.Parallel() 52 53 bucket := bucketName(t) 54 55 be := setupBackend(t, bucket, defaultPrefix, defaultKey, false) 56 defer teardownBackend(t, be) 57 58 ss, err := be.StateMgr(backend.DefaultStateName) 59 assert.Nil(t, err) 60 61 rs, ok := ss.(*remote.State) 62 assert.True(t, ok) 63 64 remote.TestClient(t, rs.Client) 65 } 66 67 func TestRemoteClientWithPrefix(t *testing.T) { 68 t.Parallel() 69 70 prefix := "prefix/test" 71 bucket := bucketName(t) 72 73 be := setupBackend(t, bucket, prefix, defaultKey, false) 74 defer teardownBackend(t, be) 75 76 ss, err := be.StateMgr(backend.DefaultStateName) 77 assert.Nil(t, err) 78 79 rs, ok := ss.(*remote.State) 80 assert.True(t, ok) 81 82 remote.TestClient(t, rs.Client) 83 } 84 85 func TestRemoteClientWithEncryption(t *testing.T) { 86 t.Parallel() 87 88 bucket := bucketName(t) 89 90 be := setupBackend(t, bucket, defaultPrefix, defaultKey, true) 91 defer teardownBackend(t, be) 92 93 ss, err := be.StateMgr(backend.DefaultStateName) 94 assert.Nil(t, err) 95 96 rs, ok := ss.(*remote.State) 97 assert.True(t, ok) 98 99 remote.TestClient(t, rs.Client) 100 } 101 102 func TestRemoteLocks(t *testing.T) { 103 t.Parallel() 104 105 bucket := bucketName(t) 106 107 be := setupBackend(t, bucket, defaultPrefix, defaultKey, false) 108 defer teardownBackend(t, be) 109 110 remoteClient := func() (remote.Client, error) { 111 ss, err := be.StateMgr(backend.DefaultStateName) 112 if err != nil { 113 return nil, err 114 } 115 116 rs, ok := ss.(*remote.State) 117 if !ok { 118 return nil, fmt.Errorf("be.StateMgr(): got a %T, want a *remote.State", ss) 119 } 120 121 return rs.Client, nil 122 } 123 124 c0, err := remoteClient() 125 assert.Nil(t, err) 126 127 c1, err := remoteClient() 128 assert.Nil(t, err) 129 130 remote.TestRemoteLocks(t, c0, c1) 131 } 132 133 func TestBackend(t *testing.T) { 134 t.Parallel() 135 136 bucket := bucketName(t) 137 138 be0 := setupBackend(t, bucket, defaultPrefix, defaultKey, false) 139 defer teardownBackend(t, be0) 140 141 be1 := setupBackend(t, bucket, defaultPrefix, defaultKey, false) 142 defer teardownBackend(t, be1) 143 144 backend.TestBackendStates(t, be0) 145 backend.TestBackendStateLocks(t, be0, be1) 146 backend.TestBackendStateForceUnlock(t, be0, be1) 147 } 148 149 func TestBackendWithPrefix(t *testing.T) { 150 t.Parallel() 151 152 prefix := "prefix/test" 153 bucket := bucketName(t) 154 155 be0 := setupBackend(t, bucket, prefix, defaultKey, false) 156 defer teardownBackend(t, be0) 157 158 be1 := setupBackend(t, bucket, prefix+"/", defaultKey, false) 159 defer teardownBackend(t, be1) 160 161 backend.TestBackendStates(t, be0) 162 backend.TestBackendStateLocks(t, be0, be1) 163 } 164 165 func TestBackendWithEncryption(t *testing.T) { 166 t.Parallel() 167 168 bucket := bucketName(t) 169 170 be0 := setupBackend(t, bucket, defaultPrefix, defaultKey, true) 171 defer teardownBackend(t, be0) 172 173 be1 := setupBackend(t, bucket, defaultPrefix, defaultKey, true) 174 defer teardownBackend(t, be1) 175 176 backend.TestBackendStates(t, be0) 177 backend.TestBackendStateLocks(t, be0, be1) 178 } 179 180 func setupBackend(t *testing.T, bucket, prefix, key string, encrypt bool) backend.Backend { 181 t.Helper() 182 183 skip := os.Getenv("TF_COS_APPID") == "" 184 if skip { 185 t.Skip("This test require setting TF_COS_APPID environment variables") 186 } 187 188 if os.Getenv(PROVIDER_REGION) == "" { 189 os.Setenv(PROVIDER_REGION, "ap-guangzhou") 190 } 191 192 appId := os.Getenv("TF_COS_APPID") 193 region := os.Getenv(PROVIDER_REGION) 194 195 config := map[string]interface{}{ 196 "region": region, 197 "bucket": bucket + appId, 198 "prefix": prefix, 199 "key": key, 200 } 201 202 b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(config)) 203 be := b.(*Backend) 204 205 c, err := be.client("tencentcloud") 206 assert.Nil(t, err) 207 208 err = c.putBucket() 209 assert.Nil(t, err) 210 211 return b 212 } 213 214 func teardownBackend(t *testing.T, b backend.Backend) { 215 t.Helper() 216 217 c, err := b.(*Backend).client("tencentcloud") 218 assert.Nil(t, err) 219 220 err = c.deleteBucket(true) 221 assert.Nil(t, err) 222 } 223 224 func bucketName(t *testing.T) string { 225 unique := fmt.Sprintf("%s-%x", t.Name(), time.Now().UnixNano()) 226 return fmt.Sprintf("terraform-test-%s-%s", fmt.Sprintf("%x", md5.Sum([]byte(unique)))[:10], "") 227 }