From aac863b896d0d865aa5dbb6e44d25dc5bb4bc627 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sat, 11 Oct 2014 19:20:47 -0700 Subject: [PATCH] moved modulus deploy to separate package with unit tests --- plugin/deploy/deployment.go | 24 ++++++------ plugin/deploy/heroku/heroku_test.go | 2 +- plugin/deploy/modulus/modulus.go | 36 ++++++++++++++++++ plugin/deploy/modulus/modulus_test.go | 54 +++++++++++++++++++++++++++ plugin/deploy/tsuru/tsuru_test.go | 2 +- 5 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 plugin/deploy/modulus/modulus.go create mode 100644 plugin/deploy/modulus/modulus_test.go diff --git a/plugin/deploy/deployment.go b/plugin/deploy/deployment.go index 635fdfc92..4d0697c4f 100644 --- a/plugin/deploy/deployment.go +++ b/plugin/deploy/deployment.go @@ -2,25 +2,27 @@ package deploy import ( "github.com/drone/drone/plugin/condition" - "github.com/drone/drone/plugin/deploy/git" - "github.com/drone/drone/plugin/deploy/heroku" - "github.com/drone/drone/plugin/deploy/tsuru" "github.com/drone/drone/shared/build/buildfile" "github.com/drone/drone/shared/build/repo" + + "github.com/drone/drone/plugin/deploy/git" + "github.com/drone/drone/plugin/deploy/heroku" + "github.com/drone/drone/plugin/deploy/modulus" + "github.com/drone/drone/plugin/deploy/tsuru" ) // Deploy stores the configuration details // for deploying build artifacts when // a Build has succeeded type Deploy struct { - CloudFoundry *CloudFoundry `yaml:"cloudfoundry,omitempty"` - Git *git.Git `yaml:"git,omitempty"` - Heroku *heroku.Heroku `yaml:"heroku,omitempty"` - Modulus *Modulus `yaml:"modulus,omitempty"` - Nodejitsu *Nodejitsu `yaml:"nodejitsu,omitempty"` - SSH *SSH `yaml:"ssh,omitempty"` - Tsuru *tsuru.Tsuru `yaml:"tsuru,omitempty"` - Bash *Bash `yaml:"bash,omitempty"` + CloudFoundry *CloudFoundry `yaml:"cloudfoundry,omitempty"` + Git *git.Git `yaml:"git,omitempty"` + Heroku *heroku.Heroku `yaml:"heroku,omitempty"` + Modulus *modulus.Modulus `yaml:"modulus,omitempty"` + Nodejitsu *Nodejitsu `yaml:"nodejitsu,omitempty"` + SSH *SSH `yaml:"ssh,omitempty"` + Tsuru *tsuru.Tsuru `yaml:"tsuru,omitempty"` + Bash *Bash `yaml:"bash,omitempty"` } func (d *Deploy) Write(f *buildfile.Buildfile, r *repo.Repo) { diff --git a/plugin/deploy/heroku/heroku_test.go b/plugin/deploy/heroku/heroku_test.go index 1a7ff70b0..b938f603f 100644 --- a/plugin/deploy/heroku/heroku_test.go +++ b/plugin/deploy/heroku/heroku_test.go @@ -8,7 +8,7 @@ import ( "github.com/franela/goblin" ) -func Test_Git(t *testing.T) { +func Test_Heroku(t *testing.T) { g := goblin.Goblin(t) g.Describe("Heroku Deploy", func() { diff --git a/plugin/deploy/modulus/modulus.go b/plugin/deploy/modulus/modulus.go new file mode 100644 index 000000000..f9f945321 --- /dev/null +++ b/plugin/deploy/modulus/modulus.go @@ -0,0 +1,36 @@ +package modulus + +import ( + "fmt" + "github.com/drone/drone/plugin/condition" + "github.com/drone/drone/shared/build/buildfile" +) + +type Modulus struct { + Project string `yaml:"project,omitempty"` + Token string `yaml:"token,omitempty"` + + Condition *condition.Condition `yaml:"when,omitempty"` +} + +func (m *Modulus) Write(f *buildfile.Buildfile) { + if len(m.Token) == 0 || len(m.Project) == 0 { + return + } + f.WriteEnv("MODULUS_TOKEN", m.Token) + + // Verify npm exists, otherwise we cannot install the + // modulus command line utility. + f.WriteCmdSilent("[ -f /usr/bin/npm ] || echo ERROR: npm is required for moduls deployments") + f.WriteCmdSilent("[ -f /usr/bin/npm ] || exit 1") + + // Install the Modulus command line interface then deploy the configured + // project. + f.WriteCmdSilent("[ -f /usr/bin/sudo ] || npm install -g modulus") + f.WriteCmdSilent("[ -f /usr/bin/sudo ] && sudo npm install -g modulus") + f.WriteCmd(fmt.Sprintf("modulus deploy -p %q", m.Project)) +} + +func (m *Modulus) GetCondition() *condition.Condition { + return m.Condition +} diff --git a/plugin/deploy/modulus/modulus_test.go b/plugin/deploy/modulus/modulus_test.go new file mode 100644 index 000000000..357bdb828 --- /dev/null +++ b/plugin/deploy/modulus/modulus_test.go @@ -0,0 +1,54 @@ +package modulus + +import ( + //"strings" + "testing" + + "github.com/drone/drone/shared/build/buildfile" + "github.com/franela/goblin" +) + +func Test_Modulus(t *testing.T) { + + g := goblin.Goblin(t) + g.Describe("Modulus Deploy", func() { + + g.It("Requires a Project name", func() { + b := new(buildfile.Buildfile) + m := Modulus{ + Project: "foo", + } + + m.Write(b) + g.Assert(b.String()).Equal("") + }) + + g.It("Requires a Token", func() { + b := new(buildfile.Buildfile) + m := Modulus{ + Token: "bar", + } + + m.Write(b) + g.Assert(b.String()).Equal("") + }) + + g.It("Should execute deploy commands", func() { + b := new(buildfile.Buildfile) + m := Modulus{ + Project: "foo", + Token: "bar", + } + + m.Write(b) + g.Assert(b.String()).Equal(`export MODULUS_TOKEN=bar +[ -f /usr/bin/npm ] || echo ERROR: npm is required for moduls deployments +[ -f /usr/bin/npm ] || exit 1 +[ -f /usr/bin/sudo ] || npm install -g modulus +[ -f /usr/bin/sudo ] && sudo npm install -g modulus +echo '#DRONE:6d6f64756c7573206465706c6f79202d702022666f6f22' +modulus deploy -p "foo" +`) + }) + }) +} diff --git a/plugin/deploy/tsuru/tsuru_test.go b/plugin/deploy/tsuru/tsuru_test.go index c456d7d44..4473e69e3 100644 --- a/plugin/deploy/tsuru/tsuru_test.go +++ b/plugin/deploy/tsuru/tsuru_test.go @@ -8,7 +8,7 @@ import ( "github.com/franela/goblin" ) -func Test_Git(t *testing.T) { +func Test_Tsuru(t *testing.T) { g := goblin.Goblin(t) g.Describe("Tsuru Deploy", func() {