github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/state/backups/legacy_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package backups_test 5 6 import ( 7 "archive/tar" 8 "bytes" 9 "compress/gzip" 10 "fmt" 11 "io" 12 "io/ioutil" 13 "os" 14 "path" 15 "path/filepath" 16 "strings" 17 18 jc "github.com/juju/testing/checkers" 19 gc "gopkg.in/check.v1" 20 21 backupsTesting "github.com/juju/juju/state/backups/testing" 22 "github.com/juju/juju/testing" 23 ) 24 25 type LegacySuite struct { 26 testing.BaseSuite 27 } 28 29 type tarContent struct { 30 Name string 31 Body string 32 Nested []tarContent 33 } 34 35 var expectedContents = map[string]string{ 36 "TarDirectoryEmpty": "", 37 "TarDirectoryPopulated": "", 38 "TarDirectoryPopulated/TarSubFile1": "TarSubFile1", 39 "TarDirectoryPopulated/TarSubDirectory": "", 40 "TarFile1": "TarFile1", 41 "TarFile2": "TarFile2", 42 } 43 44 func (s *LegacySuite) createTestFiles(c *gc.C) (string, []string, []tarContent) { 45 var expected []tarContent 46 var tempFiles []string 47 48 rootDir := c.MkDir() 49 50 for name, body := range expectedContents { 51 filename := filepath.Join(rootDir, filepath.FromSlash(name)) 52 53 top := (path.Dir(name) == ".") 54 55 if body == "" { 56 err := os.MkdirAll(filename, os.FileMode(0755)) 57 c.Check(err, jc.ErrorIsNil) 58 } else { 59 if !top { 60 err := os.MkdirAll(filepath.Dir(filename), os.FileMode(0755)) 61 c.Check(err, jc.ErrorIsNil) 62 } 63 file, err := os.Create(filename) 64 c.Assert(err, jc.ErrorIsNil) 65 file.WriteString(body) 66 file.Close() 67 } 68 69 if top { 70 tempFiles = append(tempFiles, filename) 71 } 72 content := tarContent{filepath.ToSlash(filename), body, nil} 73 expected = append(expected, content) 74 } 75 76 return rootDir, tempFiles, expected 77 } 78 79 func readTarFile(c *gc.C, tarFile io.Reader) map[string]string { 80 tr := tar.NewReader(tarFile) 81 contents := make(map[string]string) 82 83 // Iterate through the files in the archive. 84 for { 85 hdr, err := tr.Next() 86 if err == io.EOF { 87 // end of tar archive 88 break 89 } 90 c.Assert(err, jc.ErrorIsNil) 91 buf, err := ioutil.ReadAll(tr) 92 c.Assert(err, jc.ErrorIsNil) 93 contents[hdr.Name] = string(buf) 94 } 95 96 return contents 97 } 98 99 func (s *LegacySuite) checkTarContents( 100 c *gc.C, tarFile io.Reader, allExpected []tarContent, 101 ) { 102 contents := readTarFile(c, tarFile) 103 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)) 108 109 c.Log(fmt.Sprintf("checking for presence of %q on tar file", relPath)) 110 body, found := contents[relPath] 111 if !found { 112 c.Errorf("%q not found", expected.Name) 113 continue 114 } 115 116 if expected.Body != "" { 117 c.Log("Also checking the file contents") 118 c.Check(body, gc.Equals, expected.Body) 119 } 120 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) 125 } 126 } 127 128 if c.Failed() { 129 c.Log("-----------------------") 130 c.Log("expected:") 131 for _, expected := range allExpected { 132 c.Log(fmt.Sprintf("%s -> %q", expected.Name, expected.Body)) 133 } 134 c.Log("got:") 135 for name, body := range contents { 136 if len(body) > 200 { 137 body = body[:200] + "...(truncated)" 138 } 139 c.Log(fmt.Sprintf("%s -> %q", name, body)) 140 } 141 } 142 } 143 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) 147 resetFile(c, file) 148 } 149 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) 154 } 155 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}, 162 } 163 164 tarFile, err := gzip.NewReader(file) 165 c.Assert(err, jc.ErrorIsNil) 166 167 s.checkTarContents(c, tarFile, expected) 168 resetFile(c, file) 169 } 170 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) 176 }