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