diff --git a/.github/workflows/testacc.yml b/.github/workflows/testacc.yml index 96734a55..aaf373cb 100644 --- a/.github/workflows/testacc.yml +++ b/.github/workflows/testacc.yml @@ -26,6 +26,7 @@ jobs: node: pve3 port: 13453 runs-on: ${{ matrix.os }} + timeout-minutes: 30 environment: pve-acc steps: - name: Dump GitHub context diff --git a/.golangci.yml b/.golangci.yml index 6c4bf0c1..b93d4ce0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,7 +23,7 @@ issues: - path: _types\.go linters: - lll - - path: fwprovider/tests/.*\.go + - path: fwprovider/.*_test\.go linters: - paralleltest # Exclude `lll` issues for long lines with URLs. diff --git a/Makefile b/Makefile index 0ec520ef..f2413c27 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ test: .PHONY: testacc testacc: @# explicitly add TF_ACC=1 to trigger the acceptance tests, `testacc.env` might be missing or incomplete - @TF_ACC=1 env $$(cat testacc.env | xargs) go test --timeout=30m -count=1 -v github.com/bpg/terraform-provider-proxmox/fwprovider/tests/... + @TF_ACC=1 env $$(cat testacc.env | xargs) go test --timeout=30m -count=1 -v github.com/bpg/terraform-provider-proxmox/fwprovider/... .PHONY: lint lint: diff --git a/docs/guides/cloud-init.md b/docs/guides/cloud-init.md index f81d28f0..e8965a68 100644 --- a/docs/guides/cloud-init.md +++ b/docs/guides/cloud-init.md @@ -78,24 +78,24 @@ resource "proxmox_virtual_environment_file" "cloud_config" { node_name = "pve" source_raw { - data = < /tmp/cloud-config.done + data = <<-EOF + #cloud-config + users: + - default + - name: ubuntu + groups: + - sudo + shell: /bin/bash + ssh_authorized_keys: + - ${trimspace(data.local_file.ssh_public_key.content)} + sudo: ALL=(ALL) NOPASSWD:ALL + runcmd: + - apt update + - apt install -y qemu-guest-agent net-tools + - timedatectl set-timezone America/Toronto + - systemctl enable qemu-guest-agent + - systemctl start qemu-guest-agent + - echo "done" > /tmp/cloud-config.done EOF file_name = "cloud-config.yaml" diff --git a/docs/resources/virtual_environment_file.md b/docs/resources/virtual_environment_file.md index cb9a0fe9..24e6f688 100644 --- a/docs/resources/virtual_environment_file.md +++ b/docs/resources/virtual_environment_file.md @@ -56,24 +56,24 @@ resource "proxmox_virtual_environment_file" "cloud_config" { node_name = "pve" source_raw { - data = < Many attributes are marked as **optional** _and_ **computed** in the schema, hence you may seem added to the plan with "(known after apply)" status, even if they are not set in the configuration. -This is done to support the `clone` operation, when a VM is created from an existing one, -and attributes of the original VM are copied to the new one.

+This is done to support the `clone` operation, when a VM is created from an existing VM or template, +and the source attributes are copied to the clone.

Computed attributes allow the provider to set those attributes without user input. -The attributes are marked as optional to allow the user to set (or overwrite) them if needed. -In order to remove the computed attribute from the plan, you can set it to an empty value (e.g. `""` for string, `[]` for collection). - +The attributes are also marked as optional to allow the practitioner to set (or overwrite) them if needed. diff --git a/example/resource_virtual_environment_file.tf b/example/resource_virtual_environment_file.tf index f3704fa1..d7c48d37 100644 --- a/example/resource_virtual_environment_file.tf +++ b/example/resource_virtual_environment_file.tf @@ -8,21 +8,21 @@ resource "proxmox_virtual_environment_file" "user_config" { node_name = data.proxmox_virtual_environment_datastores.example.node_name source_raw { - data = < /tmp/cloud-config.done + data = <<-EOF + #cloud-config + users: + - default + - name: ubuntu + groups: + - sudo + shell: /bin/bash + ssh_authorized_keys: + - ${trimspace(data.local_file.ssh_public_key.content)} + sudo: ALL=(ALL) NOPASSWD:ALL + runcmd: + - apt update + - apt install -y qemu-guest-agent net-tools + - timedatectl set-timezone America/Toronto + - systemctl enable qemu-guest-agent + - systemctl start qemu-guest-agent + - echo "done" > /tmp/cloud-config.done EOF file_name = "cloud-config.yaml" diff --git a/fwprovider/tests/resource_acl_test.go b/fwprovider/access/resource_acl_test.go similarity index 80% rename from fwprovider/tests/resource_acl_test.go rename to fwprovider/access/resource_acl_test.go index 8b311d11..2a3b5854 100644 --- a/fwprovider/tests/resource_acl_test.go +++ b/fwprovider/access/resource_acl_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package access_test import ( "context" @@ -17,47 +17,50 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" "github.com/stretchr/testify/require" + "github.com/bpg/terraform-provider-proxmox/fwprovider/test" "github.com/bpg/terraform-provider-proxmox/proxmox/access" ) func TestAccAcl_User(t *testing.T) { - te := initTestEnvironment(t) + t.Parallel() + + te := test.InitEnvironment(t) userID := fmt.Sprintf("%s@pve", gofakeit.Username()) - te.addTemplateVars(map[string]any{ + te.AddTemplateVars(map[string]any{ "UserID": userID, }) resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, CheckDestroy: nil, PreCheck: func() { - err := te.accessClient().CreateUser(context.Background(), &access.UserCreateRequestBody{ + err := te.AccessClient().CreateUser(context.Background(), &access.UserCreateRequestBody{ ID: userID, Password: gofakeit.Password(true, true, true, true, false, 8), }) require.NoError(t, err) t.Cleanup(func() { - err := te.accessClient().DeleteUser(context.Background(), userID) + err := te.AccessClient().DeleteUser(context.Background(), userID) require.NoError(t, err) }) }, Steps: []resource.TestStep{ { - Config: te.renderConfig(`resource "proxmox_virtual_environment_acl" "test" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_acl" "test" { user_id = "{{.UserID}}" path = "/" role_id = "NoAccess" }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_acl.test", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_acl.test", map[string]string{ "path": "/", "role_id": "NoAccess", "user_id": userID, "propagate": "true", }), - testNoResourceAttributesSet("proxmox_virtual_environment_acl.test", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_acl.test", []string{ "group_id", "token_id", }), @@ -70,19 +73,19 @@ func TestAccAcl_User(t *testing.T) { ImportStateVerify: true, }, { - Config: te.renderConfig(`resource "proxmox_virtual_environment_acl" "test" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_acl" "test" { user_id = "{{.UserID}}" path = "/" role_id = "PVEPoolUser" }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_acl.test", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_acl.test", map[string]string{ "path": "/", "role_id": "PVEPoolUser", "user_id": userID, "propagate": "true", }), - testNoResourceAttributesSet("proxmox_virtual_environment_acl.test", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_acl.test", []string{ "group_id", "token_id", }), @@ -95,10 +98,10 @@ func TestAccAcl_User(t *testing.T) { func TestAccAcl_Validators(t *testing.T) { t.Parallel() - te := initTestEnvironment(t) + te := test.InitEnvironment(t) resource.UnitTest(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, CheckDestroy: nil, Steps: []resource.TestStep{ { diff --git a/fwprovider/tests/resource_user_test.go b/fwprovider/access/resource_user_test.go similarity index 74% rename from fwprovider/tests/resource_user_test.go rename to fwprovider/access/resource_user_test.go index b9db975d..d540a4e2 100644 --- a/fwprovider/tests/resource_user_test.go +++ b/fwprovider/access/resource_user_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package access_test import ( "context" @@ -15,16 +15,17 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/stretchr/testify/require" + "github.com/bpg/terraform-provider-proxmox/fwprovider/test" "github.com/bpg/terraform-provider-proxmox/proxmox/access" ) func TestAccResourceUser(t *testing.T) { t.Parallel() - te := initTestEnvironment(t) + te := test.InitEnvironment(t) userID := fmt.Sprintf("%s@pve", gofakeit.Username()) - te.addTemplateVars(map[string]any{ + te.AddTemplateVars(map[string]any{ "UserID": userID, }) @@ -34,7 +35,7 @@ func TestAccResourceUser(t *testing.T) { }{ {"create and update user", []resource.TestStep{ { - Config: te.renderConfig(`resource "proxmox_virtual_environment_user" "user" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_user" "user" { comment = "Managed by Terraform" email = "{{.UserID}}" enabled = true @@ -43,7 +44,7 @@ func TestAccResourceUser(t *testing.T) { last_name = "Last" user_id = "{{.UserID}}" }`), - Check: testResourceAttributes("proxmox_virtual_environment_user.user", map[string]string{ + Check: test.ResourceAttributes("proxmox_virtual_environment_user.user", map[string]string{ "comment": "Managed by Terraform", "email": userID, "enabled": "true", @@ -54,13 +55,13 @@ func TestAccResourceUser(t *testing.T) { }), }, { - Config: te.renderConfig(`resource "proxmox_virtual_environment_user" "user" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_user" "user" { enabled = false expiration_date = "2035-01-01T22:00:00Z" user_id = "{{.UserID}}" first_name = "First One" }`), - Check: testResourceAttributes("proxmox_virtual_environment_user.user", map[string]string{ + Check: test.ResourceAttributes("proxmox_virtual_environment_user.user", map[string]string{ "enabled": "false", "expiration_date": "2035-01-01T22:00:00Z", "first_name": "First One", @@ -78,7 +79,7 @@ func TestAccResourceUser(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: tt.steps, }) }) @@ -88,11 +89,11 @@ func TestAccResourceUser(t *testing.T) { func TestAccResourceUserToken(t *testing.T) { t.Parallel() - te := initTestEnvironment(t) + te := test.InitEnvironment(t) userID := fmt.Sprintf("%s@pve", gofakeit.Username()) tokenName := gofakeit.Word() - te.addTemplateVars(map[string]any{ + te.AddTemplateVars(map[string]any{ "UserID": userID, "TokenName": tokenName, }) @@ -105,25 +106,25 @@ func TestAccResourceUserToken(t *testing.T) { { "create and update user token", func() { - err := te.accessClient().CreateUser(context.Background(), &access.UserCreateRequestBody{ + err := te.AccessClient().CreateUser(context.Background(), &access.UserCreateRequestBody{ ID: userID, Password: gofakeit.Password(true, true, true, true, false, 8), }) require.NoError(t, err) t.Cleanup(func() { - err := te.accessClient().DeleteUser(context.Background(), userID) + err := te.AccessClient().DeleteUser(context.Background(), userID) require.NoError(t, err) }) }, []resource.TestStep{ { - Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { comment = "Managed by Terraform" token_name = "{{.TokenName}}" user_id = "{{.UserID}}" }`), - Check: testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ + Check: test.ResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ "comment": "Managed by Terraform", "id": fmt.Sprintf("%s!%s", userID, tokenName), "user_id": userID, @@ -131,7 +132,7 @@ func TestAccResourceUserToken(t *testing.T) { }), }, { - Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { comment = "Managed by Terraform 2" expiration_date = "2033-01-01T01:01:01Z" privileges_separation = false @@ -139,33 +140,33 @@ func TestAccResourceUserToken(t *testing.T) { user_id = "{{.UserID}}" }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ "comment": "Managed by Terraform 2", "expiration_date": "2033-01-01T01:01:01Z", "privileges_separation": "false", "token_name": tokenName, "user_id": userID, }), - testNoResourceAttributesSet("proxmox_virtual_environment_user_token.user_token", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_user_token.user_token", []string{ "value", }), ), }, { - Config: te.renderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_user_token" "user_token" { comment = "Managed by Terraform 2" privileges_separation = false token_name = "{{.TokenName}}" user_id = "{{.UserID}}" }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_user_token.user_token", map[string]string{ "comment": "Managed by Terraform 2", "privileges_separation": "false", "token_name": tokenName, "user_id": userID, }), - testNoResourceAttributesSet("proxmox_virtual_environment_user_token.user_token", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_user_token.user_token", []string{ "expiration_date", "value", }), @@ -183,7 +184,7 @@ func TestAccResourceUserToken(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, PreCheck: tt.preCheck, Steps: tt.steps, }) diff --git a/fwprovider/tests/resource_hardware_mapping_test.go b/fwprovider/hardwaremapping/resource_hardware_mapping_test.go similarity index 95% rename from fwprovider/tests/resource_hardware_mapping_test.go rename to fwprovider/hardwaremapping/resource_hardware_mapping_test.go index d4bc1edf..cc08abe8 100644 --- a/fwprovider/tests/resource_hardware_mapping_test.go +++ b/fwprovider/hardwaremapping/resource_hardware_mapping_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package hardwaremapping_test import ( "fmt" @@ -18,7 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/statecheck" "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" - hwm "github.com/bpg/terraform-provider-proxmox/fwprovider/hardwaremapping" + "github.com/bpg/terraform-provider-proxmox/fwprovider/test" customtypes "github.com/bpg/terraform-provider-proxmox/fwprovider/types/hardwaremapping" "github.com/bpg/terraform-provider-proxmox/fwprovider/validators" proxmoxtypes "github.com/bpg/terraform-provider-proxmox/proxmox/types/hardwaremapping" @@ -41,7 +41,7 @@ type accTestHardwareMappingFakeData struct { Names []string `fake:"{noun}" fakesize:"2"` } -func testAccResourceHardwareMappingInit(t *testing.T) (*accTestHardwareMappingFakeData, *testEnvironment) { +func testAccResourceHardwareMappingInit(t *testing.T) (*accTestHardwareMappingFakeData, *test.Environment) { t.Helper() // Register a new custom function to generate random Linux device IDs. @@ -81,7 +81,7 @@ func testAccResourceHardwareMappingInit(t *testing.T) (*accTestHardwareMappingFa }, ) - te := initTestEnvironment(t) + te := test.InitEnvironment(t) var data accTestHardwareMappingFakeData @@ -102,7 +102,7 @@ func TestAccResourceHardwareMappingPCIValidInput(t *testing.T) { resource.Test( t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Test the "Create" and "Read" implementations where all possible attributes are specified. { @@ -129,7 +129,7 @@ func TestAccResourceHardwareMappingPCIValidInput(t *testing.T) { data.MapComments[0], data.MapDeviceIDs[0], data.MapIOMMUGroups[0], - te.nodeName, + te.NodeName, data.MapPathsPCI[0], data.MapSubsystemIDs[0], data.MediatedDevices, @@ -142,7 +142,7 @@ func TestAccResourceHardwareMappingPCIValidInput(t *testing.T) { "comment": data.MapComments[0], "id": data.MapDeviceIDs[0], "iommu_group": strconv.Itoa(int(data.MapIOMMUGroups[0])), - "node": te.nodeName, + "node": te.NodeName, "path": data.MapPathsPCI[0], "subsystem_id": data.MapSubsystemIDs[0], }, @@ -189,7 +189,7 @@ func TestAccResourceHardwareMappingPCIValidInput(t *testing.T) { data.MapComments[1], data.MapDeviceIDs[0], data.MapIOMMUGroups[1], - te.nodeName, + te.NodeName, data.MapPathsPCI[1], data.MapSubsystemIDs[1], !data.MediatedDevices, @@ -202,7 +202,7 @@ func TestAccResourceHardwareMappingPCIValidInput(t *testing.T) { "comment": data.MapComments[1], "id": data.MapDeviceIDs[0], "iommu_group": strconv.Itoa(int(data.MapIOMMUGroups[1])), - "node": te.nodeName, + "node": te.NodeName, "path": data.MapPathsPCI[1], "subsystem_id": data.MapSubsystemIDs[1], }, @@ -232,7 +232,7 @@ func TestAccResourceHardwareMappingPCIValidInputMinimal(t *testing.T) { resource.Test( t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Test the "Create" and "Read" implementations with only the minimum amount of attributes being set. { @@ -251,7 +251,7 @@ func TestAccResourceHardwareMappingPCIValidInputMinimal(t *testing.T) { `, data.Names[0], data.MapDeviceIDs[0], - te.nodeName, + te.NodeName, data.MapPathsPCI[0], ), ConfigStateChecks: []statecheck.StateCheck{ @@ -273,7 +273,7 @@ func TestAccResourceHardwareMappingPCIValidInputMinimal(t *testing.T) { resource.TestCheckTypeSetElemNestedAttrs( accTestHardwareMappingNamePCI, "map.*", map[string]string{ "id": data.MapDeviceIDs[0], - "node": te.nodeName, + "node": te.NodeName, "path": data.MapPathsPCI[0], }, ), @@ -314,7 +314,7 @@ func TestAccResourceHardwareMappingPCIValidInputMinimal(t *testing.T) { data.MapComments[1], data.MapDeviceIDs[0], data.MapIOMMUGroups[1], - te.nodeName, + te.NodeName, data.MapPathsPCI[1], data.MapSubsystemIDs[1], !data.MediatedDevices, @@ -327,7 +327,7 @@ func TestAccResourceHardwareMappingPCIValidInputMinimal(t *testing.T) { "comment": data.MapComments[1], "id": data.MapDeviceIDs[0], "iommu_group": strconv.Itoa(int(data.MapIOMMUGroups[1])), - "node": te.nodeName, + "node": te.NodeName, "path": data.MapPathsPCI[1], "subsystem_id": data.MapSubsystemIDs[1], }, @@ -355,7 +355,7 @@ func TestAccResourceHardwareMappingPCIInvalidInput(t *testing.T) { resource.Test( t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Test the "Create" method implementation where all possible attributes are specified, but an error is expected // when using an invalid device path. @@ -370,7 +370,7 @@ func TestAccResourceHardwareMappingPCIInvalidInput(t *testing.T) { // References: // 1. https://pkg.go.dev/regexp/syntax `(?s).*%s(?s).*`, - hwm.ErrResourceMessageInvalidPath(proxmoxtypes.TypePCI), + `not a valid Linux device path for hardware mapping of type "`+proxmoxtypes.TypePCI.String()+`"`, ), ), Config: fmt.Sprintf( @@ -397,7 +397,7 @@ func TestAccResourceHardwareMappingPCIInvalidInput(t *testing.T) { data.Comments[1], data.MapDeviceIDs[0], data.MapIOMMUGroups[0], - te.nodeName, + te.NodeName, data.MapSubsystemIDs[0], data.MediatedDevices, ), @@ -408,7 +408,7 @@ func TestAccResourceHardwareMappingPCIInvalidInput(t *testing.T) { resource.Test( t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Test the "Create" method implementation where all possible attributes are specified, but an error is expected // when using an invalid device subsystem @@ -439,7 +439,7 @@ func TestAccResourceHardwareMappingPCIInvalidInput(t *testing.T) { data.Comments[1], data.MapDeviceIDs[0], data.MapIOMMUGroups[0], - te.nodeName, + te.NodeName, data.MapPathsPCI[0], data.MediatedDevices, ), @@ -459,7 +459,7 @@ func TestAccResourceHardwareMappingUSBValidInput(t *testing.T) { resource.Test( t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Test the "Create" and "Read" implementations where all possible attributes are specified. { @@ -482,7 +482,7 @@ func TestAccResourceHardwareMappingUSBValidInput(t *testing.T) { data.Names[0], data.MapComments[0], data.MapDeviceIDs[0], - te.nodeName, + te.NodeName, data.MapPathsUSB[0], ), Check: resource.ComposeTestCheckFunc( @@ -492,7 +492,7 @@ func TestAccResourceHardwareMappingUSBValidInput(t *testing.T) { accTestHardwareMappingNameUSB, "map.*", map[string]string{ "comment": data.MapComments[0], "id": data.MapDeviceIDs[0], - "node": te.nodeName, + "node": te.NodeName, "path": data.MapPathsUSB[0], }, ), @@ -529,7 +529,7 @@ func TestAccResourceHardwareMappingUSBValidInput(t *testing.T) { data.Names[0], data.MapComments[1], data.MapDeviceIDs[0], - te.nodeName, + te.NodeName, data.MapPathsUSB[1], ), Check: resource.ComposeTestCheckFunc( @@ -539,7 +539,7 @@ func TestAccResourceHardwareMappingUSBValidInput(t *testing.T) { accTestHardwareMappingNameUSB, "map.*", map[string]string{ "comment": data.MapComments[1], "id": data.MapDeviceIDs[0], - "node": te.nodeName, + "node": te.NodeName, "path": data.MapPathsUSB[1], }, ), @@ -563,7 +563,7 @@ func TestAccResourceHardwareMappingUSBValidInputMinimal(t *testing.T) { resource.Test( t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Test the "Create" and "Read" implementations with only the minimum amount of attributes being set. { @@ -581,7 +581,7 @@ func TestAccResourceHardwareMappingUSBValidInputMinimal(t *testing.T) { `, data.Names[0], data.MapDeviceIDs[0], - te.nodeName, + te.NodeName, ), ConfigStateChecks: []statecheck.StateCheck{ // Optional attributes should all be unset. @@ -600,7 +600,7 @@ func TestAccResourceHardwareMappingUSBValidInputMinimal(t *testing.T) { resource.TestCheckTypeSetElemNestedAttrs( accTestHardwareMappingNameUSB, "map.*", map[string]string{ "id": data.MapDeviceIDs[0], - "node": te.nodeName, + "node": te.NodeName, }, ), resource.TestCheckResourceAttr(accTestHardwareMappingNameUSB, "name", data.Names[0]), @@ -628,7 +628,7 @@ func TestAccResourceHardwareMappingUSBValidInputMinimal(t *testing.T) { data.Names[0], data.Comments[1], data.MapDeviceIDs[1], - te.nodeName, + te.NodeName, data.MapPathsUSB[0], ), Check: resource.ComposeTestCheckFunc( @@ -638,7 +638,7 @@ func TestAccResourceHardwareMappingUSBValidInputMinimal(t *testing.T) { accTestHardwareMappingNameUSB, "map.*", map[string]string{ "comment": data.Comments[1], "id": data.MapDeviceIDs[1], - "node": te.nodeName, + "node": te.NodeName, "path": data.MapPathsUSB[0], }, ), @@ -659,7 +659,7 @@ func TestAccResourceHardwareMappingUSBInvalidInput(t *testing.T) { resource.Test( t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Test the "Create" method implementation where all possible attributes are specified, but an error is expected // when using an invalid device path. @@ -684,7 +684,7 @@ func TestAccResourceHardwareMappingUSBInvalidInput(t *testing.T) { data.Names[0], data.Comments[1], data.MapDeviceIDs[0], - te.nodeName, + te.NodeName, ), ExpectError: regexp.MustCompile(`valid Linux device path for hardware mapping of type "usb"`), }, diff --git a/fwprovider/tests/resource_linux_bridge_test.go b/fwprovider/network/resource_linux_bridge_test.go similarity index 74% rename from fwprovider/tests/resource_linux_bridge_test.go rename to fwprovider/network/resource_linux_bridge_test.go index 4fa2b61b..b75e7a4b 100644 --- a/fwprovider/tests/resource_linux_bridge_test.go +++ b/fwprovider/network/resource_linux_bridge_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package network_test import ( "fmt" @@ -13,10 +13,12 @@ import ( "github.com/brianvoe/gofakeit/v7" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + + "github.com/bpg/terraform-provider-proxmox/fwprovider/test" ) func TestAccResourceLinuxBridge(t *testing.T) { - te := initTestEnvironment(t) + te := test.InitEnvironment(t) iface := fmt.Sprintf("vmbr%d", gofakeit.Number(10, 9999)) ipV4cidr1 := fmt.Sprintf("%s/24", gofakeit.IPv4Address()) @@ -24,11 +26,11 @@ func TestAccResourceLinuxBridge(t *testing.T) { ipV6cidr := "FE80:0000:0000:0000:0202:B3FF:FE1E:8329/64" resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Create and Read testing { - Config: te.renderConfig(fmt.Sprintf(` + Config: te.RenderConfig(fmt.Sprintf(` resource "proxmox_virtual_environment_network_linux_bridge" "test" { address = "%s" autostart = true @@ -40,7 +42,7 @@ func TestAccResourceLinuxBridge(t *testing.T) { } `, ipV4cidr1, iface)), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_network_linux_bridge.test", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_network_linux_bridge.test", map[string]string{ "address": ipV4cidr1, "autostart": "true", "comment": "created by terraform", @@ -48,14 +50,14 @@ func TestAccResourceLinuxBridge(t *testing.T) { "name": iface, "vlan_aware": "true", }), - testResourceAttributesSet("proxmox_virtual_environment_network_linux_bridge.test", []string{ + test.ResourceAttributesSet("proxmox_virtual_environment_network_linux_bridge.test", []string{ "id", }), ), }, // Update testing { - Config: te.renderConfig(fmt.Sprintf(` + Config: te.RenderConfig(fmt.Sprintf(` resource "proxmox_virtual_environment_network_linux_bridge" "test" { address = "%s" address6 = "%s" @@ -67,7 +69,7 @@ func TestAccResourceLinuxBridge(t *testing.T) { vlan_aware = false }`, ipV4cidr2, ipV6cidr, iface)), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_network_linux_bridge.test", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_network_linux_bridge.test", map[string]string{ "address": ipV4cidr2, "address6": ipV6cidr, "autostart": "false", @@ -75,10 +77,10 @@ func TestAccResourceLinuxBridge(t *testing.T) { "name": iface, "vlan_aware": "false", }), - testNoResourceAttributesSet("proxmox_virtual_environment_network_linux_bridge.test", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_network_linux_bridge.test", []string{ "mtu", }), - testResourceAttributesSet("proxmox_virtual_environment_network_linux_bridge.test", []string{ + test.ResourceAttributesSet("proxmox_virtual_environment_network_linux_bridge.test", []string{ "id", }), ), diff --git a/fwprovider/tests/resource_linux_vlan_test.go b/fwprovider/network/resource_linux_vlan_test.go similarity index 92% rename from fwprovider/tests/resource_linux_vlan_test.go rename to fwprovider/network/resource_linux_vlan_test.go index 77fa904d..19e28c94 100644 --- a/fwprovider/tests/resource_linux_vlan_test.go +++ b/fwprovider/network/resource_linux_vlan_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package network_test import ( "fmt" @@ -13,6 +13,8 @@ import ( "github.com/brianvoe/gofakeit/v7" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + + "github.com/bpg/terraform-provider-proxmox/fwprovider/test" ) const ( @@ -20,7 +22,7 @@ const ( ) func TestAccResourceLinuxVLAN(t *testing.T) { - te := initTestEnvironment(t) + te := test.InitEnvironment(t) iface := "ens18" vlan1 := gofakeit.Number(10, 4094) @@ -29,11 +31,11 @@ func TestAccResourceLinuxVLAN(t *testing.T) { ipV4cidr := fmt.Sprintf("%s/24", gofakeit.IPv4Address()) resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Create and Read testing { - Config: te.renderConfig(testAccResourceLinuxVLANCreatedConfig(iface, vlan1)), + Config: te.RenderConfig(testAccResourceLinuxVLANCreatedConfig(iface, vlan1)), Check: testAccResourceLinuxVLANCreatedCheck(iface, vlan1), }, // ImportState testing @@ -44,7 +46,7 @@ func TestAccResourceLinuxVLAN(t *testing.T) { }, // Create and Read with a custom name { - Config: te.renderConfig(testAccResourceLinuxVLANCustomNameCreatedConfig(customName, iface, vlan2)), + Config: te.RenderConfig(testAccResourceLinuxVLANCustomNameCreatedConfig(customName, iface, vlan2)), Check: testAccResourceLinuxVLANCustomNameCreatedCheck(customName, iface, vlan2), // PVE API is unreliable. Sometimes it returns a wrong VLAN ID for this second interface. SkipFunc: func() (bool, error) { @@ -53,7 +55,7 @@ func TestAccResourceLinuxVLAN(t *testing.T) { }, // Update testing { - Config: te.renderConfig(testAccResourceLinuxVLANUpdatedConfig(iface, vlan1, ipV4cidr)), + Config: te.RenderConfig(testAccResourceLinuxVLANUpdatedConfig(iface, vlan1, ipV4cidr)), Check: testAccResourceLinuxVLANUpdatedCheck(iface, vlan1, ipV4cidr), }, }, diff --git a/fwprovider/tests/resource_download_file_test.go b/fwprovider/resource_download_file_test.go similarity index 73% rename from fwprovider/tests/resource_download_file_test.go rename to fwprovider/resource_download_file_test.go index 462044aa..76d45c1a 100644 --- a/fwprovider/tests/resource_download_file_test.go +++ b/fwprovider/resource_download_file_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package fwprovider_test import ( "context" @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/stretchr/testify/require" + "github.com/bpg/terraform-provider-proxmox/fwprovider/test" "github.com/bpg/terraform-provider-proxmox/proxmox/helpers/ptr" "github.com/bpg/terraform-provider-proxmox/proxmox/nodes/storage" ) @@ -24,9 +25,9 @@ const ( ) func TestAccResourceDownloadFile(t *testing.T) { - te := initTestEnvironment(t) + te := test.InitEnvironment(t) - te.addTemplateVars(map[string]interface{}{ + te.AddTemplateVars(map[string]interface{}{ "FakeFileISO": fakeFileISO, "FakeFileQCOW2": fakeFileQCOW2, }) @@ -36,7 +37,7 @@ func TestAccResourceDownloadFile(t *testing.T) { steps []resource.TestStep }{ {"download qcow2 file", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_download_file" "qcow2_image" { content_type = "iso" node_name = "{{.NodeName}}" @@ -48,11 +49,11 @@ func TestAccResourceDownloadFile(t *testing.T) { overwrite_unmanaged = true }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_download_file.qcow2_image", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_download_file.qcow2_image", map[string]string{ "id": "local:iso/fake_qcow2_file.img", "content_type": "iso", - "node_name": te.nodeName, - "datastore_id": te.datastoreID, + "node_name": te.NodeName, + "datastore_id": te.DatastoreID, "url": fakeFileQCOW2, "file_name": "fake_qcow2_file.img", "upload_timeout": "600", @@ -61,14 +62,14 @@ func TestAccResourceDownloadFile(t *testing.T) { "checksum": "688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6", "checksum_algorithm": "sha256", }), - testNoResourceAttributesSet("proxmox_virtual_environment_download_file.qcow2_image", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_download_file.qcow2_image", []string{ "decompression_algorithm", }), ), }}}, {"download & update iso file", []resource.TestStep{ { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_download_file" "iso_image" { content_type = "iso" node_name = "{{.NodeName}}" @@ -77,17 +78,17 @@ func TestAccResourceDownloadFile(t *testing.T) { overwrite_unmanaged = true }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ "id": "local:iso/fake_file.iso", - "node_name": te.nodeName, - "datastore_id": te.datastoreID, + "node_name": te.NodeName, + "datastore_id": te.DatastoreID, "url": fakeFileISO, "file_name": "fake_file.iso", "upload_timeout": "600", "size": "3", "verify": "true", }), - testNoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image", []string{ "checksum", "checksum_algorithm", "decompression_algorithm", @@ -95,7 +96,7 @@ func TestAccResourceDownloadFile(t *testing.T) { ), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_download_file" "iso_image" { content_type = "iso" node_name = "{{.NodeName}}" @@ -106,18 +107,18 @@ func TestAccResourceDownloadFile(t *testing.T) { overwrite_unmanaged = true }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ "id": "local:iso/fake_iso_file.img", "content_type": "iso", - "node_name": te.nodeName, - "datastore_id": te.datastoreID, + "node_name": te.NodeName, + "datastore_id": te.DatastoreID, "url": fakeFileISO, "file_name": "fake_iso_file.img", "upload_timeout": "10000", "size": "3", "verify": "true", }), - testNoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image", []string{ "checksum", "checksum_algorithm", "decompression_algorithm", @@ -130,23 +131,23 @@ func TestAccResourceDownloadFile(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) defer cancel() - _ = te.nodeStorageClient().DeleteDatastoreFile(ctx, "iso/fake_file.iso") //nolint: errcheck + _ = te.NodeStorageClient().DeleteDatastoreFile(ctx, "iso/fake_file.iso") //nolint: errcheck - err := te.nodeStorageClient().DownloadFileByURL(ctx, &storage.DownloadURLPostRequestBody{ + err := te.NodeStorageClient().DownloadFileByURL(ctx, &storage.DownloadURLPostRequestBody{ Content: ptr.Ptr("iso"), FileName: ptr.Ptr("fake_file.iso"), - Node: ptr.Ptr(te.nodeName), - Storage: ptr.Ptr(te.datastoreID), + Node: ptr.Ptr(te.NodeName), + Storage: ptr.Ptr(te.DatastoreID), URL: ptr.Ptr(fakeFileISO), }) require.NoError(t, err) t.Cleanup(func() { - e := te.nodeStorageClient().DeleteDatastoreFile(context.Background(), "iso/fake_file.iso") + e := te.NodeStorageClient().DeleteDatastoreFile(context.Background(), "iso/fake_file.iso") require.NoError(t, e) }) }, - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_download_file" "iso_image3" { content_type = "iso" node_name = "{{.NodeName}}" @@ -156,17 +157,17 @@ func TestAccResourceDownloadFile(t *testing.T) { overwrite_unmanaged = true }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_download_file.iso_image3", map[string]string{ + test.ResourceAttributes("proxmox_virtual_environment_download_file.iso_image3", map[string]string{ "id": "local:iso/fake_iso_file3.iso", "content_type": "iso", - "node_name": te.nodeName, - "datastore_id": te.datastoreID, + "node_name": te.NodeName, + "datastore_id": te.DatastoreID, "url": fakeFileISO, "file_name": "fake_iso_file3.iso", "size": "3", "verify": "true", }), - testNoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image3", []string{ + test.NoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image3", []string{ "checksum", "checksum_algorithm", "decompression_algorithm", @@ -178,7 +179,7 @@ func TestAccResourceDownloadFile(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: tt.steps, }) }) diff --git a/fwprovider/tests/resource_options_test.go b/fwprovider/resource_options_test.go similarity index 94% rename from fwprovider/tests/resource_options_test.go rename to fwprovider/resource_options_test.go index d403809f..280372b3 100644 --- a/fwprovider/tests/resource_options_test.go +++ b/fwprovider/resource_options_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package fwprovider_test import ( "fmt" @@ -12,7 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/bpg/terraform-provider-proxmox/fwprovider" + "github.com/bpg/terraform-provider-proxmox/fwprovider/test" ) const accTestClusterOptionsName = "proxmox_virtual_environment_cluster_options.test_options" @@ -20,11 +20,11 @@ const accTestClusterOptionsName = "proxmox_virtual_environment_cluster_options.t func TestAccResourceClusterOptions(t *testing.T) { t.Parallel() - te := initTestEnvironment(t) + te := test.InitEnvironment(t) resource.Test( t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Create and Read testing { @@ -77,8 +77,8 @@ func testAccResourceClusterOptionsCreatedConfig() string { } } `, - fwprovider.ClusterOptionsNextIDLowerMinimum, - fwprovider.ClusterOptionsNextIDLowerMaximum, + 100, + 999999999, ) } @@ -100,12 +100,12 @@ func testAccResourceClusterOptionsCreatedCheck() resource.TestCheckFunc { resource.TestCheckResourceAttr( accTestClusterOptionsName, "next_id.lower", - fmt.Sprintf("%d", fwprovider.ClusterOptionsNextIDLowerMinimum), + fmt.Sprintf("%d", 100), ), resource.TestCheckResourceAttr( accTestClusterOptionsName, "next_id.upper", - fmt.Sprintf("%d", fwprovider.ClusterOptionsNextIDLowerMaximum), + fmt.Sprintf("%d", 999999999), ), resource.TestCheckResourceAttr(accTestClusterOptionsName, "notify.ha_fencing_mode", "never"), resource.TestCheckResourceAttr(accTestClusterOptionsName, "notify.ha_fencing_target", "default-matcher"), diff --git a/fwprovider/tests/datasource_node_test.go b/fwprovider/test/datasource_node_test.go similarity index 78% rename from fwprovider/tests/datasource_node_test.go rename to fwprovider/test/datasource_node_test.go index 6815d765..b27a45e6 100644 --- a/fwprovider/tests/datasource_node_test.go +++ b/fwprovider/test/datasource_node_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package test import ( "testing" @@ -15,16 +15,16 @@ import ( func TestAccDatasourceNode(t *testing.T) { t.Parallel() - te := initTestEnvironment(t) + te := InitEnvironment(t) tests := []struct { name string steps []resource.TestStep }{ {"read node attributes", []resource.TestStep{{ - Config: te.renderConfig(`data "proxmox_virtual_environment_node" "test" { node_name = "{{.NodeName}}" }`), + Config: te.RenderConfig(`data "proxmox_virtual_environment_node" "test" { node_name = "{{.NodeName}}" }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributesSet("data.proxmox_virtual_environment_node.test", []string{ + ResourceAttributesSet("data.proxmox_virtual_environment_node.test", []string{ "cpu_count", "cpu_sockets", "cpu_model", @@ -40,7 +40,7 @@ func TestAccDatasourceNode(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: tt.steps, }) }) diff --git a/fwprovider/tests/datasource_version_test.go b/fwprovider/test/datasource_version_test.go similarity index 92% rename from fwprovider/tests/datasource_version_test.go rename to fwprovider/test/datasource_version_test.go index b8540413..7cea27b7 100644 --- a/fwprovider/tests/datasource_version_test.go +++ b/fwprovider/test/datasource_version_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package test import ( "fmt" @@ -15,12 +15,12 @@ import ( ) func TestAccDatasourceVersion(t *testing.T) { - te := initTestEnvironment(t) + te := InitEnvironment(t) datasourceName := "data.proxmox_virtual_environment_version.test" resource.ParallelTest(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ // Read testing { diff --git a/fwprovider/tests/resource_container_test.go b/fwprovider/test/resource_container_test.go similarity index 81% rename from fwprovider/tests/resource_container_test.go rename to fwprovider/test/resource_container_test.go index 53a4dcee..0e1329f0 100644 --- a/fwprovider/tests/resource_container_test.go +++ b/fwprovider/test/resource_container_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package test import ( "context" @@ -32,24 +32,24 @@ func TestAccResourceContainer(t *testing.T) { //nolint:wsl // download fails with 404 or "exit code 8" if run in parallel // t.Parallel() - te := initTestEnvironment(t) + te := InitEnvironment(t) resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: []resource.TestStep{ { - Config: te.renderConfig(testAccResourceContainerCreateConfig(te, false)), + Config: te.RenderConfig(testAccResourceContainerCreateConfig(te, false)), Check: testAccResourceContainerCreateCheck(te), }, { - Config: te.renderConfig(testAccResourceContainerCreateConfig(te, true) + testAccResourceContainerCreateCloneConfig(te)), + Config: te.RenderConfig(testAccResourceContainerCreateConfig(te, true) + testAccResourceContainerCreateCloneConfig(te)), Check: testAccResourceContainerCreateCloneCheck(te), }, }, }) } -func testAccResourceContainerCreateConfig(te *testEnvironment, isTemplate bool) string { +func testAccResourceContainerCreateConfig(te *Environment, isTemplate bool) string { te.t.Helper() return fmt.Sprintf(` @@ -98,7 +98,7 @@ resource "proxmox_virtual_environment_container" "test_container" { `, accTestContainerID, isTemplate) } -func testAccResourceContainerCreateCheck(te *testEnvironment) resource.TestCheckFunc { +func testAccResourceContainerCreateCheck(te *Environment) resource.TestCheckFunc { te.t.Helper() return resource.ComposeTestCheckFunc( @@ -107,7 +107,7 @@ func testAccResourceContainerCreateCheck(te *testEnvironment) resource.TestCheck ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - err := te.nodeClient().Container(accTestContainerID).WaitForContainerStatus(ctx, "running") + err := te.NodeClient().Container(accTestContainerID).WaitForContainerStatus(ctx, "running") require.NoError(te.t, err, "container did not start") return nil @@ -115,7 +115,7 @@ func testAccResourceContainerCreateCheck(te *testEnvironment) resource.TestCheck ) } -func testAccResourceContainerCreateCloneConfig(te *testEnvironment) string { +func testAccResourceContainerCreateCloneConfig(te *Environment) string { te.t.Helper() return fmt.Sprintf(` @@ -135,7 +135,7 @@ resource "proxmox_virtual_environment_container" "test_container_clone" { `, accCloneContainerID) } -func testAccResourceContainerCreateCloneCheck(te *testEnvironment) resource.TestCheckFunc { +func testAccResourceContainerCreateCloneCheck(te *Environment) resource.TestCheckFunc { te.t.Helper() return resource.ComposeTestCheckFunc( @@ -143,7 +143,7 @@ func testAccResourceContainerCreateCloneCheck(te *testEnvironment) resource.Test ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - err := te.nodeClient().Container(accCloneContainerID).WaitForContainerStatus(ctx, "running") + err := te.NodeClient().Container(accCloneContainerID).WaitForContainerStatus(ctx, "running") require.NoError(te.t, err, "container did not start") return nil diff --git a/fwprovider/tests/resource_file_test.go b/fwprovider/test/resource_file_test.go similarity index 86% rename from fwprovider/tests/resource_file_test.go rename to fwprovider/test/resource_file_test.go index 4fa9b7b0..b3da80b7 100644 --- a/fwprovider/tests/resource_file_test.go +++ b/fwprovider/test/resource_file_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package test import ( "context" @@ -36,7 +36,7 @@ func (c *nodeResolver) Resolve(_ context.Context, _ string) (ssh.ProxmoxNode, er } func TestAccResourceFile(t *testing.T) { - te := initTestEnvironment(t) + te := InitEnvironment(t) snippetRaw := fmt.Sprintf("snippet-raw-%s.txt", gofakeit.Word()) snippetURL := "https://raw.githubusercontent.com/yaml/yaml-test-suite/main/src/229Q.yaml" @@ -44,7 +44,7 @@ func TestAccResourceFile(t *testing.T) { snippetFile2 := strings.ReplaceAll(createFile(t, "snippet-file-2-*.yaml", "test snippet 2 - file").Name(), `\`, `/`) fileISO := strings.ReplaceAll(createFile(t, "file-*.iso", "pretend it is an ISO").Name(), `\`, `/`) - te.addTemplateVars(map[string]interface{}{ + te.AddTemplateVars(map[string]interface{}{ "SnippetRaw": snippetRaw, "SnippetURL": snippetURL, "SnippetFile1": snippetFile1, @@ -53,7 +53,7 @@ func TestAccResourceFile(t *testing.T) { }) resource.ParallelTest(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, PreCheck: func() { uploadSnippetFile(t, snippetFile2) t.Cleanup(func() { @@ -67,7 +67,7 @@ func TestAccResourceFile(t *testing.T) { }, Steps: []resource.TestStep{ { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test_raw" { content_type = "snippets" datastore_id = "local" @@ -79,7 +79,7 @@ func TestAccResourceFile(t *testing.T) { file_name = "{{.SnippetRaw}}" } }`), - Check: testResourceAttributes("proxmox_virtual_environment_file.test_raw", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_file.test_raw", map[string]string{ "content_type": "snippets", "file_name": snippetRaw, "source_raw.0.file_name": snippetRaw, @@ -88,7 +88,7 @@ func TestAccResourceFile(t *testing.T) { }), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -96,14 +96,14 @@ func TestAccResourceFile(t *testing.T) { path = "{{.SnippetFile1}}" } }`), - Check: testResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ "content_type": "snippets", "file_name": filepath.Base(snippetFile1), "id": fmt.Sprintf("local:snippets/%s", filepath.Base(snippetFile1)), }), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -111,14 +111,14 @@ func TestAccResourceFile(t *testing.T) { path = "{{.SnippetURL}}" } }`), - Check: testResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ "content_type": "snippets", "file_name": filepath.Base(snippetURL), "id": fmt.Sprintf("local:snippets/%s", filepath.Base(snippetURL)), }), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -126,14 +126,14 @@ func TestAccResourceFile(t *testing.T) { path = "{{.FileISO}}" } }`), - Check: testResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ "content_type": "iso", "file_name": filepath.Base(fileISO), "id": fmt.Sprintf("local:iso/%s", filepath.Base(fileISO)), }), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -150,7 +150,7 @@ func TestAccResourceFile(t *testing.T) { ExpectError: regexp.MustCompile("please specify .* - not both"), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -162,7 +162,7 @@ func TestAccResourceFile(t *testing.T) { ExpectError: regexp.MustCompile("failed to determine file name from the URL"), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -171,7 +171,7 @@ func TestAccResourceFile(t *testing.T) { }, // Do not allow to overwrite the file { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -184,7 +184,7 @@ func TestAccResourceFile(t *testing.T) { }, // Allow to overwrite the file by default { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -192,7 +192,7 @@ func TestAccResourceFile(t *testing.T) { path = "{{.SnippetFile2}}" } }`), - Check: testResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ "content_type": "snippets", "file_name": filepath.Base(snippetFile2), "id": fmt.Sprintf("local:snippets/%s", filepath.Base(snippetFile2)), @@ -203,7 +203,7 @@ func TestAccResourceFile(t *testing.T) { PreConfig: func() { deleteSnippet(te, filepath.Base(snippetFile1)) }, - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "{{.NodeName}}" @@ -211,7 +211,7 @@ func TestAccResourceFile(t *testing.T) { path = "{{.SnippetFile1}}" } }`), - Check: testResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_file.test", map[string]string{ "content_type": "snippets", "file_name": filepath.Base(snippetFile1), "id": fmt.Sprintf("local:snippets/%s", filepath.Base(snippetFile1)), @@ -278,9 +278,9 @@ func createFile(t *testing.T, namePattern string, content string) *os.File { return f } -func deleteSnippet(te *testEnvironment, fname string) { +func deleteSnippet(te *Environment, fname string) { te.t.Helper() - err := te.nodeStorageClient().DeleteDatastoreFile(context.Background(), fmt.Sprintf("snippets/%s", fname)) + err := te.NodeStorageClient().DeleteDatastoreFile(context.Background(), fmt.Sprintf("snippets/%s", fname)) require.NoError(te.t, err) } diff --git a/fwprovider/tests/resource_firewall_test.go b/fwprovider/test/resource_firewall_test.go similarity index 78% rename from fwprovider/tests/resource_firewall_test.go rename to fwprovider/test/resource_firewall_test.go index 579430f4..b368e511 100644 --- a/fwprovider/tests/resource_firewall_test.go +++ b/fwprovider/test/resource_firewall_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package test import ( "testing" @@ -13,14 +13,14 @@ import ( ) func TestAccResourceClusterFirewall(t *testing.T) { - te := initTestEnvironment(t) + te := InitEnvironment(t) tests := []struct { name string steps []resource.TestStep }{ {"rules1", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_firewall_rules" "rules1" { rule { type = "in" @@ -32,7 +32,7 @@ func TestAccResourceClusterFirewall(t *testing.T) { } }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_firewall_rules.rules1", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_firewall_rules.rules1", map[string]string{ "rule.0.type": "in", "rule.0.action": "ACCEPT", "rule.0.iface": "vmbr0", @@ -40,7 +40,7 @@ func TestAccResourceClusterFirewall(t *testing.T) { "rule.0.proto": "tcp", "rule.0.comment": "PVE Admin Interface", }), - testNoResourceAttributesSet("proxmox_virtual_environment_firewall_rules.rules1", []string{ + NoResourceAttributesSet("proxmox_virtual_environment_firewall_rules.rules1", []string{ "node_name", }), ), @@ -50,7 +50,7 @@ func TestAccResourceClusterFirewall(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: tt.steps, }) }) diff --git a/fwprovider/tests/resource_time_test.go b/fwprovider/test/resource_time_test.go similarity index 64% rename from fwprovider/tests/resource_time_test.go rename to fwprovider/test/resource_time_test.go index 9ea6995c..9e7dce4b 100644 --- a/fwprovider/tests/resource_time_test.go +++ b/fwprovider/test/resource_time_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package test import ( "testing" @@ -15,7 +15,7 @@ import ( func TestAccResourceTime(t *testing.T) { t.Parallel() - te := initTestEnvironment(t) + te := InitEnvironment(t) tests := []struct { name string @@ -23,29 +23,29 @@ func TestAccResourceTime(t *testing.T) { }{ {"change timezone", []resource.TestStep{ { - Config: te.renderConfig(`resource "proxmox_virtual_environment_time" "node_time" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_time" "node_time" { node_name = "{{.NodeName}}" time_zone = "America/New_York" }`), - Check: testResourceAttributes("proxmox_virtual_environment_time.node_time", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_time.node_time", map[string]string{ "time_zone": "America/New_York", }), }, { - Config: te.renderConfig(`resource "proxmox_virtual_environment_time" "node_time" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_time" "node_time" { node_name = "{{.NodeName}}" time_zone = "UTC" }`), - Check: testResourceAttributes("proxmox_virtual_environment_time.node_time", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_time.node_time", map[string]string{ "time_zone": "UTC", }), }, { - Config: te.renderConfig(`resource "proxmox_virtual_environment_time" "node_time" { + Config: te.RenderConfig(`resource "proxmox_virtual_environment_time" "node_time" { node_name = "{{.NodeName}}" time_zone = "UTC" }`), - Check: testResourceAttributes("proxmox_virtual_environment_time.node_time", map[string]string{ + Check: ResourceAttributes("proxmox_virtual_environment_time.node_time", map[string]string{ "time_zone": "UTC", }), }, @@ -55,7 +55,7 @@ func TestAccResourceTime(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: tt.steps, }) }) diff --git a/fwprovider/tests/resource_vm_test.go b/fwprovider/test/resource_vm_test.go similarity index 78% rename from fwprovider/tests/resource_vm_test.go rename to fwprovider/test/resource_vm_test.go index c90ce7c3..8b1f2f7f 100644 --- a/fwprovider/tests/resource_vm_test.go +++ b/fwprovider/test/resource_vm_test.go @@ -4,7 +4,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -package tests +package test import ( "testing" @@ -15,14 +15,14 @@ import ( func TestAccResourceVM(t *testing.T) { t.Parallel() - te := initTestEnvironment(t) + te := InitEnvironment(t) tests := []struct { name string step []resource.TestStep }{ {"multiline description", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm1" { node_name = "{{.NodeName}}" started = false @@ -34,13 +34,13 @@ func TestAccResourceVM(t *testing.T) { EOT }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm1", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm1", map[string]string{ "description": "my\ndescription\nvalue", }), ), }}}, {"single line description", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm2" { node_name = "{{.NodeName}}" started = false @@ -48,13 +48,13 @@ func TestAccResourceVM(t *testing.T) { description = "my description value" }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm2", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm2", map[string]string{ "description": "my description value", }), ), }}}, {"no description", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm3" { node_name = "{{.NodeName}}" started = false @@ -62,14 +62,14 @@ func TestAccResourceVM(t *testing.T) { description = "" }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm3", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm3", map[string]string{ "description": "", }), ), }}}, { "protection", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm4" { node_name = "{{.NodeName}}" started = false @@ -77,12 +77,12 @@ func TestAccResourceVM(t *testing.T) { protection = true }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ "protection": "true", }), ), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm4" { node_name = "{{.NodeName}}" started = false @@ -90,7 +90,7 @@ func TestAccResourceVM(t *testing.T) { protection = false }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ "protection": "false", }), ), @@ -98,7 +98,7 @@ func TestAccResourceVM(t *testing.T) { }, { "update cpu block", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm5" { node_name = "{{.NodeName}}" started = false @@ -108,12 +108,12 @@ func TestAccResourceVM(t *testing.T) { } }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ "cpu.0.sockets": "1", }), ), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm5" { node_name = "{{.NodeName}}" started = false @@ -123,7 +123,7 @@ func TestAccResourceVM(t *testing.T) { } }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ "cpu.0.sockets": "1", }), ), @@ -131,7 +131,7 @@ func TestAccResourceVM(t *testing.T) { }, { "update memory block", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm6" { node_name = "{{.NodeName}}" started = false @@ -141,12 +141,12 @@ func TestAccResourceVM(t *testing.T) { } }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ "memory.0.dedicated": "2048", }), ), }, { - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm6" { node_name = "{{.NodeName}}" started = false @@ -156,7 +156,7 @@ func TestAccResourceVM(t *testing.T) { } }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ "memory.0.dedicated": "1024", }), ), @@ -169,7 +169,7 @@ func TestAccResourceVM(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: tt.step, }) }) @@ -177,27 +177,27 @@ func TestAccResourceVM(t *testing.T) { } func TestAccResourceVMInitialization(t *testing.T) { - te := initTestEnvironment(t) + te := InitEnvironment(t) tests := []struct { name string step []resource.TestStep }{ {"custom cloud-init: use SCSI interface", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "cloud_config" { content_type = "snippets" datastore_id = "local" node_name = "{{.NodeName}}" source_raw { data = <<-EOF - #cloud-config - runcmd: - - apt update - - apt install -y qemu-guest-agent - - systemctl enable qemu-guest-agent - - systemctl start qemu-guest-agent - EOF + #cloud-config + runcmd: + - apt update + - apt install -y qemu-guest-agent + - systemctl enable qemu-guest-agent + - systemctl start qemu-guest-agent + EOF file_name = "cloud-config.yaml" } } @@ -247,7 +247,7 @@ func TestAccResourceVMInitialization(t *testing.T) { }`), }}}, {"native cloud-init: do not upgrade packages", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_vm" "test_vm_cloudinit3" { node_name = "{{.NodeName}}" started = false @@ -256,7 +256,7 @@ func TestAccResourceVMInitialization(t *testing.T) { } }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_vm.test_vm_cloudinit3", map[string]string{ + ResourceAttributes("proxmox_virtual_environment_vm.test_vm_cloudinit3", map[string]string{ "initialization.0.upgrade": "false", }), ), @@ -266,7 +266,7 @@ func TestAccResourceVMInitialization(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: te.accProviders, + ProtoV6ProviderFactories: te.AccProviders, Steps: tt.step, }) }) @@ -274,27 +274,27 @@ func TestAccResourceVMInitialization(t *testing.T) { } func TestAccResourceVMNetwork(t *testing.T) { - te := initTestEnvironment(t) + te := InitEnvironment(t) tests := []struct { name string step []resource.TestStep }{ {"network interfaces", []resource.TestStep{{ - Config: te.renderConfig(` + Config: te.RenderConfig(` resource "proxmox_virtual_environment_file" "cloud_config" { content_type = "snippets" datastore_id = "local" node_name = "{{.NodeName}}" source_raw { - data = < Many attributes are marked as **optional** _and_ **computed** in the schema, hence you may seem added to the plan with "(known after apply)" status, even if they are not set in the configuration. -This is done to support the `clone` operation, when a VM is created from an existing one, -and attributes of the original VM are copied to the new one.

+This is done to support the `clone` operation, when a VM is created from an existing VM or template, +and the source attributes are copied to the clone.

Computed attributes allow the provider to set those attributes without user input. -The attributes are marked as optional to allow the user to set (or overwrite) them if needed. -In order to remove the computed attribute from the plan, you can set it to an empty value (e.g. `""` for string, `[]` for collection). - +The attributes are also marked as optional to allow the practitioner to set (or overwrite) them if needed. {{ if .HasExample -}} ## Example Usage