Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server acceptance tests #41

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/resources/server/simulation.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 2 additions & 0 deletions internal/simulator/fixtures/servers/create/get.json
Original file line number Diff line number Diff line change
@@ -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}

30 changes: 30 additions & 0 deletions internal/simulator/server.go
Original file line number Diff line number Diff line change
@@ -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()
}
121 changes: 121 additions & 0 deletions test/server/server_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
},
})
}
9 changes: 9 additions & 0 deletions test/server/simulation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// (C) Copyright 2024 Hewlett Packard Enterprise Development LP
//go:build simulation
// +build simulation

package acceptance

func init() {
simulation = true
}
Loading