Browse code

Add more tests for pkg/chrootarchive

Fixes issue #11601

Change-Id: Ifc1dbcc59cc4dc581ed43fc8fbe43fbaec4ccad0
Signed-off-by: Yestin Sun <sunyi0804@gmail.com>

Yestin Sun authored on 2015/03/25 10:20:20
Showing 1 changed files
... ...
@@ -1,9 +1,12 @@
1 1
 package chrootarchive
2 2
 
3 3
 import (
4
+	"bytes"
5
+	"fmt"
4 6
 	"io"
5 7
 	"io/ioutil"
6 8
 	"os"
9
+	"path"
7 10
 	"path/filepath"
8 11
 	"testing"
9 12
 	"time"
... ...
@@ -45,6 +48,148 @@ func TestChrootTarUntar(t *testing.T) {
45 45
 	}
46 46
 }
47 47
 
48
+func TestChrootUntarEmptyArchive(t *testing.T) {
49
+	tmpdir, err := ioutil.TempDir("", "docker-TestChrootUntarEmptyArchive")
50
+	if err != nil {
51
+		t.Fatal(err)
52
+	}
53
+	defer os.RemoveAll(tmpdir)
54
+	if err := Untar(nil, tmpdir, nil); err == nil {
55
+		t.Fatal("expected error on empty archive")
56
+	}
57
+}
58
+
59
+func prepareSourceDirectory(numberOfFiles int, targetPath string, makeLinks bool) (int, error) {
60
+	fileData := []byte("fooo")
61
+	for n := 0; n < numberOfFiles; n++ {
62
+		fileName := fmt.Sprintf("file-%d", n)
63
+		if err := ioutil.WriteFile(path.Join(targetPath, fileName), fileData, 0700); err != nil {
64
+			return 0, err
65
+		}
66
+		if makeLinks {
67
+			if err := os.Link(path.Join(targetPath, fileName), path.Join(targetPath, fileName+"-link")); err != nil {
68
+				return 0, err
69
+			}
70
+		}
71
+	}
72
+	totalSize := numberOfFiles * len(fileData)
73
+	return totalSize, nil
74
+}
75
+
76
+func TestChrootTarUntarWithSoftLink(t *testing.T) {
77
+	tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntarWithSoftLink")
78
+	if err != nil {
79
+		t.Fatal(err)
80
+	}
81
+	defer os.RemoveAll(tmpdir)
82
+	src := filepath.Join(tmpdir, "src")
83
+	if err := os.MkdirAll(src, 0700); err != nil {
84
+		t.Fatal(err)
85
+	}
86
+	if _, err := prepareSourceDirectory(10, src, true); err != nil {
87
+		t.Fatal(err)
88
+	}
89
+	dest := filepath.Join(tmpdir, "dest")
90
+	if err := TarUntar(src, dest); err != nil {
91
+		t.Fatal(err)
92
+	}
93
+}
94
+
95
+func TestChrootCopyWithTar(t *testing.T) {
96
+	tmpdir, err := ioutil.TempDir("", "docker-TestChrootCopyWithTar")
97
+	if err != nil {
98
+		t.Fatal(err)
99
+	}
100
+	defer os.RemoveAll(tmpdir)
101
+	src := filepath.Join(tmpdir, "src")
102
+	if err := os.MkdirAll(src, 0700); err != nil {
103
+		t.Fatal(err)
104
+	}
105
+	if _, err := prepareSourceDirectory(10, src, true); err != nil {
106
+		t.Fatal(err)
107
+	}
108
+	dest := filepath.Join(tmpdir, "dest")
109
+	// Copy directory
110
+	if err := CopyWithTar(src, dest); err != nil {
111
+		t.Fatal(err)
112
+	}
113
+	// Copy file
114
+	srcfile := filepath.Join(src, "file-1")
115
+	if err := CopyWithTar(srcfile, dest); err != nil {
116
+		t.Fatal(err)
117
+	}
118
+	// Copy symbolic link
119
+	linkfile := filepath.Join(src, "file-1-link")
120
+	if err := CopyWithTar(linkfile, dest); err != nil {
121
+		t.Fatal(err)
122
+	}
123
+}
124
+
125
+func TestChrootCopyFileWithTar(t *testing.T) {
126
+	tmpdir, err := ioutil.TempDir("", "docker-TestChrootCopyFileWithTar")
127
+	if err != nil {
128
+		t.Fatal(err)
129
+	}
130
+	defer os.RemoveAll(tmpdir)
131
+	src := filepath.Join(tmpdir, "src")
132
+	if err := os.MkdirAll(src, 0700); err != nil {
133
+		t.Fatal(err)
134
+	}
135
+	if _, err := prepareSourceDirectory(10, src, true); err != nil {
136
+		t.Fatal(err)
137
+	}
138
+	dest := filepath.Join(tmpdir, "dest")
139
+	// Copy directory
140
+	if err := CopyFileWithTar(src, dest); err == nil {
141
+		t.Fatal("Expected error on copying directory")
142
+	}
143
+	// Copy file
144
+	srcfile := filepath.Join(src, "file-1")
145
+	if err := CopyFileWithTar(srcfile, dest); err != nil {
146
+		t.Fatal(err)
147
+	}
148
+	// Copy symbolic link
149
+	linkfile := filepath.Join(src, "file-1-link")
150
+	if err := CopyFileWithTar(linkfile, dest); err != nil {
151
+		t.Fatal(err)
152
+	}
153
+}
154
+
155
+func TestChrootUntarPath(t *testing.T) {
156
+	tmpdir, err := ioutil.TempDir("", "docker-TestChrootUntarPath")
157
+	if err != nil {
158
+		t.Fatal(err)
159
+	}
160
+	defer os.RemoveAll(tmpdir)
161
+	src := filepath.Join(tmpdir, "src")
162
+	if err := os.MkdirAll(src, 0700); err != nil {
163
+		t.Fatal(err)
164
+	}
165
+	if _, err := prepareSourceDirectory(10, src, true); err != nil {
166
+		t.Fatal(err)
167
+	}
168
+	dest := filepath.Join(tmpdir, "dest")
169
+	// Untar a directory
170
+	if err := UntarPath(src, dest); err == nil {
171
+		t.Fatal("Expected error on untaring a directory")
172
+	}
173
+
174
+	// Untar a tar file
175
+	stream, err := archive.Tar(src, archive.Uncompressed)
176
+	if err != nil {
177
+		t.Fatal(err)
178
+	}
179
+	buf := new(bytes.Buffer)
180
+	buf.ReadFrom(stream)
181
+	tarfile := filepath.Join(tmpdir, "src.tar")
182
+	if err := ioutil.WriteFile(tarfile, buf.Bytes(), 0644); err != nil {
183
+		t.Fatal(err)
184
+	}
185
+	if err := UntarPath(tarfile, dest); err != nil {
186
+		t.Fatal(err)
187
+	}
188
+}
189
+
48 190
 type slowEmptyTarReader struct {
49 191
 	size      int
50 192
 	offset    int