terraform-provider-docker/internal/actiontests/docker_containers_data_source_test.go
Copilot 77769bd25c
Some checks are pending
Acc Tests / Detect acc-test matrix (push) Waiting to run
Acc Tests / acc-test (push) Blocked by required conditions
Compile Binaries / compile-fast (push) Waiting to run
Compile Binaries / compile (push) Waiting to run
golangci-lint / lint (push) Waiting to run
Unit Tests / unit-test (push) Waiting to run
Website Checks / markdown-link-check (push) Waiting to run
Docs and Website Lint / website-generation (push) Waiting to run
Docs and Website Lint / website-lint-spellcheck-tffmt (push) Waiting to run
Docs and Website Lint / markdown-lint (push) Waiting to run
Add Plugin Framework docker_containers data source for Docker container enumeration (#893)
* Initial plan

* feat: add docker_containers framework data source

Agent-Logs-Url: https://github.com/kreuzwerker/terraform-provider-docker/sessions/212ab7a6-43ed-4ff4-8269-4e1838c8b642

Co-authored-by: Junkern <3775779+Junkern@users.noreply.github.com>

* test: add roundtrip e2e test for docker_containers data source

Agent-Logs-Url: https://github.com/kreuzwerker/terraform-provider-docker/sessions/1eb6cd4e-0843-459e-9204-1a4ebfd18bf6

Co-authored-by: Junkern <3775779+Junkern@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Junkern <3775779+Junkern@users.noreply.github.com>
2026-04-06 15:20:19 +02:00

67 lines
1.8 KiB
Go

package actiontests
import (
"fmt"
"strings"
"testing"
"time"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
func TestDockerContainersDataSource_roundtrip(t *testing.T) {
preCheckDocker(t)
containerName := fmt.Sprintf("tf-acc-docker-containers-%d", time.Now().UnixNano())
resource.UnitTest(t, resource.TestCase{
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "docker_image" "busybox" {
name = "busybox:1.35.0"
}
resource "docker_container" "target" {
name = %q
image = docker_image.busybox.image_id
must_run = false
command = ["sh", "-c", "echo hello"]
}
data "docker_containers" "this" {
depends_on = [docker_container.target]
}
`, containerName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.docker_containers.this", "id"),
resource.TestCheckResourceAttrSet("data.docker_containers.this", "containers.#"),
testCheckDockerContainersDataSourceContainsContainer("data.docker_containers.this", containerName),
),
},
},
})
}
func testCheckDockerContainersDataSourceContainsContainer(resourceName string, containerName string) resource.TestCheckFunc {
return func(state *terraform.State) error {
rs, ok := state.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("resource %q not found in state", resourceName)
}
for key, value := range rs.Primary.Attributes {
if !strings.Contains(key, ".names.") {
continue
}
if value == containerName || strings.TrimPrefix(value, "/") == containerName {
return nil
}
}
return fmt.Errorf("container %q not found in data source state: %#v", containerName, rs.Primary.Attributes)
}
}