tar_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "archive/tar"
  16. "compress/gzip"
  17. "fmt"
  18. "io"
  19. "io/ioutil"
  20. "os"
  21. "path/filepath"
  22. "reflect"
  23. "strings"
  24. "testing"
  25. "github.com/pkg/errors"
  26. )
  27. func TestTar(t *testing.T) {
  28. tmp, err := ioutil.TempDir("", "testtar")
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. defer os.Remove(tmp)
  33. if err := os.Mkdir(filepath.Join(tmp, "subdir"), os.FileMode(0755)); err != nil {
  34. t.Fatal(err)
  35. }
  36. if err := ioutil.WriteFile(filepath.Join(tmp, "file1"), []byte(`file1 data`), os.FileMode(0644)); err != nil {
  37. t.Fatal(err)
  38. }
  39. if err := ioutil.WriteFile(filepath.Join(tmp, "file2"), []byte(`file2 data`), os.FileMode(0644)); err != nil {
  40. t.Fatal(err)
  41. }
  42. if err := ioutil.WriteFile(filepath.Join(tmp, "subdir", "file4"), []byte(`file4 data`), os.FileMode(0644)); err != nil {
  43. t.Fatal(err)
  44. }
  45. testCases := []struct {
  46. desc string
  47. dir string
  48. outpath string
  49. expectErr string
  50. expect map[string]string
  51. }{
  52. {
  53. desc: "Contents preserved and no self-reference",
  54. dir: tmp,
  55. outpath: filepath.Join(tmp, "out.tar.gz"),
  56. expect: map[string]string{
  57. "file1": "file1 data",
  58. "file2": "file2 data",
  59. "subdir/file4": "file4 data",
  60. },
  61. }, {
  62. desc: "Errors if directory does not exist",
  63. dir: filepath.Join(tmp, "does-not-exist"),
  64. outpath: filepath.Join(tmp, "out.tar.gz"),
  65. expectErr: "tar unable to stat directory",
  66. },
  67. }
  68. for _, tc := range testCases {
  69. t.Run(tc.desc, func(t *testing.T) {
  70. err := tarDir(tc.dir, tc.outpath)
  71. if err == nil {
  72. defer os.Remove(tc.outpath)
  73. }
  74. switch {
  75. case err != nil && len(tc.expectErr) == 0:
  76. t.Fatalf("Expected nil error but got %q", err)
  77. case err != nil && len(tc.expectErr) > 0:
  78. if !strings.Contains(fmt.Sprint(err), tc.expectErr) {
  79. t.Errorf("Expected error \n\t%q\nbut got\n\t%q", tc.expectErr, err)
  80. }
  81. return
  82. case err == nil && len(tc.expectErr) > 0:
  83. t.Fatalf("Expected error %q but got nil", tc.expectErr)
  84. default:
  85. // No error
  86. }
  87. data, err := readAllTar(tc.outpath)
  88. if err != nil {
  89. t.Fatalf("Failed to read tarball: %v", err)
  90. }
  91. if !reflect.DeepEqual(data, tc.expect) {
  92. t.Errorf("Expected data %v but got %v", tc.expect, data)
  93. }
  94. })
  95. }
  96. }
  97. // readAllTar walks all of the files in the archive. It returns a map
  98. // of filenames and their contents and any error encountered.
  99. func readAllTar(tarPath string) (map[string]string, error) {
  100. tarPath, err := filepath.Abs(tarPath)
  101. if err != nil {
  102. return nil, err
  103. }
  104. fileReader, err := os.Open(tarPath)
  105. if err != nil {
  106. return nil, err
  107. }
  108. defer fileReader.Close()
  109. gzStream, err := gzip.NewReader(fileReader)
  110. if err != nil {
  111. return nil, errors.Wrap(err, "couldn't uncompress reader")
  112. }
  113. defer gzStream.Close()
  114. // Open and iterate through the files in the archive.
  115. tr := tar.NewReader(gzStream)
  116. fileData := map[string]string{}
  117. for {
  118. hdr, err := tr.Next()
  119. if err == io.EOF {
  120. break // End of archive
  121. }
  122. if err != nil {
  123. return nil, err
  124. }
  125. b, err := ioutil.ReadAll(tr)
  126. if err != nil {
  127. return nil, err
  128. }
  129. fileData[filepath.ToSlash(hdr.Name)] = string(b)
  130. }
  131. return fileData, nil
  132. }