github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/cobbler/resource_cobbler_system_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 TestAccCobblerSystem_basic(t *testing.T) {
    14  	var distro cobbler.Distro
    15  	var profile cobbler.Profile
    16  	var system cobbler.System
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccCobblerPreCheck(t) },
    20  		Providers:    testAccCobblerProviders,
    21  		CheckDestroy: testAccCobblerCheckSystemDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccCobblerSystem_basic,
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
    27  					testAccCobblerCheckProfileExists(t, "cobbler_profile.foo", &profile),
    28  					testAccCobblerCheckSystemExists(t, "cobbler_system.foo", &system),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func TestAccCobblerSystem_multi(t *testing.T) {
    36  	var distro cobbler.Distro
    37  	var profile cobbler.Profile
    38  	var system cobbler.System
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccCobblerPreCheck(t) },
    42  		Providers:    testAccCobblerProviders,
    43  		CheckDestroy: testAccCobblerCheckSystemDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccCobblerSystem_multi,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
    49  					testAccCobblerCheckProfileExists(t, "cobbler_profile.foo", &profile),
    50  					testAccCobblerCheckSystemExists(t, "cobbler_system.foo.45", &system),
    51  				),
    52  			},
    53  		},
    54  	})
    55  }
    56  
    57  func TestAccCobblerSystem_change(t *testing.T) {
    58  	var distro cobbler.Distro
    59  	var profile cobbler.Profile
    60  	var system cobbler.System
    61  
    62  	resource.Test(t, resource.TestCase{
    63  		PreCheck:     func() { testAccCobblerPreCheck(t) },
    64  		Providers:    testAccCobblerProviders,
    65  		CheckDestroy: testAccCobblerCheckSystemDestroy,
    66  		Steps: []resource.TestStep{
    67  			resource.TestStep{
    68  				Config: testAccCobblerSystem_change_1,
    69  				Check: resource.ComposeTestCheckFunc(
    70  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
    71  					testAccCobblerCheckProfileExists(t, "cobbler_profile.foo", &profile),
    72  					testAccCobblerCheckSystemExists(t, "cobbler_system.foo", &system),
    73  				),
    74  			},
    75  			resource.TestStep{
    76  				Config: testAccCobblerSystem_change_2,
    77  				Check: resource.ComposeTestCheckFunc(
    78  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
    79  					testAccCobblerCheckProfileExists(t, "cobbler_profile.foo", &profile),
    80  					testAccCobblerCheckSystemExists(t, "cobbler_system.foo", &system),
    81  				),
    82  			},
    83  		},
    84  	})
    85  }
    86  
    87  func TestAccCobblerSystem_removeInterface(t *testing.T) {
    88  	var distro cobbler.Distro
    89  	var profile cobbler.Profile
    90  	var system cobbler.System
    91  
    92  	resource.Test(t, resource.TestCase{
    93  		PreCheck:     func() { testAccCobblerPreCheck(t) },
    94  		Providers:    testAccCobblerProviders,
    95  		CheckDestroy: testAccCobblerCheckSystemDestroy,
    96  		Steps: []resource.TestStep{
    97  			resource.TestStep{
    98  				Config: testAccCobblerSystem_removeInterface_1,
    99  				Check: resource.ComposeTestCheckFunc(
   100  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
   101  					testAccCobblerCheckProfileExists(t, "cobbler_profile.foo", &profile),
   102  					testAccCobblerCheckSystemExists(t, "cobbler_system.foo", &system),
   103  				),
   104  			},
   105  			resource.TestStep{
   106  				Config: testAccCobblerSystem_removeInterface_2,
   107  				Check: resource.ComposeTestCheckFunc(
   108  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
   109  					testAccCobblerCheckProfileExists(t, "cobbler_profile.foo", &profile),
   110  					testAccCobblerCheckSystemExists(t, "cobbler_system.foo", &system),
   111  				),
   112  			},
   113  		},
   114  	})
   115  }
   116  
   117  func testAccCobblerCheckSystemDestroy(s *terraform.State) error {
   118  	config := testAccCobblerProvider.Meta().(*Config)
   119  
   120  	for _, rs := range s.RootModule().Resources {
   121  		if rs.Type != "cobbler_system" {
   122  			continue
   123  		}
   124  
   125  		if _, err := config.cobblerClient.GetSystem(rs.Primary.ID); err == nil {
   126  			return fmt.Errorf("System still exists")
   127  		}
   128  	}
   129  
   130  	return nil
   131  }
   132  
   133  func testAccCobblerCheckSystemExists(t *testing.T, n string, system *cobbler.System) resource.TestCheckFunc {
   134  	return func(s *terraform.State) error {
   135  		rs, ok := s.RootModule().Resources[n]
   136  		if !ok {
   137  			return fmt.Errorf("Not found: %s", n)
   138  		}
   139  
   140  		if rs.Primary.ID == "" {
   141  			return fmt.Errorf("No ID is set")
   142  		}
   143  
   144  		config := testAccCobblerProvider.Meta().(*Config)
   145  
   146  		found, err := config.cobblerClient.GetSystem(rs.Primary.ID)
   147  		if err != nil {
   148  			return err
   149  		}
   150  
   151  		if found.Name != rs.Primary.ID {
   152  			return fmt.Errorf("System not found")
   153  		}
   154  
   155  		*system = *found
   156  
   157  		return nil
   158  	}
   159  }
   160  
   161  var testAccCobblerSystem_basic = `
   162  	resource "cobbler_distro" "foo" {
   163  		name = "foo"
   164  		breed = "ubuntu"
   165  		os_version = "trusty"
   166  		arch = "x86_64"
   167  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   168  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   169  	}
   170  
   171  	resource "cobbler_profile" "foo" {
   172  		name = "foo"
   173  		distro = "${cobbler_distro.foo.name}"
   174  	}
   175  
   176  	resource "cobbler_system" "foo" {
   177  		name = "foo"
   178  		profile = "${cobbler_profile.foo.name}"
   179  		name_servers = ["8.8.8.8", "8.8.4.4"]
   180  		comment = "I'm a system"
   181  		power_id = "foo"
   182  
   183  		interface {
   184  			name = "eth0"
   185  			mac_address = "aa:bb:cc:dd:ee:ff"
   186  			static = true
   187  			ip_address = "1.2.3.4"
   188  			netmask = "255.255.255.0"
   189  		}
   190  
   191  		interface {
   192  			name = "eth1"
   193  			mac_address = "aa:bb:cc:dd:ee:fa"
   194  			static = true
   195  			ip_address = "1.2.3.5"
   196  			netmask = "255.255.255.0"
   197  		}
   198  
   199  	}`
   200  
   201  var testAccCobblerSystem_multi = `
   202  	resource "cobbler_distro" "foo" {
   203  		name = "foo"
   204  		breed = "ubuntu"
   205  		os_version = "trusty"
   206  		arch = "x86_64"
   207  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   208  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   209  	}
   210  
   211  	resource "cobbler_profile" "foo" {
   212  		name = "foo"
   213  		distro = "${cobbler_distro.foo.name}"
   214  	}
   215  
   216  	resource "cobbler_system" "foo" {
   217  		count = 50
   218  		name = "${format("foo-%d", count.index)}"
   219  		profile = "${cobbler_profile.foo.name}"
   220  		name_servers = ["8.8.8.8", "8.8.4.4"]
   221  		comment = "I'm a system"
   222  		power_id = "foo"
   223  
   224  		interface {
   225  			name = "eth0"
   226  		}
   227  
   228  		interface {
   229  			name = "eth1"
   230  		}
   231  	}`
   232  
   233  var testAccCobblerSystem_change_1 = `
   234  	resource "cobbler_distro" "foo" {
   235  		name = "foo"
   236  		breed = "ubuntu"
   237  		os_version = "trusty"
   238  		arch = "x86_64"
   239  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   240  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   241  	}
   242  
   243  	resource "cobbler_profile" "foo" {
   244  		name = "foo"
   245  		distro = "${cobbler_distro.foo.name}"
   246  	}
   247  
   248  	resource "cobbler_system" "foo" {
   249  		name = "foo"
   250  		profile = "${cobbler_profile.foo.name}"
   251  		name_servers = ["8.8.8.8", "8.8.4.4"]
   252  		comment = "I'm a system"
   253  		power_id = "foo"
   254  
   255  		interface {
   256  			name = "eth0"
   257  			mac_address = "aa:bb:cc:dd:ee:ff"
   258  			static = true
   259  			ip_address = "1.2.3.4"
   260  			netmask = "255.255.255.0"
   261  		}
   262  
   263  		interface {
   264  			name = "eth1"
   265  			mac_address = "aa:bb:cc:dd:ee:fa"
   266  			static = true
   267  			ip_address = "1.2.3.5"
   268  			netmask = "255.255.255.0"
   269  		}
   270  
   271  	}`
   272  
   273  var testAccCobblerSystem_change_2 = `
   274  	resource "cobbler_distro" "foo" {
   275  		name = "foo"
   276  		breed = "ubuntu"
   277  		os_version = "trusty"
   278  		arch = "x86_64"
   279  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   280  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   281  	}
   282  
   283  	resource "cobbler_profile" "foo" {
   284  		name = "foo"
   285  		distro = "${cobbler_distro.foo.name}"
   286  	}
   287  
   288  	resource "cobbler_system" "foo" {
   289  		name = "foo"
   290  		profile = "${cobbler_profile.foo.name}"
   291  		name_servers = ["8.8.8.8", "8.8.4.4"]
   292  		comment = "I'm a system again"
   293  		power_id = "foo"
   294  
   295  		interface {
   296  			name = "eth0"
   297  			mac_address = "aa:bb:cc:dd:ee:ff"
   298  			static = true
   299  			ip_address = "1.2.3.6"
   300  			netmask = "255.255.255.0"
   301  		}
   302  
   303  		interface {
   304  			name = "eth1"
   305  			mac_address = "aa:bb:cc:dd:ee:fa"
   306  			static = true
   307  			ip_address = "1.2.3.5"
   308  			netmask = "255.255.255.0"
   309  		}
   310  
   311  	}`
   312  
   313  var testAccCobblerSystem_removeInterface_1 = `
   314  	resource "cobbler_distro" "foo" {
   315  		name = "foo"
   316  		breed = "ubuntu"
   317  		os_version = "trusty"
   318  		arch = "x86_64"
   319  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   320  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   321  	}
   322  
   323  	resource "cobbler_profile" "foo" {
   324  		name = "foo"
   325  		distro = "${cobbler_distro.foo.name}"
   326  	}
   327  
   328  	resource "cobbler_system" "foo" {
   329  		name = "foo"
   330  		profile = "${cobbler_profile.foo.name}"
   331  		name_servers = ["8.8.8.8", "8.8.4.4"]
   332  		power_id = "foo"
   333  
   334  		interface {
   335  			name = "eth0"
   336  			mac_address = "aa:bb:cc:dd:ee:ff"
   337  			static = true
   338  			ip_address = "1.2.3.4"
   339  			netmask = "255.255.255.0"
   340  		}
   341  
   342  		interface {
   343  			name = "eth1"
   344  			mac_address = "aa:bb:cc:dd:ee:fa"
   345  			static = true
   346  			ip_address = "1.2.3.5"
   347  			netmask = "255.255.255.0"
   348  		}
   349  
   350  	}`
   351  
   352  var testAccCobblerSystem_removeInterface_2 = `
   353  	resource "cobbler_distro" "foo" {
   354  		name = "foo"
   355  		breed = "ubuntu"
   356  		os_version = "trusty"
   357  		arch = "x86_64"
   358  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   359  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   360  	}
   361  
   362  	resource "cobbler_profile" "foo" {
   363  		name = "foo"
   364  		distro = "${cobbler_distro.foo.name}"
   365  	}
   366  
   367  	resource "cobbler_system" "foo" {
   368  		name = "foo"
   369  		profile = "${cobbler_profile.foo.name}"
   370  		name_servers = ["8.8.8.8", "8.8.4.4"]
   371  		power_id = "foo"
   372  
   373  		interface {
   374  			name = "eth0"
   375  			mac_address = "aa:bb:cc:dd:ee:ff"
   376  			static = true
   377  			ip_address = "1.2.3.4"
   378  			netmask = "255.255.255.0"
   379  		}
   380  	}`