github.com/unclejack/drone@v0.2.1-0.20140918182345-831b034aa33b/pkg/plugin/deploy/ssh_test.go (about)

     1  package deploy
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/drone/drone/pkg/build/buildfile"
     8  
     9  	"launchpad.net/goyaml"
    10  )
    11  
    12  // emulate Build struct
    13  type build struct {
    14  	Deploy *Deploy `yaml:"deploy,omitempty"`
    15  }
    16  
    17  var sampleYml = `
    18  deploy:
    19    ssh:
    20      target: user@test.example.com
    21      cmd: /opt/bin/redeploy.sh
    22  `
    23  
    24  var sampleYml1 = `
    25  deploy:
    26    ssh:
    27      target: user@test.example.com:/srv/app/location 2212
    28      artifacts:
    29        - build.result
    30      cmd: /opt/bin/redeploy.sh
    31  `
    32  
    33  var sampleYml2 = `
    34  deploy:
    35    ssh:
    36      target: user@test.example.com:/srv/app/location 2212
    37      artifacts:
    38        - build.result
    39        - config/file
    40      cmd: /opt/bin/redeploy.sh
    41  `
    42  
    43  var sampleYml3 = `
    44  deploy:
    45    ssh:
    46      target: user@test.example.com:/srv/app/location 2212
    47      artifacts:
    48        - GITARCHIVE
    49      cmd: /opt/bin/redeploy.sh
    50  `
    51  
    52  func setUp(input string) (string, error) {
    53  	var buildStruct build
    54  	err := goyaml.Unmarshal([]byte(input), &buildStruct)
    55  	if err != nil {
    56  		return "", err
    57  	}
    58  	bf := buildfile.New()
    59  	buildStruct.Deploy.Write(bf)
    60  	return bf.String(), err
    61  }
    62  
    63  func TestSSHNoArtifact(t *testing.T) {
    64  	bscr, err := setUp(sampleYml)
    65  	if err != nil {
    66  		t.Fatalf("Can't unmarshal deploy script: %s", err)
    67  	}
    68  
    69  	if strings.Contains(bscr, `scp`) {
    70  		t.Error("Expect script not to contains scp command")
    71  	}
    72  
    73  	if !strings.Contains(bscr, "ssh -o StrictHostKeyChecking=no -p 22 user@test.example.com /opt/bin/redeploy.sh") {
    74  		t.Error("Expect script to contains ssh command")
    75  	}
    76  }
    77  
    78  func TestSSHOneArtifact(t *testing.T) {
    79  	bscr, err := setUp(sampleYml1)
    80  	if err != nil {
    81  		t.Fatalf("Can't unmarshal deploy script: %s", err)
    82  	}
    83  
    84  	if !strings.Contains(bscr, "ARTIFACT=build.result") {
    85  		t.Errorf("Expect script to contains artifact")
    86  	}
    87  
    88  	if !strings.Contains(bscr, "scp -o StrictHostKeyChecking=no -P 2212 -r ${ARTIFACT} user@test.example.com:/srv/app/location") {
    89  		t.Errorf("Expect script to contains scp command, got:\n%s", bscr)
    90  	}
    91  }
    92  
    93  func TestSSHMultiArtifact(t *testing.T) {
    94  	bscr, err := setUp(sampleYml2)
    95  	if err != nil {
    96  		t.Fatalf("Can't unmarshal deploy script: %s", err)
    97  	}
    98  
    99  	if !strings.Contains(bscr, "ARTIFACT=${PWD##*/}.tar.gz") {
   100  		t.Errorf("Expect script to contains artifact")
   101  	}
   102  
   103  	if !strings.Contains(bscr, "tar -cf ${ARTIFACT} build.result config/file") {
   104  		t.Errorf("Expect script to contains tar command. got:\n", bscr)
   105  	}
   106  }
   107  
   108  func TestSSHGitArchive(t *testing.T) {
   109  	bscr, err := setUp(sampleYml3)
   110  	if err != nil {
   111  		t.Fatalf("Can't unmarshal deploy script: %s", err)
   112  	}
   113  
   114  	if !strings.Contains(bscr, "COMMIT=$(git rev-parse HEAD)") {
   115  		t.Errorf("Expect script to contains commit ref")
   116  	}
   117  
   118  	if !strings.Contains(bscr, "ARTIFACT=${PWD##*/}-${COMMIT}.tar.gz") {
   119  		t.Errorf("Expect script to contains artifact")
   120  	}
   121  
   122  	if strings.Contains(bscr, "=GITARCHIVE") {
   123  		t.Errorf("Doesn't expect script to contains GITARCHIVE literals")
   124  	}
   125  
   126  	if !strings.Contains(bscr, "git archive --format=tar.gz --prefix=${PWD##*/}/ ${COMMIT} > ${ARTIFACT}") {
   127  		t.Errorf("Expect script to run git archive")
   128  	}
   129  }