github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/resource_openstack_compute_secgroup_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
    11  )
    12  
    13  func TestAccComputeV2SecGroup_basic(t *testing.T) {
    14  	var secgroup secgroups.SecurityGroup
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckComputeV2SecGroupDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccComputeV2SecGroup_basic_orig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.foo", &secgroup),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccComputeV2SecGroup_update(t *testing.T) {
    32  	var secgroup secgroups.SecurityGroup
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckComputeV2SecGroupDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccComputeV2SecGroup_basic_orig,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.foo", &secgroup),
    43  				),
    44  			},
    45  			resource.TestStep{
    46  				Config: testAccComputeV2SecGroup_basic_update,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.foo", &secgroup),
    49  					testAccCheckComputeV2SecGroupRuleCount(t, &secgroup, 2),
    50  				),
    51  			},
    52  		},
    53  	})
    54  }
    55  
    56  func TestAccComputeV2SecGroup_groupID(t *testing.T) {
    57  	var secgroup1, secgroup2, secgroup3 secgroups.SecurityGroup
    58  
    59  	resource.Test(t, resource.TestCase{
    60  		PreCheck:     func() { testAccPreCheck(t) },
    61  		Providers:    testAccProviders,
    62  		CheckDestroy: testAccCheckComputeV2SecGroupDestroy,
    63  		Steps: []resource.TestStep{
    64  			resource.TestStep{
    65  				Config: testAccComputeV2SecGroup_groupID_orig,
    66  				Check: resource.ComposeTestCheckFunc(
    67  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.test_group_1", &secgroup1),
    68  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.test_group_2", &secgroup2),
    69  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.test_group_3", &secgroup3),
    70  					testAccCheckComputeV2SecGroupGroupIDMatch(t, &secgroup1, &secgroup3),
    71  				),
    72  			},
    73  			resource.TestStep{
    74  				Config: testAccComputeV2SecGroup_groupID_update,
    75  				Check: resource.ComposeTestCheckFunc(
    76  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.test_group_1", &secgroup1),
    77  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.test_group_2", &secgroup2),
    78  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.test_group_3", &secgroup3),
    79  					testAccCheckComputeV2SecGroupGroupIDMatch(t, &secgroup2, &secgroup3),
    80  				),
    81  			},
    82  		},
    83  	})
    84  }
    85  
    86  func TestAccComputeV2SecGroup_self(t *testing.T) {
    87  	var secgroup secgroups.SecurityGroup
    88  
    89  	resource.Test(t, resource.TestCase{
    90  		PreCheck:     func() { testAccPreCheck(t) },
    91  		Providers:    testAccProviders,
    92  		CheckDestroy: testAccCheckComputeV2SecGroupDestroy,
    93  		Steps: []resource.TestStep{
    94  			resource.TestStep{
    95  				Config: testAccComputeV2SecGroup_self,
    96  				Check: resource.ComposeTestCheckFunc(
    97  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.test_group_1", &secgroup),
    98  					testAccCheckComputeV2SecGroupGroupIDMatch(t, &secgroup, &secgroup),
    99  					resource.TestCheckResourceAttr(
   100  						"openstack_compute_secgroup_v2.test_group_1", "rule.1118853483.self", "true"),
   101  					resource.TestCheckResourceAttr(
   102  						"openstack_compute_secgroup_v2.test_group_1", "rule.1118853483.from_group_id", ""),
   103  				),
   104  			},
   105  		},
   106  	})
   107  }
   108  
   109  func testAccCheckComputeV2SecGroupDestroy(s *terraform.State) error {
   110  	config := testAccProvider.Meta().(*Config)
   111  	computeClient, err := config.computeV2Client(OS_REGION_NAME)
   112  	if err != nil {
   113  		return fmt.Errorf("(testAccCheckComputeV2SecGroupDestroy) Error creating OpenStack compute client: %s", err)
   114  	}
   115  
   116  	for _, rs := range s.RootModule().Resources {
   117  		if rs.Type != "openstack_compute_secgroup_v2" {
   118  			continue
   119  		}
   120  
   121  		_, err := secgroups.Get(computeClient, rs.Primary.ID).Extract()
   122  		if err == nil {
   123  			return fmt.Errorf("Security group still exists")
   124  		}
   125  	}
   126  
   127  	return nil
   128  }
   129  
   130  func testAccCheckComputeV2SecGroupExists(t *testing.T, n string, secgroup *secgroups.SecurityGroup) resource.TestCheckFunc {
   131  	return func(s *terraform.State) error {
   132  		rs, ok := s.RootModule().Resources[n]
   133  		if !ok {
   134  			return fmt.Errorf("Not found: %s", n)
   135  		}
   136  
   137  		if rs.Primary.ID == "" {
   138  			return fmt.Errorf("No ID is set")
   139  		}
   140  
   141  		config := testAccProvider.Meta().(*Config)
   142  		computeClient, err := config.computeV2Client(OS_REGION_NAME)
   143  		if err != nil {
   144  			return fmt.Errorf("(testAccCheckComputeV2SecGroupExists) Error creating OpenStack compute client: %s", err)
   145  		}
   146  
   147  		found, err := secgroups.Get(computeClient, rs.Primary.ID).Extract()
   148  		if err != nil {
   149  			return err
   150  		}
   151  
   152  		if found.ID != rs.Primary.ID {
   153  			return fmt.Errorf("Security group not found")
   154  		}
   155  
   156  		*secgroup = *found
   157  
   158  		return nil
   159  	}
   160  }
   161  
   162  func testAccCheckComputeV2SecGroupRuleCount(t *testing.T, secgroup *secgroups.SecurityGroup, count int) resource.TestCheckFunc {
   163  	return func(s *terraform.State) error {
   164  		if len(secgroup.Rules) != count {
   165  			return fmt.Errorf("Security group rule count does not match. Expected %d, got %d", count, len(secgroup.Rules))
   166  		}
   167  
   168  		return nil
   169  	}
   170  }
   171  
   172  func testAccCheckComputeV2SecGroupGroupIDMatch(t *testing.T, sg1, sg2 *secgroups.SecurityGroup) resource.TestCheckFunc {
   173  	return func(s *terraform.State) error {
   174  		if len(sg2.Rules) == 1 {
   175  			if sg1.Name != sg2.Rules[0].Group.Name || sg1.TenantID != sg2.Rules[0].Group.TenantID {
   176  				return fmt.Errorf("%s was not correctly applied to %s", sg1.Name, sg2.Name)
   177  			}
   178  		} else {
   179  			return fmt.Errorf("%s rule count is incorrect", sg2.Name)
   180  		}
   181  
   182  		return nil
   183  	}
   184  }
   185  
   186  var testAccComputeV2SecGroup_basic_orig = fmt.Sprintf(`
   187  	resource "openstack_compute_secgroup_v2" "foo" {
   188  		name = "test_group_1"
   189  		description = "first test security group"
   190  		rule {
   191  			from_port = 22
   192  			to_port = 22
   193  			ip_protocol = "tcp"
   194  			cidr = "0.0.0.0/0"
   195  		}
   196  		rule {
   197  			from_port = 1
   198  			to_port = 65535
   199  			ip_protocol = "udp"
   200  			cidr = "0.0.0.0/0"
   201  		}
   202  		rule {
   203  			from_port = -1
   204  			to_port = -1
   205  			ip_protocol = "icmp"
   206  			cidr = "0.0.0.0/0"
   207  		}
   208  	}`)
   209  
   210  var testAccComputeV2SecGroup_basic_update = fmt.Sprintf(`
   211  	resource "openstack_compute_secgroup_v2" "foo" {
   212  		name = "test_group_1"
   213  		description = "first test security group"
   214  		rule {
   215  			from_port = 2200
   216  			to_port = 2200
   217  			ip_protocol = "tcp"
   218  			cidr = "0.0.0.0/0"
   219  		}
   220  		rule {
   221  			from_port = -1
   222  			to_port = -1
   223  			ip_protocol = "icmp"
   224  			cidr = "0.0.0.0/0"
   225  		}
   226  }`)
   227  
   228  var testAccComputeV2SecGroup_groupID_orig = fmt.Sprintf(`
   229  	resource "openstack_compute_secgroup_v2" "test_group_1" {
   230  		name = "test_group_1"
   231  		description = "first test security group"
   232  		rule {
   233  			from_port = 22
   234  			to_port = 22
   235  			ip_protocol = "tcp"
   236  			cidr = "0.0.0.0/0"
   237  		}
   238  	}
   239  
   240  	resource "openstack_compute_secgroup_v2" "test_group_2" {
   241  		name = "test_group_2"
   242  		description = "second test security group"
   243  		rule {
   244  			from_port = -1
   245  			to_port = -1
   246  			ip_protocol = "icmp"
   247  			cidr = "0.0.0.0/0"
   248  		}
   249  	}
   250  
   251  	resource "openstack_compute_secgroup_v2" "test_group_3" {
   252  		name = "test_group_3"
   253  		description = "third test security group"
   254  		rule {
   255  			from_port = 80
   256  			to_port = 80
   257  			ip_protocol = "tcp"
   258  			from_group_id = "${openstack_compute_secgroup_v2.test_group_1.id}"
   259  		}
   260  	}`)
   261  
   262  var testAccComputeV2SecGroup_groupID_update = fmt.Sprintf(`
   263  	resource "openstack_compute_secgroup_v2" "test_group_1" {
   264  		name = "test_group_1"
   265  		description = "first test security group"
   266  		rule {
   267  			from_port = 22
   268  			to_port = 22
   269  			ip_protocol = "tcp"
   270  			cidr = "0.0.0.0/0"
   271  		}
   272  	}
   273  
   274  	resource "openstack_compute_secgroup_v2" "test_group_2" {
   275  		name = "test_group_2"
   276  		description = "second test security group"
   277  		rule {
   278  			from_port = -1
   279  			to_port = -1
   280  			ip_protocol = "icmp"
   281  			cidr = "0.0.0.0/0"
   282  		}
   283  	}
   284  
   285  	resource "openstack_compute_secgroup_v2" "test_group_3" {
   286  		name = "test_group_3"
   287  		description = "third test security group"
   288  		rule {
   289  			from_port = 80
   290  			to_port = 80
   291  			ip_protocol = "tcp"
   292  			from_group_id = "${openstack_compute_secgroup_v2.test_group_2.id}"
   293  		}
   294  	}`)
   295  
   296  var testAccComputeV2SecGroup_self = fmt.Sprintf(`
   297  	resource "openstack_compute_secgroup_v2" "test_group_1" {
   298  		name = "test_group_1"
   299  		description = "first test security group"
   300  		rule {
   301  			from_port = 22
   302  			to_port = 22
   303  			ip_protocol = "tcp"
   304  			self = true
   305  		}
   306  	}`)