github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/logentries/resource_logentries_logset_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 LogSetResource struct {
    14  	Name     string `tfresource:"name"`
    15  	Location string `tfresource:"location"`
    16  }
    17  
    18  func TestAccLogentriesLogSet_Basic(t *testing.T) {
    19  	var logSetResource LogSetResource
    20  
    21  	logSetName := fmt.Sprintf("terraform-test-%s", acctest.RandString(8))
    22  	testAccLogentriesLogSetConfig := fmt.Sprintf(`
    23  		resource "logentries_logset" "test_logset" {
    24  			name = "%s"
    25  			location = "terraform.io"
    26  		}
    27  	`, logSetName)
    28  
    29  	resource.Test(t, resource.TestCase{
    30  		PreCheck:     func() { testAccPreCheck(t) },
    31  		Providers:    testAccProviders,
    32  		CheckDestroy: testAccCheckLogentriesLogSetDestroy,
    33  		Steps: []resource.TestStep{
    34  			resource.TestStep{
    35  				Config: testAccLogentriesLogSetConfig,
    36  				Check: lexp.TestCheckResourceExpectation(
    37  					"logentries_logset.test_logset",
    38  					&logSetResource,
    39  					testAccCheckLogentriesLogSetExists,
    40  					map[string]lexp.TestExpectValue{
    41  						"name":     lexp.Equals(logSetName),
    42  						"location": lexp.Equals("terraform.io"),
    43  					},
    44  				),
    45  			},
    46  		},
    47  	})
    48  }
    49  
    50  func TestAccLogentriesLogSet_NoLocation(t *testing.T) {
    51  	var logSetResource LogSetResource
    52  
    53  	logSetName := fmt.Sprintf("terraform-test-%s", acctest.RandString(8))
    54  	testAccLogentriesLogSetConfig := fmt.Sprintf(`
    55  		resource "logentries_logset" "test_logset" {
    56  			name = "%s"
    57  		}
    58  	`, logSetName)
    59  
    60  	resource.Test(t, resource.TestCase{
    61  		PreCheck:     func() { testAccPreCheck(t) },
    62  		Providers:    testAccProviders,
    63  		CheckDestroy: testAccCheckLogentriesLogSetDestroy,
    64  		Steps: []resource.TestStep{
    65  			resource.TestStep{
    66  				Config: testAccLogentriesLogSetConfig,
    67  				Check: lexp.TestCheckResourceExpectation(
    68  					"logentries_logset.test_logset",
    69  					&logSetResource,
    70  					testAccCheckLogentriesLogSetExists,
    71  					map[string]lexp.TestExpectValue{
    72  						"name":     lexp.Equals(logSetName),
    73  						"location": lexp.Equals("nonlocation"),
    74  					},
    75  				),
    76  			},
    77  		},
    78  	})
    79  }
    80  
    81  func testAccCheckLogentriesLogSetDestroy(s *terraform.State) error {
    82  	client := testAccProvider.Meta().(*logentries.Client)
    83  
    84  	for _, rs := range s.RootModule().Resources {
    85  		if rs.Type != "logentries_logset" {
    86  			continue
    87  		}
    88  
    89  		resp, err := client.LogSet.Read(logentries.LogSetReadRequest{Key: rs.Primary.ID})
    90  
    91  		if err == nil {
    92  			return fmt.Errorf("Log set still exists: %#v", resp)
    93  		}
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func testAccCheckLogentriesLogSetExists(resource string, fact interface{}) resource.TestCheckFunc {
   100  	return func(s *terraform.State) error {
   101  		rs, ok := s.RootModule().Resources[resource]
   102  
   103  		if !ok {
   104  			return fmt.Errorf("Not found: %s", resource)
   105  		}
   106  
   107  		if rs.Primary.ID == "" {
   108  			return fmt.Errorf("No LogSet Key is set")
   109  		}
   110  
   111  		client := testAccProvider.Meta().(*logentries.Client)
   112  
   113  		resp, err := client.LogSet.Read(logentries.LogSetReadRequest{Key: rs.Primary.ID})
   114  
   115  		if err != nil {
   116  			return err
   117  		}
   118  
   119  		res := fact.(*LogSetResource)
   120  		res.Location = resp.Location
   121  		res.Name = resp.Name
   122  
   123  		return nil
   124  	}
   125  }