Skip to content

Commit 7359095

Browse files
committed
Add libvirt domain resource CPU model and topology acceptance tests
1 parent 687b414 commit 7359095

File tree

1 file changed

+98
-15
lines changed

1 file changed

+98
-15
lines changed

libvirt/resource_libvirt_domain_test.go

Lines changed: 98 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/url"
77
"os"
88
"path/filepath"
9+
"regexp"
910
"testing"
1011

1112
testhelper "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/test"
@@ -88,15 +89,14 @@ func TestAccLibvirtDomain_Detailed(t *testing.T) {
8889
name = "%s"
8990
memory = 384
9091
vcpu = 2
92+
cpu {
93+
mode = "host-passthrough"
94+
}
9195
}`, randomResourceName, randomDomainName),
9296
Check: resource.ComposeTestCheckFunc(
9397
testAccCheckLibvirtDomainExists("libvirt_domain."+randomResourceName, &domain),
9498
resource.TestCheckResourceAttr(
95-
"libvirt_domain."+randomResourceName, "name", randomDomainName),
96-
resource.TestCheckResourceAttr(
97-
"libvirt_domain."+randomResourceName, "memory", "384"),
98-
resource.TestCheckResourceAttr(
99-
"libvirt_domain."+randomResourceName, "vcpu", "2"),
99+
"libvirt_domain."+randomResourceName, "cpu.0.mode", hostPassthroughCPUMode),
100100
),
101101
},
102102
},
@@ -850,27 +850,110 @@ func TestAccLibvirtDomain_Cpu(t *testing.T) {
850850
var domain libvirt.Domain
851851
randomDomainName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
852852

853-
config := fmt.Sprintf(`
854-
resource "libvirt_domain" "%s" {
855-
name = "%s"
856-
cpu {
857-
mode = "custom"
858-
}
859-
}`, randomDomainName, randomDomainName)
860-
861853
resource.Test(t, resource.TestCase{
862854
PreCheck: func() { testAccPreCheck(t) },
863855
Providers: testAccProviders,
864856
CheckDestroy: testAccCheckLibvirtDomainDestroy,
865857
Steps: []resource.TestStep{
866858
{
867-
Config: config,
859+
Config: fmt.Sprintf(`
860+
resource "libvirt_domain" "%s" {
861+
name = "%s"
862+
vcpu = 2
863+
cpu {
864+
mode = "host-passthrough"
865+
}
866+
}`, randomDomainName, randomDomainName),
867+
Check: resource.ComposeTestCheckFunc(
868+
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
869+
resource.TestCheckResourceAttr(
870+
"libvirt_domain."+randomDomainName, "cpu.0.mode", hostPassthroughCPUMode),
871+
),
872+
},
873+
{
874+
Config: fmt.Sprintf(`
875+
resource "libvirt_domain" "%s" {
876+
name = "%s"
877+
vcpu = 2
878+
cpu {
879+
mode = "host-passthrough"
880+
topology {
881+
sockets = 1
882+
cores = 1
883+
threads = 2
884+
}
885+
}
886+
}`, randomDomainName, randomDomainName),
887+
Check: resource.ComposeTestCheckFunc(
888+
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
889+
resource.TestCheckResourceAttr(
890+
"libvirt_domain."+randomDomainName, "cpu.0.mode", hostPassthroughCPUMode),
891+
resource.TestCheckResourceAttr(
892+
"libvirt_domain."+randomDomainName, "cpu.0.topology.0.sockets", "1"),
893+
resource.TestCheckResourceAttr(
894+
"libvirt_domain."+randomDomainName, "cpu.0.topology.0.cores", "1"),
895+
resource.TestCheckResourceAttr(
896+
"libvirt_domain."+randomDomainName, "cpu.0.topology.0.threads", "2"),
897+
),
898+
},
899+
{
900+
Config: fmt.Sprintf(`
901+
resource "libvirt_domain" "%s" {
902+
name = "%s"
903+
vcpu = 2
904+
cpu {
905+
mode = "host-passthrough"
906+
topology {
907+
sockets = 1
908+
cores = 2
909+
threads = 2
910+
}
911+
}
912+
}`, randomDomainName, randomDomainName),
913+
ExpectError: regexp.MustCompile(`\(1 sockets \* 2 cores \* 2 threads\) is more than the vCPU count \(2\)`),
914+
},
915+
{
916+
Config: fmt.Sprintf(`
917+
resource "libvirt_domain" "%s" {
918+
name = "%s"
919+
vcpu = 2
920+
cpu {
921+
mode = "custom"
922+
model {
923+
fallback = "forbid"
924+
value = "EPYC"
925+
vendor_id = "AuthenticAMD"
926+
}
927+
}
928+
}`, randomDomainName, randomDomainName),
868929
Check: resource.ComposeTestCheckFunc(
869930
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
870931
resource.TestCheckResourceAttr(
871-
"libvirt_domain."+randomDomainName, "cpu.0.mode", "custom"),
932+
"libvirt_domain."+randomDomainName, "cpu.0.mode", customCPUMode),
933+
resource.TestCheckResourceAttr(
934+
"libvirt_domain."+randomDomainName, "cpu.0.model.0.value", "EPYC"),
935+
resource.TestCheckResourceAttr(
936+
"libvirt_domain."+randomDomainName, "cpu.0.model.0.vendor_id", "AuthenticAMD"),
937+
resource.TestCheckResourceAttr(
938+
"libvirt_domain."+randomDomainName, "cpu.0.model.0.fallback", "forbid"),
872939
),
873940
},
941+
{
942+
Config: fmt.Sprintf(`
943+
resource "libvirt_domain" "%s" {
944+
name = "%s"
945+
vcpu = 2
946+
cpu {
947+
mode = "host-passthrough"
948+
model {
949+
fallback = "forbid"
950+
value = "EPYC"
951+
vendor_id = "AuthenticAMD"
952+
}
953+
}
954+
}`, randomDomainName, randomDomainName),
955+
ExpectError: regexp.MustCompile("CPU model can only be defined when the CPU mode is set to 'custom'"),
956+
},
874957
},
875958
})
876959
}

0 commit comments

Comments
 (0)