github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/archive/artifacts_test.go (about) 1 package archive 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 "testing" 9 10 "github.com/evergreen-ci/evergreen/testutil" 11 "github.com/evergreen-ci/evergreen/util" 12 "github.com/mongodb/grip/send" 13 "github.com/mongodb/grip/slogger" 14 . "github.com/smartystreets/goconvey/convey" 15 ) 16 17 var logger *slogger.Logger 18 19 func init() { 20 logger = &slogger.Logger{ 21 Name: "test", 22 Appenders: []send.Sender{slogger.StdOutAppender()}, 23 } 24 } 25 26 func TestArchiveExtract(t *testing.T) { 27 Convey("After extracting a tarball", t, func() { 28 testDir := testutil.GetDirectoryOfFile() 29 //Remove the test output dir, in case it was left over from prior test 30 err := os.RemoveAll(filepath.Join(testDir, "testdata", "artifacts_test")) 31 testutil.HandleTestingErr(err, t, "Couldn't remove test dir") 32 33 f, gz, tarReader, err := TarGzReader(filepath.Join(testDir, "testdata", "artifacts.tar.gz")) 34 testutil.HandleTestingErr(err, t, "Couldn't open test tarball") 35 defer f.Close() 36 defer gz.Close() 37 38 err = Extract(tarReader, filepath.Join(testDir, "testdata", "artifacts_test")) 39 So(err, ShouldBeNil) 40 41 Convey("extracted data should match the archive contents", func() { 42 f, err := os.Open(filepath.Join(testDir, "testdata", "artifacts_test", "artifacts", "dir1", "dir2", "testfile.txt")) 43 So(err, ShouldBeNil) 44 defer f.Close() 45 data, err := ioutil.ReadAll(f) 46 So(err, ShouldBeNil) 47 So(string(data), ShouldEqual, "test\n") 48 }) 49 }) 50 } 51 52 func TestMakeArchive(t *testing.T) { 53 Convey("Making an archive should not return an error", t, func() { 54 testDir := testutil.GetDirectoryOfFile() 55 56 err := os.RemoveAll(filepath.Join(testDir, "testdata", "artifacts_out.tar.gz")) 57 testutil.HandleTestingErr(err, t, "Couldn't delete test tarball") 58 59 f, gz, tarWriter, err := TarGzWriter(filepath.Join(testDir, "testdata", "artifacts_out.tar.gz")) 60 testutil.HandleTestingErr(err, t, "Couldn't open test tarball") 61 defer f.Close() 62 defer gz.Close() 63 defer tarWriter.Close() 64 includes := []string{"artifacts/dir1/**"} 65 excludes := []string{"*.pdb"} 66 _, err = BuildArchive(tarWriter, filepath.Join(testDir, "testdata", "artifacts_in"), includes, excludes, logger) 67 So(err, ShouldBeNil) 68 }) 69 } 70 71 func TestArchiveRoundTrip(t *testing.T) { 72 Convey("After building archive with include/exclude filters", t, func() { 73 testDir := testutil.GetDirectoryOfFile() 74 75 err := os.RemoveAll(filepath.Join(testDir, "testdata", "artifacts_out.tar.gz")) 76 testutil.HandleTestingErr(err, t, "Couldn't remove test tarball") 77 78 err = os.RemoveAll(filepath.Join(testDir, "testdata", "artifacts_out")) 79 testutil.HandleTestingErr(err, t, "Couldn't remove test tarball") 80 81 f, gz, tarWriter, err := TarGzWriter(filepath.Join(testDir, "testdata", "artifacts_out.tar.gz")) 82 testutil.HandleTestingErr(err, t, "Couldn't open test tarball") 83 includes := []string{"dir1/**"} 84 excludes := []string{"*.pdb"} 85 var found int 86 found, err = BuildArchive(tarWriter, filepath.Join(testDir, "testdata", "artifacts_in"), includes, excludes, logger) 87 So(err, ShouldBeNil) 88 So(found, ShouldEqual, 2) 89 So(tarWriter.Close(), ShouldBeNil) 90 So(gz.Close(), ShouldBeNil) 91 So(f.Close(), ShouldBeNil) 92 93 f2, gz2, tarReader, err := TarGzReader(filepath.Join(testDir, "testdata", "artifacts_out.tar.gz")) 94 testutil.HandleTestingErr(err, t, "Couldn't open test tarball") 95 err = Extract(tarReader, filepath.Join(testDir, "testdata", "artifacts_out")) 96 defer f2.Close() 97 defer gz2.Close() 98 So(err, ShouldBeNil) 99 exists, err := util.FileExists(filepath.Join(testDir, "testdata", "artifacts_out")) 100 So(err, ShouldBeNil) 101 So(exists, ShouldBeTrue) 102 exists, err = util.FileExists(filepath.Join(testDir, "testdata", "artifacts_out", "dir1", "dir2", "test.pdb")) 103 So(err, ShouldBeNil) 104 So(exists, ShouldBeFalse) 105 106 // Dereference symlinks 107 exists, err = util.FileExists(filepath.Join(testDir, "testdata", "artifacts_out", "dir1", "dir2", "my_symlink.txt")) 108 So(err, ShouldBeNil) 109 So(exists, ShouldBeTrue) 110 contents, err := ioutil.ReadFile(filepath.Join(testDir, "testdata", "artifacts_out", "dir1", "dir2", "my_symlink.txt")) 111 So(err, ShouldBeNil) 112 So(strings.Trim(string(contents), "\r\n\t "), ShouldEqual, "Hello, World") 113 }) 114 }