1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
18
jc "github.com/juju/testing/checkers"
19
gc "gopkg.in/check.v1"
21
backupsTesting "github.com/juju/juju/state/backups/testing"
22
"github.com/juju/juju/testing"
25
type LegacySuite struct {
29
type tarContent struct {
35
var expectedContents = map[string]string{
36
"TarDirectoryEmpty": "",
37
"TarDirectoryPopulated": "",
38
"TarDirectoryPopulated/TarSubFile1": "TarSubFile1",
39
"TarDirectoryPopulated/TarSubDirectory": "",
40
"TarFile1": "TarFile1",
41
"TarFile2": "TarFile2",
44
func (s *LegacySuite) createTestFiles(c *gc.C) (string, []string, []tarContent) {
45
var expected []tarContent
46
var tempFiles []string
50
for name, body := range expectedContents {
51
filename := filepath.Join(rootDir, filepath.FromSlash(name))
53
top := (path.Dir(name) == ".")
56
err := os.MkdirAll(filename, os.FileMode(0755))
57
c.Check(err, jc.ErrorIsNil)
60
err := os.MkdirAll(filepath.Dir(filename), os.FileMode(0755))
61
c.Check(err, jc.ErrorIsNil)
63
file, err := os.Create(filename)
64
c.Assert(err, jc.ErrorIsNil)
65
file.WriteString(body)
70
tempFiles = append(tempFiles, filename)
72
content := tarContent{filepath.ToSlash(filename), body, nil}
73
expected = append(expected, content)
76
return rootDir, tempFiles, expected
79
func readTarFile(c *gc.C, tarFile io.Reader) map[string]string {
80
tr := tar.NewReader(tarFile)
81
contents := make(map[string]string)
83
// Iterate through the files in the archive.
90
c.Assert(err, jc.ErrorIsNil)
91
buf, err := ioutil.ReadAll(tr)
92
c.Assert(err, jc.ErrorIsNil)
93
contents[hdr.Name] = string(buf)
99
func (s *LegacySuite) checkTarContents(
100
c *gc.C, tarFile io.Reader, allExpected []tarContent,
102
contents := readTarFile(c, tarFile)
104
// Check that the expected entries are there.
105
// XXX Check for unexpected entries.
106
for _, expected := range allExpected {
107
relPath := strings.TrimPrefix(expected.Name, string(os.PathSeparator))
109
c.Log(fmt.Sprintf("checking for presence of %q on tar file", relPath))
110
body, found := contents[relPath]
112
c.Errorf("%q not found", expected.Name)
116
if expected.Body != "" {
117
c.Log("Also checking the file contents")
118
c.Check(body, gc.Equals, expected.Body)
121
if expected.Nested != nil {
122
c.Log("Also checking the nested tar file")
123
nestedFile := bytes.NewBufferString(body)
124
s.checkTarContents(c, nestedFile, expected.Nested)
129
c.Log("-----------------------")
131
for _, expected := range allExpected {
132
c.Log(fmt.Sprintf("%s -> %q", expected.Name, expected.Body))
135
for name, body := range contents {
137
body = body[:200] + "...(truncated)"
139
c.Log(fmt.Sprintf("%s -> %q", name, body))
144
func (s *LegacySuite) checkChecksum(c *gc.C, file *os.File, checksum string) {
145
fileShaSum := backupsTesting.SHA1SumFile(c, file)
146
c.Check(fileShaSum, gc.Equals, checksum)
150
func (s *LegacySuite) checkSize(c *gc.C, file *os.File, size int64) {
151
stat, err := file.Stat()
152
c.Assert(err, jc.ErrorIsNil)
153
c.Check(stat.Size(), gc.Equals, size)
156
func (s *LegacySuite) checkArchive(c *gc.C, file *os.File, bundle []tarContent) {
157
expected := []tarContent{
158
{"juju-backup", "", nil},
159
{"juju-backup/dump", "", nil},
160
{"juju-backup/root.tar", "", bundle},
161
{"juju-backup/metadata.json", "", nil},
164
tarFile, err := gzip.NewReader(file)
165
c.Assert(err, jc.ErrorIsNil)
167
s.checkTarContents(c, tarFile, expected)
171
func resetFile(c *gc.C, reader io.Reader) {
172
file, ok := reader.(*os.File)
173
c.Assert(ok, jc.IsTrue)
174
_, err := file.Seek(0, os.SEEK_SET)
175
c.Assert(err, jc.ErrorIsNil)