github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  					resource.TestCheckResourceAttr(
   104  						"cobbler_system.foo", "interface.586365610.management", "true"),
   105  				),
   106  			},
   107  			resource.TestStep{
   108  				Config: testAccCobblerSystem_removeInterface_2,
   109  				Check: resource.ComposeTestCheckFunc(
   110  					testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro),
   111  					testAccCobblerCheckProfileExists(t, "cobbler_profile.foo", &profile),
   112  					testAccCobblerCheckSystemExists(t, "cobbler_system.foo", &system),
   113  				),
   114  			},
   115  		},
   116  	})
   117  }
   118  
   119  func testAccCobblerCheckSystemDestroy(s *terraform.State) error {
   120  	config := testAccCobblerProvider.Meta().(*Config)
   121  
   122  	for _, rs := range s.RootModule().Resources {
   123  		if rs.Type != "cobbler_system" {
   124  			continue
   125  		}
   126  
   127  		if _, err := config.cobblerClient.GetSystem(rs.Primary.ID); err == nil {
   128  			return fmt.Errorf("System still exists")
   129  		}
   130  	}
   131  
   132  	return nil
   133  }
   134  
   135  func testAccCobblerCheckSystemExists(t *testing.T, n string, system *cobbler.System) resource.TestCheckFunc {
   136  	return func(s *terraform.State) error {
   137  		rs, ok := s.RootModule().Resources[n]
   138  		if !ok {
   139  			return fmt.Errorf("Not found: %s", n)
   140  		}
   141  
   142  		if rs.Primary.ID == "" {
   143  			return fmt.Errorf("No ID is set")
   144  		}
   145  
   146  		config := testAccCobblerProvider.Meta().(*Config)
   147  
   148  		found, err := config.cobblerClient.GetSystem(rs.Primary.ID)
   149  		if err != nil {
   150  			return err
   151  		}
   152  
   153  		if found.Name != rs.Primary.ID {
   154  			return fmt.Errorf("System not found")
   155  		}
   156  
   157  		*system = *found
   158  
   159  		return nil
   160  	}
   161  }
   162  
   163  var testAccCobblerSystem_basic = `
   164  	resource "cobbler_distro" "foo" {
   165  		name = "foo"
   166  		breed = "ubuntu"
   167  		os_version = "trusty"
   168  		arch = "x86_64"
   169  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   170  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   171  	}
   172  
   173  	resource "cobbler_profile" "foo" {
   174  		name = "foo"
   175  		distro = "${cobbler_distro.foo.name}"
   176  	}
   177  
   178  	resource "cobbler_system" "foo" {
   179  		name = "foo"
   180  		profile = "${cobbler_profile.foo.name}"
   181  		name_servers = ["8.8.8.8", "8.8.4.4"]
   182  		comment = "I'm a system"
   183  		power_id = "foo"
   184  
   185  		interface {
   186  			name = "eth0"
   187  			mac_address = "aa:bb:cc:dd:ee:ff"
   188  			static = true
   189  			ip_address = "1.2.3.4"
   190  			netmask = "255.255.255.0"
   191  		}
   192  
   193  		interface {
   194  			name = "eth1"
   195  			mac_address = "aa:bb:cc:dd:ee:fa"
   196  			static = true
   197  			ip_address = "1.2.3.5"
   198  			netmask = "255.255.255.0"
   199  		}
   200  
   201  	}`
   202  
   203  var testAccCobblerSystem_multi = `
   204  	resource "cobbler_distro" "foo" {
   205  		name = "foo"
   206  		breed = "ubuntu"
   207  		os_version = "trusty"
   208  		arch = "x86_64"
   209  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   210  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   211  	}
   212  
   213  	resource "cobbler_profile" "foo" {
   214  		name = "foo"
   215  		distro = "${cobbler_distro.foo.name}"
   216  	}
   217  
   218  	resource "cobbler_system" "foo" {
   219  		count = 50
   220  		name = "${format("foo-%d", count.index)}"
   221  		profile = "${cobbler_profile.foo.name}"
   222  		name_servers = ["8.8.8.8", "8.8.4.4"]
   223  		comment = "I'm a system"
   224  		power_id = "foo"
   225  
   226  		interface {
   227  			name = "eth0"
   228  		}
   229  
   230  		interface {
   231  			name = "eth1"
   232  		}
   233  	}`
   234  
   235  var testAccCobblerSystem_change_1 = `
   236  	resource "cobbler_distro" "foo" {
   237  		name = "foo"
   238  		breed = "ubuntu"
   239  		os_version = "trusty"
   240  		arch = "x86_64"
   241  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   242  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   243  	}
   244  
   245  	resource "cobbler_profile" "foo" {
   246  		name = "foo"
   247  		distro = "${cobbler_distro.foo.name}"
   248  	}
   249  
   250  	resource "cobbler_system" "foo" {
   251  		name = "foo"
   252  		profile = "${cobbler_profile.foo.name}"
   253  		name_servers = ["8.8.8.8", "8.8.4.4"]
   254  		comment = "I'm a system"
   255  		power_id = "foo"
   256  
   257  		interface {
   258  			name = "eth0"
   259  			mac_address = "aa:bb:cc:dd:ee:ff"
   260  			static = true
   261  			ip_address = "1.2.3.4"
   262  			netmask = "255.255.255.0"
   263  		}
   264  
   265  		interface {
   266  			name = "eth1"
   267  			mac_address = "aa:bb:cc:dd:ee:fa"
   268  			static = true
   269  			ip_address = "1.2.3.5"
   270  			netmask = "255.255.255.0"
   271  		}
   272  
   273  	}`
   274  
   275  var testAccCobblerSystem_change_2 = `
   276  	resource "cobbler_distro" "foo" {
   277  		name = "foo"
   278  		breed = "ubuntu"
   279  		os_version = "trusty"
   280  		arch = "x86_64"
   281  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   282  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   283  	}
   284  
   285  	resource "cobbler_profile" "foo" {
   286  		name = "foo"
   287  		distro = "${cobbler_distro.foo.name}"
   288  	}
   289  
   290  	resource "cobbler_system" "foo" {
   291  		name = "foo"
   292  		profile = "${cobbler_profile.foo.name}"
   293  		name_servers = ["8.8.8.8", "8.8.4.4"]
   294  		comment = "I'm a system again"
   295  		power_id = "foo"
   296  
   297  		interface {
   298  			name = "eth0"
   299  			mac_address = "aa:bb:cc:dd:ee:ff"
   300  			static = true
   301  			ip_address = "1.2.3.6"
   302  			netmask = "255.255.255.0"
   303  		}
   304  
   305  		interface {
   306  			name = "eth1"
   307  			mac_address = "aa:bb:cc:dd:ee:fa"
   308  			static = true
   309  			ip_address = "1.2.3.5"
   310  			netmask = "255.255.255.0"
   311  		}
   312  
   313  	}`
   314  
   315  var testAccCobblerSystem_removeInterface_1 = `
   316  	resource "cobbler_distro" "foo" {
   317  		name = "foo"
   318  		breed = "ubuntu"
   319  		os_version = "trusty"
   320  		arch = "x86_64"
   321  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   322  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   323  	}
   324  
   325  	resource "cobbler_profile" "foo" {
   326  		name = "foo"
   327  		distro = "${cobbler_distro.foo.name}"
   328  	}
   329  
   330  	resource "cobbler_system" "foo" {
   331  		name = "foo"
   332  		profile = "${cobbler_profile.foo.name}"
   333  		name_servers = ["8.8.8.8", "8.8.4.4"]
   334  		power_id = "foo"
   335  
   336  		interface {
   337  			name = "eth0"
   338  			mac_address = "aa:bb:cc:dd:ee:ff"
   339  			static = true
   340  			ip_address = "1.2.3.4"
   341  			netmask = "255.255.255.0"
   342  		}
   343  
   344  		interface {
   345  			name = "eth1"
   346  			mac_address = "aa:bb:cc:dd:ee:fa"
   347  			static = true
   348  			ip_address = "1.2.3.5"
   349  			netmask = "255.255.255.0"
   350  			management = true
   351  		}
   352  
   353  	}`
   354  
   355  var testAccCobblerSystem_removeInterface_2 = `
   356  	resource "cobbler_distro" "foo" {
   357  		name = "foo"
   358  		breed = "ubuntu"
   359  		os_version = "trusty"
   360  		arch = "x86_64"
   361  		kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux"
   362  		initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz"
   363  	}
   364  
   365  	resource "cobbler_profile" "foo" {
   366  		name = "foo"
   367  		distro = "${cobbler_distro.foo.name}"
   368  	}
   369  
   370  	resource "cobbler_system" "foo" {
   371  		name = "foo"
   372  		profile = "${cobbler_profile.foo.name}"
   373  		name_servers = ["8.8.8.8", "8.8.4.4"]
   374  		power_id = "foo"
   375  
   376  		interface {
   377  			name = "eth0"
   378  			mac_address = "aa:bb:cc:dd:ee:ff"
   379  			static = true
   380  			ip_address = "1.2.3.4"
   381  			netmask = "255.255.255.0"
   382  		}
   383  	}`