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