github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cobbler/resource_cobbler_snippet_test.go (about)

     1  package cobbler
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	cobbler "github.com/jtopjian/cobblerclient"
    11  )
    12  
    13  func TestAccCobblerSnippet_basic(t *testing.T) {
    14  	var snippet cobbler.Snippet
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccCobblerPreCheck(t) },
    18  		Providers:    testAccCobblerProviders,
    19  		CheckDestroy: testAccCobblerCheckSnippetDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccCobblerSnippet_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCobblerCheckSnippetExists(t, "cobbler_snippet.foo", &snippet),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCobblerCheckSnippetDestroy(s *terraform.State) error {
    32  	config := testAccCobblerProvider.Meta().(*Config)
    33  
    34  	for _, rs := range s.RootModule().Resources {
    35  		if rs.Type != "cobbler_snippet" {
    36  			continue
    37  		}
    38  
    39  		if _, err := config.cobblerClient.GetSnippet(rs.Primary.ID); err == nil {
    40  			return fmt.Errorf("Snippet still exists")
    41  		}
    42  	}
    43  
    44  	return nil
    45  }
    46  
    47  func testAccCobblerCheckSnippetExists(t *testing.T, n string, snippet *cobbler.Snippet) resource.TestCheckFunc {
    48  	return func(s *terraform.State) error {
    49  		rs, ok := s.RootModule().Resources[n]
    50  		if !ok {
    51  			return fmt.Errorf("Not found: %s", n)
    52  		}
    53  
    54  		if rs.Primary.ID == "" {
    55  			return fmt.Errorf("No ID is set")
    56  		}
    57  
    58  		config := testAccCobblerProvider.Meta().(*Config)
    59  
    60  		found, err := config.cobblerClient.GetSnippet(rs.Primary.ID)
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		if found.Name != rs.Primary.ID {
    66  			return fmt.Errorf("Snippet not found")
    67  		}
    68  
    69  		*snippet = *found
    70  
    71  		return nil
    72  	}
    73  }
    74  
    75  var testAccCobblerSnippet_basic = `
    76  	resource "cobbler_snippet" "foo" {
    77  		name = "/var/lib/cobbler/snippets/foo"
    78  		body = "I'm a Snippet."
    79  	}`