forked from kdomanski/iso9660
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_writer_integration_test.go
174 lines (144 loc) · 4.21 KB
/
image_writer_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//go:build integration
// +build integration
package iso9660
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// Identical to TestWriterAndMountWithAddLocal, except it uses
// AddFile to add files with a constant string in them.
func TestWriterAndMount(t *testing.T) {
//
// Image creation
//
// create a new image writer and prepare for cleanup
w, err := NewWriter()
assert.NoError(t, err)
defer func() {
if cleanupErr := w.Cleanup(); cleanupErr != nil {
t.Fatalf("failed to cleanup writer: %v", cleanupErr)
}
}()
// add 1000 files to thoroughly test descriptor writing over sector bounds
for i := 0; i < 1000; i++ {
path := fmt.Sprintf("firstlevel/dir%d/thirdlevel/file%d.txt", i, i)
err = w.AddFile(strings.NewReader("hrh2309hr320h"), path)
assert.NoError(t, err)
}
// write test image
f, err := os.CreateTemp(os.TempDir(), "iso9660_golang_test")
assert.NoError(t, err)
defer os.Remove(f.Name())
err = w.WriteTo(f, "testvolume")
assert.NoError(t, err)
//
// Image mounting
//
// create mount directory
mountDir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer func() {
if removeErr := os.RemoveAll(mountDir); removeErr != nil {
t.Fatalf("failed to delete mount directory: %v", removeErr)
}
}()
// execute mount
mountCmd := exec.Command("mount", "-t", "iso9660", f.Name(), mountDir)
output, err := mountCmd.CombinedOutput()
assert.NoError(t, err, "failed to mount the ISO image: %v\n%s", err, string(output))
// make sure that we can see the 1000 files
txtFileCount := 0
walkfn := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
if string(data) != "hrh2309hr320h" {
return fmt.Errorf("file %q has the wrong contents", path)
}
txtFileCount++
return nil
}
err = filepath.Walk(mountDir, walkfn)
assert.NoError(t, err)
assert.Equal(t, 1000, txtFileCount)
// execute umount
umountCmd := exec.Command("umount", mountDir)
output, err = umountCmd.CombinedOutput()
assert.NoError(t, err, "failed to unmount the ISO image: %v\n%s", err, string(output))
}
// Identical to TestWriterAndMount, except it uses
// AddLocalFile to add an existing file.
func TestWriterAndMountWithAddLocal(t *testing.T) {
sampleData, err := os.ReadFile("/etc/issue")
assert.NoError(t, err)
//
// Image creation
//
// create a new image writer and prepare for cleanup
w, err := NewWriter()
assert.NoError(t, err)
defer func() {
if cleanupErr := w.Cleanup(); cleanupErr != nil {
t.Fatalf("failed to cleanup writer: %v", cleanupErr)
}
}()
// add 1000 files to thoroughly test descriptor writing over sector bounds
for i := 0; i < 1000; i++ {
path := fmt.Sprintf("firstlevel/dir%d/thirdlevel/file%d.txt", i, i)
err = w.AddLocalFile("/etc/issue", path)
assert.NoError(t, err)
}
// write test image
f, err := os.CreateTemp(os.TempDir(), "iso9660_golang_test")
assert.NoError(t, err)
defer os.Remove(f.Name())
err = w.WriteTo(f, "testvolume")
assert.NoError(t, err)
//
// Image mounting
//
// create mount directory
mountDir, err := os.MkdirTemp("", "")
assert.NoError(t, err)
defer func() {
if removeErr := os.RemoveAll(mountDir); removeErr != nil {
t.Fatalf("failed to delete mount directory: %v", removeErr)
}
}()
// execute mount
mountCmd := exec.Command("mount", "-t", "iso9660", f.Name(), mountDir)
output, err := mountCmd.CombinedOutput()
assert.NoError(t, err, "failed to mount the ISO image: %v\n%s", err, string(output))
// make sure that we can see the 1000 files
txtFileCount := 0
walkfn := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
if string(data) != string(sampleData) {
return fmt.Errorf("file %q has the wrong contents", path)
}
txtFileCount++
return nil
}
err = filepath.Walk(mountDir, walkfn)
assert.NoError(t, err)
assert.Equal(t, 1000, txtFileCount)
// execute umount
umountCmd := exec.Command("umount", mountDir)
output, err = umountCmd.CombinedOutput()
assert.NoError(t, err, "failed to unmount the ISO image: %v\n%s", err, string(output))
}