github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/docker/resource_docker_volume_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	dc "github.com/fsouza/go-dockerclient"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccDockerVolume_basic(t *testing.T) {
    13  	var v dc.Volume
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:  func() { testAccPreCheck(t) },
    17  		Providers: testAccProviders,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccDockerVolumeConfig,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					checkDockerVolume("docker_volume.foo", &v),
    23  					resource.TestCheckResourceAttr("docker_volume.foo", "id", "testAccDockerVolume_basic"),
    24  					resource.TestCheckResourceAttr("docker_volume.foo", "name", "testAccDockerVolume_basic"),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func checkDockerVolume(n string, volume *dc.Volume) resource.TestCheckFunc {
    32  	return func(s *terraform.State) error {
    33  		rs, ok := s.RootModule().Resources[n]
    34  		if !ok {
    35  			return fmt.Errorf("Not found: %s", n)
    36  		}
    37  
    38  		if rs.Primary.ID == "" {
    39  			return fmt.Errorf("No ID is set")
    40  		}
    41  
    42  		client := testAccProvider.Meta().(*dc.Client)
    43  		volumes, err := client.ListVolumes(dc.ListVolumesOptions{})
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		for _, v := range volumes {
    49  			if v.Name == rs.Primary.ID {
    50  				inspected, err := client.InspectVolume(v.Name)
    51  				if err != nil {
    52  					return fmt.Errorf("Volume could not be inspected: %s", err)
    53  				}
    54  				*volume = *inspected
    55  				return nil
    56  			}
    57  		}
    58  
    59  		return fmt.Errorf("Volume not found: %s", rs.Primary.ID)
    60  	}
    61  }
    62  
    63  const testAccDockerVolumeConfig = `
    64  resource "docker_volume" "foo" {
    65  	name = "testAccDockerVolume_basic"
    66  }
    67  `