Skip to content

Commit

Permalink
Create resources required for listening worker test
Browse files Browse the repository at this point in the history
  • Loading branch information
denys-octopus committed Nov 13, 2024
1 parent 5686c7f commit 62b0763
Showing 1 changed file with 127 additions and 57 deletions.
184 changes: 127 additions & 57 deletions octopusdeploy_framework/resource_listening_tentacle_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,33 @@ import (
)

type listeningTentacleWorkerTestData struct {
name string
spaceID string
isDisabled bool
workerPoolIDs []string
machinePolicyID string
uri string
thumbprint string
proxyID string
name string
spaceID string
isDisabled bool
uri string
thumbprint string
}

type listeningTentacleWorkerTestDependenciesData struct {
policy string
pool1 string
pool2 string
proxy string
}

func TestAccOctopusDeployListeningTentacleWorker(t *testing.T) {
localName := acctest.RandStringFromCharSet(20, acctest.CharSetAlpha)
prefix := "octopusdeploy_listening_tentacle_worker." + localName
createData := listeningTentacleWorkerTestData{
name: acctest.RandStringFromCharSet(20, acctest.CharSetAlpha),
workerPoolIDs: []string{acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)},
machinePolicyID: acctest.RandStringFromCharSet(8, acctest.CharSetAlpha),
uri: "https://listening.test",
thumbprint: strconv.FormatInt(int64(acctest.RandIntRange(0, 1024)), 16),
name: acctest.RandStringFromCharSet(20, acctest.CharSetAlpha),
uri: "https://listening.test",
thumbprint: strconv.FormatInt(int64(acctest.RandIntRange(0, 1024)), 16),
}
updateData := listeningTentacleWorkerTestData{
name: createData.name + "-updated",
workerPoolIDs: append(createData.workerPoolIDs, acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)),
machinePolicyID: acctest.RandStringFromCharSet(8, acctest.CharSetAlpha),
uri: "https://listening.test.updated",
thumbprint: strconv.FormatInt(int64(acctest.RandIntRange(0, 1024)), 16),
isDisabled: true,
proxyID: acctest.RandStringFromCharSet(8, acctest.CharSetAlpha),
name: createData.name + "-updated",
uri: "https://listening.test.updated",
thumbprint: strconv.FormatInt(int64(acctest.RandIntRange(0, 1024)), 16),
isDisabled: true,
}

resource.Test(t, resource.TestCase{
Expand All @@ -48,81 +47,161 @@ func TestAccOctopusDeployListeningTentacleWorker(t *testing.T) {
ProtoV6ProviderFactories: ProtoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: testListeningTentacleWorkerMandatory(createData, localName),
Check: testAssertListeningTentacleWorkerMandatoryAttributes(createData, prefix),
Config: testListeningTentacleWorkerCreate(createData, localName),
Check: testAssertListeningTentacleWorkerCreate(createData, prefix),
},
{
Config: testListeningTentacleWorkerAll(updateData, localName),
Check: testAssertListeningTentacleWorkerAllAttributes(updateData, prefix),
Config: testListeningTentacleWorkerUpdate(updateData, localName),
Check: testAssertListeningTentacleWorkerUpdate(updateData, prefix),
},
},
})
}

func testListeningTentacleWorkerMandatory(data listeningTentacleWorkerTestData, localName string) string {
func testListeningTentacleWorkerCreate(data listeningTentacleWorkerTestData, localName string) string {
source, references := testListeningTentacleWorkerDependencies(localName)

return fmt.Sprintf(`
%s
resource "octopusdeploy_listening_tentacle_worker" "%s" {
name = "%s"
machine_policy_id = "%s"
worker_pool_ids = %s
worker_pool_ids = [%s]
uri = "%s"
thumbprint = "%s"
}
`,
source,
localName,
data.name,
data.machinePolicyID,
testSerializeWorkerPoolIdsForResource(data.workerPoolIDs),
references.policy,
references.pool1,
data.uri,
data.thumbprint,
)
}

func testListeningTentacleWorkerAll(data listeningTentacleWorkerTestData, localName string) string {
func testAssertListeningTentacleWorkerCreate(expected listeningTentacleWorkerTestData, prefix string) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(prefix, "name", expected.name),
resource.TestCheckResourceAttrWith(prefix, "machine_policy_id", func(value string) error {
if len(value) > 0 {
return nil
}
return fmt.Errorf("machine policy should be set")
}),
resource.TestCheckResourceAttrWith(prefix, "worker_pool_ids", func(value string) error {
if strings.HasPrefix(value, "[") && strings.HasSuffix(value, "]") {
return nil
}
return fmt.Errorf("worker pools should be set")
}),
resource.TestCheckResourceAttr(prefix, "uri", expected.uri),
resource.TestCheckResourceAttr(prefix, "thumbprint", expected.thumbprint),
resource.TestCheckNoResourceAttr(prefix, "proxy_id"),
resource.TestCheckNoResourceAttr(prefix, "is_disabled"),
)
}

func testListeningTentacleWorkerUpdate(data listeningTentacleWorkerTestData, localName string) string {
source, references := testListeningTentacleWorkerDependencies(localName)

return fmt.Sprintf(`
%s
resource "octopusdeploy_listening_tentacle_worker" "%s" {
name = "%s"
machine_policy_id = "%s"
worker_pool_ids = %s
worker_pool_ids = [%s, %s]
uri = "%s"
thumbprint = "%s"
proxy_id = "%s"
proxy_id = %s
is_disabled = "%v"
}
`,
source,
localName,
data.name,
data.machinePolicyID,
testSerializeWorkerPoolIdsForResource(data.workerPoolIDs),
references.policy,
references.pool1,
references.pool2,
data.uri,
data.thumbprint,
data.proxyID,
references.proxy,
data.isDisabled,
)
}

func testAssertListeningTentacleWorkerMandatoryAttributes(expected listeningTentacleWorkerTestData, prefix string) resource.TestCheckFunc {
func testAssertListeningTentacleWorkerUpdate(expected listeningTentacleWorkerTestData, prefix string) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(prefix, "name", expected.name),
resource.TestCheckResourceAttr(prefix, "machine_policy_id", expected.machinePolicyID),
resource.TestCheckResourceAttr(prefix, "worker_pool_ids", testSerializeWorkerPoolIdsForResource(expected.workerPoolIDs)),
resource.TestCheckResourceAttrWith(prefix, "machine_policy_id", func(value string) error {
if len(value) > 0 {
return nil
}
return fmt.Errorf("machine policy should be set")
}),
resource.TestCheckResourceAttrWith(prefix, "worker_pool_ids", func(value string) error {
if strings.HasPrefix(value, "[") && strings.HasSuffix(value, "]") {
return nil
}
return fmt.Errorf("worker pools should be set")
}),
resource.TestCheckResourceAttr(prefix, "uri", expected.uri),
resource.TestCheckResourceAttr(prefix, "thumbprint", expected.thumbprint),
resource.TestCheckNoResourceAttr(prefix, "proxy_id"),
resource.TestCheckNoResourceAttr(prefix, "is_disabled"),
resource.TestCheckResourceAttrWith(prefix, "proxy_id", func(value string) error {
if len(value) > 0 {
return nil
}
return fmt.Errorf("machine proxy should be set")
}),
resource.TestCheckResourceAttr(prefix, "is_disabled", strconv.FormatBool(expected.isDisabled)),
)
}

func testAssertListeningTentacleWorkerAllAttributes(expected listeningTentacleWorkerTestData, prefix string) resource.TestCheckFunc {
return resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(prefix, "name", expected.name),
resource.TestCheckResourceAttr(prefix, "machine_policy_id", expected.machinePolicyID),
resource.TestCheckResourceAttr(prefix, "worker_pool_ids", testSerializeWorkerPoolIdsForResource(expected.workerPoolIDs)),
resource.TestCheckResourceAttr(prefix, "uri", expected.uri),
resource.TestCheckResourceAttr(prefix, "thumbprint", expected.thumbprint),
resource.TestCheckResourceAttr(prefix, "proxy_id", expected.proxyID),
resource.TestCheckResourceAttr(prefix, "is_disabled", strconv.FormatBool(expected.isDisabled)),
func testListeningTentacleWorkerDependencies(localName string) (string, listeningTentacleWorkerTestDependenciesData) {
policy := fmt.Sprintf("policy_%s", localName)
pool1 := fmt.Sprintf("pool_1_%s", localName)
pool2 := fmt.Sprintf("pool_2_%s", localName)
proxy := fmt.Sprintf("proxy_%s", localName)
source := fmt.Sprintf(`
resource "octopusdeploy_machine_policy" "%s" {
name = "Listening policy"
}
resource "octopusdeploy_static_worker_pool" "%s" {
name = "Listening poll 1"
description = "First pool of listening workers"
sort_order = 42
}
resource "octopusdeploy_static_worker_pool" "%s" {
name = "Listening poll 2"
description = "Second pool of listening workers"
sort_order = 43
}
resource "octopusdeploy_machine_proxy" "%s" {
name = "Listening proxy"
host = "localhost"
port = 20034
username = "user_proxy"
password = "secret_proxy"
}
`,
policy,
pool1,
pool2,
proxy,
)

dependencies := listeningTentacleWorkerTestDependenciesData{
policy: fmt.Sprintf("octopusdeploy_machine_policy.%s.id", policy),
pool1: fmt.Sprintf("octopusdeploy_static_worker_pool.%s.id", pool1),
pool2: fmt.Sprintf("octopusdeploy_static_worker_pool.%s.id", pool2),
proxy: fmt.Sprintf("octopusdeploy_machine_proxy.%s.id", proxy),
}

return source, dependencies
}

func testListeningTentacleWorkerCheckDestroy(s *terraform.State) error {
Expand All @@ -139,12 +218,3 @@ func testListeningTentacleWorkerCheckDestroy(s *terraform.State) error {

return nil
}

func testSerializeWorkerPoolIdsForResource(poolIds []string) string {
quotedPoolIds := make([]string, len(poolIds))
for i, poolId := range poolIds {
quotedPoolIds[i] = fmt.Sprintf(`"%s"`, poolId)
}

return "[" + strings.Join(quotedPoolIds, ",") + "]"
}

0 comments on commit 62b0763

Please sign in to comment.