github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/archive/tar_gz_pack_command_test.go (about)

     1  package archive_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/evergreen-ci/evergreen/command"
    10  	. "github.com/evergreen-ci/evergreen/plugin/builtin/archive"
    11  	"github.com/evergreen-ci/evergreen/plugin/plugintest"
    12  	"github.com/evergreen-ci/evergreen/testutil"
    13  	"github.com/evergreen-ci/evergreen/util"
    14  	. "github.com/smartystreets/goconvey/convey"
    15  )
    16  
    17  func TestTarGzPackParseParams(t *testing.T) {
    18  	Convey("With a targz pack command", t, func() {
    19  		var cmd *TarGzPackCommand
    20  
    21  		Convey("when parsing params into the command", func() {
    22  
    23  			cmd = &TarGzPackCommand{}
    24  
    25  			Convey("a missing target should cause an error", func() {
    26  
    27  				params := map[string]interface{}{
    28  					"source_dir": "s",
    29  					"include":    []string{"i"},
    30  				}
    31  				So(cmd.ParseParams(params), ShouldNotBeNil)
    32  
    33  			})
    34  
    35  			Convey("a missing source_dir should cause an error", func() {
    36  
    37  				params := map[string]interface{}{
    38  					"target":  "t",
    39  					"include": []string{"i"},
    40  				}
    41  				So(cmd.ParseParams(params), ShouldNotBeNil)
    42  
    43  			})
    44  
    45  			Convey("an empty include field should cause an error", func() {
    46  
    47  				params := map[string]interface{}{
    48  					"target":     "t",
    49  					"source_dir": "s",
    50  				}
    51  				So(cmd.ParseParams(params), ShouldNotBeNil)
    52  
    53  			})
    54  
    55  			Convey("a valid set of params should be parsed into the"+
    56  				" corresponding fields of the targz pack command", func() {
    57  
    58  				params := map[string]interface{}{
    59  					"target":        "t",
    60  					"source_dir":    "s",
    61  					"include":       []string{"i", "j"},
    62  					"exclude_files": []string{"e", "f"},
    63  				}
    64  				So(cmd.ParseParams(params), ShouldBeNil)
    65  
    66  				So(cmd.Target, ShouldEqual, params["target"])
    67  				So(cmd.SourceDir, ShouldEqual, params["source_dir"])
    68  				So(cmd.Include, ShouldResemble, params["include"])
    69  				So(cmd.ExcludeFiles, ShouldResemble, params["exclude_files"])
    70  
    71  			})
    72  
    73  		})
    74  	})
    75  }
    76  
    77  func TestTarGzCommandBuildArchive(t *testing.T) {
    78  
    79  	Convey("With a targz pack command", t, func() {
    80  		testDataDir := filepath.Join(testutil.GetDirectoryOfFile(), "testdata")
    81  		var cmd *TarGzPackCommand
    82  
    83  		Convey("when building an archive", func() {
    84  
    85  			cmd = &TarGzPackCommand{}
    86  
    87  			Convey("the correct files should be included and excluded", func() {
    88  
    89  				target := filepath.Join(testDataDir, "target.tgz")
    90  				outputDir := filepath.Join(testDataDir, "output")
    91  
    92  				testutil.HandleTestingErr(os.RemoveAll(target), t, "Error removing tgz file")
    93  				testutil.HandleTestingErr(os.RemoveAll(outputDir), t, "Error removing output dir")
    94  
    95  				params := map[string]interface{}{
    96  					"target":        target,
    97  					"source_dir":    testDataDir,
    98  					"include":       []string{"targz_me/dir1/**"},
    99  					"exclude_files": []string{"*.pdb"},
   100  				}
   101  
   102  				So(cmd.ParseParams(params), ShouldBeNil)
   103  				numFound, err := cmd.BuildArchive(&plugintest.MockLogger{})
   104  				So(err, ShouldBeNil)
   105  				So(numFound, ShouldEqual, 1)
   106  
   107  				exists, err := util.FileExists(target)
   108  				So(err, ShouldBeNil)
   109  				So(exists, ShouldBeTrue)
   110  
   111  				// untar the file
   112  				So(os.MkdirAll(outputDir, 0755), ShouldBeNil)
   113  				untarCmd := &command.LocalCommand{
   114  					CmdString:        "tar xvf ../target.tgz",
   115  					WorkingDirectory: outputDir,
   116  					Stdout:           ioutil.Discard,
   117  					Stderr:           ioutil.Discard,
   118  				}
   119  				So(untarCmd.Run(), ShouldBeNil)
   120  
   121  				// make sure that the correct files were included
   122  				exists, err = util.FileExists(
   123  					filepath.Join(outputDir, "targz_me/dir1/dir2/testfile.txt"))
   124  				So(err, ShouldBeNil)
   125  				So(exists, ShouldBeTrue)
   126  
   127  				exists, err = util.FileExists(
   128  					filepath.Join(outputDir, "targz_me/dir1/dir2/test.pdb"),
   129  				)
   130  				So(err, ShouldBeNil)
   131  				So(exists, ShouldBeFalse)
   132  
   133  			})
   134  
   135  		})
   136  	})
   137  }