Commit cf21e948 authored by Yann KAISER's avatar Yann KAISER
Browse files

Added tests for resource_disk

parent 886a401f
package customtesting
import "fmt"
type CustomTest func() error
func ExecuteCustomTests(ct ...CustomTest) error {
for _, t := range ct {
if err := t(); err != nil {
return fmt.Errorf("%s", err)
}
}
return nil
}
package one
import (
"fmt"
"log"
"testing"
"gitlab.ippon.fr/ims/opennebula-api/image"
"gitlab.ippon.fr/ims/opennebula-api/vm"
"gitlab.ippon.fr/ims/terraform-provider-one/one/customtesting"
)
func TestAccDiskCreate_notEnabled(t *testing.T) {
var d vm.Disk
client, err := GenerateClientInfos("./testinfo.json")
if err != nil {
log.Fatal(err)
}
err = customtesting.ExecuteCustomTests(
testAccCreateNotEnabledDisk(&d, client.ImageSession),
testAccDiskNotEnabled(&d),
)
if err != nil {
t.Fail()
}
}
func TestAccDiskCreate_withExistingImageByID(t *testing.T) {
var d vm.Disk
client, err := GenerateClientInfos("./testinfo.json")
if err != nil {
log.Fatal(err)
}
err = customtesting.ExecuteCustomTests(
testAccCreateDiskWithImageID(1005, &d, client.ImageSession),
)
if err != nil {
log.Print(err)
t.Fail()
}
}
func TestAccDiskCreate_withExistingImageByName(t *testing.T) {
var d vm.Disk
client, err := GenerateClientInfos("./testinfo.json")
if err != nil {
log.Fatal(err)
}
err = customtesting.ExecuteCustomTests(
testAccCreateDiskWithImageName("test-vdb", &d, client.ImageSession),
)
if err != nil {
log.Print(err)
t.Fail()
}
}
func TestAccDiskCreate_withCreateImage(t *testing.T) {
var d *vm.Disk = nil
client, err := GenerateClientInfos("./testinfo.json")
if err != nil {
log.Fatal(err)
}
/*
* The function is called that way to be consistent with other testAcc*
* functions which return a `func() error`
* It does not work with ExecuteCustomTests since hat way, every function is
* created before being called and so the values might not be changed between
* two functions. This is the case with disk, which is nil in the first
* function call. Even though it is modified, and that at the exit, disk could
* be `not nil`, the second function is already created and parameters are
* already filled before it is called.
*/
err = testAccCreateDiskWithImageCreation(&d, client.ImageSession)()
if err != nil {
log.Print(err)
t.Fail()
}
err = testAccDeleteImage(d, client.ImageSession)()
if err != nil {
log.Print(err)
t.Fail()
}
if err != nil {
log.Print(err)
t.Fail()
}
}
func testAccCreateNotEnabledDisk(disk *vm.Disk, imageSession *image.Client) customtesting.CustomTest {
return func() error {
var diskMap = map[string]interface{}{
"enabled": false,
}
var err error
disk, err = CreateDisk("", diskMap, imageSession)
if err != nil {
return fmt.Errorf("error in CreateDisk: %s", err)
}
if disk != nil {
return fmt.Errorf("disk should not be created")
}
return nil
}
}
func testAccDiskNotEnabled(disk *vm.Disk) customtesting.CustomTest {
return func() error {
if disk != nil {
return nil
}
return fmt.Errorf("Disk should not be created")
}
}
func testAccCreateDiskWithImageID(imageID int, disk *vm.Disk, imageSession *image.Client) customtesting.CustomTest {
return func() error {
var diskMap = map[string]interface{}{
"enabled": true,
"image_id": imageID,
"target": "vda",
"size_mb": 10,
}
var err error
disk, err = CreateDisk("", diskMap, imageSession)
if err != nil || disk == nil || disk.ImageID != imageID {
return fmt.Errorf("Disk not created: %s", err)
}
return nil
}
}
func testAccCreateDiskWithImageName(image string, disk *vm.Disk, imageSession *image.Client) customtesting.CustomTest {
return func() error {
var diskMap = map[string]interface{}{
"enabled": true,
"image_name": image,
"target": "vda",
"size_mb": 10,
}
var err error
disk, err = CreateDisk("", diskMap, imageSession)
if err != nil || disk == nil || disk.Image != image {
return fmt.Errorf("Disk not created: %s", err)
}
return nil
}
}
func testAccCreateDiskWithImageCreation(disk **vm.Disk, imageSession *image.Client) customtesting.CustomTest {
return func() error {
var diskMap = map[string]interface{}{
"enabled": true,
"create_image": true,
"target": "vda",
"size_mb": 10,
"image_datastore_id": 121,
}
var err error
cd, err := CreateDisk("vmtest-acc", diskMap, imageSession)
if err != nil || cd == nil || cd.ImageID == 0 {
return fmt.Errorf("Error while creating disk: %s", err)
}
*disk = cd
return nil
}
}
func testAccDeleteImage(disk *vm.Disk, imageSession *image.Client) customtesting.CustomTest {
return func() error {
err := imageSession.DeleteImage(disk.ImageID)
log.Print(err)
return err
}
}
......@@ -13,10 +13,11 @@ import (
)
func TestAccVMInstance(t *testing.T) {
t.SkipNow()
var v vm.VMData
resource.Test(t, resource.TestCase{
IDRefreshName: "one_vm.test",
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
......
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