@@ -58,6 +58,85 @@ func testAccCheckLibvirtPoolDoesNotExists(n string, pool *libvirt.StoragePool) r
5858 }
5959}
6060
61+ func TestAccLibvirtPool_Import (t * testing.T ) {
62+ var pool libvirt.StoragePool
63+ randomPoolResource := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
64+ randomPoolName := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
65+ poolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
66+ resource .Test (t , resource.TestCase {
67+ PreCheck : func () { testAccPreCheck (t ) },
68+ Providers : testAccProviders ,
69+ CheckDestroy : testAccCheckLibvirtPoolDestroy ,
70+ Steps : []resource.TestStep {
71+ {
72+ Config : fmt .Sprintf (`
73+ resource "libvirt_pool" "%s" {
74+ name = "%s"
75+ type = "dir"
76+ path = "%s"
77+ }` , randomPoolResource , randomPoolName , poolPath ),
78+ Check : testAccCheckLibvirtPoolExists ("libvirt_pool." + randomPoolResource , & pool ),
79+ Destroy : false ,
80+ },
81+ {
82+ ResourceName : "libvirt_pool." + randomPoolResource ,
83+ ImportState : true ,
84+ ImportStateCheck : func (instanceState []* terraform.InstanceState ) error {
85+ // check all instance state imported with same assert
86+ for i , f := range instanceState {
87+ if err := composeTestImportStateCheckFunc (
88+ testImportStateCheckResourceAttr ("libvirt_pool." + randomPoolResource , "name" , randomPoolName ),
89+ testImportStateCheckResourceAttr ("libvirt_pool." + randomPoolResource , "type" , "dir" ),
90+ testImportStateCheckResourceAttr ("libvirt_pool." + randomPoolResource , "path" , poolPath ),
91+ )(f ); err != nil {
92+ return fmt .Errorf ("Check InstanceState n°%d / %d error: %s" , i + 1 , len (instanceState ), err )
93+ }
94+ }
95+
96+ return nil
97+ },
98+ },
99+ },
100+ })
101+ }
102+
103+ // ImportStateCheckFunc one import instance state check function
104+ // differ from github.com/hashicorp/terraform-plugin-sdk/helper/resource.ImportStateCheckFunc
105+ // which is multiple import Instance State check function
106+ type ImportStateCheckFunc func (is * terraform.InstanceState ) error
107+
108+ // composeTestImportStateCheckFunc compose multiple InstanceState check
109+ func composeTestImportStateCheckFunc (fs ... ImportStateCheckFunc ) ImportStateCheckFunc {
110+ return func (is * terraform.InstanceState ) error {
111+ for i , f := range fs {
112+ if err := f (is ); err != nil {
113+ return fmt .Errorf ("Check %d/%d error: %s" , i + 1 , len (fs ), err )
114+ }
115+ }
116+
117+ return nil
118+ }
119+ }
120+
121+ // testImportStateCheckResourceAttr assert if a terraform.InstanceState as attribute name[key] with value
122+ func testImportStateCheckResourceAttr (name string , key string , value string ) ImportStateCheckFunc {
123+ return func (instanceState * terraform.InstanceState ) error {
124+ if v , ok := instanceState .Attributes [key ]; ! ok || v != value {
125+ if ! ok {
126+ return fmt .Errorf ("%s: Attribute '%s' not found" , name , key )
127+ }
128+
129+ return fmt .Errorf (
130+ "%s: Attribute '%s' expected %#v, got %#v" ,
131+ name ,
132+ key ,
133+ value ,
134+ v )
135+ }
136+ return nil
137+ }
138+ }
139+
61140func TestAccLibvirtPool_Basic (t * testing.T ) {
62141 var pool libvirt.StoragePool
63142 randomPoolResource := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
0 commit comments