feat: refactor testing
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 5s
CI Workflow / CI (pull_request) Failing after 8m41s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 11m58s
Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 5s
CI Workflow / CI (pull_request) Failing after 8m41s
CI Workflow / Code coverage report (pull_request) Has been skipped
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 11m58s
This commit is contained in:
parent
1dd19d57b6
commit
e852e0d73d
14 changed files with 512 additions and 112 deletions
1
.github/actions/acc_test/README.md
vendored
Normal file
1
.github/actions/acc_test/README.md
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# acceptance test action
|
||||||
114
.github/actions/acc_test/action.yaml
vendored
Normal file
114
.github/actions/acc_test/action.yaml
vendored
Normal file
|
|
@ -0,0 +1,114 @@
|
||||||
|
name: Acceptance Testing
|
||||||
|
description: "Acceptance Testing pipeline"
|
||||||
|
|
||||||
|
inputs:
|
||||||
|
go-version:
|
||||||
|
description: "go version to install"
|
||||||
|
default: '1.25'
|
||||||
|
required: true
|
||||||
|
|
||||||
|
project_id:
|
||||||
|
description: "STACKIT project ID for tests"
|
||||||
|
required: true
|
||||||
|
|
||||||
|
region:
|
||||||
|
description: "STACKIT region for tests"
|
||||||
|
default: 'eu01'
|
||||||
|
required: true
|
||||||
|
|
||||||
|
service_account_json:
|
||||||
|
description: "STACKIT service account JSON file contents"
|
||||||
|
required: true
|
||||||
|
|
||||||
|
test_file:
|
||||||
|
description: "testfile to run"
|
||||||
|
default: ''
|
||||||
|
|
||||||
|
outputs:
|
||||||
|
random-number:
|
||||||
|
description: "Random number"
|
||||||
|
value: ${{ steps.random-number-generator.outputs.random-number }}
|
||||||
|
|
||||||
|
runs:
|
||||||
|
using: "composite"
|
||||||
|
steps:
|
||||||
|
- name: Random Number Generator
|
||||||
|
id: random-number-generator
|
||||||
|
run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
|
||||||
|
shell: bash
|
||||||
|
|
||||||
|
- name: Install needed tools
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -e
|
||||||
|
apt-get -y -qq update
|
||||||
|
apt-get -y -qq install jq python3 python3-pip python-is-python3 s3cmd git make wget
|
||||||
|
|
||||||
|
- name: Setup JAVA
|
||||||
|
uses: actions/setup-java@v5
|
||||||
|
with:
|
||||||
|
distribution: 'temurin' # See 'Supported distributions' for available options
|
||||||
|
java-version: '21'
|
||||||
|
|
||||||
|
- name: Install Go ${{ inputs.go-version }}
|
||||||
|
uses: actions/setup-go@v6
|
||||||
|
with:
|
||||||
|
go-version: ${{ inputs.go-version }}
|
||||||
|
check-latest: true
|
||||||
|
go-version-file: 'go.mod'
|
||||||
|
|
||||||
|
- name: Install go tools
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
set -e
|
||||||
|
go mod download
|
||||||
|
go install golang.org/x/tools/cmd/goimports@latest
|
||||||
|
go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.7.2
|
||||||
|
go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@v0.24.0
|
||||||
|
|
||||||
|
- name: Prepare pkg_gen directory
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
go run cmd/main.go build -p
|
||||||
|
|
||||||
|
- name: Run acceptance test file
|
||||||
|
if: ${{ inputs.test_file != '' }}
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
echo "Running acceptance tests for the terraform provider"
|
||||||
|
echo "${STACKIT_SERVICE_ACCOUNT_JSON}" > ~/.service_account.json
|
||||||
|
cd stackit
|
||||||
|
TF_ACC=1 \
|
||||||
|
TF_ACC_PROJECT_ID=${TF_ACC_PROJECT_ID} \
|
||||||
|
TF_ACC_REGION=${TF_ACC_REGION} \
|
||||||
|
go test ${{ inputs.test_file }} -count=1 -timeout=30m
|
||||||
|
env:
|
||||||
|
STACKIT_SERVICE_ACCOUNT_JSON: ${{ inputs.service_account_json }}
|
||||||
|
TF_PROJECT_ID: ${{ inputs.project_id }}
|
||||||
|
TF_ACC_REGION: ${{ inputs.region }}
|
||||||
|
# TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_EMAIL: ${{ secrets.TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_EMAIL }}
|
||||||
|
# TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN: ${{ secrets.TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN }}
|
||||||
|
# TF_ACC_TEST_PROJECT_PARENT_CONTAINER_ID: ${{ secrets.TF_ACC_TEST_PROJECT_PARENT_CONTAINER_ID }}
|
||||||
|
# TF_ACC_TEST_PROJECT_PARENT_UUID: ${{ secrets.TF_ACC_TEST_PROJECT_PARENT_UUID }}
|
||||||
|
# TF_ACC_TEST_PROJECT_USER_EMAIL: ${{ secrets.TF_ACC_TEST_PROJECT_USER_EMAIL }}
|
||||||
|
|
||||||
|
- name: Run acceptance tests
|
||||||
|
if: ${{ inputs.test_file == '' }}
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
echo "Running acceptance tests for the terraform provider"
|
||||||
|
echo "${STACKIT_SERVICE_ACCOUNT_JSON}" > ~/.service_account.json
|
||||||
|
cd stackit
|
||||||
|
TF_ACC=1 \
|
||||||
|
TF_ACC_PROJECT_ID=${TF_ACC_PROJECT_ID} \
|
||||||
|
TF_ACC_REGION=${TF_ACC_REGION} \
|
||||||
|
go test ./... -count=1 -timeout=30m
|
||||||
|
env:
|
||||||
|
STACKIT_SERVICE_ACCOUNT_JSON: ${{ inputs.service_account_json }}
|
||||||
|
TF_PROJECT_ID: ${{ inputs.project_id }}
|
||||||
|
TF_ACC_REGION: ${{ inputs.region }}
|
||||||
|
# TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_EMAIL: ${{ secrets.TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_EMAIL }}
|
||||||
|
# TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN: ${{ secrets.TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN }}
|
||||||
|
# TF_ACC_TEST_PROJECT_PARENT_CONTAINER_ID: ${{ secrets.TF_ACC_TEST_PROJECT_PARENT_CONTAINER_ID }}
|
||||||
|
# TF_ACC_TEST_PROJECT_PARENT_UUID: ${{ secrets.TF_ACC_TEST_PROJECT_PARENT_UUID }}
|
||||||
|
# TF_ACC_TEST_PROJECT_USER_EMAIL: ${{ secrets.TF_ACC_TEST_PROJECT_USER_EMAIL }}
|
||||||
1
.github/actions/build/action.yaml
vendored
1
.github/actions/build/action.yaml
vendored
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
name: Build
|
name: Build
|
||||||
description: "Build pipeline"
|
description: "Build pipeline"
|
||||||
inputs:
|
inputs:
|
||||||
|
|
|
||||||
4
.github/workflows/ci.yaml
vendored
4
.github/workflows/ci.yaml
vendored
|
|
@ -106,7 +106,7 @@ jobs:
|
||||||
needs: config
|
needs: config
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
uses: ./.github/actions/build
|
uses: ./.github/actions/build
|
||||||
|
|
@ -150,7 +150,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
|
|
||||||
- name: Check GoReleaser
|
- name: Check GoReleaser
|
||||||
uses: goreleaser/goreleaser-action@v6
|
uses: goreleaser/goreleaser-action@v6
|
||||||
|
|
|
||||||
2
.github/workflows/release.yaml
vendored
2
.github/workflows/release.yaml
vendored
|
|
@ -18,7 +18,7 @@ jobs:
|
||||||
goreleaser:
|
goreleaser:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v6
|
||||||
with:
|
with:
|
||||||
# Allow goreleaser to access older tag information.
|
# Allow goreleaser to access older tag information.
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
|
||||||
2
.github/workflows/renovate.yaml
vendored
2
.github/workflows/renovate.yaml
vendored
|
|
@ -11,7 +11,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
- name: Self-hosted Renovate
|
- name: Self-hosted Renovate
|
||||||
uses: renovatebot/github-action@v41.0.0
|
uses: renovatebot/github-action@v41.0.0
|
||||||
with:
|
with:
|
||||||
|
|
|
||||||
24
.github/workflows/tf-acc-test.yaml
vendored
24
.github/workflows/tf-acc-test.yaml
vendored
|
|
@ -7,21 +7,17 @@ on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
main:
|
acc_test:
|
||||||
name: Acceptance Tests
|
name: Acceptance Tests
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v6
|
||||||
- name: Install project tools and dependencies
|
|
||||||
run: make project-tools
|
- name: Run Test
|
||||||
- name: Run tests
|
uses: ./.github/actions/acc_test
|
||||||
run: |
|
with:
|
||||||
make test-acceptance-tf TF_ACC_PROJECT_ID=$${{ secrets.TF_ACC_PROJECT_ID }} TF_ACC_ORGANIZATION_ID=$${{ secrets.TF_ACC_ORGANIZATION_ID }} TF_ACC_REGION="eu01"
|
go-version: ${{ env.GO_VERSION }}
|
||||||
env:
|
project_id: ${{ vars.TEST_PROJECT_ID }}
|
||||||
STACKIT_SERVICE_ACCOUNT_TOKEN: ${{ secrets.TF_ACC_SERVICE_ACCOUNT_TOKEN }}
|
region: 'eu01'
|
||||||
TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_EMAIL: ${{ secrets.TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_EMAIL }}
|
service_account_json: ${{ secrets.TF_ACC_SERVICE_ACCOUNT_JSON }}
|
||||||
TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN: ${{ secrets.TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN }}
|
|
||||||
TF_ACC_TEST_PROJECT_PARENT_CONTAINER_ID: ${{ secrets.TF_ACC_TEST_PROJECT_PARENT_CONTAINER_ID }}
|
|
||||||
TF_ACC_TEST_PROJECT_PARENT_UUID: ${{ secrets.TF_ACC_TEST_PROJECT_PARENT_UUID }}
|
|
||||||
TF_ACC_TEST_PROJECT_USER_EMAIL: ${{ secrets.TF_ACC_TEST_PROJECT_USER_EMAIL }}
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@
|
||||||
<br>
|
<br>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
# STACKIT Terraform Provider
|
# STACKIT Terraform Provider (PRIVATE PREVIEW)
|
||||||
|
|
||||||
[](https://goreportcard.com/report/github.com/stackitcloud/terraform-provider-stackit) [](https://registry.terraform.io/providers/stackitcloud/stackit/latest)  [](https://www.apache.org/licenses/LICENSE-2.0)
|
[](https://registry.terraform.io/providers/stackitcloud/stackit/latest)  [](https://www.apache.org/licenses/LICENSE-2.0)
|
||||||
|
|
||||||
This project is the official [Terraform Provider](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs) for [STACKIT](https://www.stackit.de/en/), which allows you to manage STACKIT resources through Terraform.
|
This project is the official [Terraform Provider](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs) for [STACKIT](https://www.stackit.de/en/), which allows you to manage STACKIT resources through Terraform.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,17 +39,15 @@ type version struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Builder struct {
|
type Builder struct {
|
||||||
SkipClone bool
|
SkipClone bool
|
||||||
SkipCleanup bool
|
SkipCleanup bool
|
||||||
|
PackagesOnly bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Builder) Build() error {
|
func (b *Builder) Build() error {
|
||||||
slog.Info("Starting Builder")
|
slog.Info("Starting Builder")
|
||||||
|
if b.PackagesOnly {
|
||||||
slog.Info(" ... Checking needed commands available")
|
slog.Info(" >>> only generating pkg_gen <<<")
|
||||||
err := checkCommands([]string{"tfplugingen-framework", "tfplugingen-openapi"})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
root, err := getRoot()
|
root, err := getRoot()
|
||||||
|
|
@ -61,13 +59,23 @@ func (b *Builder) Build() error {
|
||||||
}
|
}
|
||||||
slog.Info(" ... using root directory", "dir", *root)
|
slog.Info(" ... using root directory", "dir", *root)
|
||||||
|
|
||||||
if !b.SkipCleanup {
|
if !b.PackagesOnly {
|
||||||
slog.Info("Cleaning up old generator directory")
|
slog.Info(" ... Checking needed commands available")
|
||||||
err = os.RemoveAll(path.Join(*root, GEN_REPO_NAME))
|
err := checkCommands([]string{"tfplugingen-framework", "tfplugingen-openapi"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !b.SkipCleanup {
|
||||||
|
slog.Info("Cleaning up old packages directory")
|
||||||
|
err = os.RemoveAll(path.Join(*root, "pkg_gen"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !b.SkipCleanup && !b.PackagesOnly {
|
||||||
slog.Info("Cleaning up old packages directory")
|
slog.Info("Cleaning up old packages directory")
|
||||||
err = os.RemoveAll(path.Join(*root, "pkg_gen"))
|
err = os.RemoveAll(path.Join(*root, "pkg_gen"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -211,24 +219,26 @@ func (b *Builder) Build() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("Generating service boilerplate")
|
if !b.PackagesOnly {
|
||||||
err = generateServiceFiles(*root, path.Join(*root, GEN_REPO_NAME))
|
slog.Info("Generating service boilerplate")
|
||||||
if err != nil {
|
err = generateServiceFiles(*root, path.Join(*root, GEN_REPO_NAME))
|
||||||
return err
|
if err != nil {
|
||||||
}
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
slog.Info("Copying all service files")
|
slog.Info("Copying all service files")
|
||||||
err = CopyDirectory(
|
err = CopyDirectory(
|
||||||
path.Join(*root, "generated", "internal", "services"),
|
path.Join(*root, "generated", "internal", "services"),
|
||||||
path.Join(*root, "stackit", "internal", "services"),
|
path.Join(*root, "stackit", "internal", "services"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = createBoilerplate(*root, path.Join(*root, "stackit", "internal", "services"))
|
err = createBoilerplate(*root, path.Join(*root, "stackit", "internal", "services"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !b.SkipCleanup {
|
if !b.SkipCleanup {
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
skipCleanup bool
|
skipCleanup bool
|
||||||
skipClone bool
|
skipClone bool
|
||||||
|
packagesOnly bool
|
||||||
)
|
)
|
||||||
|
|
||||||
var buildCmd = &cobra.Command{
|
var buildCmd = &cobra.Command{
|
||||||
|
|
@ -16,8 +17,9 @@ var buildCmd = &cobra.Command{
|
||||||
Long: `...`,
|
Long: `...`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
b := build.Builder{
|
b := build.Builder{
|
||||||
SkipClone: skipClone,
|
SkipClone: skipClone,
|
||||||
SkipCleanup: skipCleanup,
|
SkipCleanup: skipCleanup,
|
||||||
|
PackagesOnly: packagesOnly,
|
||||||
}
|
}
|
||||||
return b.Build()
|
return b.Build()
|
||||||
},
|
},
|
||||||
|
|
@ -30,4 +32,5 @@ func NewBuildCmd() *cobra.Command {
|
||||||
func init() { // nolint: gochecknoinits
|
func init() { // nolint: gochecknoinits
|
||||||
buildCmd.Flags().BoolVarP(&skipCleanup, "skip-clean", "c", false, "Skip cleanup steps")
|
buildCmd.Flags().BoolVarP(&skipCleanup, "skip-clean", "c", false, "Skip cleanup steps")
|
||||||
buildCmd.Flags().BoolVarP(&skipClone, "skip-clone", "g", false, "Skip cloning from git")
|
buildCmd.Flags().BoolVarP(&skipClone, "skip-clone", "g", false, "Skip cloning from git")
|
||||||
|
buildCmd.Flags().BoolVarP(&packagesOnly, "packages-only", "p", false, "Only generate packages")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,233 @@
|
||||||
package postgresflexalpha
|
package postgresflexalpha
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
|
||||||
|
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
||||||
|
"github.com/hashicorp/terraform-plugin-testing/terraform"
|
||||||
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
validFlavor = "2.4"
|
||||||
|
kekKeyRingId = ""
|
||||||
|
kekKeyVersion = ""
|
||||||
|
kekKeySA = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
func testAccPreCheck(t *testing.T) {
|
||||||
|
// TODO: if needed ...
|
||||||
|
}
|
||||||
|
|
||||||
|
//func TestAccResourceExample_parallel(t *testing.T) {
|
||||||
|
// t.Parallel()
|
||||||
|
//
|
||||||
|
// exData := resData{
|
||||||
|
// Region: "eu01",
|
||||||
|
// ServiceAccountFilePath: sa_file,
|
||||||
|
// ProjectID: project_id,
|
||||||
|
// Name: acctest.RandomWithPrefix("tf-acc"),
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// resource.Test(t, resource.TestCase{
|
||||||
|
// ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
|
||||||
|
// Steps: []resource.TestStep{
|
||||||
|
// {
|
||||||
|
// Config: testAccResourceEncryptionExampleConfig(exData),
|
||||||
|
// Check: resource.TestCheckResourceAttrSet("example_resource.test", "id"),
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// })
|
||||||
|
//}
|
||||||
|
|
||||||
|
type resData struct {
|
||||||
|
ServiceAccountFilePath string
|
||||||
|
ProjectID string
|
||||||
|
Region string
|
||||||
|
Name string
|
||||||
|
Flavor string
|
||||||
|
BackupSchedule string
|
||||||
|
RetentionDays uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func getExample() resData {
|
||||||
|
return resData{
|
||||||
|
Region: testutil.Region,
|
||||||
|
ServiceAccountFilePath: testutil.ServiceAccountFile,
|
||||||
|
ProjectID: testutil.ProjectId,
|
||||||
|
Name: acctest.RandomWithPrefix("tf-acc"),
|
||||||
|
Flavor: "2.4",
|
||||||
|
BackupSchedule: "0 0 * * *",
|
||||||
|
RetentionDays: 33,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAccResourceExample_basic(t *testing.T) {
|
||||||
|
exData := getExample()
|
||||||
|
|
||||||
|
resource.Test(t, resource.TestCase{
|
||||||
|
PreCheck: func() { testAccPreCheck(t) },
|
||||||
|
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
|
||||||
|
Steps: []resource.TestStep{
|
||||||
|
{
|
||||||
|
Config: testAccResourceNoEncryptionExampleConfig(exData),
|
||||||
|
Check: resource.ComposeAggregateTestCheckFunc(
|
||||||
|
resource.TestCheckResourceAttr("example_resource.test", "name", exData.Name),
|
||||||
|
resource.TestCheckResourceAttrSet("example_resource.test", "id"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
//// Create and verify
|
||||||
|
//{
|
||||||
|
// Config: testAccResourceNoEncryptionExampleConfig(exData),
|
||||||
|
// Check: resource.ComposeTestCheckFunc(
|
||||||
|
// resource.TestCheckResourceAttr("example_resource.test", "name", exData.Name),
|
||||||
|
// ),
|
||||||
|
//},
|
||||||
|
//// Update and verify
|
||||||
|
//{
|
||||||
|
// Config: testAccResourceNoEncryptionExampleConfig(exData),
|
||||||
|
// Check: resource.ComposeTestCheckFunc(
|
||||||
|
// resource.TestCheckResourceAttr("example_resource.test", "name", exData.Name),
|
||||||
|
// ),
|
||||||
|
//},
|
||||||
|
// Import test
|
||||||
|
{
|
||||||
|
ResourceName: "example_resource.test",
|
||||||
|
ImportState: true,
|
||||||
|
ImportStateVerify: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAccResourceNoEncryptionExampleConfig(data resData) string {
|
||||||
|
return fmt.Sprintf(`
|
||||||
|
|
||||||
|
%[1]s
|
||||||
|
|
||||||
|
resource "stackitprivatepreview_postgresflexalpha_instance" "test" {
|
||||||
|
project_id = %[2]q
|
||||||
|
name = %[3]q
|
||||||
|
backup_schedule = %[4]q
|
||||||
|
retention_days = %[5]d
|
||||||
|
flavor_id = %[6]q
|
||||||
|
replicas = 1
|
||||||
|
storage = {
|
||||||
|
performance_class = "premium-perf2-stackit"
|
||||||
|
size = 10
|
||||||
|
}
|
||||||
|
network = {
|
||||||
|
acl = ["0.0.0.0/0"]
|
||||||
|
access_scope = "PUBLIC"
|
||||||
|
}
|
||||||
|
version = 17
|
||||||
|
}
|
||||||
|
|
||||||
|
`,
|
||||||
|
testutil.PostgresFlexProviderConfig(data.ServiceAccountFilePath),
|
||||||
|
data.ProjectID,
|
||||||
|
data.Name,
|
||||||
|
data.BackupSchedule,
|
||||||
|
data.RetentionDays,
|
||||||
|
data.Flavor,
|
||||||
|
data.Name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testAccResourceEncryptionExampleConfig(data resData) string {
|
||||||
|
return fmt.Sprintf(`
|
||||||
|
|
||||||
|
%[1]s
|
||||||
|
|
||||||
|
resource "stackitprivatepreview_postgresflexalpha_instance" "test" {
|
||||||
|
project_id = %[2]q
|
||||||
|
name = %[3]q
|
||||||
|
backup_schedule = "0 0 * * *"
|
||||||
|
retention_days = 45
|
||||||
|
flavor_id = "2.1"
|
||||||
|
replicas = 1
|
||||||
|
storage = {
|
||||||
|
performance_class = "premium-perf2-stackit"
|
||||||
|
size = 10
|
||||||
|
}
|
||||||
|
encryption = {
|
||||||
|
kek_key_id = "key01"
|
||||||
|
kek_key_ring_id = "key_ring_01"
|
||||||
|
kek_key_version = 1
|
||||||
|
service_account = "service@account.email"
|
||||||
|
}
|
||||||
|
network = {
|
||||||
|
acl = ["0.0.0.0/0"]
|
||||||
|
access_scope = "PUBLIC"
|
||||||
|
}
|
||||||
|
version = 14
|
||||||
|
}
|
||||||
|
|
||||||
|
`,
|
||||||
|
testutil.PostgresFlexProviderConfig(data.ServiceAccountFilePath),
|
||||||
|
data.ProjectID,
|
||||||
|
data.Name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testCheckResourceExists(resourceName string) resource.TestCheckFunc {
|
||||||
|
return func(s *terraform.State) error {
|
||||||
|
rs, ok := s.RootModule().Resources[resourceName]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("resource not found: %s", resourceName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rs.Primary.ID == "" {
|
||||||
|
return fmt.Errorf("resource ID not set")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify resource exists in the API
|
||||||
|
//client := testAccProvider.Meta().(*APIClient)
|
||||||
|
//_, err := client.GetResource(rs.Primary.ID)
|
||||||
|
//if err != nil {
|
||||||
|
// return fmt.Errorf("error fetching resource: %w", err)
|
||||||
|
//}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupMockServer() *httptest.Server {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/resources", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.Method {
|
||||||
|
case http.MethodPost:
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
|
"id": "mock-id-123",
|
||||||
|
"name": "test-resource",
|
||||||
|
})
|
||||||
|
case http.MethodGet:
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode([]map[string]string{})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return httptest.NewServer(mux)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnitResourceCreate(t *testing.T) {
|
||||||
|
server := setupMockServer()
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
// Configure provider to use mock server URL
|
||||||
|
os.Setenv("API_ENDPOINT", server.URL)
|
||||||
|
|
||||||
|
// Run unit tests against mock
|
||||||
|
}
|
||||||
|
|
||||||
// type postgresFlexClientMocked struct {
|
// type postgresFlexClientMocked struct {
|
||||||
// returnError bool
|
// returnError bool
|
||||||
// getFlavorsResp *postgresflex.GetFlavorsResponse
|
// getFlavorsResp *postgresflex.GetFlavorsResponse
|
||||||
|
|
@ -20,21 +241,40 @@ import (
|
||||||
// return c.getFlavorsResp, nil
|
// return c.getFlavorsResp, nil
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func TestNewInstanceResource(t *testing.T) {
|
//func TestNewInstanceResource(t *testing.T) {
|
||||||
tests := []struct {
|
// exData := resData{
|
||||||
name string
|
// Region: "eu01",
|
||||||
want resource.Resource
|
// ServiceAccountFilePath: sa_file,
|
||||||
}{
|
// ProjectID: project_id,
|
||||||
{
|
// Name: "testRes",
|
||||||
name: "create empty instance resource",
|
// }
|
||||||
want: &instanceResource{},
|
// resource.Test(t, resource.TestCase{
|
||||||
},
|
// ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
|
||||||
}
|
// Steps: []resource.TestStep{
|
||||||
for _, tt := range tests {
|
// {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
// Config: testAccResourceEncryptionExampleConfig(exData),
|
||||||
if got := NewInstanceResource(); !reflect.DeepEqual(got, tt.want) {
|
// Check: resource.ComposeAggregateTestCheckFunc(
|
||||||
t.Errorf("NewInstanceResource() = %v, want %v", got, tt.want)
|
// resource.TestCheckResourceAttr("example_resource.test", "name", exData.Name),
|
||||||
}
|
// resource.TestCheckResourceAttrSet("example_resource.test", "id"),
|
||||||
})
|
// ),
|
||||||
}
|
// },
|
||||||
}
|
// },
|
||||||
|
// })
|
||||||
|
//
|
||||||
|
// //tests := []struct {
|
||||||
|
// // name string
|
||||||
|
// // want resource.Resource
|
||||||
|
// //}{
|
||||||
|
// // {
|
||||||
|
// // name: "create empty instance resource",
|
||||||
|
// // want: &instanceResource{},
|
||||||
|
// // },
|
||||||
|
// //}
|
||||||
|
// //for _, tt := range tests {
|
||||||
|
// // t.Run(tt.name, func(t *testing.T) {
|
||||||
|
// // if got := NewInstanceResource(); !reflect.DeepEqual(got, tt.want) {
|
||||||
|
// // t.Errorf("NewInstanceResource() = %v, want %v", got, tt.want)
|
||||||
|
// // }
|
||||||
|
// // })
|
||||||
|
// //}
|
||||||
|
//}
|
||||||
|
|
|
||||||
|
|
@ -146,15 +146,11 @@ func (d *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
|
||||||
data.Id = types.Int64Value(dbId)
|
data.Id = types.Int64Value(dbId)
|
||||||
data.TfId = utils.BuildInternalTerraformId(projectId, region, instanceId, databaseName)
|
data.TfId = utils.BuildInternalTerraformId(projectId, region, instanceId, databaseName)
|
||||||
data.Owner = types.StringValue(databaseResp.GetOwner())
|
data.Owner = types.StringValue(databaseResp.GetOwner())
|
||||||
|
data.CollationName = types.StringValue(databaseResp.GetCollationName())
|
||||||
// TODO: fill remaining fields
|
data.CompatibilityLevel = types.Int64Value(databaseResp.GetCompatibilityLevel())
|
||||||
// data.CollationName = types.Sometype(apiResponse.GetCollationName())
|
data.DatabaseName = types.StringValue(databaseResp.GetName())
|
||||||
// data.CompatibilityLevel = types.Sometype(apiResponse.GetCompatibilityLevel())
|
// data.InstanceId = types.StringValue(databaseResp.GetInstanceId())
|
||||||
// data.DatabaseName = types.Sometype(apiResponse.GetDatabaseName())
|
|
||||||
// data.Id = types.Sometype(apiResponse.GetId())
|
|
||||||
// data.InstanceId = types.Sometype(apiResponse.GetInstanceId())
|
|
||||||
// data.Name = types.Sometype(apiResponse.GetName())
|
// data.Name = types.Sometype(apiResponse.GetName())
|
||||||
// data.Owner = types.Sometype(apiResponse.GetOwner())
|
|
||||||
// data.ProjectId = types.Sometype(apiResponse.GetProjectId())
|
// data.ProjectId = types.Sometype(apiResponse.GetProjectId())
|
||||||
// data.Region = types.Sometype(apiResponse.GetRegion())
|
// data.Region = types.Sometype(apiResponse.GetRegion())
|
||||||
|
|
||||||
|
|
|
||||||
11
stackit/internal/testutil/assert.go
Normal file
11
stackit/internal/testutil/assert.go
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package testutil
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Equal[V comparable](t *testing.T, got, expected V) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if expected != got {
|
||||||
|
t.Errorf("assert equal failed:\ngot: %v \nexpected: %v", got, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
package testutil
|
package testutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -20,7 +19,8 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Default location of credentials JSON
|
// Default location of credentials JSON
|
||||||
credentialsFilePath = ".stackit/credentials.json" //nolint:gosec // linter false positive
|
// credentialsFilePath = ".stackit/credentials.json" //nolint:gosec // linter false positive
|
||||||
|
serviceAccountFilePath = ".stackit/service_account.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
@ -52,6 +52,8 @@ var (
|
||||||
// ProjectId is the id of project used for tests
|
// ProjectId is the id of project used for tests
|
||||||
ProjectId = os.Getenv("TF_ACC_PROJECT_ID")
|
ProjectId = os.Getenv("TF_ACC_PROJECT_ID")
|
||||||
Region = os.Getenv("TF_ACC_REGION")
|
Region = os.Getenv("TF_ACC_REGION")
|
||||||
|
// ServiceAccountFile is the json file of the service account
|
||||||
|
ServiceAccountFile = os.Getenv("TF_ACC_SERVICE_ACCOUNT_FILE")
|
||||||
// ServerId is the id of a server used for some tests
|
// ServerId is the id of a server used for some tests
|
||||||
ServerId = getenv("TF_ACC_SERVER_ID", "")
|
ServerId = getenv("TF_ACC_SERVER_ID", "")
|
||||||
// TestProjectParentContainerID is the container id of the parent resource under which projects are created as part of the resource-manager acceptance tests
|
// TestProjectParentContainerID is the container id of the parent resource under which projects are created as part of the resource-manager acceptance tests
|
||||||
|
|
@ -108,6 +110,7 @@ func ObservabilityProviderConfig() string {
|
||||||
ObservabilityCustomEndpoint,
|
ObservabilityCustomEndpoint,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CdnProviderConfig() string {
|
func CdnProviderConfig() string {
|
||||||
if CdnCustomEndpoint == "" {
|
if CdnCustomEndpoint == "" {
|
||||||
return `
|
return `
|
||||||
|
|
@ -316,8 +319,10 @@ func PostgresFlexProviderConfig(saFile string) string {
|
||||||
}
|
}
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
provider "stackitprivatepreview" {
|
provider "stackitprivatepreview" {
|
||||||
|
service_account_key_path = "%s"
|
||||||
postgresflex_custom_endpoint = "%s"
|
postgresflex_custom_endpoint = "%s"
|
||||||
}`,
|
}`,
|
||||||
|
saFile,
|
||||||
PostgresFlexCustomEndpoint,
|
PostgresFlexCustomEndpoint,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -353,13 +358,13 @@ func RedisProviderConfig() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResourceManagerProviderConfig() string {
|
func ResourceManagerProviderConfig() string {
|
||||||
token := GetTestProjectServiceAccountToken("")
|
key := GetTestProjectServiceAccountJson("")
|
||||||
if ResourceManagerCustomEndpoint == "" || AuthorizationCustomEndpoint == "" {
|
if ResourceManagerCustomEndpoint == "" || AuthorizationCustomEndpoint == "" {
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
provider "stackitprivatepreview" {
|
provider "stackitprivatepreview" {
|
||||||
service_account_token = "%s"
|
service_account_key = "%s"
|
||||||
}`,
|
}`,
|
||||||
token,
|
key,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
|
|
@ -370,7 +375,7 @@ func ResourceManagerProviderConfig() string {
|
||||||
}`,
|
}`,
|
||||||
ResourceManagerCustomEndpoint,
|
ResourceManagerCustomEndpoint,
|
||||||
AuthorizationCustomEndpoint,
|
AuthorizationCustomEndpoint,
|
||||||
token,
|
key,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -389,17 +394,20 @@ func SecretsManagerProviderConfig() string {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SQLServerFlexProviderConfig() string {
|
func SQLServerFlexProviderConfig(saFile string) string {
|
||||||
if SQLServerFlexCustomEndpoint == "" {
|
if SQLServerFlexCustomEndpoint == "" {
|
||||||
return `
|
return fmt.Sprintf(`
|
||||||
provider "stackitprivatepreview" {
|
provider "stackitprivatepreview" {
|
||||||
default_region = "eu01"
|
default_region = "eu01"
|
||||||
}`
|
service_account_key_path = "%s"
|
||||||
|
}`, saFile)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
provider "stackitprivatepreview" {
|
provider "stackitprivatepreview" {
|
||||||
|
service_account_key_path = "%s"
|
||||||
sqlserverflex_custom_endpoint = "%s"
|
sqlserverflex_custom_endpoint = "%s"
|
||||||
}`,
|
}`,
|
||||||
|
saFile,
|
||||||
SQLServerFlexCustomEndpoint,
|
SQLServerFlexCustomEndpoint,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -531,7 +539,7 @@ func GetTestProjectServiceAccountJson(path string) string {
|
||||||
var err error
|
var err error
|
||||||
token, tokenSet := os.LookupEnv("TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_JSON")
|
token, tokenSet := os.LookupEnv("TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_JSON")
|
||||||
if !tokenSet || token == "" {
|
if !tokenSet || token == "" {
|
||||||
token, err = readTestTokenFromCredentialsFile(path)
|
token, err = readTestServiceAccountJsonFromFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
@ -539,23 +547,53 @@ func GetTestProjectServiceAccountJson(path string) string {
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTestProjectServiceAccountToken(path string) string {
|
//func GetTestProjectServiceAccountToken(path string) string {
|
||||||
var err error
|
// var err error
|
||||||
token, tokenSet := os.LookupEnv("TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN")
|
// token, tokenSet := os.LookupEnv("TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN")
|
||||||
if !tokenSet || token == "" {
|
// if !tokenSet || token == "" {
|
||||||
token, err = readTestTokenFromCredentialsFile(path)
|
// token, err = readTestTokenFromCredentialsFile(path)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return ""
|
// return ""
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return token
|
// return token
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
|
//func readTestTokenFromCredentialsFile(path string) (string, error) {
|
||||||
|
// if path == "" {
|
||||||
|
// customPath, customPathSet := os.LookupEnv("STACKIT_CREDENTIALS_PATH")
|
||||||
|
// if !customPathSet || customPath == "" {
|
||||||
|
// path = credentialsFilePath
|
||||||
|
// home, err := os.UserHomeDir()
|
||||||
|
// if err != nil {
|
||||||
|
// return "", fmt.Errorf("getting home directory: %w", err)
|
||||||
|
// }
|
||||||
|
// path = filepath.Join(home, path)
|
||||||
|
// } else {
|
||||||
|
// path = customPath
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// credentialsRaw, err := os.ReadFile(path)
|
||||||
|
// if err != nil {
|
||||||
|
// return "", fmt.Errorf("opening file: %w", err)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// var credentials struct {
|
||||||
|
// TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN string `json:"TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN"`
|
||||||
|
// }
|
||||||
|
// err = json.Unmarshal(credentialsRaw, &credentials)
|
||||||
|
// if err != nil {
|
||||||
|
// return "", fmt.Errorf("unmarshalling credentials: %w", err)
|
||||||
|
// }
|
||||||
|
// return credentials.TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN, nil
|
||||||
|
//}
|
||||||
|
|
||||||
func readTestTokenFromCredentialsFile(path string) (string, error) {
|
func readTestServiceAccountJsonFromFile(path string) (string, error) {
|
||||||
if path == "" {
|
if path == "" {
|
||||||
customPath, customPathSet := os.LookupEnv("STACKIT_CREDENTIALS_PATH")
|
customPath, customPathSet := os.LookupEnv("STACKIT_SERVICE_ACCOUNT_PATH")
|
||||||
if !customPathSet || customPath == "" {
|
if !customPathSet || customPath == "" {
|
||||||
path = credentialsFilePath
|
path = serviceAccountFilePath
|
||||||
home, err := os.UserHomeDir()
|
home, err := os.UserHomeDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("getting home directory: %w", err)
|
return "", fmt.Errorf("getting home directory: %w", err)
|
||||||
|
|
@ -570,15 +608,7 @@ func readTestTokenFromCredentialsFile(path string) (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("opening file: %w", err)
|
return "", fmt.Errorf("opening file: %w", err)
|
||||||
}
|
}
|
||||||
|
return string(credentialsRaw), nil
|
||||||
var credentials struct {
|
|
||||||
TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN string `json:"TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN"`
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(credentialsRaw, &credentials)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("unmarshalling credentials: %w", err)
|
|
||||||
}
|
|
||||||
return credentials.TF_ACC_TEST_PROJECT_SERVICE_ACCOUNT_TOKEN, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getenv(key, defaultValue string) string {
|
func getenv(key, defaultValue string) string {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue