2014-02-23 13:19:00 +00:00
|
|
|
package deploy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/drone/shared/build/buildfile"
|
2014-02-23 13:19:00 +00:00
|
|
|
|
2014-06-12 21:38:12 +00:00
|
|
|
"gopkg.in/yaml.v1"
|
2014-02-23 13:19:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// emulate Build struct
|
|
|
|
type build struct {
|
|
|
|
Deploy *Deploy `yaml:"deploy,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleYml = `
|
|
|
|
deploy:
|
|
|
|
ssh:
|
|
|
|
target: user@test.example.com
|
|
|
|
cmd: /opt/bin/redeploy.sh
|
|
|
|
`
|
|
|
|
|
|
|
|
var sampleYml1 = `
|
|
|
|
deploy:
|
|
|
|
ssh:
|
|
|
|
target: user@test.example.com:/srv/app/location 2212
|
|
|
|
artifacts:
|
|
|
|
- build.result
|
|
|
|
cmd: /opt/bin/redeploy.sh
|
|
|
|
`
|
|
|
|
|
|
|
|
var sampleYml2 = `
|
|
|
|
deploy:
|
|
|
|
ssh:
|
|
|
|
target: user@test.example.com:/srv/app/location 2212
|
|
|
|
artifacts:
|
|
|
|
- build.result
|
|
|
|
- config/file
|
|
|
|
cmd: /opt/bin/redeploy.sh
|
|
|
|
`
|
|
|
|
|
|
|
|
var sampleYml3 = `
|
|
|
|
deploy:
|
|
|
|
ssh:
|
|
|
|
target: user@test.example.com:/srv/app/location 2212
|
|
|
|
artifacts:
|
|
|
|
- GITARCHIVE
|
|
|
|
cmd: /opt/bin/redeploy.sh
|
|
|
|
`
|
|
|
|
|
|
|
|
func setUp(input string) (string, error) {
|
|
|
|
var buildStruct build
|
2014-06-12 21:38:12 +00:00
|
|
|
err := yaml.Unmarshal([]byte(input), &buildStruct)
|
2014-02-23 13:19:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
bf := buildfile.New()
|
|
|
|
buildStruct.Deploy.Write(bf)
|
|
|
|
return bf.String(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHNoArtifact(t *testing.T) {
|
|
|
|
bscr, err := setUp(sampleYml)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal deploy script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(bscr, `scp`) {
|
|
|
|
t.Error("Expect script not to contains scp command")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "ssh -o StrictHostKeyChecking=no -p 22 user@test.example.com /opt/bin/redeploy.sh") {
|
|
|
|
t.Error("Expect script to contains ssh command")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHOneArtifact(t *testing.T) {
|
|
|
|
bscr, err := setUp(sampleYml1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal deploy script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "ARTIFACT=build.result") {
|
2014-06-12 21:13:03 +00:00
|
|
|
t.Error("Expect script to contains artifact")
|
2014-02-23 13:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-06 20:25:42 +00:00
|
|
|
if !strings.Contains(bscr, "scp -o StrictHostKeyChecking=no -P 2212 -r ${ARTIFACT} user@test.example.com:/srv/app/location") {
|
2014-02-23 13:19:00 +00:00
|
|
|
t.Errorf("Expect script to contains scp command, got:\n%s", bscr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHMultiArtifact(t *testing.T) {
|
|
|
|
bscr, err := setUp(sampleYml2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal deploy script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "ARTIFACT=${PWD##*/}.tar.gz") {
|
|
|
|
t.Errorf("Expect script to contains artifact")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "tar -cf ${ARTIFACT} build.result config/file") {
|
2014-06-12 21:13:03 +00:00
|
|
|
t.Errorf("Expect script to contains tar command. got: %s\n", bscr)
|
2014-02-23 13:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHGitArchive(t *testing.T) {
|
|
|
|
bscr, err := setUp(sampleYml3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Can't unmarshal deploy script: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "COMMIT=$(git rev-parse HEAD)") {
|
|
|
|
t.Errorf("Expect script to contains commit ref")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(bscr, "ARTIFACT=${PWD##*/}-${COMMIT}.tar.gz") {
|
|
|
|
t.Errorf("Expect script to contains artifact")
|
|
|
|
}
|
|
|
|
|
2014-03-11 06:49:10 +00:00
|
|
|
if strings.Contains(bscr, "=GITARCHIVE") {
|
|
|
|
t.Errorf("Doesn't expect script to contains GITARCHIVE literals")
|
|
|
|
}
|
|
|
|
|
2014-02-23 13:19:00 +00:00
|
|
|
if !strings.Contains(bscr, "git archive --format=tar.gz --prefix=${PWD##*/}/ ${COMMIT} > ${ARTIFACT}") {
|
|
|
|
t.Errorf("Expect script to run git archive")
|
|
|
|
}
|
|
|
|
}
|