github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/vsphere/resource_vsphere_file_test.go (about)

     1  package vsphere
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/vmware/govmomi"
    12  	"github.com/vmware/govmomi/find"
    13  	"github.com/vmware/govmomi/object"
    14  	"golang.org/x/net/context"
    15  )
    16  
    17  // Basic file creation (upload to vSphere)
    18  func TestAccVSphereFile_basic(t *testing.T) {
    19  	testVmdkFileData := []byte("# Disk DescriptorFile\n")
    20  	testVmdkFile := "/tmp/tf_test.vmdk"
    21  	err := ioutil.WriteFile(testVmdkFile, testVmdkFileData, 0644)
    22  	if err != nil {
    23  		t.Errorf("error %s", err)
    24  		return
    25  	}
    26  
    27  	datacenter := os.Getenv("VSPHERE_DATACENTER")
    28  	datastore := os.Getenv("VSPHERE_DATASTORE")
    29  	testMethod := "basic"
    30  	resourceName := "vsphere_file." + testMethod
    31  	destinationFile := "tf_file_test.vmdk"
    32  	sourceFile := testVmdkFile
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckVSphereFileDestroy,
    38  		Steps: []resource.TestStep{
    39  			{
    40  				Config: fmt.Sprintf(
    41  					testAccCheckVSphereFileConfig,
    42  					testMethod,
    43  					datacenter,
    44  					datastore,
    45  					sourceFile,
    46  					destinationFile,
    47  				),
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckVSphereFileExists(resourceName, destinationFile, true),
    50  					resource.TestCheckResourceAttr(resourceName, "destination_file", destinationFile),
    51  				),
    52  			},
    53  		},
    54  	})
    55  	os.Remove(testVmdkFile)
    56  }
    57  
    58  // Basic file copy within vSphere
    59  func TestAccVSphereFile_basicUploadAndCopy(t *testing.T) {
    60  	testVmdkFileData := []byte("# Disk DescriptorFile\n")
    61  	sourceFile := "/tmp/tf_test.vmdk"
    62  	uploadResourceName := "myfileupload"
    63  	copyResourceName := "myfilecopy"
    64  	sourceDatacenter := os.Getenv("VSPHERE_DATACENTER")
    65  	datacenter := sourceDatacenter
    66  	sourceDatastore := os.Getenv("VSPHERE_DATASTORE")
    67  	datastore := sourceDatastore
    68  	destinationFile := "tf_file_test.vmdk"
    69  	sourceFileCopy := "${vsphere_file." + uploadResourceName + ".destination_file}"
    70  	destinationFileCopy := "tf_file_test_copy.vmdk"
    71  
    72  	err := ioutil.WriteFile(sourceFile, testVmdkFileData, 0644)
    73  	if err != nil {
    74  		t.Errorf("error %s", err)
    75  		return
    76  	}
    77  
    78  	resource.Test(t, resource.TestCase{
    79  		PreCheck:     func() { testAccPreCheck(t) },
    80  		Providers:    testAccProviders,
    81  		CheckDestroy: testAccCheckVSphereFileDestroy,
    82  		Steps: []resource.TestStep{
    83  			{
    84  				Config: fmt.Sprintf(
    85  					testAccCheckVSphereFileCopyConfig,
    86  					uploadResourceName,
    87  					datacenter,
    88  					datastore,
    89  					sourceFile,
    90  					destinationFile,
    91  					copyResourceName,
    92  					datacenter,
    93  					datacenter,
    94  					datastore,
    95  					datastore,
    96  					sourceFileCopy,
    97  					destinationFileCopy,
    98  				),
    99  				Check: resource.ComposeTestCheckFunc(
   100  					testAccCheckVSphereFileExists("vsphere_file."+uploadResourceName, destinationFile, true),
   101  					testAccCheckVSphereFileExists("vsphere_file."+copyResourceName, destinationFileCopy, true),
   102  					resource.TestCheckResourceAttr("vsphere_file."+uploadResourceName, "destination_file", destinationFile),
   103  					resource.TestCheckResourceAttr("vsphere_file."+copyResourceName, "destination_file", destinationFileCopy),
   104  				),
   105  			},
   106  		},
   107  	})
   108  	os.Remove(sourceFile)
   109  }
   110  
   111  // file creation followed by a rename of file (update)
   112  func TestAccVSphereFile_renamePostCreation(t *testing.T) {
   113  	testVmdkFileData := []byte("# Disk DescriptorFile\n")
   114  	testVmdkFile := "/tmp/tf_test.vmdk"
   115  	err := ioutil.WriteFile(testVmdkFile, testVmdkFileData, 0644)
   116  	if err != nil {
   117  		t.Errorf("error %s", err)
   118  		return
   119  	}
   120  
   121  	datacenter := os.Getenv("VSPHERE_DATACENTER")
   122  	datastore := os.Getenv("VSPHERE_DATASTORE")
   123  	testMethod := "create_upgrade"
   124  	resourceName := "vsphere_file." + testMethod
   125  	destinationFile := "tf_test_file.vmdk"
   126  	destinationFileMoved := "tf_test_file_moved.vmdk"
   127  	sourceFile := testVmdkFile
   128  
   129  	resource.Test(t, resource.TestCase{
   130  		PreCheck:     func() { testAccPreCheck(t) },
   131  		Providers:    testAccProviders,
   132  		CheckDestroy: testAccCheckVSphereFileDestroy,
   133  		Steps: []resource.TestStep{
   134  			{
   135  				Config: fmt.Sprintf(
   136  					testAccCheckVSphereFileConfig,
   137  					testMethod,
   138  					datacenter,
   139  					datastore,
   140  					sourceFile,
   141  					destinationFile,
   142  				),
   143  				Check: resource.ComposeTestCheckFunc(
   144  					testAccCheckVSphereFileExists(resourceName, destinationFile, true),
   145  					testAccCheckVSphereFileExists(resourceName, destinationFileMoved, false),
   146  					resource.TestCheckResourceAttr(resourceName, "destination_file", destinationFile),
   147  				),
   148  			},
   149  			{
   150  				Config: fmt.Sprintf(
   151  					testAccCheckVSphereFileConfig,
   152  					testMethod,
   153  					datacenter,
   154  					datastore,
   155  					sourceFile,
   156  					destinationFileMoved,
   157  				),
   158  				Check: resource.ComposeTestCheckFunc(
   159  					testAccCheckVSphereFileExists(resourceName, destinationFile, false),
   160  					testAccCheckVSphereFileExists(resourceName, destinationFileMoved, true),
   161  					resource.TestCheckResourceAttr(resourceName, "destination_file", destinationFileMoved),
   162  				),
   163  			},
   164  		},
   165  	})
   166  	os.Remove(testVmdkFile)
   167  }
   168  
   169  // file upload, then copy, finally the copy is renamed (moved) (update)
   170  func TestAccVSphereFile_uploadAndCopyAndUpdate(t *testing.T) {
   171  	testVmdkFileData := []byte("# Disk DescriptorFile\n")
   172  	sourceFile := "/tmp/tf_test.vmdk"
   173  	uploadResourceName := "myfileupload"
   174  	copyResourceName := "myfilecopy"
   175  	sourceDatacenter := os.Getenv("VSPHERE_DATACENTER")
   176  	datacenter := sourceDatacenter
   177  	sourceDatastore := os.Getenv("VSPHERE_DATASTORE")
   178  	datastore := sourceDatastore
   179  	destinationFile := "tf_file_test.vmdk"
   180  	sourceFileCopy := "${vsphere_file." + uploadResourceName + ".destination_file}"
   181  	destinationFileCopy := "tf_file_test_copy.vmdk"
   182  	destinationFileMoved := "tf_test_file_moved.vmdk"
   183  
   184  	err := ioutil.WriteFile(sourceFile, testVmdkFileData, 0644)
   185  	if err != nil {
   186  		t.Errorf("error %s", err)
   187  		return
   188  	}
   189  
   190  	resource.Test(t, resource.TestCase{
   191  		PreCheck:     func() { testAccPreCheck(t) },
   192  		Providers:    testAccProviders,
   193  		CheckDestroy: testAccCheckVSphereFileDestroy,
   194  		Steps: []resource.TestStep{
   195  			{
   196  				Config: fmt.Sprintf(
   197  					testAccCheckVSphereFileCopyConfig,
   198  					uploadResourceName,
   199  					datacenter,
   200  					datastore,
   201  					sourceFile,
   202  					destinationFile,
   203  					copyResourceName,
   204  					datacenter,
   205  					datacenter,
   206  					datastore,
   207  					datastore,
   208  					sourceFileCopy,
   209  					destinationFileCopy,
   210  				),
   211  				Check: resource.ComposeTestCheckFunc(
   212  					testAccCheckVSphereFileExists("vsphere_file."+uploadResourceName, destinationFile, true),
   213  					testAccCheckVSphereFileExists("vsphere_file."+copyResourceName, destinationFileCopy, true),
   214  					resource.TestCheckResourceAttr("vsphere_file."+uploadResourceName, "destination_file", destinationFile),
   215  					resource.TestCheckResourceAttr("vsphere_file."+copyResourceName, "destination_file", destinationFileCopy),
   216  				),
   217  			},
   218  			{
   219  				Config: fmt.Sprintf(
   220  					testAccCheckVSphereFileCopyConfig,
   221  					uploadResourceName,
   222  					datacenter,
   223  					datastore,
   224  					sourceFile,
   225  					destinationFile,
   226  					copyResourceName,
   227  					datacenter,
   228  					datacenter,
   229  					datastore,
   230  					datastore,
   231  					sourceFileCopy,
   232  					destinationFileMoved,
   233  				),
   234  				Check: resource.ComposeTestCheckFunc(
   235  					testAccCheckVSphereFileExists("vsphere_file."+uploadResourceName, destinationFile, true),
   236  					testAccCheckVSphereFileExists("vsphere_file."+copyResourceName, destinationFileCopy, false),
   237  					testAccCheckVSphereFileExists("vsphere_file."+copyResourceName, destinationFileMoved, true),
   238  					resource.TestCheckResourceAttr("vsphere_file."+uploadResourceName, "destination_file", destinationFile),
   239  					resource.TestCheckResourceAttr("vsphere_file."+copyResourceName, "destination_file", destinationFileMoved),
   240  				),
   241  			},
   242  		},
   243  	})
   244  	os.Remove(sourceFile)
   245  }
   246  
   247  func testAccCheckVSphereFileDestroy(s *terraform.State) error {
   248  	client := testAccProvider.Meta().(*govmomi.Client)
   249  	finder := find.NewFinder(client.Client, true)
   250  
   251  	for _, rs := range s.RootModule().Resources {
   252  		if rs.Type != "vsphere_file" {
   253  			continue
   254  		}
   255  
   256  		dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
   257  		if err != nil {
   258  			return fmt.Errorf("error %s", err)
   259  		}
   260  
   261  		finder = finder.SetDatacenter(dc)
   262  
   263  		ds, err := getDatastore(finder, rs.Primary.Attributes["datastore"])
   264  		if err != nil {
   265  			return fmt.Errorf("error %s", err)
   266  		}
   267  
   268  		_, err = ds.Stat(context.TODO(), rs.Primary.Attributes["destination_file"])
   269  		if err != nil {
   270  			switch e := err.(type) {
   271  			case object.DatastoreNoSuchFileError:
   272  				fmt.Printf("Expected error received: %s\n", e.Error())
   273  				return nil
   274  			default:
   275  				return err
   276  			}
   277  		} else {
   278  			return fmt.Errorf("File %s still exists", rs.Primary.Attributes["destination_file"])
   279  		}
   280  	}
   281  
   282  	return nil
   283  }
   284  
   285  func testAccCheckVSphereFileExists(n string, df string, exists bool) resource.TestCheckFunc {
   286  	return func(s *terraform.State) error {
   287  		rs, ok := s.RootModule().Resources[n]
   288  		if !ok {
   289  			return fmt.Errorf("Resource not found: %s", n)
   290  		}
   291  
   292  		if rs.Primary.ID == "" {
   293  			return fmt.Errorf("No ID is set")
   294  		}
   295  
   296  		client := testAccProvider.Meta().(*govmomi.Client)
   297  		finder := find.NewFinder(client.Client, true)
   298  
   299  		dc, err := finder.Datacenter(context.TODO(), rs.Primary.Attributes["datacenter"])
   300  		if err != nil {
   301  			return fmt.Errorf("error %s", err)
   302  		}
   303  		finder = finder.SetDatacenter(dc)
   304  
   305  		ds, err := getDatastore(finder, rs.Primary.Attributes["datastore"])
   306  		if err != nil {
   307  			return fmt.Errorf("error %s", err)
   308  		}
   309  
   310  		_, err = ds.Stat(context.TODO(), df)
   311  		if err != nil {
   312  			switch e := err.(type) {
   313  			case object.DatastoreNoSuchFileError:
   314  				if exists {
   315  					return fmt.Errorf("File does not exist: %s", e.Error())
   316  				}
   317  				fmt.Printf("Expected error received: %s\n", e.Error())
   318  				return nil
   319  			default:
   320  				return err
   321  			}
   322  		}
   323  		return nil
   324  	}
   325  }
   326  
   327  const testAccCheckVSphereFileConfig = `
   328  resource "vsphere_file" "%s" {
   329  	datacenter = "%s"
   330  	datastore = "%s"
   331  	source_file = "%s"
   332  	destination_file = "%s"
   333  }
   334  `
   335  const testAccCheckVSphereFileCopyConfig = `
   336  resource "vsphere_file" "%s" {
   337  	datacenter = "%s"
   338  	datastore = "%s"
   339  	source_file = "%s"
   340  	destination_file = "%s"
   341  }
   342  resource "vsphere_file" "%s" {
   343  	source_datacenter = "%s"
   344  	datacenter = "%s"
   345  	source_datastore = "%s"
   346  	datastore = "%s"
   347  	source_file = "%s"
   348  	destination_file = "%s"
   349  }
   350  `