1
1
package libvirt
2
2
3
3
import (
4
+ "crypto/sha256"
4
5
"fmt"
6
+ "io"
5
7
"os"
8
+ "path/filepath"
6
9
"testing"
7
10
"time"
8
11
9
12
libvirt "github.com/digitalocean/go-libvirt"
10
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
11
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
15
+ "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
16
+ "github.com/stretchr/testify/assert"
17
+ "github.com/stretchr/testify/require"
12
18
)
13
19
14
20
func TestTimeFromEpoch (t * testing.T ) {
@@ -29,27 +35,17 @@ func TestTimeFromEpoch(t *testing.T) {
29
35
}
30
36
}
31
37
32
- func TestAccUtilsVolume_UploadVolumeCopier (t * testing.T ) {
38
+ func TestAccUtilsVolume_UploaderDownloader (t * testing.T ) {
33
39
var volume libvirt.StorageVol
34
40
randomVolumeResource := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
35
41
randomVolumeName := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
36
42
randomPoolName := acctest .RandStringFromCharSet (10 , acctest .CharSetAlpha )
37
43
randomPoolPath := t .TempDir ()
38
44
39
- tmpfile , err := os .CreateTemp (t .TempDir (), "test-image-" )
40
- if err != nil {
41
- t .Fatal (err )
42
- }
43
-
44
- // simulate uploading a 1G file usig a sparse file
45
- if err := tmpfile .Truncate (1024 * 1024 * 1024 ); err != nil {
46
- t .Fatal (err )
47
- }
45
+ imagePath , err := filepath .Abs ("testdata/test.qcow2" )
46
+ require .NoError (t , err )
48
47
49
- defer os .Remove (tmpfile .Name ())
50
- defer tmpfile .Close ()
51
-
52
- url := fmt .Sprintf ("file://%s" , tmpfile .Name ())
48
+ url := fmt .Sprintf ("file://%s" , imagePath )
53
49
54
50
config := fmt .Sprintf (`
55
51
resource "libvirt_pool" "%s" {
@@ -74,8 +70,32 @@ func TestAccUtilsVolume_UploadVolumeCopier(t *testing.T) {
74
70
testAccCheckLibvirtVolumeExists ("libvirt_volume." + randomVolumeResource , & volume ),
75
71
resource .TestCheckResourceAttr (
76
72
"libvirt_volume." + randomVolumeResource , "name" , randomVolumeName ),
73
+ func (state * terraform.State ) error {
74
+ virConn := testAccProvider .Meta ().(* Client ).libvirt
75
+
76
+ file , err := os .CreateTemp ("" , "downloader-" )
77
+ require .NoError (t , err )
78
+
79
+ defer os .Remove (file .Name ())
80
+ defer file .Close ()
81
+
82
+ downloader := newVolumeDownloader (virConn , & volume )
83
+ err = downloader (file )
84
+ require .NoError (t , err )
85
+
86
+ _ , err = file .Seek (0 , 0 )
87
+ require .NoError (t , err )
88
+
89
+ h := sha256 .New ()
90
+ _ , err = io .Copy (h , file )
91
+ require .NoError (t , err )
92
+
93
+ assert .Equal (t , "0f71acdc66da59b04121b939573bec2e5be78a6cdf829b64142cf0a93a7076f5" , fmt .Sprintf ("%x" , h .Sum (nil )))
94
+ return nil
95
+ },
77
96
),
78
97
},
79
98
},
80
99
})
100
+
81
101
}
0 commit comments