Commit c40e96a0 authored by Yann KAISER's avatar Yann KAISER

Added tests for services

parent b40086fe
package one
import (
"fmt"
"strconv"
"testing"
"gitlab.ippon.fr/ims/opennebula-api/vm"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
func TestAccService(t *testing.T) {
var v vm.VMData
var role0Cardinality = testContext.ResourceService["role_0_cardinality"].(string)
var role0MinVMs = testContext.ResourceService["role_0_min_vms"].(string)
var role0MaxVMs = testContext.ResourceService["role_0_max_vms"].(string)
var role0VMTemplate = testContext.ResourceService["role_0_vm_template"].(string)
var role0Cooldown = testContext.ResourceService["role_0_cooldown"].(string)
var role0EPType = testContext.ResourceService["role_0_ep_type"].(string)
var role0EPAdjust = testContext.ResourceService["role_0_ep_adjust"].(string)
var role0EPExpression = testContext.ResourceService["role_0_ep_expression"].(string)
var role0EPPeriod = testContext.ResourceService["role_0_ep_period"].(string)
var role0EPPeriodNum = testContext.ResourceService["role_0_ep_period_num"].(string)
var role0EPCooldown = testContext.ResourceService["role_0_ep_cooldown"].(string)
resource.Test(t, resource.TestCase{
IDRefreshName: "one_service.test",
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccServiceConfig(),
Check: resource.ComposeTestCheckFunc(
testAccServiceExists("one_service.test", &v),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.cardinality", role0Cardinality),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.min_vms", role0MinVMs),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.max_vms", role0MaxVMs),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.vm_template", role0VMTemplate),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.cooldown", role0Cooldown),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.elasticity_policy.0.type", role0EPType),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.elasticity_policy.0.adjust", role0EPAdjust),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.elasticity_policy.0.expression", role0EPExpression),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.elasticity_policy.0.period", role0EPPeriod),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.elasticity_policy.0.period_number", role0EPPeriodNum),
resource.TestCheckResourceAttr("one_service_template.testtpl", "role.0.elasticity_policy.0.cooldown", role0EPCooldown),
),
},
},
CheckDestroy: testAccCheckServiceDestroy,
})
}
func testAccServiceConfig() string {
var role0Cardinality = testContext.ResourceService["role_0_cardinality"].(string)
var role0MinVMs = testContext.ResourceService["role_0_min_vms"].(string)
var role0MaxVMs = testContext.ResourceService["role_0_max_vms"].(string)
var role0VMTemplate = testContext.ResourceService["role_0_vm_template"].(string)
var role0Cooldown = testContext.ResourceService["role_0_cooldown"].(string)
var role0EPType = testContext.ResourceService["role_0_ep_type"].(string)
var role0EPAdjust = testContext.ResourceService["role_0_ep_adjust"].(string)
var role0EPExpression = testContext.ResourceService["role_0_ep_expression"].(string)
var role0EPPeriod = testContext.ResourceService["role_0_ep_period"].(string)
var role0EPPeriodNum = testContext.ResourceService["role_0_ep_period_num"].(string)
var role0EPCooldown = testContext.ResourceService["role_0_ep_cooldown"].(string)
return fmt.Sprintf(`
resource "one_service_template" "testtpl" {
name = "Test Service Template"
role {
name = "role0"
cardinality = "%s"
min_vms = "%s"
max_vms = "%s"
vm_template = "%s"
cooldown = "%s"
elasticity_policy {
type = "%s"
adjust = "%s"
expression = "%s"
period = "%s"
period_number = "%s"
cooldown = "%s"
}
}
}
resource "one_service" "test" {
template_id = "${one_service_template.testtpl.id}"
}`, role0Cardinality, role0MinVMs, role0MaxVMs, role0VMTemplate, role0Cooldown,
role0EPType, role0EPAdjust, role0EPExpression, role0EPPeriod, role0EPPeriodNum,
role0EPCooldown)
}
func testAccServiceExists(n string, vm *vm.VMData) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Resource not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("Resource is not created")
}
var providers = []*schema.Provider{testAccProvider}
for _, provider := range providers {
if provider.Meta() == nil {
continue
}
session := provider.Meta().(*Client).VMSession
id, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return err
}
vm, err = session.GetVMState(id)
if err != nil {
return err
}
if vm != nil {
return nil
}
}
return fmt.Errorf("VM instance not found")
}
}
func testAccCheckServiceDestroy(s *terraform.State) error {
session := testAccProvider.Meta().(*Client).VMSession
for _, rs := range s.RootModule().Resources {
if rs.Type != "one_vm" {
continue
}
instanceID, err := strconv.Atoi(rs.Primary.ID)
if err != nil {
return err
}
vmData, err := session.GetVMState(instanceID)
if err != nil {
return err
}
if vmData == nil {
return nil
}
if vmData.State != vm.StateDone {
return fmt.Errorf("found unterminated vm instance with id: %d", instanceID)
}
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment