smutel / terraform-provider-netbox

Terraform provider for Netbox
ISC License
58 stars 19 forks source link

feat: Add first tests to provider #149

Closed amhn closed 1 year ago

amhn commented 1 year ago

fixes: #155

This is a draft PR to experiment with Test cases.

Tests can be started with

TF_ACC=1 go test -v ./...

The netbox connection can be configured with the NETBOX_ env vars. NETBOX_TOKEN is required.

Some ids are currently hard-coded as there is no resource yet to set them up. Notably cluster_id for virtualization_vm and rir_id for ipam_aggregate,

TestAccNetboxVirtualizationVMPrimaryIPAddress is a test case for the current problem in https://github.com/smutel/terraform-provider-netbox/pull/147

To keep this PR small, the dependencies are not yet added to vendor/.

Do you think this is a direction worth going?

This is the current output:

?       github.com/smutel/terraform-provider-netbox/v4  [no test files]
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestProvider_impl
--- PASS: TestProvider_impl (0.00s)
=== RUN   TestAccNetboxIpamAggregateMinimal
--- PASS: TestAccNetboxIpamAggregateMinimal (0.89s)
=== RUN   TestAccNetboxVirtualizationVMBasic
--- PASS: TestAccNetboxVirtualizationVMBasic (2.61s)
=== RUN   TestAccNetboxVirtualizationVMMinimal
--- PASS: TestAccNetboxVirtualizationVMMinimal (1.47s)
=== RUN   TestAccNetboxVirtualizationVMPrimaryIPAddress
    resource_netbox_virtualization_vm_test.go:68: Step 2/2 error: After applying this test step and performing a `terraform refresh`, the plan was not empty.
        stdout

        Terraform used the selected providers to generate the following execution
        plan. Resource actions are indicated with the following symbols:
        -/+ destroy and then create replacement

        Terraform will perform the following actions:

          # netbox_ipam_ip_addresses.ip_test must be replaced
        -/+ resource "netbox_ipam_ip_addresses" "ip_test" {
              ~ content_type  = "ipam.ipaddresse" -> (known after apply)
              ~ id            = "3005" -> (known after apply)
              - nat_inside_id = 0 -> null
              ~ primary_ip4   = false -> true # forces replacement
              - tenant_id     = 0 -> null
              - vrf_id        = 0 -> null
                # (4 unchanged attributes hidden)
            }

        Plan: 1 to add, 0 to change, 1 to destroy.
--- FAIL: TestAccNetboxVirtualizationVMPrimaryIPAddress (3.95s)
FAIL
FAIL    github.com/smutel/terraform-provider-netbox/v4/netbox   8.944s
FAIL
smutel commented 1 year ago

I am not very familiar with test in golang but it's really a good idea. We can start with manual tests on our computer but the best for the future would be to integrate this into the CI.

amhn commented 1 year ago

I did some more work on this and I think it might be ready for merging.

There are currently 3 Tests for each resource: Minimal: Only required fields are set. Full: All possible fields are set. MinimalFullMinimal: Create a minimal resource, update to set all fields and go back to the first, possibly with an intermediate step to prevent errors on destroying the other resources.

I am currently working on a check for the last scenario to detect if the resource has been recreated. But I don't think this needs to go into this PR.

smutel commented 1 year ago

Still in draft ?

amhn commented 1 year ago

I think that is just the label. I don't think I have the necessary permissions to remove it.