@@ -2,6 +2,7 @@ package provider
22
33import (
44 "fmt"
5+ "regexp"
56 "strconv"
67 "testing"
78
@@ -13,7 +14,12 @@ import (
1314)
1415
1516func TestAccEnvironment (t * testing.T ) {
16- envName := "dev"
17+ identifier := "new_environment"
18+ resourceName := fmt .Sprintf ("bytebase_environment.%s" , identifier )
19+
20+ name := "dev"
21+ order := 1
22+ nameUpdated := fmt .Sprintf ("%s-updated" , name )
1723
1824 resource .Test (t , resource.TestCase {
1925 PreCheck : func () {
@@ -22,16 +28,47 @@ func TestAccEnvironment(t *testing.T) {
2228 Providers : testAccProviders ,
2329 CheckDestroy : testAccCheckEnvironmentDestroy ,
2430 Steps : []resource.TestStep {
31+ // resource create
32+ {
33+ Config : testAccCheckEnvironmentConfigBasic (identifier , name , order ),
34+ Check : resource .ComposeTestCheckFunc (
35+ testAccCheckEnvironmentExists (resourceName ),
36+ resource .TestCheckResourceAttr (resourceName , "name" , name ),
37+ resource .TestCheckResourceAttr (resourceName , "order" , fmt .Sprintf ("%d" , order )),
38+ ),
39+ },
40+ // resource update
2541 {
26- Config : testAccCheckEnvironmentConfigBasic (envName ),
42+ Config : testAccCheckEnvironmentConfigBasic (identifier , nameUpdated , order + 1 ),
2743 Check : resource .ComposeTestCheckFunc (
28- testAccCheckEnvironmentExists ("bytebase_environment.new" ),
44+ testAccCheckEnvironmentExists (resourceName ),
45+ resource .TestCheckResourceAttr (resourceName , "name" , nameUpdated ),
46+ resource .TestCheckResourceAttr (resourceName , "order" , fmt .Sprintf ("%d" , order + 1 )),
2947 ),
3048 },
3149 },
3250 })
3351}
3452
53+ func TestAccEnvironment_InvalidInput (t * testing.T ) {
54+ identifier := "another_environment"
55+
56+ resource .Test (t , resource.TestCase {
57+ PreCheck : func () {
58+ testAccPreCheck (t )
59+ },
60+ Providers : testAccProviders ,
61+ CheckDestroy : testAccCheckEnvironmentDestroy ,
62+ Steps : []resource.TestStep {
63+ // Invalid environment name
64+ {
65+ Config : testAccCheckEnvironmentConfigBasic (identifier , "" , 0 ),
66+ ExpectError : regexp .MustCompile ("not be an empty string" ),
67+ },
68+ },
69+ })
70+ }
71+
3572func testAccCheckEnvironmentDestroy (s * terraform.State ) error {
3673 c := testAccProvider .Meta ().(api.Client )
3774
@@ -53,12 +90,13 @@ func testAccCheckEnvironmentDestroy(s *terraform.State) error {
5390 return nil
5491}
5592
56- func testAccCheckEnvironmentConfigBasic (envName string ) string {
93+ func testAccCheckEnvironmentConfigBasic (identifier , envName string , order int ) string {
5794 return fmt .Sprintf (`
58- resource "bytebase_environment" "new " {
95+ resource "bytebase_environment" "%s " {
5996 name = "%s"
97+ order = %d
6098 }
61- ` , envName )
99+ ` , identifier , envName , order )
62100}
63101
64102func testAccCheckEnvironmentExists (n string ) resource.TestCheckFunc {
0 commit comments