Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AV-65766] Acceptance Tests AppServices #98

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions internal/resources/acceptance_tests/appservice_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package acceptance_tests

import (
"fmt"
"testing"

cfg "terraform-provider-capella/internal/testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAppServiceResource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing
{
Config: testAccAppServiceResourceConfig(cfg.Cfg),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "name", "test-terraform-app-service"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "description", "description"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "compute.cpu", "2"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "compute.ram", "4"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "nodes", "2"),
),
},
//// ImportState testing
{
ResourceName: "capella_app_service.new_app_service",
ImportStateIdFunc: generateAppServiceImportId,
ImportState: true,
ImportStateVerify: true,
},
// Update and Read testing
{
Config: testAccAppServiceResourceConfigUpdate(cfg.Cfg),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "name", "test-terraform-app-service"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "description", "description"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "compute.cpu", "2"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "compute.ram", "4"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "nodes", "3"),
),
},
{
Config: testAccAppServiceResourceConfigUpdateWithIfMatch(cfg.Cfg),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "name", "test-terraform-app-service"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "description", "description"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "compute.cpu", "4"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "compute.ram", "8"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "nodes", "2"),
resource.TestCheckResourceAttr("capella_app_service.new_app_service", "if_match", "2"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}

func testAccAppServiceResourceConfig(cfg string) string {
return fmt.Sprintf(`
%[1]s

resource "capella_app_service" "new_app_service" {
organization_id = var.organization_id
project_id = var.project_id
cluster_id = var.cluster_id
name = "test-terraform-app-service"
description = "description"
compute = {
cpu = 2
ram = 4
}
}
`, cfg)
}

func testAccAppServiceResourceConfigUpdate(cfg string) string {
return fmt.Sprintf(`
%[1]s

resource "capella_app_service" "new_app_service" {
organization_id = var.organization_id
project_id = var.project_id
cluster_id = var.cluster_id
name = "test-terraform-app-service"
description = "description"
compute = {
cpu = 2
ram = 4
}
nodes = 3
}
`, cfg)
}

func testAccAppServiceResourceConfigUpdateWithIfMatch(cfg string) string {
return fmt.Sprintf(`
%[1]s

resource "capella_app_service" "new_app_service" {
organization_id = var.organization_id
project_id = var.project_id
cluster_id = var.cluster_id
name = "test-terraform-app-service"
description = "description"
if_match = 2
compute = {
cpu = 4
ram = 8
}
nodes = 2
}
`, cfg)
}

func generateAppServiceImportId(state *terraform.State) (string, error) {
resourceName := "capella_app_service.new_app_service"
var rawState map[string]string
for _, m := range state.Modules {
if len(m.Resources) > 0 {
if v, ok := m.Resources[resourceName]; ok {
rawState = v.Primary.Attributes
}
}
}
fmt.Printf("raw state %s", rawState)
return fmt.Sprintf("id=%s,cluster_id=%s,project_id=%s,organization_id=%s", rawState["id"], rawState["cluster_id"], rawState["project_id"], rawState["organization_id"]), nil
}
4 changes: 4 additions & 0 deletions internal/resources/appservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (a *AppService) Read(ctx context.Context, req resource.ReadRequest, resp *r
return
}

if !state.IfMatch.IsUnknown() && !state.IfMatch.IsNull() {
refreshedState.IfMatch = state.IfMatch
}

// Set refreshed state
diags = resp.State.Set(ctx, &refreshedState)
resp.Diagnostics.Append(diags...)
Expand Down
9 changes: 9 additions & 0 deletions internal/testing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ variable "organization_id" {
description = "Capella Organization ID"
}

variable "project_id" {
description = "Capella Project ID"
}

variable "cluster_id" {
description = "Capella Project ID"
}

variable "auth_token" {
description = "Authentication API Key"
sensitive = true
}

provider "capella" {
host = var.host
authentication_token = var.auth_token
Expand Down