From c25e644f4a6da33f1de49318ea542a5827519684 Mon Sep 17 00:00:00 2001 From: Stuart McLaren Date: Mon, 2 Sep 2024 18:15:54 +0100 Subject: [PATCH] Add server acceptance tests Like other acceptance tests these can be run in 'real' or 'simulation' mode, however only the 'simuation' mode will pass currently due to API limitations. --- internal/resources/server/simulation.go | 12 ++ .../fixtures/servers/create/get.json | 2 + internal/simulator/server.go | 30 +++++ test/server/server_test.go | 121 ++++++++++++++++++ test/server/simulation.go | 9 ++ 5 files changed, 174 insertions(+) create mode 100644 internal/resources/server/simulation.go create mode 100644 internal/simulator/fixtures/servers/create/get.json create mode 100644 internal/simulator/server.go create mode 100644 test/server/server_test.go create mode 100644 test/server/simulation.go diff --git a/internal/resources/server/simulation.go b/internal/resources/server/simulation.go new file mode 100644 index 0000000..1e5794c --- /dev/null +++ b/internal/resources/server/simulation.go @@ -0,0 +1,12 @@ +// (C) Copyright 2024 Hewlett Packard Enterprise Development LP +//go:build simulation + +package server + +import ( + "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/simulator" +) + +func init() { + simulator.Server() +} diff --git a/internal/simulator/fixtures/servers/create/get.json b/internal/simulator/fixtures/servers/create/get.json new file mode 100644 index 0000000..8daedcf --- /dev/null +++ b/internal/simulator/fixtures/servers/create/get.json @@ -0,0 +1,2 @@ +{"systemId":"126fd201-9e6e-5e31-9ffb-a766265b1fd3","id":"697e8cbf-df7e-570c-a3c7-912d4ce8375a","generation":13259,"customerId":"bb20052ac81011ed895f7a6e2fba2c57","name":"16.182.105.217","resourceUri":"/private-cloud-business/v1beta1/systems/126fd201-9e6e-5e31-9ffb-a766265b1fd3/servers/697e8cbf-df7e-570c-a3c7-912d4ce8375a","serialNumber":"MXQ1220D7P","hypervisorHost":{"id":"530b1894-9bd0-5627-9362-565aff1e5cbd","name":"16.182.105.217","type":"HYPERVISOR_TYPE_ESXI","ResourceUri":"/api/v1/hypervisor-hosts/530b1894-9bd0-5627-9362-565aff1e5cbd","hypervisorHostIp":"16.182.105.217","hypervisorClusterName":"5305-CL","hypervisorClusterId":"acd4daea-e5e3-5f35-8be3-ce4a4b6d946c"},"health":{"biosOrHardwareHealth":"OK","fanHealth":"OK","memoryHealth":"OK","networkHealth":"OK","overallServerHealth":"WARNING","powerSuppliesHealth":"CRITICAL","processorHealth":"OK","smartStorageBatteryHealth":"","storageHealth":"OK","temperaturesHealth":"OK","fanRedundancy":"REDUNDANT","powerSuppliesRedundancy":"NONREDUNDANT","agentlessManagementService":"READY","hbLastUpdateTimestamp":"Wed Jul 17 11:01:03 2024","powerState":""},"powerState":"ON","iloNetworkInfo":{"iloHostname":"16.182.105.216","iloIp":"16.182.105.216","network":"","subnetMask":"255.255.248.0","gateway":"16.182.104.1"},"iloState":"ENABLED","iloStatus":"OK","indicatorLedStatus":"OFF","iloFirmwareVersion":"iLO 5 v2.72","model":"ProLiant DL325 Gen10 Plus v2","memoryGib":"512","processorCount":"1","processorModel":"AMD EPYC 7413 24-Core Processor ","ncmVersion":"7.0.2-700014","onPremUniqueID":"34383350-3137-584d-5131-323230443750","onPremAgentId":"AF-836032","cloudModuleConfig":null,"consumedBy":null,"operatingSystemInfo":null,"linkLocalInfo":null,"gpus":null} + diff --git a/internal/simulator/server.go b/internal/simulator/server.go new file mode 100644 index 0000000..aa830a0 --- /dev/null +++ b/internal/simulator/server.go @@ -0,0 +1,30 @@ +// (C) Copyright 2024 Hewlett Packard Enterprise Development LP + +package simulator + +import ( + _ "embed" + + "github.com/h2non/gock" +) + +// TODO: (API) Replace fake data with real data when possible +// +//go:embed fixtures/servers/create/get.json +var serverGet string + +func simulateServerCreate() { + systemID := "126fd201-9e6e-5e31-9ffb-a766265b1fd3" + serverID := "697e8cbf-df7e-570c-a3c7-912d4ce8375a" + + gock.New("http://localhost"). + Get("/private-cloud-business/v1beta1/systems/"+ + systemID+"/servers/"+serverID). + Reply(200). + SetHeader("Content-Type", "application/json"). + BodyString(serverGet) +} + +func Server() { + simulateServerCreate() +} diff --git a/test/server/server_test.go b/test/server/server_test.go new file mode 100644 index 0000000..f641bc9 --- /dev/null +++ b/test/server/server_test.go @@ -0,0 +1,121 @@ +// (C) Copyright 2024 Hewlett Packard Enterprise Development LP + +package acceptance + +import ( + "fmt" + "testing" + + "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/provider" + "github.com/google/uuid" + + "github.com/hashicorp/terraform-plugin-framework/providerserver" + "github.com/hashicorp/terraform-plugin-go/tfprotov6" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +const ( + providerConfig = ` +terraform { + required_providers { + hpegl = { + source = "github.com/HewlettPackard/hpegl-pcbe-terraform-resources" + } + } +} + +provider "hpegl" { + pc { + host = "http://localhost:8080" + token = "abcdefghijklmnopqrstuvwxyz-0123456789" + + http_dump = true + poll_interval = 0.001 + max_polls = 10 + } +} +` +) + +var simulation = false + +var testAccProtoV6ProviderFactories = map[string]func() ( + tfprotov6.ProviderServer, error, +){ + "scaffolding": providerserver.NewProtocol6WithError( + provider.New("test")(), + ), +} + +func checkUUIDAttr(resource string, attr string) func(*terraform.State) error { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resource] + if !ok { + return fmt.Errorf("resource not found: %s", resource) + } + + attrValue := rs.Primary.Attributes[attr] + _, err := uuid.Parse(attrValue) + + return err + } +} + +func TestAccServerResource(t *testing.T) { + config := providerConfig + ` + resource "hpegl_pc_server" "test" { + name = "16.182.105.217" + system_id = "126fd201-9e6e-5e31-9ffb-a766265b1fd3" + } + ` + + checks := []resource.TestCheckFunc{ + resource.TestCheckResourceAttr( + "hpegl_pc_server.test", + "name", + "16.182.105.217", + ), + checkUUIDAttr("hpegl_pc_server.test", "id"), + checkUUIDAttr("hpegl_pc_server.test", "system_id"), + } + + if simulation { + checks = append(checks, + resource.TestCheckResourceAttr( + "hpegl_pc_server.test", + "id", + "697e8cbf-df7e-570c-a3c7-912d4ce8375a", + ), + resource.TestCheckResourceAttr( + "hpegl_pc_server.test", + "system_id", + "126fd201-9e6e-5e31-9ffb-a766265b1fd3", + ), + ) + } + + checkFn := resource.ComposeAggregateTestCheckFunc(checks...) + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checkFn, + PlanOnly: true, + ExpectNonEmptyPlan: true, + }, + { + Config: config, + Check: checkFn, + }, + { + Config: config, + Check: checkFn, + PlanOnly: true, + ExpectNonEmptyPlan: false, + }, + }, + }) +} diff --git a/test/server/simulation.go b/test/server/simulation.go new file mode 100644 index 0000000..f8bc213 --- /dev/null +++ b/test/server/simulation.go @@ -0,0 +1,9 @@ +// (C) Copyright 2024 Hewlett Packard Enterprise Development LP +//go:build simulation +// +build simulation + +package acceptance + +func init() { + simulation = true +}