github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/scaleway/resource_scaleway_security_group_test.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestAccScalewaySecurityGroup_Basic(t *testing.T) {
    12  	resource.Test(t, resource.TestCase{
    13  		PreCheck:     func() { testAccPreCheck(t) },
    14  		Providers:    testAccProviders,
    15  		CheckDestroy: testAccCheckScalewaySecurityGroupDestroy,
    16  		Steps: []resource.TestStep{
    17  			resource.TestStep{
    18  				Config: testAccCheckScalewaySecurityGroupConfig,
    19  				Check: resource.ComposeTestCheckFunc(
    20  					testAccCheckScalewaySecurityGroupExists("scaleway_security_group.base"),
    21  					testAccCheckScalewaySecurityGroupAttributes("scaleway_security_group.base"),
    22  					resource.TestCheckResourceAttr("scaleway_security_group.base", "name", "public"),
    23  					resource.TestCheckResourceAttr("scaleway_security_group.base", "description", "public gateway"),
    24  				),
    25  			},
    26  		},
    27  	})
    28  }
    29  
    30  func testAccCheckScalewaySecurityGroupDestroy(s *terraform.State) error {
    31  	client := testAccProvider.Meta().(*Client).scaleway
    32  
    33  	for _, rs := range s.RootModule().Resources {
    34  		if rs.Type != "scaleway" {
    35  			continue
    36  		}
    37  
    38  		_, err := client.GetASecurityGroup(rs.Primary.ID)
    39  
    40  		if err == nil {
    41  			return fmt.Errorf("Security Group still exists")
    42  		}
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func testAccCheckScalewaySecurityGroupAttributes(n string) resource.TestCheckFunc {
    49  	return func(s *terraform.State) error {
    50  		rs, ok := s.RootModule().Resources[n]
    51  		if !ok {
    52  			return fmt.Errorf("Unknown resource: %s", n)
    53  		}
    54  
    55  		client := testAccProvider.Meta().(*Client).scaleway
    56  		group, err := client.GetASecurityGroup(rs.Primary.ID)
    57  		if err != nil {
    58  			return err
    59  		}
    60  
    61  		if group.SecurityGroups.Name != "public" {
    62  			return fmt.Errorf("Security Group has wrong name")
    63  		}
    64  		if group.SecurityGroups.Description != "public gateway" {
    65  			return fmt.Errorf("Security Group has wrong description")
    66  		}
    67  
    68  		return nil
    69  	}
    70  }
    71  
    72  func testAccCheckScalewaySecurityGroupExists(n string) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		rs, ok := s.RootModule().Resources[n]
    75  
    76  		if !ok {
    77  			return fmt.Errorf("Not found: %s", n)
    78  		}
    79  
    80  		if rs.Primary.ID == "" {
    81  			return fmt.Errorf("No Security Group ID is set")
    82  		}
    83  
    84  		client := testAccProvider.Meta().(*Client).scaleway
    85  		group, err := client.GetASecurityGroup(rs.Primary.ID)
    86  
    87  		if err != nil {
    88  			return err
    89  		}
    90  
    91  		if group.SecurityGroups.ID != rs.Primary.ID {
    92  			return fmt.Errorf("Record not found")
    93  		}
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  var testAccCheckScalewaySecurityGroupConfig = `
   100  resource "scaleway_security_group" "base" {
   101    name = "public"
   102    description = "public gateway"
   103  }
   104  `