From 268c8616494219463fc2f20b2279a166f17c791a Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Sun, 7 Apr 2024 00:41:57 -0400 Subject: [PATCH] chore: improve acceptance tests on CI (#1173) Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --- .github/workflows/test.yml | 8 - .github/workflows/testacc.yml | 21 ++- CONTRIBUTING.md | 3 + fwprovider/tests/datasource_node_test.go | 9 +- fwprovider/tests/datasource_version_test.go | 5 +- fwprovider/tests/resource_container_test.go | 49 +++--- .../tests/resource_download_file_test.go | 28 ++-- fwprovider/tests/resource_file_test.go | 67 ++++---- .../tests/resource_linux_bridge_test.go | 9 +- fwprovider/tests/resource_linux_vlan_test.go | 23 ++- fwprovider/tests/resource_options_test.go | 5 +- fwprovider/tests/resource_user_test.go | 10 +- fwprovider/tests/resource_vm_test.go | 152 ++++++++--------- fwprovider/tests/test_environment.go | 157 ++++++++++++++++++ fwprovider/tests/test_support.go | 122 -------------- proxmox/nodes/query_url_metadata.go | 2 +- utils/env.go | 18 +- 17 files changed, 370 insertions(+), 318 deletions(-) create mode 100644 fwprovider/tests/test_environment.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9aaf12e9..d38ffab5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -99,11 +99,3 @@ jobs: filters: | go: - '**/*.go' - -# - name: Invoke acceptance tests workflow -# if: ${{ steps.filter.outputs.go == 'true' }} -# uses: benc-uk/workflow-dispatch@v1 -# with: -# workflow: testacc.yml -# ref: ${{ github.event.pull_request.head.ref }} -# inputs: '{"ref": "${{ github.head_ref }}" }' diff --git a/.github/workflows/testacc.yml b/.github/workflows/testacc.yml index 478b75ef..9d32cb97 100644 --- a/.github/workflows/testacc.yml +++ b/.github/workflows/testacc.yml @@ -7,14 +7,27 @@ on: description: 'Branch or tag to run tests against' required: true default: 'main' + push: + branches: + - main jobs: acceptance: strategy: - max-parallel: 1 + fail-fast: false matrix: - os: [ ubuntu-latest, windows-latest, macos-latest ] terraform: [ 1.6 ] + os: [ ubuntu-latest, windows-latest, macos-latest ] + include: + - os: ubuntu-latest + node: pve1 + port: 13451 + - os: windows-latest + node: pve2 + port: 13452 + - os: macos-latest + node: pve3 + port: 13453 runs-on: ${{ matrix.os }} environment: pve-acc steps: @@ -53,5 +66,7 @@ jobs: PROXMOX_VE_SSH_AGENT: false PROXMOX_VE_SSH_USERNAME: "terraform" PROXMOX_VE_SSH_PRIVATE_KEY: "${{ secrets.PROXMOX_VE_SSH_PRIVATE_KEY }}" + PROXMOX_VE_ACC_NODE_NAME: ${{ matrix.node }} + PROXMOX_VE_ACC_NODE_SSH_ADDRESS: ${{ secrets.PROXMOX_VE_HOST }} + PROXMOX_VE_ACC_NODE_SSH_PORT: ${{ matrix.port }} run: make testacc - diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e098e1fd..3ae50689 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,6 +61,9 @@ PROXMOX_VE_API_TOKEN="root@pam!=" PROXMOX_VE_ENDPOINT="https://:8006/" PROXMOX_VE_SSH_AGENT="true" PROXMOX_VE_SSH_USERNAME="root" +# optionally, youcan override the default node name and ssh address +#PROXMOX_VE_ACC_NODE_NAME="pve1" +#PROXMOX_VE_ACC_NODE_SSH_ADDRESS="10.0.0.11" ``` Then use `make testacc` to run the acceptance tests. diff --git a/fwprovider/tests/datasource_node_test.go b/fwprovider/tests/datasource_node_test.go index 75264928..aa61b5a6 100644 --- a/fwprovider/tests/datasource_node_test.go +++ b/fwprovider/tests/datasource_node_test.go @@ -7,7 +7,6 @@ package tests import ( - "context" "fmt" "testing" @@ -17,12 +16,14 @@ import ( func TestAccDatasourceNode(t *testing.T) { t.Parallel() + te := initTestEnvironment(t) + tests := []struct { name string steps []resource.TestStep }{ {"read node attributes", []resource.TestStep{{ - Config: fmt.Sprintf(`data "proxmox_virtual_environment_node" "test" { node_name = "%s" }`, accTestNodeName), + Config: fmt.Sprintf(`data "proxmox_virtual_environment_node" "test" { node_name = "%s" }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributesSet("data.proxmox_virtual_environment_node.test", []string{ "cpu_count", @@ -37,14 +38,12 @@ func TestAccDatasourceNode(t *testing.T) { }}}, } - accProviders := testAccMuxProviders(context.Background(), t) - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: tt.steps, }) }) diff --git a/fwprovider/tests/datasource_version_test.go b/fwprovider/tests/datasource_version_test.go index fc1723b1..58d07992 100644 --- a/fwprovider/tests/datasource_version_test.go +++ b/fwprovider/tests/datasource_version_test.go @@ -7,7 +7,6 @@ package tests import ( - "context" "fmt" "strings" "testing" @@ -18,12 +17,12 @@ import ( func TestAccDatasourceVersion(t *testing.T) { t.Parallel() - accProviders := testAccMuxProviders(context.Background(), t) + te := initTestEnvironment(t) datasourceName := "data.proxmox_virtual_environment_version.test" resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: []resource.TestStep{ // Read testing { diff --git a/fwprovider/tests/resource_container_test.go b/fwprovider/tests/resource_container_test.go index d9d50185..551c7de8 100644 --- a/fwprovider/tests/resource_container_test.go +++ b/fwprovider/tests/resource_container_test.go @@ -28,35 +28,40 @@ var ( accCloneContainerID = 200000 + rand.Intn(99999) //nolint:gosec ) -func TestAccResourceContainer(t *testing.T) { - accProviders := testAccMuxProviders(context.Background(), t) +func TestAccResourceContainer(t *testing.T) { //nolint:wsl + // download fails with 404 or "exit code 8" if run in parallel + // t.Parallel() + + te := initTestEnvironment(t) resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: []resource.TestStep{ { - Config: testAccResourceContainerCreateConfig(false), - Check: testAccResourceContainerCreateCheck(t), + Config: testAccResourceContainerCreateConfig(te, false), + Check: testAccResourceContainerCreateCheck(te), }, { - Config: testAccResourceContainerCreateConfig(true) + testAccResourceContainerCreateCloneConfig(), - Check: testAccResourceContainerCreateCloneCheck(t), + Config: testAccResourceContainerCreateConfig(te, true) + testAccResourceContainerCreateCloneConfig(te), + Check: testAccResourceContainerCreateCloneCheck(te), }, }, }) } -func testAccResourceContainerCreateConfig(isTemplate bool) string { +func testAccResourceContainerCreateConfig(te *testEnvironment, isTemplate bool) string { + te.t.Helper() + return fmt.Sprintf(` resource "proxmox_virtual_environment_download_file" "ubuntu_container_template" { content_type = "vztmpl" datastore_id = "local" - node_name = "pve" + node_name = "%[1]s" url = "http://download.proxmox.com/images/system/ubuntu-23.04-standard_23.04-1_amd64.tar.zst" overwrite_unmanaged = true } resource "proxmox_virtual_environment_container" "test_container" { - node_name = "%s" + node_name = "%[1]s" vm_id = %d template = %t @@ -90,24 +95,26 @@ resource "proxmox_virtual_environment_container" "test_container" { type = "ubuntu" } } -`, accTestNodeName, accTestContainerID, isTemplate) +`, te.nodeName, accTestContainerID, isTemplate) } -func testAccResourceContainerCreateCheck(t *testing.T) resource.TestCheckFunc { - t.Helper() +func testAccResourceContainerCreateCheck(te *testEnvironment) resource.TestCheckFunc { + te.t.Helper() return resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(accTestContainerName, "description", "my\ndescription\nvalue\n"), func(*terraform.State) error { - err := getNodesClient().Container(accTestContainerID).WaitForContainerStatus(context.Background(), "running", 10, 1) - require.NoError(t, err, "container did not start") + err := te.nodeClient().Container(accTestContainerID).WaitForContainerStatus(context.Background(), "running", 10, 1) + require.NoError(te.t, err, "container did not start") return nil }, ) } -func testAccResourceContainerCreateCloneConfig() string { +func testAccResourceContainerCreateCloneConfig(te *testEnvironment) string { + te.t.Helper() + return fmt.Sprintf(` resource "proxmox_virtual_environment_container" "test_container_clone" { depends_on = [proxmox_virtual_environment_container.test_container] @@ -123,16 +130,16 @@ resource "proxmox_virtual_environment_container" "test_container_clone" { hostname = "test-clone" } } -`, accTestNodeName, accCloneContainerID) +`, te.nodeName, accCloneContainerID) } -func testAccResourceContainerCreateCloneCheck(t *testing.T) resource.TestCheckFunc { - t.Helper() +func testAccResourceContainerCreateCloneCheck(te *testEnvironment) resource.TestCheckFunc { + te.t.Helper() return resource.ComposeTestCheckFunc( func(*terraform.State) error { - err := getNodesClient().Container(accCloneContainerID).WaitForContainerStatus(context.Background(), "running", 10, 1) - require.NoError(t, err, "container did not start") + err := te.nodeClient().Container(accCloneContainerID).WaitForContainerStatus(context.Background(), "running", 10, 1) + require.NoError(te.t, err, "container did not start") return nil }, diff --git a/fwprovider/tests/resource_download_file_test.go b/fwprovider/tests/resource_download_file_test.go index 3ccb6e01..96ba9531 100644 --- a/fwprovider/tests/resource_download_file_test.go +++ b/fwprovider/tests/resource_download_file_test.go @@ -24,6 +24,8 @@ const ( ) func TestAccResourceDownloadFile(t *testing.T) { + te := initTestEnvironment(t) + tests := []struct { name string steps []resource.TestStep @@ -37,11 +39,11 @@ func TestAccResourceDownloadFile(t *testing.T) { url = "%s" overwrite_unmanaged = true } - `, accTestNodeName, accTestStorageName, fakeFileISO), + `, te.nodeName, accTestStorageName, fakeFileISO), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ "id": "local:iso/fake_file.iso", - "node_name": accTestNodeName, + "node_name": te.nodeName, "datastore_id": accTestStorageName, "url": fakeFileISO, "file_name": "fake_file.iso", @@ -68,12 +70,12 @@ func TestAccResourceDownloadFile(t *testing.T) { checksum_algorithm = "sha256" overwrite_unmanaged = true } - `, accTestNodeName, accTestStorageName, fakeFileQCOW2), + `, te.nodeName, accTestStorageName, fakeFileQCOW2), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_download_file.qcow2_image", map[string]string{ "id": "local:iso/fake_qcow2_file.img", "content_type": "iso", - "node_name": accTestNodeName, + "node_name": te.nodeName, "datastore_id": accTestStorageName, "url": fakeFileQCOW2, "file_name": "fake_qcow2_file.img", @@ -99,12 +101,12 @@ func TestAccResourceDownloadFile(t *testing.T) { upload_timeout = 10000 overwrite_unmanaged = true } - `, accTestNodeName, accTestStorageName, fakeFileISO), + `, te.nodeName, accTestStorageName, fakeFileISO), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ "id": "local:iso/fake_iso_file.img", "content_type": "iso", - "node_name": accTestNodeName, + "node_name": te.nodeName, "datastore_id": accTestStorageName, "url": fakeFileISO, "file_name": "fake_iso_file.img", @@ -121,16 +123,16 @@ func TestAccResourceDownloadFile(t *testing.T) { }}}, {"override unmanaged file", []resource.TestStep{{ PreConfig: func() { - err := getNodeStorageClient().DownloadFileByURL(context.Background(), &storage.DownloadURLPostRequestBody{ + err := te.nodeStorageClient().DownloadFileByURL(context.Background(), &storage.DownloadURLPostRequestBody{ Content: types.StrPtr("iso"), FileName: types.StrPtr("fake_file.iso"), - Node: types.StrPtr(accTestNodeName), + Node: types.StrPtr(te.nodeName), Storage: types.StrPtr(accTestStorageName), URL: types.StrPtr(fakeFileISO), }, 600) require.NoError(t, err) t.Cleanup(func() { - err := getNodeStorageClient().DeleteDatastoreFile(context.Background(), "iso/fake_file.iso") + err := te.nodeStorageClient().DeleteDatastoreFile(context.Background(), "iso/fake_file.iso") require.NoError(t, err) }) }, @@ -142,12 +144,12 @@ func TestAccResourceDownloadFile(t *testing.T) { url = "%s" overwrite_unmanaged = true } - `, accTestNodeName, accTestStorageName, fakeFileISO), + `, te.nodeName, accTestStorageName, fakeFileISO), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ "id": "local:iso/fake_file.iso", "content_type": "iso", - "node_name": accTestNodeName, + "node_name": te.nodeName, "datastore_id": accTestStorageName, "url": fakeFileISO, "file_name": "fake_file.iso", @@ -163,12 +165,10 @@ func TestAccResourceDownloadFile(t *testing.T) { }}}, } - accProviders := testAccMuxProviders(context.Background(), t) - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: tt.steps, }) }) diff --git a/fwprovider/tests/resource_file_test.go b/fwprovider/tests/resource_file_test.go index 98919b68..646b05c0 100644 --- a/fwprovider/tests/resource_file_test.go +++ b/fwprovider/tests/resource_file_test.go @@ -43,7 +43,7 @@ func (c *nodeResolver) Resolve(_ context.Context, _ string) (ssh.ProxmoxNode, er func TestAccResourceFile(t *testing.T) { t.Parallel() - accProviders := testAccMuxProviders(context.Background(), t) + te := initTestEnvironment(t) snippetRaw := fmt.Sprintf("snippet-raw-%s.txt", gofakeit.Word()) snippetURL := "https://raw.githubusercontent.com/yaml/yaml-test-suite/main/src/229Q.yaml" @@ -52,55 +52,55 @@ func TestAccResourceFile(t *testing.T) { fileISO := createFile(t, "file-*.iso", "pretend it is an ISO") resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, PreCheck: func() { uploadSnippetFile(t, snippetFile2) }, Steps: []resource.TestStep{ { - Config: testAccResourceFileSnippetRawCreatedConfig(t, snippetRaw), + Config: testAccResourceFileSnippetRawCreatedConfig(te, snippetRaw), Check: testAccResourceFileSnippetRawCreatedCheck(snippetRaw), }, { - Config: testAccResourceFileCreatedConfig(t, snippetFile1.Name()), + Config: testAccResourceFileCreatedConfig(te, snippetFile1.Name()), Check: testAccResourceFileCreatedCheck("snippets", snippetFile1.Name()), }, { - Config: testAccResourceFileCreatedConfig(t, snippetURL), + Config: testAccResourceFileCreatedConfig(te, snippetURL), Check: testAccResourceFileCreatedCheck("snippets", snippetURL), }, { - Config: testAccResourceFileCreatedConfig(t, fileISO.Name()), + Config: testAccResourceFileCreatedConfig(te, fileISO.Name()), Check: testAccResourceFileCreatedCheck("iso", fileISO.Name()), }, { - Config: testAccResourceFileTwoSourcesCreatedConfig(t), + Config: testAccResourceFileTwoSourcesCreatedConfig(te), ExpectError: regexp.MustCompile("please specify .* - not both"), }, { - Config: testAccResourceFileCreatedConfig(t, "https://github.com", "content_type = \"iso\""), + Config: testAccResourceFileCreatedConfig(te, "https://github.com", "content_type = \"iso\""), ExpectError: regexp.MustCompile("failed to determine file name from the URL"), }, { - Config: testAccResourceFileMissingSourceConfig(t), + Config: testAccResourceFileMissingSourceConfig(te), ExpectError: regexp.MustCompile("missing argument"), }, // Do not allow to overwrite the file { - Config: testAccResourceFileCreatedConfig(t, snippetFile2.Name(), "overwrite = false"), + Config: testAccResourceFileCreatedConfig(te, snippetFile2.Name(), "overwrite = false"), ExpectError: regexp.MustCompile("already exists"), }, // Allow to overwrite the file by default { - Config: testAccResourceFileCreatedConfig(t, snippetFile2.Name()), + Config: testAccResourceFileCreatedConfig(te, snippetFile2.Name()), Check: testAccResourceFileCreatedCheck("snippets", snippetFile2.Name()), }, // Update testing { PreConfig: func() { - deleteSnippet(t, filepath.Base(snippetFile1.Name())) + deleteSnippet(te, filepath.Base(snippetFile1.Name())) }, - Config: testAccResourceFileSnippetUpdateConfig(t, snippetFile1.Name()), + Config: testAccResourceFileSnippetUpdateConfig(te, snippetFile1.Name()), Check: testAccResourceFileSnippetUpdatedCheck(snippetFile1.Name()), }, // ImportState testing @@ -129,13 +129,14 @@ func uploadSnippetFile(t *testing.T, file *os.File) { sshUsername := utils.GetAnyStringEnv("PROXMOX_VE_SSH_USERNAME") sshAgentSocket := utils.GetAnyStringEnv("SSH_AUTH_SOCK", "PROXMOX_VE_SSH_AUTH_SOCK") sshPrivateKey := utils.GetAnyStringEnv("PROXMOX_VE_SSH_PRIVATE_KEY") + sshPort := utils.GetAnyIntEnv("PROXMOX_VE_ACC_NODE_SSH_PORT") sshClient, err := ssh.NewClient( sshUsername, "", sshAgent, sshAgentSocket, sshPrivateKey, "", "", "", &nodeResolver{ node: ssh.ProxmoxNode{ Address: u.Hostname(), - Port: 22, + Port: int32(sshPort), }, }, ) @@ -174,15 +175,15 @@ func createFile(t *testing.T, namePattern string, content string) *os.File { return f } -func deleteSnippet(t *testing.T, fname string) { - t.Helper() +func deleteSnippet(te *testEnvironment, fname string) { + te.t.Helper() - err := getNodeStorageClient().DeleteDatastoreFile(context.Background(), fmt.Sprintf("snippets/%s", fname)) - require.NoError(t, err) + err := te.nodeStorageClient().DeleteDatastoreFile(context.Background(), fmt.Sprintf("snippets/%s", fname)) + require.NoError(te.t, err) } -func testAccResourceFileSnippetRawCreatedConfig(t *testing.T, fname string) string { - t.Helper() +func testAccResourceFileSnippetRawCreatedConfig(te *testEnvironment, fname string) string { + te.t.Helper() return fmt.Sprintf(`%s resource "proxmox_virtual_environment_file" "test_raw" { @@ -196,11 +197,11 @@ test snippet file_name = "%s" } } - `, getProviderConfig(t), accTestNodeName, fname) + `, te.providerConfig, te.nodeName, fname) } -func testAccResourceFileCreatedConfig(t *testing.T, fname string, extra ...string) string { - t.Helper() +func testAccResourceFileCreatedConfig(te *testEnvironment, fname string, extra ...string) string { + te.t.Helper() return fmt.Sprintf(`%s resource "proxmox_virtual_environment_file" "test" { @@ -211,11 +212,11 @@ resource "proxmox_virtual_environment_file" "test" { } %s } - `, getProviderConfig(t), accTestNodeName, strings.ReplaceAll(fname, `\`, `/`), strings.Join(extra, "\n")) + `, te.providerConfig, te.nodeName, strings.ReplaceAll(fname, `\`, `/`), strings.Join(extra, "\n")) } -func testAccResourceFileTwoSourcesCreatedConfig(t *testing.T) string { - t.Helper() +func testAccResourceFileTwoSourcesCreatedConfig(te *testEnvironment) string { + te.t.Helper() return fmt.Sprintf(`%s resource "proxmox_virtual_environment_file" "test" { @@ -231,18 +232,18 @@ test snippet path = "bar.yaml" } } - `, getProviderConfig(t), accTestNodeName) + `, te.providerConfig, te.nodeName) } -func testAccResourceFileMissingSourceConfig(t *testing.T) string { - t.Helper() +func testAccResourceFileMissingSourceConfig(te *testEnvironment) string { + te.t.Helper() return fmt.Sprintf(`%s resource "proxmox_virtual_environment_file" "test" { datastore_id = "local" node_name = "%s" } - `, getProviderConfig(t), accTestNodeName) + `, te.providerConfig, te.nodeName) } func testAccResourceFileSnippetRawCreatedCheck(fname string) resource.TestCheckFunc { @@ -263,8 +264,8 @@ func testAccResourceFileCreatedCheck(ctype string, fname string) resource.TestCh ) } -func testAccResourceFileSnippetUpdateConfig(t *testing.T, fname string) string { - t.Helper() +func testAccResourceFileSnippetUpdateConfig(te *testEnvironment, fname string) string { + te.t.Helper() return fmt.Sprintf(`%s resource "proxmox_virtual_environment_file" "test" { @@ -274,7 +275,7 @@ resource "proxmox_virtual_environment_file" "test" { path = "%s" } } - `, getProviderConfig(t), accTestNodeName, strings.ReplaceAll(fname, `\`, `/`)) + `, te.providerConfig, te.nodeName, strings.ReplaceAll(fname, `\`, `/`)) } func testAccResourceFileSnippetUpdatedCheck(fname string) resource.TestCheckFunc { diff --git a/fwprovider/tests/resource_linux_bridge_test.go b/fwprovider/tests/resource_linux_bridge_test.go index 452bf944..40ede33f 100644 --- a/fwprovider/tests/resource_linux_bridge_test.go +++ b/fwprovider/tests/resource_linux_bridge_test.go @@ -7,7 +7,6 @@ package tests import ( - "context" "fmt" "testing" @@ -17,7 +16,7 @@ import ( ) func TestAccResourceLinuxBridge(t *testing.T) { - accProviders := testAccMuxProviders(context.Background(), t) + te := initTestEnvironment(t) iface := fmt.Sprintf("vmbr%d", gofakeit.Number(10, 9999)) ipV4cidr1 := fmt.Sprintf("%s/24", gofakeit.IPv4Address()) @@ -25,7 +24,7 @@ func TestAccResourceLinuxBridge(t *testing.T) { ipV6cidr := "FE80:0000:0000:0000:0202:B3FF:FE1E:8329/64" resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: []resource.TestStep{ // Create and Read testing { @@ -39,7 +38,7 @@ func TestAccResourceLinuxBridge(t *testing.T) { node_name = "%s" vlan_aware = true } - `, ipV4cidr1, iface, accTestNodeName), + `, ipV4cidr1, iface, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_network_linux_bridge.test", map[string]string{ "address": ipV4cidr1, @@ -66,7 +65,7 @@ func TestAccResourceLinuxBridge(t *testing.T) { name = "%s" node_name = "%s" vlan_aware = false - }`, ipV4cidr2, ipV6cidr, iface, accTestNodeName), + }`, ipV4cidr2, ipV6cidr, iface, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_network_linux_bridge.test", map[string]string{ "address": ipV4cidr2, diff --git a/fwprovider/tests/resource_linux_vlan_test.go b/fwprovider/tests/resource_linux_vlan_test.go index e2dd381b..4854ba04 100644 --- a/fwprovider/tests/resource_linux_vlan_test.go +++ b/fwprovider/tests/resource_linux_vlan_test.go @@ -7,7 +7,6 @@ package tests import ( - "context" "fmt" "strconv" "testing" @@ -21,7 +20,7 @@ const ( ) func TestAccResourceLinuxVLAN(t *testing.T) { - accProviders := testAccMuxProviders(context.Background(), t) + te := initTestEnvironment(t) iface := "ens18" vlan1 := gofakeit.Number(10, 4094) @@ -30,11 +29,11 @@ func TestAccResourceLinuxVLAN(t *testing.T) { ipV4cidr := fmt.Sprintf("%s/24", gofakeit.IPv4Address()) resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: []resource.TestStep{ // Create and Read testing { - Config: testAccResourceLinuxVLANCreatedConfig(iface, vlan1), + Config: testAccResourceLinuxVLANCreatedConfig(te, iface, vlan1), Check: testAccResourceLinuxVLANCreatedCheck(iface, vlan1), }, // ImportState testing @@ -45,7 +44,7 @@ func TestAccResourceLinuxVLAN(t *testing.T) { }, // Create and Read with a custom name { - Config: testAccResourceLinuxVLANCustomNameCreatedConfig(customName, iface, vlan2), + Config: testAccResourceLinuxVLANCustomNameCreatedConfig(te, 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) { @@ -54,14 +53,14 @@ func TestAccResourceLinuxVLAN(t *testing.T) { }, // Update testing { - Config: testAccResourceLinuxVLANUpdatedConfig(iface, vlan1, ipV4cidr), + Config: testAccResourceLinuxVLANUpdatedConfig(te, iface, vlan1, ipV4cidr), Check: testAccResourceLinuxVLANUpdatedCheck(iface, vlan1, ipV4cidr), }, }, }) } -func testAccResourceLinuxVLANCreatedConfig(iface string, vlan int) string { +func testAccResourceLinuxVLANCreatedConfig(te *testEnvironment, iface string, vlan int) string { return fmt.Sprintf(` resource "proxmox_virtual_environment_network_linux_vlan" "test" { comment = "created by terraform" @@ -69,7 +68,7 @@ func testAccResourceLinuxVLANCreatedConfig(iface string, vlan int) string { name = "%s.%d" node_name = "%s" } - `, iface, vlan, accTestNodeName) + `, iface, vlan, te.nodeName) } func testAccResourceLinuxVLANCreatedCheck(iface string, vlan int) resource.TestCheckFunc { @@ -82,7 +81,7 @@ func testAccResourceLinuxVLANCreatedCheck(iface string, vlan int) resource.TestC ) } -func testAccResourceLinuxVLANCustomNameCreatedConfig(name string, iface string, vlan int) string { +func testAccResourceLinuxVLANCustomNameCreatedConfig(te *testEnvironment, name string, iface string, vlan int) string { return fmt.Sprintf(` resource "proxmox_virtual_environment_network_linux_vlan" "%s" { comment = "created by terraform" @@ -92,7 +91,7 @@ func testAccResourceLinuxVLANCustomNameCreatedConfig(name string, iface string, node_name = "%s" vlan = %d } - `, name, iface, name, accTestNodeName, vlan) + `, name, iface, name, te.nodeName, vlan) } func testAccResourceLinuxVLANCustomNameCreatedCheck(name string, iface string, vlan int) resource.TestCheckFunc { @@ -107,7 +106,7 @@ func testAccResourceLinuxVLANCustomNameCreatedCheck(name string, iface string, v ) } -func testAccResourceLinuxVLANUpdatedConfig(iface string, vlan int, ipV4cidr string) string { +func testAccResourceLinuxVLANUpdatedConfig(te *testEnvironment, iface string, vlan int, ipV4cidr string) string { return fmt.Sprintf(` resource "proxmox_virtual_environment_network_linux_vlan" "test" { address = "%s" @@ -116,7 +115,7 @@ func testAccResourceLinuxVLANUpdatedConfig(iface string, vlan int, ipV4cidr stri name = "%s.%d" node_name = "%s" } - `, ipV4cidr, iface, vlan, accTestNodeName) + `, ipV4cidr, iface, vlan, te.nodeName) } func testAccResourceLinuxVLANUpdatedCheck(iface string, vlan int, ipV4cidr string) resource.TestCheckFunc { diff --git a/fwprovider/tests/resource_options_test.go b/fwprovider/tests/resource_options_test.go index 570135d6..56a8e677 100644 --- a/fwprovider/tests/resource_options_test.go +++ b/fwprovider/tests/resource_options_test.go @@ -7,7 +7,6 @@ package tests import ( - "context" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -18,10 +17,10 @@ const accTestClusterOptionsName = "proxmox_virtual_environment_cluster_options.t func TestAccResourceClusterOptions(t *testing.T) { t.Parallel() - accProviders := testAccMuxProviders(context.Background(), t) + te := initTestEnvironment(t) resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: []resource.TestStep{ // Create and Read testing { diff --git a/fwprovider/tests/resource_user_test.go b/fwprovider/tests/resource_user_test.go index 812f3e05..68d4fd53 100644 --- a/fwprovider/tests/resource_user_test.go +++ b/fwprovider/tests/resource_user_test.go @@ -7,13 +7,16 @@ package tests import ( - "context" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestAccResourceUser(t *testing.T) { + t.Parallel() + + te := initTestEnvironment(t) + tests := []struct { name string steps []resource.TestStep @@ -27,7 +30,6 @@ func TestAccResourceUser(t *testing.T) { expiration_date = "2034-01-01T22:00:00Z" first_name = "First" last_name = "Last" - //password = "password" user_id = "user1@pve" } `, @@ -60,12 +62,10 @@ func TestAccResourceUser(t *testing.T) { }}}, } - accProviders := testAccMuxProviders(context.Background(), t) - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: tt.steps, }) }) diff --git a/fwprovider/tests/resource_vm_test.go b/fwprovider/tests/resource_vm_test.go index 16c1e8b0..b0d4318b 100644 --- a/fwprovider/tests/resource_vm_test.go +++ b/fwprovider/tests/resource_vm_test.go @@ -7,7 +7,7 @@ package tests import ( - "context" + "fmt" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -16,16 +16,16 @@ import ( func TestAccResourceVM(t *testing.T) { t.Parallel() - providerConfig := getProviderConfig(t) + te := initTestEnvironment(t) tests := []struct { name string step []resource.TestStep }{ {"multiline description", []resource.TestStep{{ - Config: providerConfig + ` + Config: te.providerConfig + fmt.Sprintf(` resource "proxmox_virtual_environment_vm" "test_vm1" { - node_name = "pve" + node_name = "%s" started = false description = <<-EOT @@ -33,7 +33,7 @@ func TestAccResourceVM(t *testing.T) { description value EOT - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm1", map[string]string{ "description": "my\ndescription\nvalue", @@ -41,13 +41,13 @@ func TestAccResourceVM(t *testing.T) { ), }}}, {"single line description", []resource.TestStep{{ - Config: providerConfig + ` + Config: te.providerConfig + fmt.Sprintf(` resource "proxmox_virtual_environment_vm" "test_vm2" { - node_name = "pve" + node_name = "%s" started = false description = "my description value" - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm2", map[string]string{ "description": "my description value", @@ -55,13 +55,13 @@ func TestAccResourceVM(t *testing.T) { ), }}}, {"no description", []resource.TestStep{{ - Config: ` + Config: fmt.Sprintf(` resource "proxmox_virtual_environment_vm" "test_vm3" { - node_name = "pve" + node_name = "%s" started = false description = "" - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm3", map[string]string{ "description": "", @@ -70,26 +70,26 @@ func TestAccResourceVM(t *testing.T) { }}}, { "protection", []resource.TestStep{{ - Config: ` + Config: fmt.Sprintf(` resource "proxmox_virtual_environment_vm" "test_vm4" { - node_name = "pve" + node_name = "%s" started = false protection = true - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ "protection": "true", }), ), }, { - Config: ` + Config: fmt.Sprintf(` resource "proxmox_virtual_environment_vm" "test_vm4" { - node_name = "pve" + node_name = "%s" started = false protection = false - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ "protection": "false", @@ -99,30 +99,28 @@ func TestAccResourceVM(t *testing.T) { }, { "update cpu block", []resource.TestStep{{ - Config: ` - resource "proxmox_virtual_environment_vm" "test_vm5" { - node_name = "pve" + Config: fmt.Sprintf(`resource "proxmox_virtual_environment_vm" "test_vm5" { + node_name = "%s" started = false cpu { cores = 2 } - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ "cpu.0.sockets": "1", }), ), }, { - Config: ` - resource "proxmox_virtual_environment_vm" "test_vm5" { - node_name = "pve" + Config: fmt.Sprintf(`resource "proxmox_virtual_environment_vm" "test_vm5" { + node_name = "%s" started = false cpu { cores = 1 } - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ "cpu.0.sockets": "1", @@ -132,30 +130,28 @@ func TestAccResourceVM(t *testing.T) { }, { "update memory block", []resource.TestStep{{ - Config: ` - resource "proxmox_virtual_environment_vm" "test_vm6" { - node_name = "pve" + Config: fmt.Sprintf(`resource "proxmox_virtual_environment_vm" "test_vm6" { + node_name = "%s" started = false memory { dedicated = 2048 } - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ "memory.0.dedicated": "2048", }), ), }, { - Config: ` - resource "proxmox_virtual_environment_vm" "test_vm6" { - node_name = "pve" + Config: fmt.Sprintf(`resource "proxmox_virtual_environment_vm" "test_vm6" { + node_name = "%s" started = false memory { dedicated = 1024 } - }`, + }`, te.nodeName), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ "memory.0.dedicated": "1024", @@ -165,14 +161,12 @@ func TestAccResourceVM(t *testing.T) { }, } - accProviders := testAccMuxProviders(context.Background(), t) - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ - ProtoV6ProviderFactories: accProviders, + ProtoV6ProviderFactories: te.accProviders, Steps: tt.step, }) }) @@ -180,18 +174,18 @@ func TestAccResourceVM(t *testing.T) { } func TestAccResourceVMInitialization(t *testing.T) { - providerConfig := getProviderConfig(t) + te := initTestEnvironment(t) tests := []struct { name string step []resource.TestStep }{ {"initialization works with cloud-init config provided over SCSI interface", []resource.TestStep{{ - Config: providerConfig + ` + Config: te.providerConfig + fmt.Sprintf(` resource "proxmox_virtual_environment_file" "cloud_config" { content_type = "snippets" datastore_id = "local" - node_name = "pve" + node_name = "%[1]s" source_raw { data = <