github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/mysql/resource_grant_test.go (about)

     1  package mysql
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  	"testing"
     8  
     9  	mysqlc "github.com/ziutek/mymysql/mysql"
    10  
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccGrant(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccGrantCheckDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccGrantConfig_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccPrivilegeExists("mysql_grant.test", "SELECT"),
    25  					resource.TestCheckResourceAttr("mysql_grant.test", "user", "jdoe"),
    26  					resource.TestCheckResourceAttr("mysql_grant.test", "host", "example.com"),
    27  					resource.TestCheckResourceAttr("mysql_grant.test", "database", "foo"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func testAccPrivilegeExists(rn string, privilege string) resource.TestCheckFunc {
    35  	return func(s *terraform.State) error {
    36  		rs, ok := s.RootModule().Resources[rn]
    37  		if !ok {
    38  			return fmt.Errorf("resource not found: %s", rn)
    39  		}
    40  
    41  		if rs.Primary.ID == "" {
    42  			return fmt.Errorf("grant id not set")
    43  		}
    44  
    45  		id := strings.Split(rs.Primary.ID, ":")
    46  		userhost := strings.Split(id[0], "@")
    47  		user := userhost[0]
    48  		host := userhost[1]
    49  
    50  		conn := testAccProvider.Meta().(*providerConfiguration).Conn
    51  		stmtSQL := fmt.Sprintf("SHOW GRANTS for '%s'@'%s'", user, host)
    52  		log.Println("Executing statement:", stmtSQL)
    53  		rows, _, err := conn.Query(stmtSQL)
    54  		if err != nil {
    55  			return fmt.Errorf("error reading grant: %s", err)
    56  		}
    57  
    58  		if len(rows) == 0 {
    59  			return fmt.Errorf("grant not found for '%s'@'%s'", user, host)
    60  		}
    61  
    62  		privilegeFound := false
    63  		for _, row := range rows {
    64  			log.Printf("Result Row: %s", row[0])
    65  			privIndex := strings.Index(string(row[0].([]byte)), privilege)
    66  			if privIndex != -1 {
    67  				privilegeFound = true
    68  			}
    69  		}
    70  
    71  		if !privilegeFound {
    72  			return fmt.Errorf("grant no found for '%s'@'%s'", user, host)
    73  		}
    74  
    75  		return nil
    76  	}
    77  }
    78  
    79  func testAccGrantCheckDestroy(s *terraform.State) error {
    80  	conn := testAccProvider.Meta().(*providerConfiguration).Conn
    81  
    82  	for _, rs := range s.RootModule().Resources {
    83  		if rs.Type != "mysql_grant" {
    84  			continue
    85  		}
    86  
    87  		id := strings.Split(rs.Primary.ID, ":")
    88  		userhost := strings.Split(id[0], "@")
    89  		user := userhost[0]
    90  		host := userhost[1]
    91  
    92  		stmtSQL := fmt.Sprintf("SHOW GRANTS for '%s'@'%s'", user, host)
    93  		log.Println("Executing statement:", stmtSQL)
    94  		rows, _, err := conn.Query(stmtSQL)
    95  		if err != nil {
    96  			if mysqlErr, ok := err.(*mysqlc.Error); ok {
    97  				if mysqlErr.Code == mysqlc.ER_NONEXISTING_GRANT {
    98  					return nil
    99  				}
   100  			}
   101  
   102  			return fmt.Errorf("error reading grant: %s", err)
   103  		}
   104  
   105  		if len(rows) != 0 {
   106  			return fmt.Errorf("grant still exists for'%s'@'%s'", user, host)
   107  		}
   108  	}
   109  	return nil
   110  }
   111  
   112  const testAccGrantConfig_basic = `
   113  resource "mysql_user" "test" {
   114          user = "jdoe"
   115  				host = "example.com"
   116  				password = "password"
   117  }
   118  
   119  resource "mysql_grant" "test" {
   120          user = "${mysql_user.test.user}"
   121          host = "${mysql_user.test.host}"
   122          database = "foo"
   123          privileges = ["UPDATE", "SELECT"]
   124  }
   125  `