github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 TestAccCobblerProfile_withRepo(t *testing.T) { 61 var distro cobbler.Distro 62 var profile cobbler.Profile 63 64 resource.Test(t, resource.TestCase{ 65 PreCheck: func() { testAccCobblerPreCheck(t) }, 66 Providers: testAccCobblerProviders, 67 CheckDestroy: testAccCobblerCheckProfileDestroy, 68 Steps: []resource.TestStep{ 69 resource.TestStep{ 70 Config: testAccCobblerProfile_withRepo, 71 Check: resource.ComposeTestCheckFunc( 72 testAccCobblerCheckDistroExists(t, "cobbler_distro.foo", &distro), 73 testAccCobblerCheckProfileExists(t, "cobbler_profile.foo", &profile), 74 ), 75 }, 76 }, 77 }) 78 } 79 80 func testAccCobblerCheckProfileDestroy(s *terraform.State) error { 81 config := testAccCobblerProvider.Meta().(*Config) 82 83 for _, rs := range s.RootModule().Resources { 84 if rs.Type != "cobbler_profile" { 85 continue 86 } 87 88 if _, err := config.cobblerClient.GetProfile(rs.Primary.ID); err == nil { 89 return fmt.Errorf("Profile still exists") 90 } 91 } 92 93 return nil 94 } 95 96 func testAccCobblerCheckProfileExists(t *testing.T, n string, profile *cobbler.Profile) resource.TestCheckFunc { 97 return func(s *terraform.State) error { 98 rs, ok := s.RootModule().Resources[n] 99 if !ok { 100 return fmt.Errorf("Not found: %s", n) 101 } 102 103 if rs.Primary.ID == "" { 104 return fmt.Errorf("No ID is set") 105 } 106 107 config := testAccCobblerProvider.Meta().(*Config) 108 109 found, err := config.cobblerClient.GetProfile(rs.Primary.ID) 110 if err != nil { 111 return err 112 } 113 114 if found.Name != rs.Primary.ID { 115 return fmt.Errorf("Profile not found") 116 } 117 118 *profile = *found 119 120 return nil 121 } 122 } 123 124 var testAccCobblerProfile_basic = ` 125 resource "cobbler_distro" "foo" { 126 name = "foo" 127 breed = "ubuntu" 128 os_version = "trusty" 129 arch = "x86_64" 130 kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux" 131 initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz" 132 } 133 134 resource "cobbler_profile" "foo" { 135 name = "foo" 136 distro = "${cobbler_distro.foo.name}" 137 }` 138 139 var testAccCobblerProfile_change_1 = ` 140 resource "cobbler_distro" "foo" { 141 name = "foo" 142 breed = "ubuntu" 143 os_version = "trusty" 144 arch = "x86_64" 145 kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux" 146 initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz" 147 } 148 149 resource "cobbler_profile" "foo" { 150 name = "foo" 151 comment = "I am a profile" 152 distro = "${cobbler_distro.foo.name}" 153 }` 154 155 var testAccCobblerProfile_change_2 = ` 156 resource "cobbler_distro" "foo" { 157 name = "foo" 158 breed = "ubuntu" 159 os_version = "trusty" 160 arch = "x86_64" 161 kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux" 162 initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz" 163 } 164 165 resource "cobbler_profile" "foo" { 166 name = "foo" 167 comment = "I am a profile again" 168 distro = "${cobbler_distro.foo.name}" 169 }` 170 171 var testAccCobblerProfile_withRepo = ` 172 resource "cobbler_distro" "foo" { 173 name = "foo" 174 breed = "ubuntu" 175 os_version = "trusty" 176 arch = "x86_64" 177 kernel = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/linux" 178 initrd = "/var/www/cobbler/ks_mirror/Ubuntu-14.04/install/netboot/ubuntu-installer/amd64/initrd.gz" 179 } 180 181 resource "cobbler_profile" "foo" { 182 name = "foo" 183 comment = "I am a profile again" 184 distro = "${cobbler_distro.foo.name}" 185 repos = ["Ubuntu-14.04-x86_64"] 186 }`