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

     1  package logentries
     2  
     3  import (
     4  	"fmt"
     5  	lexp "github.com/hashicorp/terraform/builtin/providers/logentries/expect"
     6  	"github.com/hashicorp/terraform/helper/acctest"
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/logentries/le_goclient"
    10  	"testing"
    11  )
    12  
    13  type LogResource struct {
    14  	Name            string `tfresource:"name"`
    15  	RetentionPeriod string `tfresource:"retention_period"`
    16  	Source          string `tfresource:"source"`
    17  	Token           string `tfresource:"token"`
    18  	Type            string `tfresource:"type"`
    19  }
    20  
    21  func TestAccLogentriesLog_Token(t *testing.T) {
    22  	var logResource LogResource
    23  
    24  	logName := fmt.Sprintf("terraform-test-%s", acctest.RandString(8))
    25  	testAccLogentriesLogConfig := fmt.Sprintf(`
    26  		resource "logentries_logset" "test_logset" {
    27  			name = "%s"
    28  		}
    29  		resource "logentries_log" "test_log" {
    30  			logset_id = "${logentries_logset.test_logset.id}"
    31  			name = "%s"
    32  		}
    33  	`, logName, logName)
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:  func() { testAccPreCheck(t) },
    37  		Providers: testAccProviders,
    38  		CheckDestroy: func(s *terraform.State) error {
    39  			testAccCheckLogentriesLogDestroy(s)
    40  			testAccCheckLogentriesLogSetDestroy(s)
    41  			return nil
    42  		},
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccLogentriesLogConfig,
    46  				Check: lexp.TestCheckResourceExpectation(
    47  					"logentries_log.test_log",
    48  					&logResource,
    49  					testAccCheckLogentriesLogExists,
    50  					map[string]lexp.TestExpectValue{
    51  						"name":   lexp.Equals(logName),
    52  						"source": lexp.Equals("token"),
    53  						"token":  lexp.RegexMatches("[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}"),
    54  					},
    55  				),
    56  			},
    57  		},
    58  	})
    59  }
    60  
    61  func TestAccLogentriesLog_SourceApi(t *testing.T) {
    62  	var logResource LogResource
    63  
    64  	logName := fmt.Sprintf("terraform-test-%s", acctest.RandString(8))
    65  	testAccLogentriesLogConfig := fmt.Sprintf(`
    66  		resource "logentries_logset" "test_logset" {
    67  			name = "%s"
    68  		}
    69  		resource "logentries_log" "test_log" {
    70  			logset_id = "${logentries_logset.test_logset.id}"
    71  			name = "%s"
    72  			source = "api"
    73  		}
    74  	`, logName, logName)
    75  
    76  	resource.Test(t, resource.TestCase{
    77  		PreCheck:  func() { testAccPreCheck(t) },
    78  		Providers: testAccProviders,
    79  		CheckDestroy: func(s *terraform.State) error {
    80  			testAccCheckLogentriesLogDestroy(s)
    81  			testAccCheckLogentriesLogSetDestroy(s)
    82  			return nil
    83  		},
    84  		Steps: []resource.TestStep{
    85  			resource.TestStep{
    86  				Config: testAccLogentriesLogConfig,
    87  				Check: lexp.TestCheckResourceExpectation(
    88  					"logentries_log.test_log",
    89  					&logResource,
    90  					testAccCheckLogentriesLogExists,
    91  					map[string]lexp.TestExpectValue{
    92  						"name":   lexp.Equals(logName),
    93  						"source": lexp.Equals("api"),
    94  					},
    95  				),
    96  			},
    97  		},
    98  	})
    99  }
   100  
   101  func TestAccLogentriesLog_SourceAgent(t *testing.T) {
   102  	var logResource LogResource
   103  
   104  	logName := fmt.Sprintf("terraform-test-%s", acctest.RandString(8))
   105  	fileName := "/opt/foo"
   106  	testAccLogentriesLogConfig := fmt.Sprintf(`
   107  		resource "logentries_logset" "test_logset" {
   108  			name = "%s"
   109  		}
   110  		resource "logentries_log" "test_log" {
   111  			logset_id = "${logentries_logset.test_logset.id}"
   112  			name = "%s"
   113  			source = "agent"
   114  			filename = "%s"
   115  		}
   116  	`, logName, logName, fileName)
   117  
   118  	resource.Test(t, resource.TestCase{
   119  		PreCheck:  func() { testAccPreCheck(t) },
   120  		Providers: testAccProviders,
   121  		CheckDestroy: func(s *terraform.State) error {
   122  			testAccCheckLogentriesLogDestroy(s)
   123  			testAccCheckLogentriesLogSetDestroy(s)
   124  			return nil
   125  		},
   126  		Steps: []resource.TestStep{
   127  			resource.TestStep{
   128  				Config: testAccLogentriesLogConfig,
   129  				Check: lexp.TestCheckResourceExpectation(
   130  					"logentries_log.test_log",
   131  					&logResource,
   132  					testAccCheckLogentriesLogExists,
   133  					map[string]lexp.TestExpectValue{
   134  						"name":     lexp.Equals(logName),
   135  						"source":   lexp.Equals("agent"),
   136  						"filename": lexp.Equals(fileName),
   137  					},
   138  				),
   139  			},
   140  		},
   141  	})
   142  }
   143  
   144  func TestAccLogentriesLog_RetentionPeriod1M(t *testing.T) {
   145  	var logResource LogResource
   146  
   147  	logName := fmt.Sprintf("terraform-test-%s", acctest.RandString(8))
   148  	testAccLogentriesLogConfig := fmt.Sprintf(`
   149  		resource "logentries_logset" "test_logset" {
   150  			name = "%s"
   151  		}
   152  		resource "logentries_log" "test_log" {
   153  			logset_id = "${logentries_logset.test_logset.id}"
   154  			name = "%s"
   155  			retention_period = "1M"
   156  		}
   157  	`, logName, logName)
   158  
   159  	resource.Test(t, resource.TestCase{
   160  		PreCheck:  func() { testAccPreCheck(t) },
   161  		Providers: testAccProviders,
   162  		CheckDestroy: func(s *terraform.State) error {
   163  			testAccCheckLogentriesLogDestroy(s)
   164  			testAccCheckLogentriesLogSetDestroy(s)
   165  			return nil
   166  		},
   167  		Steps: []resource.TestStep{
   168  			resource.TestStep{
   169  				Config: testAccLogentriesLogConfig,
   170  				Check: lexp.TestCheckResourceExpectation(
   171  					"logentries_log.test_log",
   172  					&logResource,
   173  					testAccCheckLogentriesLogExists,
   174  					map[string]lexp.TestExpectValue{
   175  						"name":             lexp.Equals(logName),
   176  						"retention_period": lexp.Equals("1M"),
   177  					},
   178  				),
   179  			},
   180  		},
   181  	})
   182  }
   183  
   184  func TestAccLogentriesLog_RetentionPeriodAccountDefault(t *testing.T) {
   185  	var logResource LogResource
   186  
   187  	logName := fmt.Sprintf("terraform-test-%s", acctest.RandString(8))
   188  	testAccLogentriesLogConfig := fmt.Sprintf(`
   189  		resource "logentries_logset" "test_logset" {
   190  			name = "%s"
   191  		}
   192  		resource "logentries_log" "test_log" {
   193  			logset_id = "${logentries_logset.test_logset.id}"
   194  			name = "%s"
   195  		}
   196  	`, logName, logName)
   197  
   198  	resource.Test(t, resource.TestCase{
   199  		PreCheck:  func() { testAccPreCheck(t) },
   200  		Providers: testAccProviders,
   201  		CheckDestroy: func(s *terraform.State) error {
   202  			testAccCheckLogentriesLogDestroy(s)
   203  			testAccCheckLogentriesLogSetDestroy(s)
   204  			return nil
   205  		},
   206  		Steps: []resource.TestStep{
   207  			resource.TestStep{
   208  				Config: testAccLogentriesLogConfig,
   209  				Check: lexp.TestCheckResourceExpectation(
   210  					"logentries_log.test_log",
   211  					&logResource,
   212  					testAccCheckLogentriesLogExists,
   213  					map[string]lexp.TestExpectValue{
   214  						"name":             lexp.Equals(logName),
   215  						"retention_period": lexp.Equals("ACCOUNT_DEFAULT"),
   216  					},
   217  				),
   218  			},
   219  		},
   220  	})
   221  }
   222  
   223  func TestAccLogentriesLog_RetentionPeriodAccountUnlimited(t *testing.T) {
   224  	var logResource LogResource
   225  
   226  	logName := fmt.Sprintf("terraform-test-%s", acctest.RandString(8))
   227  	testAccLogentriesLogConfig := fmt.Sprintf(`
   228  		resource "logentries_logset" "test_logset" {
   229  			name = "%s"
   230  		}
   231  		resource "logentries_log" "test_log" {
   232  			logset_id = "${logentries_logset.test_logset.id}"
   233  			name = "%s"
   234  			retention_period = "UNLIMITED"
   235  		}
   236  	`, logName, logName)
   237  
   238  	resource.Test(t, resource.TestCase{
   239  		PreCheck:  func() { testAccPreCheck(t) },
   240  		Providers: testAccProviders,
   241  		CheckDestroy: func(s *terraform.State) error {
   242  			testAccCheckLogentriesLogDestroy(s)
   243  			testAccCheckLogentriesLogSetDestroy(s)
   244  			return nil
   245  		},
   246  		Steps: []resource.TestStep{
   247  			resource.TestStep{
   248  				Config: testAccLogentriesLogConfig,
   249  				Check: lexp.TestCheckResourceExpectation(
   250  					"logentries_log.test_log",
   251  					&logResource,
   252  					testAccCheckLogentriesLogExists,
   253  					map[string]lexp.TestExpectValue{
   254  						"name":             lexp.Equals(logName),
   255  						"retention_period": lexp.Equals("UNLIMITED"),
   256  					},
   257  				),
   258  			},
   259  		},
   260  	})
   261  }
   262  
   263  func testAccCheckLogentriesLogDestroy(s *terraform.State) error {
   264  	client := testAccProvider.Meta().(*logentries.Client)
   265  
   266  	for _, rs := range s.RootModule().Resources {
   267  		if rs.Type != "logentries_logset" {
   268  			continue
   269  		}
   270  
   271  		resp, err := client.Log.Read(logentries.LogReadRequest{Key: rs.Primary.ID})
   272  
   273  		if err == nil {
   274  			return fmt.Errorf("Log still exists: %#v", resp)
   275  		}
   276  	}
   277  
   278  	return nil
   279  }
   280  
   281  func testAccCheckLogentriesLogExists(n string, fact interface{}) resource.TestCheckFunc {
   282  	return func(s *terraform.State) error {
   283  		rs, ok := s.RootModule().Resources[n]
   284  
   285  		if !ok {
   286  			return fmt.Errorf("Not found: %s", n)
   287  		}
   288  
   289  		if rs.Primary.ID == "" {
   290  			return fmt.Errorf("No LogSet Key is set")
   291  		}
   292  
   293  		client := testAccProvider.Meta().(*logentries.Client)
   294  
   295  		resp, err := client.Log.Read(logentries.LogReadRequest{Key: rs.Primary.ID})
   296  
   297  		if err != nil {
   298  			return err
   299  		}
   300  
   301  		res := fact.(*LogResource)
   302  		res.Name = resp.Name
   303  		res.RetentionPeriod, _ = enumForRetentionPeriod(resp.Retention)
   304  		res.Source = resp.Source
   305  		res.Token = resp.Token
   306  		res.Type = resp.Type
   307  
   308  		return nil
   309  	}
   310  }