github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/google/resource_compute_firewall_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "code.google.com/p/google-api-go-client/compute/v1" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 ) 11 12 func TestAccComputeFirewall_basic(t *testing.T) { 13 var firewall compute.Firewall 14 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckComputeFirewallDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccComputeFirewall_basic, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckComputeFirewallExists( 24 "google_compute_firewall.foobar", &firewall), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func TestAccComputeFirewall_update(t *testing.T) { 32 var firewall compute.Firewall 33 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 CheckDestroy: testAccCheckComputeFirewallDestroy, 38 Steps: []resource.TestStep{ 39 resource.TestStep{ 40 Config: testAccComputeFirewall_basic, 41 Check: resource.ComposeTestCheckFunc( 42 testAccCheckComputeFirewallExists( 43 "google_compute_firewall.foobar", &firewall), 44 ), 45 }, 46 resource.TestStep{ 47 Config: testAccComputeFirewall_update, 48 Check: resource.ComposeTestCheckFunc( 49 testAccCheckComputeFirewallExists( 50 "google_compute_firewall.foobar", &firewall), 51 testAccCheckComputeFirewallPorts( 52 &firewall, "80-255"), 53 ), 54 }, 55 }, 56 }) 57 } 58 59 func testAccCheckComputeFirewallDestroy(s *terraform.State) error { 60 config := testAccProvider.Meta().(*Config) 61 62 for _, rs := range s.RootModule().Resources { 63 if rs.Type != "google_compute_firewall" { 64 continue 65 } 66 67 _, err := config.clientCompute.Firewalls.Get( 68 config.Project, rs.Primary.ID).Do() 69 if err == nil { 70 return fmt.Errorf("Firewall still exists") 71 } 72 } 73 74 return nil 75 } 76 77 func testAccCheckComputeFirewallExists(n string, firewall *compute.Firewall) resource.TestCheckFunc { 78 return func(s *terraform.State) error { 79 rs, ok := s.RootModule().Resources[n] 80 if !ok { 81 return fmt.Errorf("Not found: %s", n) 82 } 83 84 if rs.Primary.ID == "" { 85 return fmt.Errorf("No ID is set") 86 } 87 88 config := testAccProvider.Meta().(*Config) 89 90 found, err := config.clientCompute.Firewalls.Get( 91 config.Project, rs.Primary.ID).Do() 92 if err != nil { 93 return err 94 } 95 96 if found.Name != rs.Primary.ID { 97 return fmt.Errorf("Firewall not found") 98 } 99 100 *firewall = *found 101 102 return nil 103 } 104 } 105 106 func testAccCheckComputeFirewallPorts( 107 firewall *compute.Firewall, ports string) resource.TestCheckFunc { 108 return func(s *terraform.State) error { 109 if len(firewall.Allowed) == 0 { 110 return fmt.Errorf("no allowed rules") 111 } 112 113 if firewall.Allowed[0].Ports[0] != ports { 114 return fmt.Errorf("bad: %#v", firewall.Allowed[0].Ports) 115 } 116 117 return nil 118 } 119 } 120 121 const testAccComputeFirewall_basic = ` 122 resource "google_compute_network" "foobar" { 123 name = "terraform-test" 124 ipv4_range = "10.0.0.0/16" 125 } 126 127 resource "google_compute_firewall" "foobar" { 128 name = "terraform-test" 129 network = "${google_compute_network.foobar.name}" 130 source_tags = ["foo"] 131 132 allow { 133 protocol = "icmp" 134 } 135 }` 136 137 const testAccComputeFirewall_update = ` 138 resource "google_compute_network" "foobar" { 139 name = "terraform-test" 140 ipv4_range = "10.0.0.0/16" 141 } 142 143 resource "google_compute_firewall" "foobar" { 144 name = "terraform-test" 145 network = "${google_compute_network.foobar.name}" 146 source_tags = ["foo"] 147 148 allow { 149 protocol = "tcp" 150 ports = ["80-255"] 151 } 152 }`