github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/archive/tar_gz_unpack_command_test.go (about) 1 package archive_test 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 . "github.com/evergreen-ci/evergreen/plugin/builtin/archive" 9 "github.com/evergreen-ci/evergreen/plugin/plugintest" 10 "github.com/evergreen-ci/evergreen/testutil" 11 "github.com/evergreen-ci/evergreen/util" 12 . "github.com/smartystreets/goconvey/convey" 13 ) 14 15 func TestTarGzUnpackParseParams(t *testing.T) { 16 17 Convey("With a targz unpack command", t, func() { 18 19 var cmd *TarGzUnpackCommand 20 21 Convey("when parsing params into the command", func() { 22 23 cmd = &TarGzUnpackCommand{} 24 25 Convey("a missing source should cause an error", func() { 26 27 params := map[string]interface{}{ 28 "dest_dir": "dest", 29 } 30 So(cmd.ParseParams(params), ShouldNotBeNil) 31 32 }) 33 34 Convey("a missing dest_dir should cause an error", func() { 35 36 params := map[string]interface{}{ 37 "source": "s", 38 } 39 So(cmd.ParseParams(params), ShouldNotBeNil) 40 41 }) 42 43 Convey("a valid set of params should be parsed into the"+ 44 " corresponding fields of the targz pack command", func() { 45 46 params := map[string]interface{}{ 47 "source": "a.tgz", 48 "dest_dir": "dest", 49 } 50 So(cmd.ParseParams(params), ShouldBeNil) 51 52 So(cmd.Source, ShouldEqual, params["source"]) 53 So(cmd.DestDir, ShouldEqual, params["dest_dir"]) 54 55 }) 56 57 }) 58 }) 59 } 60 61 func TestTarGzCommandUnpackArchive(t *testing.T) { 62 63 Convey("With a targz unpack command", t, func() { 64 testDataDir := filepath.Join(testutil.GetDirectoryOfFile(), "testdata") 65 66 Convey("when unpacking an archive", func() { 67 Convey("the archive's contents should be expanded into the"+ 68 " specified target directory", func() { 69 70 target := filepath.Join(testDataDir, "target.tgz") 71 output := filepath.Join(testDataDir, "output") 72 73 testutil.HandleTestingErr(os.RemoveAll(target), t, 74 "Error removing tgz file") 75 testutil.HandleTestingErr(os.RemoveAll(output), t, 76 "Error removing output directory") 77 78 // create the output directory 79 testutil.HandleTestingErr(os.MkdirAll(output, 0755), t, 80 "Error creating output directory") 81 82 // use the tar gz pack command to create a tarball 83 tarPackCmd := &TarGzPackCommand{} 84 tarPackParams := map[string]interface{}{ 85 "target": target, 86 "source_dir": testDataDir, 87 "include": []string{"targz_me/dir1/**"}, 88 "exclude_files": []string{}, 89 } 90 91 So(tarPackCmd.ParseParams(tarPackParams), ShouldBeNil) 92 numFound, err := tarPackCmd.BuildArchive(&plugintest.MockLogger{}) 93 So(err, ShouldBeNil) 94 So(numFound, ShouldEqual, 2) 95 96 // make sure it was built 97 exists, err := util.FileExists(target) 98 testutil.HandleTestingErr(err, t, "Error checking for file"+ 99 " existence") 100 So(exists, ShouldBeTrue) 101 102 // now, use a tar gz unpacking command to untar the tarball 103 tarUnpackCmd := &TarGzUnpackCommand{} 104 tarUnpackParams := map[string]interface{}{ 105 "source": target, 106 "dest_dir": output, 107 } 108 109 So(tarUnpackCmd.ParseParams(tarUnpackParams), ShouldBeNil) 110 So(tarUnpackCmd.UnpackArchive(), ShouldBeNil) 111 112 // make sure the tarball was unpacked successfully 113 exists, err = util.FileExists( 114 filepath.Join(output, "targz_me/dir1/dir2/test.pdb")) 115 testutil.HandleTestingErr(err, t, "Error checking file existence") 116 So(exists, ShouldBeTrue) 117 exists, err = util.FileExists( 118 filepath.Join(output, "targz_me/dir1/dir2/testfile.txt")) 119 testutil.HandleTestingErr(err, t, "Error checking file existence") 120 So(exists, ShouldBeTrue) 121 122 }) 123 124 }) 125 }) 126 }