/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ package tests import ( "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestAccResourceVM(t *testing.T) { t.Parallel() te := initTestEnvironment(t) tests := []struct { name string step []resource.TestStep }{ {"multiline description", []resource.TestStep{{ Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm1" { node_name = "{{.NodeName}}" started = false description = <<-EOT my description value EOT }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm1", map[string]string{ "description": "my\ndescription\nvalue", }), ), }}}, {"single line description", []resource.TestStep{{ Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm2" { node_name = "{{.NodeName}}" started = false description = "my description value" }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm2", map[string]string{ "description": "my description value", }), ), }}}, {"no description", []resource.TestStep{{ Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm3" { node_name = "{{.NodeName}}" started = false description = "" }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm3", map[string]string{ "description": "", }), ), }}}, { "protection", []resource.TestStep{{ Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm4" { node_name = "{{.NodeName}}" started = false protection = true }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ "protection": "true", }), ), }, { Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm4" { node_name = "{{.NodeName}}" started = false protection = false }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm4", map[string]string{ "protection": "false", }), ), }}, }, { "update cpu block", []resource.TestStep{{ Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm5" { node_name = "{{.NodeName}}" started = false cpu { cores = 2 } }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ "cpu.0.sockets": "1", }), ), }, { Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm5" { node_name = "{{.NodeName}}" started = false cpu { cores = 1 } }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm5", map[string]string{ "cpu.0.sockets": "1", }), ), }}, }, { "update memory block", []resource.TestStep{{ Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm6" { node_name = "{{.NodeName}}" started = false memory { dedicated = 2048 } }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ "memory.0.dedicated": "2048", }), ), }, { Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm6" { node_name = "{{.NodeName}}" started = false memory { dedicated = 1024 } }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm6", map[string]string{ "memory.0.dedicated": "1024", }), ), }}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: te.accProviders, Steps: tt.step, }) }) } } func TestAccResourceVMInitialization(t *testing.T) { te := initTestEnvironment(t) tests := []struct { name string step []resource.TestStep }{ {"custom cloud-init: use SCSI interface", []resource.TestStep{{ 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 file_name = "cloud-config.yaml" } } resource "proxmox_virtual_environment_vm" "test_vm_cloudinit1" { node_name = "{{.NodeName}}" started = true agent { enabled = true } cpu { cores = 2 } memory { dedicated = 2048 } disk { datastore_id = "local-lvm" file_id = proxmox_virtual_environment_download_file.ubuntu_cloud_image.id interface = "virtio0" iothread = true discard = "on" size = 20 } initialization { interface = "scsi1" ip_config { ipv4 { address = "dhcp" } } user_data_file_id = proxmox_virtual_environment_file.cloud_config.id } network_device { bridge = "vmbr0" } } resource "proxmox_virtual_environment_download_file" "ubuntu_cloud_image" { content_type = "iso" datastore_id = "local" node_name = "{{.NodeName}}" url = "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img" overwrite_unmanaged = true }`), }}}, {"native cloud-init: do not upgrade packages", []resource.TestStep{{ Config: te.renderConfig(` resource "proxmox_virtual_environment_vm" "test_vm_cloudinit3" { node_name = "{{.NodeName}}" started = false initialization { upgrade = false } }`), Check: resource.ComposeTestCheckFunc( testResourceAttributes("proxmox_virtual_environment_vm.test_vm_cloudinit3", map[string]string{ "initialization.0.upgrade": "false", }), ), }}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: te.accProviders, Steps: tt.step, }) }) } } func TestAccResourceVMNetwork(t *testing.T) { te := initTestEnvironment(t) tests := []struct { name string step []resource.TestStep }{ {"network interfaces", []resource.TestStep{{ Config: te.renderConfig(` resource "proxmox_virtual_environment_file" "cloud_config" { content_type = "snippets" datastore_id = "local" node_name = "{{.NodeName}}" source_raw { data = <