Skip to content

Commit

Permalink
Add test for project scope retrieve in deployment freeze data-source
Browse files Browse the repository at this point in the history
  • Loading branch information
HuyPhanNguyen committed Dec 9, 2024
1 parent 5f4a2db commit 0900726
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
46 changes: 34 additions & 12 deletions octopusdeploy_framework/datasource_deployment_freeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"strings"
"testing"
"time"
)
Expand All @@ -24,30 +25,40 @@ func TestAccDataSourceDeploymentFreezes(t *testing.T) {
ProtoV6ProviderFactories: ProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, false, projectName, environmentName, tenantName),
Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, false, false, projectName, environmentName, tenantName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttr(dataSourceName, "partial_name", freezeName),
resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.name", freezeName),
resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.project_environment_scope.%", "0"),
),
},
{
Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, true, projectName, environmentName, tenantName),
Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, true, false, projectName, environmentName, tenantName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.project_environment_scope.%", "1"),
),
},
{
Config: testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName, startTime, endTime, true, true, projectName, environmentName, tenantName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "id"),
resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.#", "1"),
resource.TestCheckResourceAttrSet(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.0.tenant_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.0.project_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "deployment_freezes.0.tenant_project_environment_scope.0.environment_id"),
resource.TestCheckResourceAttr(dataSourceName, "deployment_freezes.0.project_environment_scope.%", "1"),
),
},
},
})
}

func testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName string, startTime, endTime time.Time, includeTenant bool, projectName, environmentName, tenantName string) string {
func testAccDataSourceDeploymentFreezesConfig(spaceName, freezeName string, startTime, endTime time.Time, includeProject bool, includeTenant bool, projectName, environmentName, tenantName string) string {
baseConfig := fmt.Sprintf(`
resource "octopusdeploy_space" "test_space" {
name = "%s"
Expand Down Expand Up @@ -98,6 +109,17 @@ resource "octopusdeploy_deployment_freeze" "test_freeze" {
}
`, spaceName, environmentName, projectName, freezeName, startTime.Format(time.RFC3339), endTime.Format(time.RFC3339))

if includeProject {
projectConfig := fmt.Sprintf(`
resource "octopusdeploy_deployment_freeze_project" "test_project_scope" {
deploymentfreeze_id = octopusdeploy_deployment_freeze.test_freeze.id
project_id = octopusdeploy_project.test_project.id
environment_ids = [octopusdeploy_environment.test_environment.id]
}
`)
baseConfig = baseConfig + projectConfig
}

if includeTenant {
tenantConfig := fmt.Sprintf(`
resource "octopusdeploy_tenant" "test_tenant" {
Expand Down Expand Up @@ -134,17 +156,16 @@ data "octopusdeploy_deployment_freezes" "test_freeze" {
take = 1
depends_on = [`

deps := []string{"octopusdeploy_deployment_freeze.test_freeze"}
if includeProject {
deps = append(deps, "octopusdeploy_deployment_freeze_project.test_project_scope")
}
if includeTenant {
datasourceConfig += `
octopusdeploy_deployment_freeze.test_freeze,
octopusdeploy_deployment_freeze_tenant.test_tenant_scope
`
} else {
datasourceConfig += `
octopusdeploy_deployment_freeze.test_freeze
`
deps = append(deps, "octopusdeploy_deployment_freeze_tenant.test_tenant_scope")
}

datasourceConfig += "\n " + strings.Join(deps, ",\n ") + "\n "

datasourceConfig += `]
}
Expand All @@ -156,5 +177,6 @@ output "octopus_freeze_id" {
value = data.octopusdeploy_deployment_freezes.test_freeze.deployment_freezes[0].id
}
`
return baseConfig + fmt.Sprintf(datasourceConfig, freezeName)
var config = baseConfig + fmt.Sprintf(datasourceConfig, freezeName)
return config
}
4 changes: 4 additions & 0 deletions octopusdeploy_framework/schemas/deployment_freeze.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,23 @@ func (d DeploymentFreezeSchema) GetDatasourceSchema() datasourceSchema.Schema {
"name": GetReadonlyNameDatasourceSchema(),
"start": datasourceSchema.StringAttribute{
Description: "The start time of the freeze",
Optional: false,
Computed: true,
},
"end": datasourceSchema.StringAttribute{
Description: "The end time of the freeze",
Optional: false,
Computed: true,
},
"project_environment_scope": datasourceSchema.MapAttribute{
ElementType: types.ListType{ElemType: types.StringType},
Description: "The project environment scope of the deployment freeze",
Optional: false,
Computed: true,
},
"tenant_project_environment_scope": datasourceSchema.ListNestedAttribute{
Description: "The tenant project environment scope of the deployment freeze",
Optional: false,
Computed: true,
NestedObject: datasourceSchema.NestedAttributeObject{
Attributes: map[string]datasourceSchema.Attribute{
Expand Down

0 comments on commit 0900726

Please sign in to comment.