Commit 7969105d authored by Yann KAISER's avatar Yann KAISER

Updated acceptance tests

parent d430e835
package one
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceDatastore_withDatastoreID(t *testing.T) {
var dsID = testContext.DataDatastore["id"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDatastoreByID(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_datastore.test", "id", dsID),
),
},
},
})
}
func TestAccDataSourceDatastore_withDatastoreName(t *testing.T) {
var dsName = testContext.DataDatastore["name"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDatastoreByName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_datastore.test", "name", dsName),
),
},
},
})
}
func testAccDataSourceDatastoreByID() string {
return fmt.Sprintf(`
data "one_datastore" "test" {
id = "%s"
}
`, testContext.DataDatastore["id"].(string))
}
func testAccDataSourceDatastoreByName() string {
return fmt.Sprintf(`
data "one_datastore" "test" {
name = "%s"
}
`, testContext.DataDatastore["name"].(string))
}
package one
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceHost_withHostID(t *testing.T) {
var hostID = testContext.DataHost["id"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceHostByID(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_host.test", "id", hostID),
),
},
},
})
}
func TestAccDataSourceHost_withHostName(t *testing.T) {
var hostName = testContext.DataHost["name"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceHostByName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_host.test", "name", hostName),
),
},
},
})
}
func testAccDataSourceHostByID() string {
return fmt.Sprintf(`
data "one_host" "test" {
id = "%s"
strategy = "name-id"
}
`, testContext.DataHost["id"].(string))
}
func testAccDataSourceHostByName() string {
return fmt.Sprintf(`
data "one_host" "test" {
name = "%s"
strategy = "name-id"
}
`, testContext.DataHost["name"].(string))
}
package one
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceImage_withImageID(t *testing.T) {
var imageID = testContext.DataImage["id"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceImageByID(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_image.test", "id", imageID),
),
},
},
CheckDestroy: testAccCheckImageDestroy,
})
}
func TestAccDataSourceImage_withImageName(t *testing.T) {
var imageName = testContext.DataImage["name"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceImageByName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_image.test", "name", imageName),
),
},
},
CheckDestroy: testAccCheckImageDestroy,
})
}
func testAccDataSourceImageByID() string {
return fmt.Sprintf(`
data "one_image" "test" {
id = "%s"
}
`, testContext.DataImage["id"].(string))
}
func testAccDataSourceImageByName() string {
return fmt.Sprintf(`
data "one_image" "test" {
name = "%s"
}
`, testContext.DataImage["name"].(string))
}
package one
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceService_withServiceID(t *testing.T) {
t.Skip()
var serviceID = testContext.DataService["id"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceServiceByID(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_service.test", "id", serviceID),
),
},
},
})
}
func TestAccDataSourceService_withServiceName(t *testing.T) {
t.Skip()
var serviceName = testContext.DataService["name"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceServiceByName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_service.test", "name", serviceName),
),
},
},
})
}
func testAccDataSourceServiceByID() string {
return fmt.Sprintf(`
data "one_service" "test" {
id = "%s"
}
`, testContext.DataService["id"].(string))
}
func testAccDataSourceServiceByName() string {
return fmt.Sprintf(`
data "one_service" "test" {
name = "%s"
}
`, testContext.DataService["name"].(string))
}
package one
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceUser_withUsername(t *testing.T) {
var username = testContext.DataUser["username"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceUserByUsername(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_user.test", "username", username),
),
},
},
})
}
func testAccDataSourceUserByUsername() string {
return fmt.Sprintf(`
data "one_user" "test" {
username = "%s"
}
`, testContext.DataUser["username"].(string))
}
package one
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceVMTemplate_withTemplateID(t *testing.T) {
var vmID = testContext.DataVMTemplate["id"].(string)
var vmName = testContext.DataVMTemplate["name"].(string)
var cpu = testContext.DataVMTemplate["cpu"].(string)
var vcpu = testContext.DataVMTemplate["vcpu"].(string)
var memory = testContext.DataVMTemplate["memory"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVMTemplateByID(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_vm_template.test", "id", vmID),
resource.TestCheckResourceAttr("data.one_vm_template.test", "name", vmName),
resource.TestCheckResourceAttr("data.one_vm_template.test", "cpu", cpu),
resource.TestCheckResourceAttr("data.one_vm_template.test", "vcpu", vcpu),
resource.TestCheckResourceAttr("data.one_vm_template.test", "memory", memory),
),
},
},
})
}
func TestAccDataSourceVMTemplate_withTemplateName(t *testing.T) {
var vmID = testContext.DataVMTemplate["id"].(string)
var vmName = testContext.DataVMTemplate["name"].(string)
var cpu = testContext.DataVMTemplate["cpu"].(string)
var vcpu = testContext.DataVMTemplate["vcpu"].(string)
var memory = testContext.DataVMTemplate["memory"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVMTemplateByName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_vm_template.test", "id", vmID),
resource.TestCheckResourceAttr("data.one_vm_template.test", "name", vmName),
resource.TestCheckResourceAttr("data.one_vm_template.test", "cpu", cpu),
resource.TestCheckResourceAttr("data.one_vm_template.test", "vcpu", vcpu),
resource.TestCheckResourceAttr("data.one_vm_template.test", "memory", memory),
),
},
},
})
}
func testAccDataSourceVMTemplateByID() string {
return fmt.Sprintf(`
data "one_vm_template" "test" {
id = "%s"
}
`, testContext.DataVMTemplate["id"].(string))
}
func testAccDataSourceVMTemplateByName() string {
return fmt.Sprintf(`
data "one_vm_template" "test" {
name = "%s"
}
`, testContext.DataVMTemplate["name"].(string))
}
package one
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccDataSourceVNetwork_withVNetworkID(t *testing.T) {
var vnName = testContext.DataVNetwork["name"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVNetworkByName(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_vnetwork.test", "name", vnName),
),
},
},
})
}
func TestAccDataSourceVNetwork_withVNetworkName(t *testing.T) {
var vnAddr = testContext.DataVNetwork["address"].(string)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceVNetworkByAddress(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.one_vnetwork.test", "address", vnAddr),
),
},
},
})
}
func testAccDataSourceVNetworkByName() string {
return fmt.Sprintf(`
data "one_vnetwork" "test" {
name = "%s"
}
`, testContext.DataVNetwork["name"].(string))
}
func testAccDataSourceVNetworkByAddress() string {
return fmt.Sprintf(`
data "one_vnetwork" "test" {
address = "%s"
}
`, testContext.DataVNetwork["address"].(string))
}
......@@ -3,9 +3,9 @@ package one
import (
"fmt"
"log"
"strconv"
"testing"
"gitlab.ippon.fr/ims/opennebula-api/image"
"gitlab.ippon.fr/ims/opennebula-api/vm"
"gitlab.ippon.fr/ims/terraform-provider-one/one/customtesting"
)
......@@ -13,13 +13,8 @@ import (
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),
err := customtesting.ExecuteCustomTests(
testAccCreateNotEnabledDisk(&d),
testAccDiskNotEnabled(&d),
)
......@@ -31,31 +26,22 @@ func TestAccDiskCreate_notEnabled(t *testing.T) {
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),
err := customtesting.ExecuteCustomTests(
testAccCreateDiskWithImageID(testContext.ResourceDisk["with_existing_image_id"].(string), &d),
)
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),
err := customtesting.ExecuteCustomTests(
testAccCreateDiskWithImageName(testContext.ResourceDisk["with_existing_image_name"].(string), &d),
)
if err != nil {
......@@ -67,11 +53,6 @@ func TestAccDiskCreate_withExistingImageByName(t *testing.T) {
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`
......@@ -82,13 +63,13 @@ func TestAccDiskCreate_withCreateImage(t *testing.T) {
* be `not nil`, the second function is already created and parameters are
* already filled before it is called.
*/
err = testAccCreateDiskWithImageCreation(&d, client.ImageSession)()
err := testAccCreateDiskWithImageCreation(&d)()
if err != nil {
log.Print(err)
t.Fail()
}
err = testAccDeleteImage(d, client.ImageSession)()
err = testAccDeleteImage(d)()
if err != nil {
log.Print(err)
......@@ -101,13 +82,13 @@ func TestAccDiskCreate_withCreateImage(t *testing.T) {
}
}
func testAccCreateNotEnabledDisk(disk *vm.Disk, imageSession *image.Client) customtesting.CustomTest {
func testAccCreateNotEnabledDisk(disk *vm.Disk) customtesting.CustomTest {
return func() error {
var diskMap = map[string]interface{}{
"enabled": false,
}
var err error
disk, err = CreateDisk("", diskMap, imageSession)
disk, err = CreateDisk("", diskMap, testClient.ImageSession)
if err != nil {
return fmt.Errorf("error in CreateDisk: %s", err)
......@@ -131,16 +112,20 @@ func testAccDiskNotEnabled(disk *vm.Disk) customtesting.CustomTest {
}
}
func testAccCreateDiskWithImageID(imageID int, disk *vm.Disk, imageSession *image.Client) customtesting.CustomTest {
func testAccCreateDiskWithImageID(imageIDstr string, disk *vm.Disk) customtesting.CustomTest {
return func() error {
var imageID, err = strconv.Atoi(imageIDstr)
if err != nil {
return err
}
var diskMap = map[string]interface{}{
"enabled": true,
"image_id": imageID,
"target": "vda",
"size_mb": 10,
}
var err error
disk, err = CreateDisk("", diskMap, imageSession)
disk, err = CreateDisk("", diskMap, testClient.ImageSession)
if err != nil || disk == nil || disk.ImageID != imageID {
return fmt.Errorf("Disk not created: %s", err)
......@@ -150,7 +135,7 @@ func testAccCreateDiskWithImageID(imageID int, disk *vm.Disk, imageSession *imag
}
}
func testAccCreateDiskWithImageName(image string, disk *vm.Disk, imageSession *image.Client) customtesting.CustomTest {
func testAccCreateDiskWithImageName(image string, disk *vm.Disk) customtesting.CustomTest {
return func() error {
var diskMap = map[string]interface{}{
"enabled": true,
......@@ -159,7 +144,7 @@ func testAccCreateDiskWithImageName(image string, disk *vm.Disk, imageSession *i
"size_mb": 10,
}
var err error
disk, err = CreateDisk("", diskMap, imageSession)
disk, err = CreateDisk("", diskMap, testClient.ImageSession)
if err != nil || disk == nil || disk.Image != image {
return fmt.Errorf("Disk not created: %s", err)
......@@ -169,7 +154,7 @@ func testAccCreateDiskWithImageName(image string, disk *vm.Disk, imageSession *i
}
}
func testAccCreateDiskWithImageCreation(disk **vm.Disk, imageSession *image.Client) customtesting.CustomTest {
func testAccCreateDiskWithImageCreation(disk **vm.Disk) customtesting.CustomTest {
return func() error {
var diskMap = map[string]interface{}{
"enabled": true,
......@@ -179,7 +164,7 @@ func testAccCreateDiskWithImageCreation(disk **vm.Disk, imageSession *image.Clie
"image_datastore_id": 121,
}
var err error
cd, err := CreateDisk("vmtest-acc", diskMap, imageSession)
cd, err := CreateDisk("vmtest-acc", diskMap, testClient.ImageSession)
if err != nil || cd == nil || cd.ImageID == 0 {
return fmt.Errorf("Error while creating disk: %s", err)
......@@ -191,9 +176,9 @@ func testAccCreateDiskWithImageCreation(disk **vm.Disk, imageSession *image.Clie
}
}
func testAccDeleteImage(disk *vm.Disk, imageSession *image.Client) customtesting.CustomTest {
func testAccDeleteImage(disk *vm.Disk) customtesting.CustomTest {
return func() error {
err := imageSession.DeleteImage(disk.ImageID)
err := testClient.ImageSession.DeleteImage(disk.ImageID)
return err
}
}
package one
import (
"fmt"
"log"
"strconv"
"testing"
"gitlab.ippon.fr/ims/opennebula-api/image"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
func TestAccImageCreate_basic(t *testing.T) {
var im image.ImageData
var sizeMb = testContext.ResourceImage["size_mb"].(string)
var persistent = testContext.ResourceImage["persistent"].(string)
resource.Test(t, resource.TestCase{
IDRefreshName: "one_image.test",
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccImageBasicConfig(),
Check: resource.ComposeTestCheckFunc(
testAccImageExistsByID("one_image.test", &im),
resource.TestCheckResourceAttr("one_image.test", "size_mb", sizeMb),
resource.TestCheckResourceAttr("one_image.test", "persistent", persistent),
),
},
},
CheckDestroy: testAccCheckImageDestroy,
})
}
func testAccImageBasicConfig() string {
return fmt.Sprintf(`
resource "one_image" "test" {
name = "test"
size_mb = "%s"
persistent = "%s"
}
`, testContext.ResourceImage["size_mb"].(string), testContext.ResourceImage["persistent"].(string))
}
func testAccImageExistsByID(n string, img *image.ImageData) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {