From f4799fa98621af59161ea8d84a9690952cd016ac Mon Sep 17 00:00:00 2001 From: Paolo Invernizzi Date: Tue, 19 Nov 2024 17:54:48 +0100 Subject: [PATCH] Add libvirt domain resource CPU model and topology acceptance tests --- libvirt/resource_libvirt_domain_test.go | 104 +++++++++++++++++++++--- 1 file changed, 94 insertions(+), 10 deletions(-) diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index 8127f2d67..482625a6e 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -6,6 +6,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "testing" testhelper "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/test" @@ -850,27 +851,110 @@ func TestAccLibvirtDomain_Cpu(t *testing.T) { var domain libvirt.Domain randomDomainName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) - config := fmt.Sprintf(` - resource "libvirt_domain" "%s" { - name = "%s" - cpu { - mode = "custom" - } - }`, randomDomainName, randomDomainName) - resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckLibvirtDomainDestroy, Steps: []resource.TestStep{ { - Config: config, + Config: fmt.Sprintf(` + resource "libvirt_domain" "%s" { + name = "%s" + vcpu = 2 + cpu { + mode = "host-passthrough" + } + }`, randomDomainName, randomDomainName), Check: resource.ComposeTestCheckFunc( testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain), resource.TestCheckResourceAttr( - "libvirt_domain."+randomDomainName, "cpu.0.mode", "custom"), + "libvirt_domain."+randomDomainName, "cpu.0.mode", hostPassthroughCPUMode), ), }, + { + Config: fmt.Sprintf(` + resource "libvirt_domain" "%s" { + name = "%s" + vcpu = 2 + cpu { + mode = "host-passthrough" + topology { + sockets = 1 + cores = 1 + threads = 2 + } + } + }`, randomDomainName, randomDomainName), + Check: resource.ComposeTestCheckFunc( + testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "cpu.0.mode", hostPassthroughCPUMode), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "cpu.0.topology.0.sockets", "1"), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "cpu.0.topology.0.cores", "1"), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "cpu.0.topology.0.threads", "2"), + ), + }, + { + Config: fmt.Sprintf(` + resource "libvirt_domain" "%s" { + name = "%s" + vcpu = 2 + cpu { + mode = "host-passthrough" + topology { + sockets = 1 + cores = 2 + threads = 2 + } + } + }`, randomDomainName, randomDomainName), + ExpectError: regexp.MustCompile(`\(1 sockets \* 2 cores \* 2 threads\) is more than the vCPU count \(2\)`), + }, + { + Config: fmt.Sprintf(` + resource "libvirt_domain" "%s" { + name = "%s" + vcpu = 2 + cpu { + mode = "custom" + model { + fallback = "forbid" + value = "EPYC" + vendor_id = "AuthenticAMD" + } + } + }`, randomDomainName, randomDomainName), + Check: resource.ComposeTestCheckFunc( + testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "cpu.0.mode", customCPUMode), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "cpu.0.model.0.value", "EPYC"), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "cpu.0.model.0.vendor_id", "AuthenticAMD"), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "cpu.0.model.0.fallback", "forbid"), + ), + }, + { + Config: fmt.Sprintf(` + resource "libvirt_domain" "%s" { + name = "%s" + vcpu = 2 + cpu { + mode = "host-passthrough" + model { + fallback = "forbid" + value = "EPYC" + vendor_id = "AuthenticAMD" + } + } + }`, randomDomainName, randomDomainName), + ExpectError: regexp.MustCompile("CPU model can only be defined when the CPU mode is set to 'custom'"), + }, }, }) }