From c772fb3cf658fcda9051f8305125fdfc78ce3b3b Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:01:32 -0400 Subject: [PATCH] chore: refactor acceptance tests (#1195) * misc: refactor acceptance tests Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> * moar refactoring Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> * fix cleanup in TestAccResourceFile Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --------- Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --- .vscode/settings.json | 1 + fwprovider/tests/datasource_node_test.go | 9 +- fwprovider/tests/datasource_version_test.go | 4 +- fwprovider/tests/resource_container_test.go | 15 +- .../tests/resource_download_file_test.go | 155 +++++----- fwprovider/tests/resource_file_test.go | 291 +++++++++--------- .../tests/resource_linux_bridge_test.go | 12 +- fwprovider/tests/resource_linux_vlan_test.go | 24 +- fwprovider/tests/resource_user_test.go | 37 +-- fwprovider/tests/resource_vm_test.go | 129 ++++---- fwprovider/tests/test_environment.go | 50 ++- 11 files changed, 375 insertions(+), 352 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 53d7cf40..6cc92b9e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -26,6 +26,7 @@ "cputype", "cpuunits", "customdiff", + "Datasource", "deepcode", "directsync", "efidisk", diff --git a/fwprovider/tests/datasource_node_test.go b/fwprovider/tests/datasource_node_test.go index aa61b5a6..79d941bf 100644 --- a/fwprovider/tests/datasource_node_test.go +++ b/fwprovider/tests/datasource_node_test.go @@ -7,15 +7,12 @@ package tests import ( - "fmt" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestAccDatasourceNode(t *testing.T) { - t.Parallel() - te := initTestEnvironment(t) tests := []struct { @@ -23,7 +20,7 @@ func TestAccDatasourceNode(t *testing.T) { steps []resource.TestStep }{ {"read node attributes", []resource.TestStep{{ - Config: fmt.Sprintf(`data "proxmox_virtual_environment_node" "test" { node_name = "%s" }`, te.nodeName), + Config: te.renderConfig(`data "proxmox_virtual_environment_node" "test" { node_name = "{{.NodeName}}" }`), Check: resource.ComposeTestCheckFunc( testResourceAttributesSet("data.proxmox_virtual_environment_node.test", []string{ "cpu_count", @@ -40,9 +37,7 @@ func TestAccDatasourceNode(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - t.Parallel() - - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ ProtoV6ProviderFactories: te.accProviders, Steps: tt.steps, }) diff --git a/fwprovider/tests/datasource_version_test.go b/fwprovider/tests/datasource_version_test.go index 58d07992..7bec1950 100644 --- a/fwprovider/tests/datasource_version_test.go +++ b/fwprovider/tests/datasource_version_test.go @@ -15,13 +15,11 @@ import ( ) func TestAccDatasourceVersion(t *testing.T) { - t.Parallel() - te := initTestEnvironment(t) datasourceName := "data.proxmox_virtual_environment_version.test" - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ 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 551c7de8..d1d127b6 100644 --- a/fwprovider/tests/resource_container_test.go +++ b/fwprovider/tests/resource_container_test.go @@ -38,11 +38,11 @@ func TestAccResourceContainer(t *testing.T) { //nolint:wsl ProtoV6ProviderFactories: te.accProviders, Steps: []resource.TestStep{ { - Config: testAccResourceContainerCreateConfig(te, false), + Config: te.renderConfig(testAccResourceContainerCreateConfig(te, false)), Check: testAccResourceContainerCreateCheck(te), }, { - Config: testAccResourceContainerCreateConfig(te, true) + testAccResourceContainerCreateCloneConfig(te), + Config: te.renderConfig(testAccResourceContainerCreateConfig(te, true) + testAccResourceContainerCreateCloneConfig(te)), Check: testAccResourceContainerCreateCloneCheck(te), }, }, @@ -56,12 +56,12 @@ func testAccResourceContainerCreateConfig(te *testEnvironment, isTemplate bool) resource "proxmox_virtual_environment_download_file" "ubuntu_container_template" { content_type = "vztmpl" datastore_id = "local" - node_name = "%[1]s" + node_name = "{{.NodeName}}" 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 = "%[1]s" + node_name = "{{.NodeName}}" vm_id = %d template = %t @@ -95,7 +95,7 @@ resource "proxmox_virtual_environment_container" "test_container" { type = "ubuntu" } } -`, te.nodeName, accTestContainerID, isTemplate) +`, accTestContainerID, isTemplate) } func testAccResourceContainerCreateCheck(te *testEnvironment) resource.TestCheckFunc { @@ -118,8 +118,7 @@ func testAccResourceContainerCreateCloneConfig(te *testEnvironment) string { return fmt.Sprintf(` resource "proxmox_virtual_environment_container" "test_container_clone" { depends_on = [proxmox_virtual_environment_container.test_container] - - node_name = "%s" + node_name = "{{.NodeName}}" vm_id = %d clone { @@ -130,7 +129,7 @@ resource "proxmox_virtual_environment_container" "test_container_clone" { hostname = "test-clone" } } -`, te.nodeName, accCloneContainerID) +`, accCloneContainerID) } func testAccResourceContainerCreateCloneCheck(te *testEnvironment) resource.TestCheckFunc { diff --git a/fwprovider/tests/resource_download_file_test.go b/fwprovider/tests/resource_download_file_test.go index 96ba9531..ab151d37 100644 --- a/fwprovider/tests/resource_download_file_test.go +++ b/fwprovider/tests/resource_download_file_test.go @@ -8,7 +8,6 @@ package tests import ( "context" - "fmt" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -26,57 +25,33 @@ const ( func TestAccResourceDownloadFile(t *testing.T) { te := initTestEnvironment(t) + te.addTemplateVars(map[string]interface{}{ + "FakeFileISO": fakeFileISO, + "FakeFileQCOW2": fakeFileQCOW2, + }) + tests := []struct { name string steps []resource.TestStep }{ - {"download iso file", []resource.TestStep{{ - Config: fmt.Sprintf(` - resource "proxmox_virtual_environment_download_file" "iso_image" { - content_type = "iso" - node_name = "%s" - datastore_id = "%s" - url = "%s" - overwrite_unmanaged = true - } - `, 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": te.nodeName, - "datastore_id": accTestStorageName, - "url": fakeFileISO, - "file_name": "fake_file.iso", - "upload_timeout": "600", - "size": "3", - "verify": "true", - }), - testNoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image", []string{ - "checksum", - "checksum_algorithm", - "decompression_algorithm", - }), - ), - }}}, {"download qcow2 file", []resource.TestStep{{ - Config: fmt.Sprintf(` + Config: te.renderConfig(` resource "proxmox_virtual_environment_download_file" "qcow2_image" { content_type = "iso" - node_name = "%s" - datastore_id = "%s" + node_name = "{{.NodeName}}" + datastore_id = "{{.DatastoreID}}" file_name = "fake_qcow2_file.img" - url = "%s" + url = "{{.FakeFileQCOW2}}" checksum = "688787d8ff144c502c7f5cffaafe2cc588d86079f9de88304c26b0cb99ce91c6" checksum_algorithm = "sha256" overwrite_unmanaged = true - } - `, 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": te.nodeName, - "datastore_id": accTestStorageName, + "datastore_id": te.datastoreID, "url": fakeFileQCOW2, "file_name": "fake_qcow2_file.img", "upload_timeout": "600", @@ -90,44 +65,72 @@ func TestAccResourceDownloadFile(t *testing.T) { }), ), }}}, - {"update file", []resource.TestStep{{ - Config: fmt.Sprintf(` + {"download & update iso file", []resource.TestStep{ + { + Config: te.renderConfig(` + resource "proxmox_virtual_environment_download_file" "iso_image" { + content_type = "iso" + node_name = "{{.NodeName}}" + datastore_id = "{{.DatastoreID}}" + url = "{{.FakeFileISO}}" + overwrite_unmanaged = true + }`), + Check: resource.ComposeTestCheckFunc( + testResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ + "id": "local:iso/fake_file.iso", + "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{ + "checksum", + "checksum_algorithm", + "decompression_algorithm", + }), + ), + }, + { + Config: te.renderConfig(` resource "proxmox_virtual_environment_download_file" "iso_image" { content_type = "iso" - node_name = "%s" - datastore_id = "%s" + node_name = "{{.NodeName}}" + datastore_id = "{{.DatastoreID}}" file_name = "fake_iso_file.img" - url = "%s" + url = "{{.FakeFileISO}}" upload_timeout = 10000 overwrite_unmanaged = true - } - `, 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": te.nodeName, - "datastore_id": accTestStorageName, - "url": fakeFileISO, - "file_name": "fake_iso_file.img", - "upload_timeout": "10000", - "size": "3", - "verify": "true", - }), - testNoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image", []string{ - "checksum", - "checksum_algorithm", - "decompression_algorithm", - }), - ), - }}}, + }`), + 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": 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{ + "checksum", + "checksum_algorithm", + "decompression_algorithm", + }), + ), + }, + }}, {"override unmanaged file", []resource.TestStep{{ PreConfig: func() { err := te.nodeStorageClient().DownloadFileByURL(context.Background(), &storage.DownloadURLPostRequestBody{ Content: types.StrPtr("iso"), FileName: types.StrPtr("fake_file.iso"), Node: types.StrPtr(te.nodeName), - Storage: types.StrPtr(accTestStorageName), + Storage: types.StrPtr(te.datastoreID), URL: types.StrPtr(fakeFileISO), }, 600) require.NoError(t, err) @@ -136,27 +139,27 @@ func TestAccResourceDownloadFile(t *testing.T) { require.NoError(t, err) }) }, - Config: fmt.Sprintf(` - resource "proxmox_virtual_environment_download_file" "iso_image" { + Config: te.renderConfig(` + resource "proxmox_virtual_environment_download_file" "iso_image3" { content_type = "iso" - node_name = "%s" - datastore_id = "%s" - url = "%s" + node_name = "{{.NodeName}}" + datastore_id = "{{.DatastoreID}}" + url = "{{.FakeFileISO}}" + file_name = "fake_iso_file3.iso" overwrite_unmanaged = true - } - `, te.nodeName, accTestStorageName, fakeFileISO), + }`), Check: resource.ComposeTestCheckFunc( - testResourceAttributes("proxmox_virtual_environment_download_file.iso_image", map[string]string{ - "id": "local:iso/fake_file.iso", + testResourceAttributes("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": accTestStorageName, + "datastore_id": te.datastoreID, "url": fakeFileISO, - "file_name": "fake_file.iso", + "file_name": "fake_iso_file3.iso", "size": "3", "verify": "true", }), - testNoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image", []string{ + testNoResourceAttributesSet("proxmox_virtual_environment_download_file.iso_image3", []string{ "checksum", "checksum_algorithm", "decompression_algorithm", @@ -167,7 +170,7 @@ func TestAccResourceDownloadFile(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - resource.Test(t, resource.TestCase{ + resource.ParallelTest(t, resource.TestCase{ ProtoV6ProviderFactories: te.accProviders, Steps: tt.steps, }) diff --git a/fwprovider/tests/resource_file_test.go b/fwprovider/tests/resource_file_test.go index 646b05c0..4fa9b7b0 100644 --- a/fwprovider/tests/resource_file_test.go +++ b/fwprovider/tests/resource_file_test.go @@ -27,11 +27,6 @@ import ( "github.com/bpg/terraform-provider-proxmox/utils" ) -const ( - accTestFileRawName = "proxmox_virtual_environment_file.test_raw" - accTestFileName = "proxmox_virtual_environment_file.test" -) - type nodeResolver struct { node ssh.ProxmoxNode } @@ -41,84 +36,192 @@ func (c *nodeResolver) Resolve(_ context.Context, _ string) (ssh.ProxmoxNode, er } func TestAccResourceFile(t *testing.T) { - t.Parallel() - 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" - snippetFile1 := createFile(t, "snippet-file-1-*.yaml", "test snippet 1 - file") - snippetFile2 := createFile(t, "snippet-file-2-*.yaml", "test snippet 2 - file") - fileISO := createFile(t, "file-*.iso", "pretend it is an ISO") + snippetFile1 := strings.ReplaceAll(createFile(t, "snippet-file-1-*.yaml", "test snippet 1 - file").Name(), `\`, `/`) + 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(), `\`, `/`) - resource.Test(t, resource.TestCase{ + te.addTemplateVars(map[string]interface{}{ + "SnippetRaw": snippetRaw, + "SnippetURL": snippetURL, + "SnippetFile1": snippetFile1, + "SnippetFile2": snippetFile2, + "FileISO": fileISO, + }) + + resource.ParallelTest(t, resource.TestCase{ ProtoV6ProviderFactories: te.accProviders, PreCheck: func() { uploadSnippetFile(t, snippetFile2) + t.Cleanup(func() { + deleteSnippet(te, filepath.Base(snippetFile1)) + deleteSnippet(te, filepath.Base(snippetFile2)) + + _ = os.Remove(snippetFile1) + _ = os.Remove(snippetFile2) + _ = os.Remove(fileISO) + }) }, Steps: []resource.TestStep{ { - Config: testAccResourceFileSnippetRawCreatedConfig(te, snippetRaw), - Check: testAccResourceFileSnippetRawCreatedCheck(snippetRaw), + Config: te.renderConfig(` + resource "proxmox_virtual_environment_file" "test_raw" { + content_type = "snippets" + datastore_id = "local" + node_name = "{{.NodeName}}" + source_raw { + data = <