## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Reviewed-on: #49
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
272 lines
7.2 KiB
Go
272 lines
7.2 KiB
Go
package sqlserverflexalpha_test
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
|
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
|
sqlserverflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/sqlserverflexalpha/instance"
|
|
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils"
|
|
// 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"
|
|
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
|
|
)
|
|
|
|
func TestInstanceResourceSchema(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
schemaRequest := fwresource.SchemaRequest{}
|
|
schemaResponse := &fwresource.SchemaResponse{}
|
|
|
|
// Instantiate the resource.Resource and call its Schema method
|
|
sqlserverflexalpha.NewInstanceResource().Schema(ctx, schemaRequest, schemaResponse)
|
|
|
|
if schemaResponse.Diagnostics.HasError() {
|
|
t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics)
|
|
}
|
|
|
|
// Validate the schema
|
|
diagnostics := schemaResponse.Schema.ValidateImplementation(ctx)
|
|
|
|
if diagnostics.HasError() {
|
|
t.Fatalf("Schema validation diagnostics: %+v", diagnostics)
|
|
}
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
testutils.Setup()
|
|
code := m.Run()
|
|
// shutdown()
|
|
os.Exit(code)
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|
|
|
|
type resData struct {
|
|
ServiceAccountFilePath string
|
|
ProjectId string
|
|
Region string
|
|
Name string
|
|
TfName string
|
|
FlavorId string
|
|
BackupSchedule string
|
|
UseEncryption bool
|
|
KekKeyId string
|
|
KekKeyRingId string
|
|
KekKeyVersion uint8
|
|
KekServiceAccount string
|
|
PerformanceClass string
|
|
Size uint32
|
|
AclString string
|
|
AccessScope string
|
|
RetentionDays uint32
|
|
Version string
|
|
Users []User
|
|
Databases []Database
|
|
}
|
|
|
|
type User struct {
|
|
Name string
|
|
ProjectId string
|
|
Roles []string
|
|
}
|
|
|
|
type Database struct {
|
|
Name string
|
|
ProjectId string
|
|
Owner string
|
|
}
|
|
|
|
func getExample() resData {
|
|
name := acctest.RandomWithPrefix("tf-acc")
|
|
return resData{
|
|
Region: os.Getenv("TF_ACC_REGION"),
|
|
ServiceAccountFilePath: os.Getenv("TF_ACC_SERVICE_ACCOUNT_FILE"),
|
|
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
|
Name: name,
|
|
TfName: name,
|
|
FlavorId: "4.16-Single",
|
|
BackupSchedule: "0 0 * * *",
|
|
UseEncryption: false,
|
|
RetentionDays: 33,
|
|
PerformanceClass: "premium-perf2-stackit",
|
|
Size: 10,
|
|
AclString: "0.0.0.0/0",
|
|
AccessScope: "PUBLIC",
|
|
Version: "2022",
|
|
}
|
|
}
|
|
|
|
func TestAccInstance(t *testing.T) {
|
|
exData := getExample()
|
|
resName := fmt.Sprintf(
|
|
"stackitprivatepreview_sqlserverflexalpha_instance.%s",
|
|
exData.TfName,
|
|
)
|
|
|
|
updNameData := exData
|
|
updNameData.Name = "name-updated"
|
|
|
|
updSizeData := exData
|
|
updSizeData.Size = 25
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
|
Steps: []resource.TestStep{
|
|
// Create and verify
|
|
{
|
|
Config: testutils.StringFromTemplateMust(
|
|
"testdata/instance_template.gompl",
|
|
exData,
|
|
),
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckResourceAttr(resName, "name", exData.Name),
|
|
resource.TestCheckResourceAttrSet(resName, "id"),
|
|
),
|
|
},
|
|
// Update name and verify
|
|
{
|
|
Config: testutils.StringFromTemplateMust(
|
|
"testdata/instance_template.gompl",
|
|
updNameData,
|
|
),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr(resName, "name", updNameData.Name),
|
|
),
|
|
},
|
|
// Update size and verify
|
|
{
|
|
Config: testutils.StringFromTemplateMust(
|
|
"testdata/instance_template.gompl",
|
|
updSizeData,
|
|
),
|
|
Check: resource.ComposeTestCheckFunc(
|
|
resource.TestCheckResourceAttr(
|
|
resName,
|
|
"storage.size",
|
|
strconv.Itoa(int(updSizeData.Size)),
|
|
),
|
|
),
|
|
},
|
|
//// Import test
|
|
//{
|
|
// ResourceName: "example_resource.test",
|
|
// ImportState: true,
|
|
// ImportStateVerify: true,
|
|
//},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccInstanceWithUsers(t *testing.T) {
|
|
data := getExample()
|
|
userName := "testUser"
|
|
data.Users = []User{
|
|
{
|
|
Name: userName,
|
|
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
|
Roles: []string{"##STACKIT_LoginManager##", "##STACKIT_DatabaseManager##"},
|
|
},
|
|
}
|
|
|
|
resName := fmt.Sprintf(
|
|
"stackitprivatepreview_sqlserverflexalpha_instance.%s",
|
|
data.TfName,
|
|
)
|
|
|
|
resUserName := fmt.Sprintf(
|
|
"stackitprivatepreview_sqlserverflexalpha_user.%s",
|
|
userName,
|
|
)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
|
Steps: []resource.TestStep{
|
|
// Create and verify
|
|
{
|
|
Config: testutils.StringFromTemplateMust(
|
|
"testdata/instance_template.gompl",
|
|
data,
|
|
),
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckResourceAttr(resName, "name", data.Name),
|
|
resource.TestCheckResourceAttrSet(resName, "id"),
|
|
resource.TestCheckResourceAttr(resUserName, "name", userName),
|
|
resource.TestCheckResourceAttrSet(resUserName, "id"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
func TestAccInstanceWithDatabases(t *testing.T) {
|
|
data := getExample()
|
|
dbName := "testDb"
|
|
userName := "testUser"
|
|
data.Users = []User{
|
|
{
|
|
Name: userName,
|
|
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
|
Roles: []string{"##STACKIT_LoginManager##", "##STACKIT_DatabaseManager##"},
|
|
},
|
|
}
|
|
data.Databases = []Database{
|
|
{
|
|
Name: dbName,
|
|
ProjectId: os.Getenv("TF_ACC_PROJECT_ID"),
|
|
Owner: userName,
|
|
},
|
|
}
|
|
|
|
resName := fmt.Sprintf(
|
|
"stackitprivatepreview_sqlserverflexalpha_instance.%s",
|
|
data.TfName,
|
|
)
|
|
|
|
resUserName := fmt.Sprintf(
|
|
"stackitprivatepreview_sqlserverflexalpha_user.%s",
|
|
userName,
|
|
)
|
|
|
|
resDbName := fmt.Sprintf(
|
|
"stackitprivatepreview_sqlserverflexalpha_database.%s",
|
|
dbName,
|
|
)
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
|
|
Steps: []resource.TestStep{
|
|
// Create and verify
|
|
{
|
|
Config: testutils.StringFromTemplateMust(
|
|
"testdata/instance_template.gompl",
|
|
data,
|
|
),
|
|
Check: resource.ComposeAggregateTestCheckFunc(
|
|
resource.TestCheckResourceAttr(resName, "name", data.Name),
|
|
resource.TestCheckResourceAttrSet(resName, "id"),
|
|
resource.TestCheckResourceAttr(resUserName, "name", userName),
|
|
resource.TestCheckResourceAttrSet(resUserName, "id"),
|
|
resource.TestCheckResourceAttr(resDbName, "name", dbName),
|
|
resource.TestCheckResourceAttr(resDbName, "owner", userName),
|
|
resource.TestCheckResourceAttrSet(resDbName, "id"),
|
|
),
|
|
},
|
|
},
|
|
})
|
|
}
|