github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/cobbler/resource_cobbler_kickstart_file_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 TestAccCobblerKickstartFile_basic(t *testing.T) { 14 var ks cobbler.KickstartFile 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccCobblerPreCheck(t) }, 18 Providers: testAccCobblerProviders, 19 CheckDestroy: testAccCobblerCheckKickstartFileDestroy, 20 Steps: []resource.TestStep{ 21 resource.TestStep{ 22 Config: testAccCobblerKickstartFile_basic, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCobblerCheckKickstartFileExists(t, "cobbler_kickstart_file.foo", &ks), 25 ), 26 }, 27 }, 28 }) 29 } 30 31 func testAccCobblerCheckKickstartFileDestroy(s *terraform.State) error { 32 config := testAccCobblerProvider.Meta().(*Config) 33 34 for _, rs := range s.RootModule().Resources { 35 if rs.Type != "cobbler_kickstart_file" { 36 continue 37 } 38 39 if _, err := config.cobblerClient.GetKickstartFile(rs.Primary.ID); err == nil { 40 return fmt.Errorf("Kickstart File still exists") 41 } 42 } 43 44 return nil 45 } 46 47 func testAccCobblerCheckKickstartFileExists(t *testing.T, n string, ks *cobbler.KickstartFile) resource.TestCheckFunc { 48 return func(s *terraform.State) error { 49 rs, ok := s.RootModule().Resources[n] 50 if !ok { 51 return fmt.Errorf("Not found: %s", n) 52 } 53 54 if rs.Primary.ID == "" { 55 return fmt.Errorf("No ID is set") 56 } 57 58 config := testAccCobblerProvider.Meta().(*Config) 59 60 found, err := config.cobblerClient.GetKickstartFile(rs.Primary.ID) 61 if err != nil { 62 return err 63 } 64 65 if found.Name != rs.Primary.ID { 66 return fmt.Errorf("Kickstart File not found") 67 } 68 69 *ks = *found 70 71 return nil 72 } 73 } 74 75 var testAccCobblerKickstartFile_basic = ` 76 resource "cobbler_kickstart_file" "foo" { 77 name = "/var/lib/cobbler/kickstarts/foo.ks" 78 body = "I'm a kickstart file." 79 }`