github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/command/scp_command_test.go (about)

     1  // +build linux freebsd solaris darwin
     2  
     3  // the syscall.Umask call in this test is unix only. This test has
     4  // never run on non-linux platforms, but perhaps it should, in the future
     5  
     6  package command
     7  
     8  import (
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"syscall"
    13  	"testing"
    14  
    15  	"github.com/evergreen-ci/evergreen"
    16  	"github.com/evergreen-ci/evergreen/util"
    17  	. "github.com/smartystreets/goconvey/convey"
    18  )
    19  
    20  func TestScpCommand(t *testing.T) {
    21  	t.Skip("skipping because local testing ssh configuration is not implemented")
    22  
    23  	Convey("With files to scp", t, func() {
    24  
    25  		// the local files and target directory for scping
    26  		evgHome := evergreen.FindEvergreenHome()
    27  
    28  		tmpBase := filepath.Join(evgHome, "command/testdata/tmp")
    29  		fileToScp := filepath.Join(tmpBase, "copy_me_please.txt")
    30  		directoryToScp := filepath.Join(tmpBase, "copy_my_children_please")
    31  		nestedFileToScp := filepath.Join(directoryToScp,
    32  			"copy_me_too_please.txt")
    33  		targetDirectory := filepath.Join(tmpBase, "feed_me_files")
    34  
    35  		// remove the files and directories, if they exist (start clean)
    36  		exists, err := util.FileExists(tmpBase)
    37  		So(err, ShouldBeNil)
    38  		if exists {
    39  			So(os.RemoveAll(tmpBase), ShouldBeNil)
    40  		}
    41  		So(os.MkdirAll(tmpBase, 0777), ShouldBeNil)
    42  
    43  		// prevent permissions issues
    44  		syscall.Umask(0000)
    45  
    46  		// create the files / directories to be used
    47  		So(ioutil.WriteFile(fileToScp, []byte("hello"), 0777), ShouldBeNil)
    48  		So(os.Mkdir(directoryToScp, 0777), ShouldBeNil)
    49  		So(ioutil.WriteFile(nestedFileToScp, []byte("hi"), 0777), ShouldBeNil)
    50  		So(os.Mkdir(targetDirectory, 0777), ShouldBeNil)
    51  
    52  		Convey("when running scp commands", func() {
    53  
    54  			Convey("copying files should work in both directions (local to"+
    55  				" remote and remote to local)", func() {
    56  
    57  				// scp the file from local to remote
    58  				scpCommand := &ScpCommand{
    59  					Source:         fileToScp,
    60  					Dest:           targetDirectory,
    61  					Stdout:         ioutil.Discard,
    62  					Stderr:         ioutil.Discard,
    63  					RemoteHostName: TestRemote,
    64  					User:           TestRemoteUser,
    65  					Options:        []string{"-i", TestRemoteKey},
    66  					SourceIsRemote: false,
    67  				}
    68  				So(scpCommand.Run(), ShouldBeNil)
    69  
    70  				// make sure the file was scp-ed over
    71  				newFileContents, err := ioutil.ReadFile(
    72  					filepath.Join(targetDirectory, "copy_me_please.txt"))
    73  				So(err, ShouldBeNil)
    74  				So(newFileContents, ShouldResemble, []byte("hello"))
    75  
    76  				// remove the file
    77  				So(os.Remove(filepath.Join(targetDirectory,
    78  					"copy_me_please.txt")), ShouldBeNil)
    79  
    80  				// scp the file from remote to local
    81  				scpCommand = &ScpCommand{
    82  					Source:         fileToScp,
    83  					Dest:           targetDirectory,
    84  					Stdout:         ioutil.Discard,
    85  					Stderr:         ioutil.Discard,
    86  					RemoteHostName: TestRemote,
    87  					User:           TestRemoteUser,
    88  					Options:        []string{"-i", TestRemoteKey},
    89  					SourceIsRemote: true,
    90  				}
    91  				So(scpCommand.Run(), ShouldBeNil)
    92  
    93  				// make sure the file was scp-ed over
    94  				newFileContents, err = ioutil.ReadFile(
    95  					filepath.Join(targetDirectory, "copy_me_please.txt"))
    96  				So(err, ShouldBeNil)
    97  				So(newFileContents, ShouldResemble, []byte("hello"))
    98  
    99  			})
   100  
   101  			Convey("additional scp options should be passed correctly to the"+
   102  				" command", func() {
   103  
   104  				// scp recursively, using the -r flag
   105  				scpCommand := &ScpCommand{
   106  					Source:         directoryToScp,
   107  					Dest:           targetDirectory,
   108  					Stdout:         ioutil.Discard,
   109  					Stderr:         ioutil.Discard,
   110  					RemoteHostName: TestRemote,
   111  					User:           TestRemoteUser,
   112  					Options:        []string{"-i", TestRemoteKey, "-r"},
   113  					SourceIsRemote: false,
   114  				}
   115  				So(scpCommand.Run(), ShouldBeNil)
   116  
   117  				// make sure the entire directory was scp-ed over
   118  				nestedFileContents, err := ioutil.ReadFile(
   119  					filepath.Join(targetDirectory, "copy_my_children_please",
   120  						"copy_me_too_please.txt"))
   121  				So(err, ShouldBeNil)
   122  				So(nestedFileContents, ShouldResemble, []byte("hi"))
   123  			})
   124  
   125  		})
   126  
   127  	})
   128  }