github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"google.golang.org/api/compute/v1"
    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  	description = "Resource created for Terraform acceptance testing"
   130  	network = "${google_compute_network.foobar.name}"
   131  	source_tags = ["foo"]
   132  
   133  	allow {
   134  		protocol = "icmp"
   135  	}
   136  }`
   137  
   138  const testAccComputeFirewall_update = `
   139  resource "google_compute_network" "foobar" {
   140  	name = "terraform-test"
   141  	ipv4_range = "10.0.0.0/16"
   142  }
   143  
   144  resource "google_compute_firewall" "foobar" {
   145  	name = "terraform-test"
   146  	description = "Resource created for Terraform acceptance testing"
   147  	network = "${google_compute_network.foobar.name}"
   148  	source_tags = ["foo"]
   149  
   150  	allow {
   151  		protocol = "tcp"
   152  		ports = ["80-255"]
   153  	}
   154  }`