github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/digitalocean/datasource_digitaloceal_image_test.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "log" 6 "regexp" 7 "testing" 8 9 "github.com/digitalocean/godo" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccDigitalOceanImage_Basic(t *testing.T) { 16 var droplet godo.Droplet 17 var snapshotsId []int 18 rInt := acctest.RandInt() 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 Providers: testAccProviders, 23 CheckDestroy: testAccCheckDigitalOceanDropletDestroy, 24 Steps: []resource.TestStep{ 25 { 26 Config: testAccCheckDigitalOceanDropletConfig_basic(rInt), 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckDigitalOceanDropletExists("digitalocean_droplet.foobar", &droplet), 29 takeSnapshotsOfDroplet(rInt, &droplet, &snapshotsId), 30 ), 31 }, 32 { 33 Config: testAccCheckDigitalOceanImageConfig_basic(rInt, 1), 34 Check: resource.ComposeTestCheckFunc( 35 resource.TestCheckResourceAttr( 36 "data.digitalocean_image.foobar", "name", fmt.Sprintf("snap-%d-1", rInt)), 37 resource.TestCheckResourceAttr( 38 "data.digitalocean_image.foobar", "min_disk_size", "20"), 39 resource.TestCheckResourceAttr( 40 "data.digitalocean_image.foobar", "private", "true"), 41 resource.TestCheckResourceAttr( 42 "data.digitalocean_image.foobar", "type", "snapshot"), 43 ), 44 }, 45 { 46 Config: testAccCheckDigitalOceanImageConfig_basic(rInt, 0), 47 ExpectError: regexp.MustCompile(`.*too many user images found with name snap-.*\ .found 2, expected 1.`), 48 }, 49 { 50 Config: testAccCheckDigitalOceanImageConfig_nonexisting(rInt), 51 Destroy: false, 52 ExpectError: regexp.MustCompile(`.*no user image found with name snap-.*-nonexisting`), 53 }, 54 { 55 Config: " ", 56 Check: resource.ComposeTestCheckFunc( 57 deleteSnapshots(&snapshotsId), 58 ), 59 }, 60 }, 61 }) 62 } 63 64 func takeSnapshotsOfDroplet(rInt int, droplet *godo.Droplet, snapshotsId *[]int) resource.TestCheckFunc { 65 return func(s *terraform.State) error { 66 client := testAccProvider.Meta().(*godo.Client) 67 for i := 0; i < 3; i++ { 68 err := takeSnapshotOfDroplet(rInt, i%2, droplet) 69 if err != nil { 70 return err 71 } 72 } 73 retrieveDroplet, _, err := client.Droplets.Get((*droplet).ID) 74 if err != nil { 75 return err 76 } 77 *snapshotsId = retrieveDroplet.SnapshotIDs 78 return nil 79 } 80 } 81 82 func takeSnapshotOfDroplet(rInt, sInt int, droplet *godo.Droplet) error { 83 client := testAccProvider.Meta().(*godo.Client) 84 action, _, err := client.DropletActions.Snapshot((*droplet).ID, fmt.Sprintf("snap-%d-%d", rInt, sInt)) 85 if err != nil { 86 return err 87 } 88 waitForAction(client, action) 89 return nil 90 } 91 92 func deleteSnapshots(snapshotsId *[]int) resource.TestCheckFunc { 93 return func(s *terraform.State) error { 94 log.Printf("XXX Deleting snaps") 95 client := testAccProvider.Meta().(*godo.Client) 96 snapshots := *snapshotsId 97 for _, value := range snapshots { 98 log.Printf("XXX Deleting %d", value) 99 _, err := client.Images.Delete(value) 100 if err != nil { 101 return err 102 } 103 } 104 return nil 105 } 106 } 107 108 func testAccCheckDigitalOceanImageConfig_basic(rInt, sInt int) string { 109 return fmt.Sprintf(` 110 data "digitalocean_image" "foobar" { 111 name = "snap-%d-%d" 112 } 113 `, rInt, sInt) 114 } 115 116 func testAccCheckDigitalOceanImageConfig_nonexisting(rInt int) string { 117 return fmt.Sprintf(` 118 data "digitalocean_image" "foobar" { 119 name = "snap-%d-nonexisting" 120 } 121 `, rInt) 122 }