Skip to content

Commit 34b3d8e

Browse files
committed
integration test: image creation and verification in separate functions
1 parent 4962b64 commit 34b3d8e

File tree

4 files changed

+168
-84
lines changed

4 files changed

+168
-84
lines changed

.github/workflows/go.yml

+54-1
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,62 @@ jobs:
4040
- name: Integration test
4141
# The test need sudo to be able to mount/umount
4242
run: sudo go test -v --tags=integration -coverprofile=coverage_integration.txt -covermode=atomic .
43+
env:
44+
ISO_WRITER_TEST_IMAGE: /tmp/writer_test
4345

4446
- name: Upload coverage to Codecov
4547
if: matrix.go == '1.20'
4648
uses: codecov/codecov-action@v2
4749
with:
48-
files: ./coverage_unit.txt,coverage_integration.txt
50+
files: coverage_unit.txt,coverage_integration.txt
51+
52+
test_create:
53+
name: Writer integration test - creation
54+
runs-on: ubuntu-latest
55+
steps:
56+
- name: Set up Go
57+
uses: actions/setup-go@v1
58+
with:
59+
go-version: 1.20
60+
- name: Check out code into the Go module directory
61+
uses: actions/checkout@v1
62+
- name: Create image
63+
run: go test -v --tags=integration,integration_create -coverprofile=coverage_integration_create.txt -covermode=atomic .
64+
env:
65+
ISO_WRITER_TEST_IMAGE: /tmp/writer_test.iso
66+
- name: Save created
67+
uses: actions/upload-artifact@v3
68+
with:
69+
path: /tmp/writer_test.iso
70+
name: writer_test.iso
71+
- name: Upload coverage to Codecov
72+
uses: codecov/codecov-action@v2
73+
with:
74+
files: coverage_integration_create.txt
75+
76+
test_verify:
77+
name: Writer integration test - verification
78+
runs-on: ubuntu-latest
79+
needs: test_create
80+
steps:
81+
- name: Set up Go
82+
uses: actions/setup-go@v1
83+
with:
84+
go-version: 1.20
85+
- name: Check out code into the Go module directory
86+
uses: actions/checkout@v1
87+
- name: Download a single artifact
88+
uses: actions/download-artifact@v3
89+
with:
90+
name: writer_test.iso
91+
- run: ls
92+
- run: ls -la writer_test.iso
93+
- name: Integration test - verify image
94+
# The test need sudo to be able to mount/umount
95+
run: sudo ISO_WRITER_TEST_IMAGE=${ISO_WRITER_TEST_IMAGE} go test -v --tags=integration,integration_verify -coverprofile=coverage_integration_verify.txt -covermode=atomic .
96+
env:
97+
ISO_WRITER_TEST_IMAGE: writer_test.iso
98+
- name: Upload coverage to Codecov
99+
uses: codecov/codecov-action@v2
100+
with:
101+
files: coverage_integration_verify.txt

image_writer_int_creation_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build integration_create
2+
// +build integration_create
3+
4+
package iso9660
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"strings"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestWriterCreate(t *testing.T) {
16+
writerTestImagePath := os.Getenv("ISO_WRITER_TEST_IMAGE")
17+
if !assert.NotEmpty(t, writerTestImagePath) {
18+
t.FailNow()
19+
}
20+
21+
// create a new image writer and prepare for cleanup
22+
w, err := NewWriter()
23+
assert.NoError(t, err)
24+
defer func() {
25+
if cleanupErr := w.Cleanup(); cleanupErr != nil {
26+
t.Fatalf("failed to cleanup writer: %v", cleanupErr)
27+
}
28+
}()
29+
30+
// add 1000 files to thoroughly test descriptor writing over sector bounds
31+
for i := 0; i < 1000; i++ {
32+
path := fmt.Sprintf("firstlevel/dir%d/thirdlevel/file%d.txt", i, i)
33+
err = w.AddFile(strings.NewReader("hrh2309hr320h"), path)
34+
assert.NoError(t, err)
35+
}
36+
37+
// write test image
38+
f, err := os.Create(writerTestImagePath)
39+
assert.NoError(t, err)
40+
41+
err = w.WriteTo(f, "testvolume")
42+
assert.NoError(t, err)
43+
}

image_writer_int_verification_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//go:build integration_verify
2+
// +build integration_verify
3+
4+
package iso9660
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
"testing"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
// Identical to TestWriterAndMountWithAddLocal, except it uses
17+
// AddFile to add files with a constant string in them.
18+
func TestWriterVerify(t *testing.T) {
19+
writerTestImagePath := os.Getenv("ISO_WRITER_TEST_IMAGE")
20+
if !assert.NotEmpty(t, writerTestImagePath) {
21+
t.FailNow()
22+
}
23+
24+
//
25+
// Image mounting
26+
//
27+
28+
// create mount directory
29+
mountDir, err := os.MkdirTemp("", "")
30+
assert.NoError(t, err)
31+
defer func() {
32+
if removeErr := os.RemoveAll(mountDir); removeErr != nil {
33+
t.Fatalf("failed to delete mount directory: %v", removeErr)
34+
}
35+
}()
36+
37+
// execute mount
38+
mountCmd := exec.Command("mount", "-t", "iso9660", writerTestImagePath, mountDir)
39+
output, err := mountCmd.CombinedOutput()
40+
assert.NoError(t, err, "failed to mount the ISO image: %v\n%s", err, string(output))
41+
42+
// make sure that we can see the 1000 files
43+
txtFileCount := 0
44+
walkfn := func(path string, info os.FileInfo, err error) error {
45+
if info.IsDir() {
46+
return nil
47+
}
48+
49+
data, err := os.ReadFile(path)
50+
if err != nil {
51+
return err
52+
}
53+
54+
if string(data) != "hrh2309hr320h" {
55+
return fmt.Errorf("file %q has the wrong contents", path)
56+
}
57+
58+
txtFileCount++
59+
return nil
60+
}
61+
err = filepath.Walk(mountDir, walkfn)
62+
assert.NoError(t, err)
63+
assert.Equal(t, 1000, txtFileCount)
64+
65+
// execute umount
66+
umountCmd := exec.Command("umount", mountDir)
67+
output, err = umountCmd.CombinedOutput()
68+
assert.NoError(t, err, "failed to unmount the ISO image: %v\n%s", err, string(output))
69+
}

image_writer_integration_test.go

+2-83
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build integration
2-
// +build integration
1+
//go:build integration && !integration_create
2+
// +build integration,!integration_create
33

44
package iso9660
55

@@ -8,92 +8,11 @@ import (
88
"os"
99
"os/exec"
1010
"path/filepath"
11-
"strings"
1211
"testing"
1312

1413
"github.com/stretchr/testify/assert"
1514
)
1615

17-
// Identical to TestWriterAndMountWithAddLocal, except it uses
18-
// AddFile to add files with a constant string in them.
19-
func TestWriterAndMount(t *testing.T) {
20-
//
21-
// Image creation
22-
//
23-
24-
// create a new image writer and prepare for cleanup
25-
w, err := NewWriter()
26-
assert.NoError(t, err)
27-
defer func() {
28-
if cleanupErr := w.Cleanup(); cleanupErr != nil {
29-
t.Fatalf("failed to cleanup writer: %v", cleanupErr)
30-
}
31-
}()
32-
33-
// add 1000 files to thoroughly test descriptor writing over sector bounds
34-
for i := 0; i < 1000; i++ {
35-
path := fmt.Sprintf("firstlevel/dir%d/thirdlevel/file%d.txt", i, i)
36-
err = w.AddFile(strings.NewReader("hrh2309hr320h"), path)
37-
assert.NoError(t, err)
38-
}
39-
40-
// write test image
41-
f, err := os.CreateTemp(os.TempDir(), "iso9660_golang_test")
42-
assert.NoError(t, err)
43-
defer os.Remove(f.Name())
44-
45-
err = w.WriteTo(f, "testvolume")
46-
assert.NoError(t, err)
47-
48-
//
49-
// Image mounting
50-
//
51-
52-
// create mount directory
53-
mountDir, err := os.MkdirTemp("", "")
54-
assert.NoError(t, err)
55-
defer func() {
56-
if removeErr := os.RemoveAll(mountDir); removeErr != nil {
57-
t.Fatalf("failed to delete mount directory: %v", removeErr)
58-
}
59-
}()
60-
61-
// execute mount
62-
mountCmd := exec.Command("mount", "-t", "iso9660", f.Name(), mountDir)
63-
output, err := mountCmd.CombinedOutput()
64-
assert.NoError(t, err, "failed to mount the ISO image: %v\n%s", err, string(output))
65-
66-
// make sure that we can see the 1000 files
67-
txtFileCount := 0
68-
walkfn := func(path string, info os.FileInfo, err error) error {
69-
if info.IsDir() {
70-
return nil
71-
}
72-
73-
data, err := os.ReadFile(path)
74-
if err != nil {
75-
return err
76-
}
77-
78-
if string(data) != "hrh2309hr320h" {
79-
return fmt.Errorf("file %q has the wrong contents", path)
80-
}
81-
82-
txtFileCount++
83-
return nil
84-
}
85-
err = filepath.Walk(mountDir, walkfn)
86-
assert.NoError(t, err)
87-
assert.Equal(t, 1000, txtFileCount)
88-
89-
// execute umount
90-
umountCmd := exec.Command("umount", mountDir)
91-
output, err = umountCmd.CombinedOutput()
92-
assert.NoError(t, err, "failed to unmount the ISO image: %v\n%s", err, string(output))
93-
}
94-
95-
// Identical to TestWriterAndMount, except it uses
96-
// AddLocalFile to add an existing file.
9716
func TestWriterAndMountWithAddLocal(t *testing.T) {
9817
sampleData, err := os.ReadFile("/etc/issue")
9918
assert.NoError(t, err)

0 commit comments

Comments
 (0)