github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/circonus/resource_circonus_check_postgresql_test.go (about)

     1  package circonus
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  )
    10  
    11  func TestAccCirconusCheckPostgreSQL_basic(t *testing.T) {
    12  	checkName := fmt.Sprintf("PostgreSQL ops per table check - %s", acctest.RandString(5))
    13  
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckDestroyCirconusCheckBundle,
    18  		Steps: []resource.TestStep{
    19  			{
    20  				Config: fmt.Sprintf(testAccCirconusCheckPostgreSQLConfigFmt, checkName),
    21  				Check: resource.ComposeTestCheckFunc(
    22  					resource.TestCheckResourceAttr("circonus_check.table_ops", "active", "true"),
    23  					resource.TestCheckResourceAttr("circonus_check.table_ops", "collector.#", "1"),
    24  					resource.TestCheckResourceAttr("circonus_check.table_ops", "collector.2388330941.id", "/broker/1"),
    25  					resource.TestCheckResourceAttr("circonus_check.table_ops", "postgresql.#", "1"),
    26  					resource.TestCheckResourceAttr("circonus_check.table_ops", "postgresql.1831600166.dsn", "user=postgres host=pg1.example.org port=5432 password=12345 sslmode=require"),
    27  					resource.TestCheckResourceAttr("circonus_check.table_ops", "postgresql.1831600166.query", `SELECT 'tables', sum(n_tup_ins) as inserts, sum(n_tup_upd) as updates, sum(n_tup_del) as deletes, sum(idx_scan)  as index_scans, sum(seq_scan) as seq_scans, sum(idx_tup_fetch) as index_tup_fetch, sum(seq_tup_read) as seq_tup_read from pg_stat_all_tables`),
    28  					resource.TestCheckResourceAttr("circonus_check.table_ops", "name", checkName),
    29  					resource.TestCheckResourceAttr("circonus_check.table_ops", "period", "300s"),
    30  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.#", "7"),
    31  
    32  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.997634628.name", "tables`inserts"),
    33  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.997634628.tags.#", "2"),
    34  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.997634628.tags.2087084518", "author:terraform"),
    35  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.997634628.tags.1401442048", "lifecycle:unittest"),
    36  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.997634628.type", "numeric"),
    37  
    38  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.565883273.name", "tables`updates"),
    39  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.565883273.tags.#", "2"),
    40  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.565883273.tags.2087084518", "author:terraform"),
    41  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.565883273.tags.1401442048", "lifecycle:unittest"),
    42  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.565883273.type", "numeric"),
    43  
    44  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3965415003.name", "tables`deletes"),
    45  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3965415003.tags.#", "2"),
    46  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3965415003.tags.2087084518", "author:terraform"),
    47  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3965415003.tags.1401442048", "lifecycle:unittest"),
    48  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3965415003.type", "numeric"),
    49  
    50  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3868690100.name", "tables`index_scans"),
    51  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3868690100.tags.#", "2"),
    52  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3868690100.tags.2087084518", "author:terraform"),
    53  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3868690100.tags.1401442048", "lifecycle:unittest"),
    54  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3868690100.type", "numeric"),
    55  
    56  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2772400178.name", "tables`seq_scans"),
    57  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2772400178.tags.#", "2"),
    58  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2772400178.tags.2087084518", "author:terraform"),
    59  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2772400178.tags.1401442048", "lifecycle:unittest"),
    60  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2772400178.type", "numeric"),
    61  
    62  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3278042831.name", "tables`index_tup_fetch"),
    63  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3278042831.tags.#", "2"),
    64  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3278042831.tags.2087084518", "author:terraform"),
    65  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3278042831.tags.1401442048", "lifecycle:unittest"),
    66  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.3278042831.type", "numeric"),
    67  
    68  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2685537214.name", "tables`seq_tup_read"),
    69  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2685537214.tags.#", "2"),
    70  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2685537214.tags.2087084518", "author:terraform"),
    71  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2685537214.tags.1401442048", "lifecycle:unittest"),
    72  					resource.TestCheckResourceAttr("circonus_check.table_ops", "metric.2685537214.type", "numeric"),
    73  
    74  					resource.TestCheckResourceAttr("circonus_check.table_ops", "tags.#", "2"),
    75  					resource.TestCheckResourceAttr("circonus_check.table_ops", "tags.2087084518", "author:terraform"),
    76  					resource.TestCheckResourceAttr("circonus_check.table_ops", "tags.1401442048", "lifecycle:unittest"),
    77  					resource.TestCheckResourceAttr("circonus_check.table_ops", "target", "pgdb.example.org"),
    78  					resource.TestCheckResourceAttr("circonus_check.table_ops", "type", "postgres"),
    79  				),
    80  			},
    81  		},
    82  	})
    83  }
    84  
    85  const testAccCirconusCheckPostgreSQLConfigFmt = `
    86  variable "test_tags" {
    87    type = "list"
    88    default = [ "author:terraform", "lifecycle:unittest" ]
    89  }
    90  resource "circonus_check" "table_ops" {
    91    active = true
    92    name = "%s"
    93    period = "300s"
    94  
    95    collector {
    96      id = "/broker/1"
    97    }
    98  
    99    postgresql {
   100      dsn = "user=postgres host=pg1.example.org port=5432 password=12345 sslmode=require"
   101      query = <<EOF
   102  SELECT 'tables', sum(n_tup_ins) as inserts, sum(n_tup_upd) as updates, sum(n_tup_del) as deletes, sum(idx_scan)  as index_scans, sum(seq_scan) as seq_scans, sum(idx_tup_fetch) as index_tup_fetch, sum(seq_tup_read) as seq_tup_read from pg_stat_all_tables
   103  EOF
   104    }
   105  
   106    metric {
   107      name = "tables` + "`" + `inserts"
   108      tags = [ "${var.test_tags}" ]
   109      type = "numeric"
   110    }
   111  
   112    metric {
   113      name = "tables` + "`" + `updates"
   114      tags = [ "${var.test_tags}" ]
   115      type = "numeric"
   116    }
   117  
   118    metric {
   119      name = "tables` + "`" + `deletes"
   120      tags = [ "${var.test_tags}" ]
   121      type = "numeric"
   122    }
   123  
   124    metric {
   125      name = "tables` + "`" + `index_scans"
   126      tags = [ "${var.test_tags}" ]
   127      type = "numeric"
   128    }
   129  
   130    metric {
   131      name = "tables` + "`" + `seq_scans"
   132      tags = [ "${var.test_tags}" ]
   133      type = "numeric"
   134    }
   135  
   136    metric {
   137      name = "tables` + "`" + `index_tup_fetch"
   138      tags = [ "${var.test_tags}" ]
   139      type = "numeric"
   140    }
   141  
   142    metric {
   143      name = "tables` + "`" + `seq_tup_read"
   144      tags = [ "${var.test_tags}" ]
   145      type = "numeric"
   146    }
   147  
   148    tags = [ "${var.test_tags}" ]
   149    target = "pgdb.example.org"
   150  }
   151  `