Skip to content

Commit 273fef2

Browse files
committed
chore: add test
1 parent a8ea6a4 commit 273fef2

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

provider/resource_instance_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package provider
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
"github.com/pkg/errors"
11+
12+
"github.com/bytebase/terraform-provider-bytebase/api"
13+
)
14+
15+
func TestAccInstance(t *testing.T) {
16+
name := "dev instance"
17+
engine := "POSTGRES"
18+
host := "127.0.0.1"
19+
env := "dev"
20+
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() {
23+
testAccPreCheck(t)
24+
},
25+
Providers: testAccProviders,
26+
CheckDestroy: testAccCheckInstanceDestroy,
27+
Steps: []resource.TestStep{
28+
{
29+
Config: testAccCheckInstanceConfigBasic(name, engine, host, env),
30+
Check: resource.ComposeTestCheckFunc(
31+
testAccCheckInstanceExists("bytebase_instance.new"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func testAccCheckInstanceDestroy(s *terraform.State) error {
39+
c := testAccProvider.Meta().(api.Client)
40+
41+
for _, rs := range s.RootModule().Resources {
42+
if rs.Type != "bytebase_instance" {
43+
continue
44+
}
45+
46+
instanceID, err := strconv.Atoi(rs.Primary.ID)
47+
if err != nil {
48+
return err
49+
}
50+
51+
if err := c.DeleteInstance(instanceID); err != nil {
52+
return err
53+
}
54+
}
55+
56+
return nil
57+
}
58+
59+
func testAccCheckInstanceConfigBasic(name, engine, host, env string) string {
60+
return fmt.Sprintf(`
61+
resource "bytebase_instance" "new" {
62+
name = "%s"
63+
engine = "%s"
64+
host = "%s"
65+
environment = "%s"
66+
}
67+
`, name, engine, host, env)
68+
}
69+
70+
func testAccCheckInstanceExists(n string) resource.TestCheckFunc {
71+
return func(s *terraform.State) error {
72+
rs, ok := s.RootModule().Resources[n]
73+
74+
if !ok {
75+
return errors.Errorf("Not found: %s", n)
76+
}
77+
78+
if rs.Primary.ID == "" {
79+
return errors.Errorf("No instance set")
80+
}
81+
82+
return nil
83+
}
84+
}

0 commit comments

Comments
 (0)