fix: tests #57
6 changed files with 172 additions and 39 deletions
|
|
@ -132,7 +132,7 @@ func TestMapFields(t *testing.T) {
|
|||
|
||||
func TestMapResourceFields(t *testing.T) {
|
||||
type given struct {
|
||||
source *postgresflexalpha.ListDatabase
|
||||
source *postgresflexalpha.GetDatabaseResponse
|
||||
model *resourceModel
|
||||
}
|
||||
type expected struct {
|
||||
|
|
@ -148,10 +148,10 @@ func TestMapResourceFields(t *testing.T) {
|
|||
{
|
||||
name: "should map fields correctly",
|
||||
given: given{
|
||||
source: &postgresflexalpha.ListDatabase{
|
||||
source: &postgresflexalpha.GetDatabaseResponse{
|
||||
Id: utils.Ptr(int64(1)),
|
||||
Name: utils.Ptr("my-db"),
|
||||
Owner: utils.Ptr("\"my-owner\""),
|
||||
Owner: utils.Ptr("my-owner"),
|
||||
},
|
||||
model: &resourceModel{},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -21,6 +21,38 @@ import (
|
|||
|
||||
const pfx = "stackitprivatepreview_postgresflexalpha"
|
||||
|
||||
var createdInstances []string
|
||||
|
||||
func init() {
|
||||
sweeperName := fmt.Sprintf("%s_%s", pfx, "sweeper")
|
||||
resource.AddTestSweepers(sweeperName, &resource.Sweeper{
|
||||
Name: sweeperName,
|
||||
F: func(region string) error {
|
||||
|
||||
//client, err := sharedClientForRegion(region)
|
||||
//if err != nil {
|
||||
// return fmt.Errorf("Error getting client: %s", err)
|
||||
//}
|
||||
//conn := client.(*ExampleClient)
|
||||
//
|
||||
//instances, err := conn.DescribeComputeInstances()
|
||||
//if err != nil {
|
||||
// return fmt.Errorf("Error getting instances: %s", err)
|
||||
//}
|
||||
//for _, instance := range instances {
|
||||
// if strings.HasPrefix(instance.Name, "test-acc") {
|
||||
// err := conn.DestroyInstance(instance.ID)
|
||||
//
|
||||
// if err != nil {
|
||||
// log.Printf("Error destroying %s during sweep: %s", instance.Name, err)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestInstanceResourceSchema(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
@ -58,13 +90,6 @@ func TestMain(m *testing.M) {
|
|||
os.Exit(code)
|
||||
}
|
||||
|
||||
//var (
|
||||
// validFlavor = "2.4"
|
||||
// kekKeyRingId = ""
|
||||
// kekKeyVersion = ""
|
||||
// kekKeySA = ""
|
||||
//)
|
||||
|
||||
func testAccPreCheck(t *testing.T) {
|
||||
if _, ok := os.LookupEnv("TF_ACC_PROJECT_ID"); !ok {
|
||||
t.Fatalf("could not find env var TF_ACC_PROJECT_ID")
|
||||
|
|
@ -151,7 +176,6 @@ func getExample() resData {
|
|||
|
||||
func TestAccInstance(t *testing.T) {
|
||||
exData := getExample()
|
||||
t.Logf(" ... working on instance %s", exData.TfName)
|
||||
|
||||
resName := fmt.Sprintf(
|
||||
"stackitprivatepreview_postgresflexalpha_instance.%s",
|
||||
|
|
@ -164,8 +188,11 @@ func TestAccInstance(t *testing.T) {
|
|||
updSizeData := exData
|
||||
updSizeData.Size = 25
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", exData.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
|
|
@ -215,7 +242,6 @@ func TestAccInstance(t *testing.T) {
|
|||
|
||||
func TestAccInstanceWithUsers(t *testing.T) {
|
||||
data := getExample()
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
|
||||
userName := "testUser"
|
||||
data.Users = []User{
|
||||
|
|
@ -226,8 +252,11 @@ func TestAccInstanceWithUsers(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
|
|
@ -249,7 +278,6 @@ func TestAccInstanceWithUsers(t *testing.T) {
|
|||
|
||||
func TestAccInstanceWithDatabases(t *testing.T) {
|
||||
data := getExample()
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
|
||||
dbName := "testDb"
|
||||
userName := "testUser"
|
||||
|
|
@ -284,8 +312,11 @@ func TestAccInstanceWithDatabases(t *testing.T) {
|
|||
dbName,
|
||||
)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
|
|
@ -367,7 +398,7 @@ func TestAccInstanceWithDatabases(t *testing.T) {
|
|||
// ProjectID: project_id,
|
||||
// Name: "testRes",
|
||||
// }
|
||||
// resource.Test(t, resource.TestCase{
|
||||
// resource.ParallelTest(t, resource.TestCase{
|
||||
// ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
// Steps: []resource.TestStep{
|
||||
// {
|
||||
|
|
@ -510,7 +541,7 @@ func TestAccInstanceWithDatabases(t *testing.T) {
|
|||
//}
|
||||
//
|
||||
//func TestAccPostgresFlexFlexResource(t *testing.T) {
|
||||
// resource.Test(
|
||||
// resource.ParallelTest(
|
||||
// t, resource.TestCase{
|
||||
// ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
// CheckDestroy: testAccCheckPostgresFlexDestroy,
|
||||
|
|
|
|||
|
|
@ -452,7 +452,7 @@ func (r *userResource) Update(
|
|||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"read user",
|
||||
fmt.Sprintf("Instance creation waiting: %v", err),
|
||||
fmt.Sprintf("user update waiting: %v", err),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
|
@ -461,8 +461,8 @@ func (r *userResource) Update(
|
|||
core.LogAndAddError(
|
||||
ctx,
|
||||
&resp.Diagnostics,
|
||||
"read user",
|
||||
"Instance creation waiting: returned id is nil or wrong",
|
||||
"update user",
|
||||
"User creation waiting: returned id is nil or wrong",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,16 +4,19 @@ import (
|
|||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||
sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance"
|
||||
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
|
||||
sqlserverflexbeta2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexbeta"
|
||||
sqlserverflexbeta "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexbeta/instance"
|
||||
// The fwresource import alias is so there is no collision
|
||||
// with the more typical acceptance testing import:
|
||||
// "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||
|
|
@ -22,6 +25,39 @@ import (
|
|||
|
||||
const providerPrefix = "stackitprivatepreview_sqlserverflexbeta"
|
||||
|
||||
func init() {
|
||||
sweeperName := fmt.Sprintf("%s_%s", providerPrefix, "sweeper")
|
||||
|
||||
resource.AddTestSweepers(sweeperName, &resource.Sweeper{
|
||||
Name: sweeperName,
|
||||
F: func(region string) error {
|
||||
ctx := context.Background()
|
||||
apiClientConfigOptions := []config.ConfigurationOption{}
|
||||
apiClient, err := sqlserverflexbeta2.NewAPIClient(apiClientConfigOptions...)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
instances, err := apiClient.ListInstancesRequest(ctx, testutils.ProjectId, testutils.Region).
|
||||
Size(100).
|
||||
Execute()
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
for _, inst := range instances.GetInstances() {
|
||||
if strings.HasPrefix(inst.GetName(), "tf-acc-") {
|
||||
delErr := apiClient.DeleteInstanceRequestExecute(ctx, testutils.ProjectId, testutils.Region, inst.GetId())
|
||||
if delErr != nil {
|
||||
log.Fatalln(delErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestInstanceResourceSchema(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
@ -120,7 +156,6 @@ func getExample() resData {
|
|||
|
||||
func TestAccInstance(t *testing.T) {
|
||||
exData := getExample()
|
||||
t.Logf(" ... working on instance %s", exData.TfName)
|
||||
|
||||
updNameData := exData
|
||||
updNameData.Name = "name-updated"
|
||||
|
|
@ -128,8 +163,11 @@ func TestAccInstance(t *testing.T) {
|
|||
updSizeData := exData
|
||||
updSizeData.Size = 25
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", exData.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
|
|
@ -182,14 +220,21 @@ func TestAccInstance(t *testing.T) {
|
|||
|
||||
func TestAccInstanceNoEncryption(t *testing.T) {
|
||||
data := getExample()
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
|
||||
dbName := "testDb"
|
||||
userName := "testUser"
|
||||
data.Users = []User{
|
||||
{
|
||||
Name: userName,
|
||||
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
||||
Roles: []string{"##STACKIT_LoginManager##", "##STACKIT_DatabaseManager##"},
|
||||
Roles: []string{
|
||||
"##STACKIT_DatabaseManager##",
|
||||
"##STACKIT_LoginManager##",
|
||||
"##STACKIT_ProcessManager##",
|
||||
"##STACKIT_ServerManager##",
|
||||
"##STACKIT_SQLAgentManager##",
|
||||
"##STACKIT_SQLAgentUser##",
|
||||
},
|
||||
},
|
||||
}
|
||||
data.Databases = []Database{
|
||||
|
|
@ -200,8 +245,11 @@ func TestAccInstanceNoEncryption(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
|
|
@ -255,7 +303,7 @@ func TestAccInstanceNoEncryption(t *testing.T) {
|
|||
|
||||
// check user values are correct
|
||||
resource.TestCheckResourceAttr(resName("user", userName), "username", userName),
|
||||
resource.TestCheckResourceAttr(resName("user", userName), "roles.#", "2"),
|
||||
resource.TestCheckResourceAttr(resName("user", userName), "roles.#", strconv.Itoa(len(data.Users[0].Roles))),
|
||||
|
||||
// check database values are set
|
||||
resource.TestCheckResourceAttrSet(resName("database", dbName), "id"),
|
||||
|
|
@ -275,7 +323,7 @@ func TestAccInstanceNoEncryption(t *testing.T) {
|
|||
|
||||
func TestAccInstanceEncryption(t *testing.T) {
|
||||
data := getExample()
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
|
||||
dbName := "testDb"
|
||||
userName := "testUser"
|
||||
data.Users = []User{
|
||||
|
|
@ -299,8 +347,11 @@ func TestAccInstanceEncryption(t *testing.T) {
|
|||
data.KekKeyVersion = 1
|
||||
data.KekServiceAccount = "henselinm-u2v3ex1@sa.stackit.cloud"
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
testAccPreCheck(t)
|
||||
t.Logf(" ... working on instance %s", data.TfName)
|
||||
},
|
||||
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
||||
Steps: []resource.TestStep{
|
||||
// Create and verify
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ func CreateInstanceWaitHandler(
|
|||
}
|
||||
switch strings.ToLower(string(*s.Status)) {
|
||||
case strings.ToLower(InstanceStateSuccess):
|
||||
if *s.Network.AccessScope == "SNA" {
|
||||
if s.Network != nil && s.Network.AccessScope != nil && *s.Network.AccessScope == "SNA" {
|
||||
if s.Network.InstanceAddress == nil {
|
||||
tflog.Info(ctx, "Waiting for instance_address")
|
||||
return false, nil, nil
|
||||
|
|
|
|||
|
|
@ -20,6 +20,57 @@ type apiClientInstanceMocked struct {
|
|||
instanceGetFails bool
|
||||
}
|
||||
|
||||
type ListUsersRequestRequest struct{}
|
||||
|
||||
func (l ListUsersRequestRequest) Page(page int64) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Size(size int64) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Sort(sort sqlserverflex.UserSort) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l ListUsersRequestRequest) Execute() (*sqlserverflex.ListUserResponse, error) {
|
||||
//TODO implement me
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListUsersRequest(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) sqlserverflex.ApiListUsersRequestRequest {
|
||||
return ListUsersRequestRequest{}
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListRolesRequestExecute(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) (*sqlserverflex.ListRolesResponse, error) {
|
||||
return &sqlserverflex.ListRolesResponse{
|
||||
Roles: &[]string{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) ListUsersRequestExecute(
|
||||
ctx context.Context,
|
||||
projectId string,
|
||||
region string,
|
||||
instanceId string,
|
||||
) (*sqlserverflex.ListUserResponse, error) {
|
||||
return &sqlserverflex.ListUserResponse{
|
||||
Pagination: nil,
|
||||
Users: nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (a *apiClientInstanceMocked) GetDatabaseRequestExecute(
|
||||
_ context.Context,
|
||||
projectId string,
|
||||
|
|
@ -63,7 +114,7 @@ func (a *apiClientInstanceMocked) GetInstanceRequestExecute(
|
|||
}, nil
|
||||
}
|
||||
func TestCreateInstanceWaitHandler(t *testing.T) {
|
||||
t.Skip("skipping - needs refactoring")
|
||||
//t.Skip("skipping - needs refactoring")
|
||||
tests := []struct {
|
||||
desc string
|
||||
instanceGetFails bool
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue