github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cobbler/resource_cobbler_distro_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 TestAccCobblerDistro_basic(t *testing.T) {
    14  	var distro cobbler.Distro
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccCobblerPreCheck(t) },
    18  		Providers:    testAccCobblerProviders,
    19  		CheckDestroy: testAccCobblerCheckDistroDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccCobblerDistro_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func TestAccCobblerDistro_change(t *testing.T) {
    32  	var distro cobbler.Distro
    33  
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccCobblerPreCheck(t) },
    36  		Providers:    testAccCobblerProviders,
    37  		CheckDestroy: testAccCobblerCheckDistroDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccCobblerDistro_change_1,
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
    43  				),
    44  			},
    45  			resource.TestStep{
    46  				Config: testAccCobblerDistro_change_2,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func testAccCobblerCheckDistroDestroy(s *terraform.State) error {
    56  	config := testAccCobblerProvider.Meta().(*Config)
    57  
    58  	for _, rs := range s.RootModule().Resources {
    59  		if rs.Type != "cobbler_distro" {
    60  			continue
    61  		}
    62  
    63  		if _, err := config.cobblerClient.GetDistro(rs.Primary.ID); err == nil {
    64  			return fmt.Errorf("Distro still exists")
    65  		}
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func testAccCobblerCheckDistroExists(t *testing.T, n string, distro *cobbler.Distro) resource.TestCheckFunc {
    72  	return func(s *terraform.State) error {
    73  		rs, ok := s.RootModule().Resources[n]
    74  		if !ok {
    75  			return fmt.Errorf("Not found: %s", n)
    76  		}
    77  
    78  		if rs.Primary.ID == "" {
    79  			return fmt.Errorf("No ID is set")
    80  		}
    81  
    82  		config := testAccCobblerProvider.Meta().(*Config)
    83  
    84  		found, err := config.cobblerClient.GetDistro(rs.Primary.ID)
    85  		if err != nil {
    86  			return err
    87  		}
    88  
    89  		if found.Name != rs.Primary.ID {
    90  			return fmt.Errorf("Distro not found")
    91  		}
    92  
    93  		*distro = *found
    94  
    95  		return nil
    96  	}
    97  }
    98  
    99  var testAccCobblerDistro_basic = `
   100  	resource "cobbler_distro" "foo" {
   101  		name = "foo"
   102  		breed = "ubuntu"
   103  		os_version = "trusty"
   104  		arch = "x86_64"
   105  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   106  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   107  	}`
   108  
   109  var testAccCobblerDistro_change_1 = `
   110  	resource "cobbler_distro" "foo" {
   111  		name = "foo"
   112  		comment = "I am a distro"
   113  		breed = "ubuntu"
   114  		os_version = "trusty"
   115  		arch = "x86_64"
   116  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   117  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   118  	}`
   119  
   120  var testAccCobblerDistro_change_2 = `
   121  	resource "cobbler_distro" "foo" {
   122  		name = "foo"
   123  		comment = "I am a distro again"
   124  		breed = "ubuntu"
   125  		os_version = "trusty"
   126  		arch = "x86_64"
   127  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   128  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   129  	}`