|
| 1 | +package embeddedpostgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "io/ioutil" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/xi2/xz" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_decompressTarXz(t *testing.T) { |
| 17 | + tempDir, err := ioutil.TempDir("", "temp_tar_test") |
| 18 | + if err != nil { |
| 19 | + panic(err) |
| 20 | + } |
| 21 | + |
| 22 | + archive, cleanUp := createTempXzArchive() |
| 23 | + defer cleanUp() |
| 24 | + |
| 25 | + err = decompressTarXz(defaultTarReader, archive, tempDir) |
| 26 | + |
| 27 | + assert.NoError(t, err) |
| 28 | + |
| 29 | + expectedExtractedFileLocation := filepath.Join(tempDir, "dir1", "dir2", "some_content") |
| 30 | + assert.FileExists(t, expectedExtractedFileLocation) |
| 31 | + |
| 32 | + fileContentBytes, err := ioutil.ReadFile(expectedExtractedFileLocation) |
| 33 | + assert.NoError(t, err) |
| 34 | + |
| 35 | + assert.Equal(t, "b33r is g00d", string(fileContentBytes)) |
| 36 | +} |
| 37 | + |
| 38 | +func Test_decompressTarXz_ErrorWhenFileNotExists(t *testing.T) { |
| 39 | + err := decompressTarXz(defaultTarReader, "/does-not-exist", "/also-fake") |
| 40 | + |
| 41 | + assert.EqualError(t, err, "unable to extract postgres archive /does-not-exist to /also-fake, if running parallel tests, configure RuntimePath to isolate testing directories") |
| 42 | +} |
| 43 | + |
| 44 | +func Test_decompressTarXz_ErrorWhenErrorDuringRead(t *testing.T) { |
| 45 | + tempDir, err := ioutil.TempDir("", "temp_tar_test") |
| 46 | + if err != nil { |
| 47 | + panic(err) |
| 48 | + } |
| 49 | + |
| 50 | + archive, cleanUp := createTempXzArchive() |
| 51 | + defer cleanUp() |
| 52 | + |
| 53 | + err = decompressTarXz(func(reader *xz.Reader) (func() (*tar.Header, error), func() io.Reader) { |
| 54 | + return func() (*tar.Header, error) { |
| 55 | + return nil, errors.New("oh noes") |
| 56 | + }, nil |
| 57 | + }, archive, tempDir) |
| 58 | + |
| 59 | + assert.EqualError(t, err, "unable to extract postgres archive: oh noes") |
| 60 | +} |
| 61 | + |
| 62 | +func Test_decompressTarXz_ErrorWhenFailedToReadFileToCopy(t *testing.T) { |
| 63 | + tempDir, err := ioutil.TempDir("", "temp_tar_test") |
| 64 | + if err != nil { |
| 65 | + panic(err) |
| 66 | + } |
| 67 | + |
| 68 | + archive, cleanUp := createTempXzArchive() |
| 69 | + defer cleanUp() |
| 70 | + |
| 71 | + blockingFile := filepath.Join(tempDir, "blocking") |
| 72 | + |
| 73 | + if err = ioutil.WriteFile(blockingFile, []byte("wazz"), 0000); err != nil { |
| 74 | + panic(err) |
| 75 | + } |
| 76 | + |
| 77 | + fileBlockingExtractTarReader := func(reader *xz.Reader) (func() (*tar.Header, error), func() io.Reader) { |
| 78 | + shouldReadFile := true |
| 79 | + |
| 80 | + return func() (*tar.Header, error) { |
| 81 | + if shouldReadFile { |
| 82 | + shouldReadFile = false |
| 83 | + |
| 84 | + return &tar.Header{ |
| 85 | + Typeflag: tar.TypeReg, |
| 86 | + Name: "blocking", |
| 87 | + }, nil |
| 88 | + } |
| 89 | + |
| 90 | + return nil, io.EOF |
| 91 | + }, func() io.Reader { |
| 92 | + open, _ := os.Open("file_not_exists") |
| 93 | + return open |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + err = decompressTarXz(fileBlockingExtractTarReader, archive, tempDir) |
| 98 | + |
| 99 | + assert.Regexp(t, "^unable to extract postgres archive:.+$", err) |
| 100 | +} |
| 101 | + |
| 102 | +func Test_decompressTarXz_ErrorWhenFileToCopyToNotExists(t *testing.T) { |
| 103 | + tempDir, err := ioutil.TempDir("", "temp_tar_test") |
| 104 | + if err != nil { |
| 105 | + panic(err) |
| 106 | + } |
| 107 | + |
| 108 | + archive, cleanUp := createTempXzArchive() |
| 109 | + defer cleanUp() |
| 110 | + |
| 111 | + fileBlockingExtractTarReader := func(reader *xz.Reader) (func() (*tar.Header, error), func() io.Reader) { |
| 112 | + shouldReadFile := true |
| 113 | + |
| 114 | + return func() (*tar.Header, error) { |
| 115 | + if shouldReadFile { |
| 116 | + shouldReadFile = false |
| 117 | + |
| 118 | + return &tar.Header{ |
| 119 | + Typeflag: tar.TypeReg, |
| 120 | + Name: "some_dir/wazz/dazz/fazz", |
| 121 | + }, nil |
| 122 | + } |
| 123 | + |
| 124 | + return nil, io.EOF |
| 125 | + }, func() io.Reader { |
| 126 | + open, _ := os.Open("file_not_exists") |
| 127 | + return open |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + err = decompressTarXz(fileBlockingExtractTarReader, archive, tempDir) |
| 132 | + |
| 133 | + assert.Regexp(t, "^unable to extract postgres archive:.+$", err) |
| 134 | +} |
0 commit comments