Merge pull request #535 from bradrydzewski/master

working on unit tests for Deploy plugins + misc
This commit is contained in:
Brad Rydzewski 2014-10-11 20:43:36 -07:00
commit 12757476d8
20 changed files with 466 additions and 119 deletions

View file

@ -1,5 +1,6 @@
[![Build Status](http://test.drone.io/v1/badge/github.com/drone/drone/status.svg)](http://test.drone.io/github.com/drone/drone)
[![GoDoc](https://godoc.org/github.com/drone/drone?status.png)](https://godoc.org/github.com/drone/drone)
[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/drone/drone?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
## System Requirements
@ -29,7 +30,7 @@ described in the **Setup** section.
Below are some example configurations that you can use as reference:
```
```toml
# to use postgres
[database]
driver="postgres"
@ -39,7 +40,6 @@ datasource="host=127.0.0.1 user=postgres dbname=drone sslmode=disable"
[database]
driver="mysql"
datasource="root@tcp(127.0.0.1:3306)/drone"
```
## Setup
@ -64,6 +64,10 @@ port=""
key=""
cert=""
[session]
secret=""
duration=""
[database]
driver=""
datasource=""

View file

@ -3,16 +3,18 @@
port=":80"
#####################################################################
# SSL configuration for Drone. Provide you key and cert chain
# to server Drone over https.
# SSL configuration
#
# [server.ssl]
# key=""
# cert=""
# [session]
# secret=""
# duration=""
#####################################################################
# Database configuration for Drone, by default using SQLite3.
# Database configuration, by default using SQLite3.
# You can also use postgres and mysql. See the documentation
# for more details.

View file

@ -1,12 +0,0 @@
package deploy
import (
"github.com/drone/drone/shared/build/buildfile"
)
type AppFog struct {
}
func (a *AppFog) Write(f *buildfile.Buildfile) {
}

View file

@ -1,12 +0,0 @@
package deploy
import (
"github.com/drone/drone/shared/build/buildfile"
)
type CloudControl struct {
}
func (c *CloudControl) Write(f *buildfile.Buildfile) {
}

View file

@ -0,0 +1,51 @@
package cloudfoundry
import (
"fmt"
"github.com/drone/drone/plugin/condition"
"github.com/drone/drone/shared/build/buildfile"
)
type CloudFoundry struct {
Target string `yaml:"target,omitempty"`
Username string `yaml:"username,omitempty"`
Password string `yaml:"password,omitempty"`
Org string `yaml:"org,omitempty"`
Space string `yaml:"space,omitempty"`
App string `yaml:"app,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
}
func (cf *CloudFoundry) Write(f *buildfile.Buildfile) {
downloadCmd := "curl -sLO http://go-cli.s3-website-us-east-1.amazonaws.com/releases/latest/cf-cli_amd64.deb"
installCmd := "dpkg -i cf-cli_amd64.deb 1> /dev/null 2> /dev/null"
// download and install the cf tool
f.WriteCmdSilent(fmt.Sprintf("[ -f /usr/bin/sudo ] && sudo %s || %s", downloadCmd, downloadCmd))
f.WriteCmdSilent(fmt.Sprintf("[ -f /usr/bin/sudo ] && sudo %s || %s", installCmd, installCmd))
// login
loginCmd := "cf login -a %s -u %s -p %s"
organization := cf.Org
if organization != "" {
loginCmd += fmt.Sprintf(" -o %s", organization)
}
space := cf.Space
if space != "" {
loginCmd += fmt.Sprintf(" -s %s", space)
}
f.WriteCmdSilent(fmt.Sprintf(loginCmd, cf.Target, cf.Username, cf.Password))
// push app
pushCmd := "cf push %s"
f.WriteCmd(fmt.Sprintf(pushCmd, cf.App))
}
func (cf *CloudFoundry) GetCondition() *condition.Condition {
return cf.Condition
}

View file

@ -4,24 +4,26 @@ import (
"github.com/drone/drone/plugin/condition"
"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/nodejitsu"
"github.com/drone/drone/plugin/deploy/tsuru"
)
// Deploy stores the configuration details
// for deploying build artifacts when
// a Build has succeeded
type Deploy struct {
AppFog *AppFog `yaml:"appfog,omitempty"`
CloudControl *CloudControl `yaml:"cloudcontrol,omitempty"`
CloudFoundry *CloudFoundry `yaml:"cloudfoundry,omitempty"`
EngineYard *EngineYard `yaml:"engineyard,omitempty"`
Git *Git `yaml:"git,omitempty"`
Heroku *Heroku `yaml:"heroku,omitempty"`
Modulus *Modulus `yaml:"modulus,omitempty"`
Nodejitsu *Nodejitsu `yaml:"nodejitsu,omitempty"`
Openshift *Openshift `yaml:"openshift,omitempty"`
SSH *SSH `yaml:"ssh,omitempty"`
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.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) {

View file

@ -1,12 +0,0 @@
package deploy
import (
"github.com/drone/drone/shared/build/buildfile"
)
type EngineYard struct {
}
func (e *EngineYard) Write(f *buildfile.Buildfile) {
}

View file

@ -1,4 +1,4 @@
package deploy
package git
import (
"fmt"
@ -6,6 +6,16 @@ import (
"github.com/drone/drone/shared/build/buildfile"
)
const (
// Gommand to the current commit hash
CmdRevParse = "COMMIT=$(git rev-parse HEAD)"
// Command to set the git user and email based on the
// individual that made the commit.
CmdGlobalEmail = "git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')"
CmdGlobalUser = "git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')"
)
type Git struct {
Target string `yaml:"target,omitempty"`
Force bool `yaml:"force,omitempty"`
@ -15,20 +25,16 @@ type Git struct {
}
func (g *Git) Write(f *buildfile.Buildfile) {
// get the current commit hash
f.WriteCmdSilent("COMMIT=$(git rev-parse HEAD)")
// set the git user and email based on the individual
// that made the commit.
f.WriteCmdSilent("git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')")
f.WriteCmdSilent("git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')")
f.WriteCmdSilent(CmdRevParse)
f.WriteCmdSilent(CmdGlobalUser)
f.WriteCmdSilent(CmdGlobalEmail)
// add target as a git remote
f.WriteCmd(fmt.Sprintf("git remote add deploy %s", g.Target))
destinationBranch := g.Branch
if destinationBranch == "" {
destinationBranch = "master"
dest := g.Branch
if len(dest) == 0 {
dest = "master"
}
switch g.Force {
@ -38,10 +44,10 @@ func (g *Git) Write(f *buildfile.Buildfile) {
// that need to be deployed to git remote.
f.WriteCmd(fmt.Sprintf("git add -A"))
f.WriteCmd(fmt.Sprintf("git commit -m 'add build artifacts'"))
f.WriteCmd(fmt.Sprintf("git push deploy HEAD:%s --force", destinationBranch))
f.WriteCmd(fmt.Sprintf("git push deploy HEAD:%s --force", dest))
case false:
// otherwise we just do a standard git push
f.WriteCmd(fmt.Sprintf("git push deploy $COMMIT:%s", destinationBranch))
f.WriteCmd(fmt.Sprintf("git push deploy $COMMIT:%s", dest))
}
}

View file

@ -0,0 +1,78 @@
package git
import (
"strings"
"testing"
"github.com/drone/drone/shared/build/buildfile"
"github.com/franela/goblin"
)
func Test_Git(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Git Deploy", func() {
g.It("Should set git.config", func() {
b := new(buildfile.Buildfile)
d := Git{
Target: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, CmdRevParse)).Equal(true)
g.Assert(strings.Contains(out, CmdGlobalUser)).Equal(true)
g.Assert(strings.Contains(out, CmdGlobalEmail)).Equal(true)
})
g.It("Should add remote", func() {
b := new(buildfile.Buildfile)
d := Git{
Target: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit remote add deploy git://foo.com/bar/baz.git\n")).Equal(true)
})
g.It("Should push to remote", func() {
b := new(buildfile.Buildfile)
d := Git{
Target: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit push deploy $COMMIT:master\n")).Equal(true)
})
g.It("Should push to alternate branch", func() {
b := new(buildfile.Buildfile)
d := Git{
Branch: "foo",
Target: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit push deploy $COMMIT:foo\n")).Equal(true)
})
g.It("Should force push to remote", func() {
b := new(buildfile.Buildfile)
d := Git{
Force: true,
Target: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit add -A\n")).Equal(true)
g.Assert(strings.Contains(out, "\ngit commit -m 'add build artifacts'\n")).Equal(true)
g.Assert(strings.Contains(out, "\ngit push deploy HEAD:master --force\n")).Equal(true)
})
})
}

View file

@ -1,4 +1,4 @@
package deploy
package heroku
import (
"fmt"
@ -6,22 +6,27 @@ import (
"github.com/drone/drone/shared/build/buildfile"
)
const (
// Gommand to the current commit hash
CmdRevParse = "COMMIT=$(git rev-parse HEAD)"
// Command to set the git user and email based on the
// individual that made the commit.
CmdGlobalEmail = "git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')"
CmdGlobalUser = "git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')"
)
type Heroku struct {
App string `yaml:"app,omitempty"`
Force bool `yaml:"force,omitempty"`
Branch string `yaml:"branch,omitempty"`
App string `yaml:"app,omitempty"`
Force bool `yaml:"force,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
}
func (h *Heroku) Write(f *buildfile.Buildfile) {
// get the current commit hash
f.WriteCmdSilent("COMMIT=$(git rev-parse HEAD)")
// set the git user and email based on the individual
// that made the commit.
f.WriteCmdSilent("git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')")
f.WriteCmdSilent("git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')")
f.WriteCmdSilent(CmdRevParse)
f.WriteCmdSilent(CmdGlobalUser)
f.WriteCmdSilent(CmdGlobalEmail)
// add heroku as a git remote
f.WriteCmd(fmt.Sprintf("git remote add heroku git@heroku.com:%s.git", h.App))

View file

@ -0,0 +1,66 @@
package heroku
import (
"strings"
"testing"
"github.com/drone/drone/shared/build/buildfile"
"github.com/franela/goblin"
)
func Test_Heroku(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Heroku Deploy", func() {
g.It("Should set git.config", func() {
b := new(buildfile.Buildfile)
h := Heroku{
App: "drone",
}
h.Write(b)
out := b.String()
g.Assert(strings.Contains(out, CmdRevParse)).Equal(true)
g.Assert(strings.Contains(out, CmdGlobalUser)).Equal(true)
g.Assert(strings.Contains(out, CmdGlobalEmail)).Equal(true)
})
g.It("Should add remote", func() {
b := new(buildfile.Buildfile)
h := Heroku{
App: "drone",
}
h.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit remote add heroku git@heroku.com:drone.git\n")).Equal(true)
})
g.It("Should push to remote", func() {
b := new(buildfile.Buildfile)
d := Heroku{
App: "drone",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit push heroku $COMMIT:master\n")).Equal(true)
})
g.It("Should force push to remote", func() {
b := new(buildfile.Buildfile)
h := Heroku{
Force: true,
App: "drone",
}
h.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit add -A\n")).Equal(true)
g.Assert(strings.Contains(out, "\ngit commit -m 'adding build artifacts'\n")).Equal(true)
g.Assert(strings.Contains(out, "\ngit push heroku HEAD:master --force\n")).Equal(true)
})
})
}

View file

@ -1,4 +1,4 @@
package deploy
package modulus
import (
"fmt"
@ -14,13 +14,21 @@ type Modulus struct {
}
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 modulus.io 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 '%s'", m.Project))
f.WriteCmd(fmt.Sprintf("modulus deploy -p %q", m.Project))
}
func (m *Modulus) GetCondition() *condition.Condition {

View file

@ -0,0 +1,53 @@
package modulus
import (
"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 modulus.io 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"
`)
})
})
}

View file

@ -1,4 +1,4 @@
package deploy
package nodejitsu
import (
"github.com/drone/drone/plugin/condition"
@ -6,7 +6,6 @@ import (
)
type Nodejitsu struct {
App string `yaml:"app,omitempty"`
User string `yaml:"user,omitempty"`
Token string `yaml:"token,omitempty"`
@ -14,6 +13,10 @@ type Nodejitsu struct {
}
func (n *Nodejitsu) Write(f *buildfile.Buildfile) {
if len(n.Token) == 0 || len(n.User) == 0 {
return
}
f.WriteEnv("username", n.User)
f.WriteEnv("apiToken", n.Token)

View file

@ -0,0 +1,52 @@
package nodejitsu
import (
"testing"
"github.com/drone/drone/shared/build/buildfile"
"github.com/franela/goblin"
)
func Test_Modulus(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Nodejitsu Deploy", func() {
g.It("Requires a User", func() {
b := new(buildfile.Buildfile)
n := Nodejitsu{
User: "foo",
}
n.Write(b)
g.Assert(b.String()).Equal("")
})
g.It("Requires a Token", func() {
b := new(buildfile.Buildfile)
n := Nodejitsu{
Token: "bar",
}
n.Write(b)
g.Assert(b.String()).Equal("")
})
g.It("Should execute deploy commands", func() {
b := new(buildfile.Buildfile)
n := Nodejitsu{
User: "foo",
Token: "bar",
}
n.Write(b)
g.Assert(b.String()).Equal(`export username=foo
export apiToken=bar
[ -f /usr/bin/sudo ] || npm install -g jitsu
[ -f /usr/bin/sudo ] && sudo npm install -g jitsu
echo '#DRONE:6a69747375206465706c6f79'
jitsu deploy
`)
})
})
}

View file

@ -1,12 +0,0 @@
package deploy
import (
"github.com/drone/drone/shared/build/buildfile"
)
type Openshift struct {
}
func (o *Openshift) Write(f *buildfile.Buildfile) {
}

View file

@ -1,4 +1,4 @@
package deploy
package tsuru
import (
"fmt"
@ -6,22 +6,27 @@ import (
"github.com/drone/drone/shared/build/buildfile"
)
const (
// Gommand to the current commit hash
CmdRevParse = "COMMIT=$(git rev-parse HEAD)"
// Command to set the git user and email based on the
// individual that made the commit.
CmdGlobalEmail = "git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')"
CmdGlobalUser = "git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')"
)
type Tsuru struct {
Force bool `yaml:"force,omitempty"`
Branch string `yaml:"branch,omitempty"`
Remote string `yaml:"remote,omitempty"`
Condition *condition.Condition `yaml:"when,omitempty"`
}
func (t *Tsuru) Write(f *buildfile.Buildfile) {
// get the current commit hash
f.WriteCmdSilent("COMMIT=$(git rev-parse HEAD)")
// set the git user and email based on the individual
// that made the commit.
f.WriteCmdSilent("git config --global user.name $(git --no-pager log -1 --pretty=format:'%an')")
f.WriteCmdSilent("git config --global user.email $(git --no-pager log -1 --pretty=format:'%ae')")
f.WriteCmdSilent(CmdRevParse)
f.WriteCmdSilent(CmdGlobalUser)
f.WriteCmdSilent(CmdGlobalEmail)
// add tsuru as a git remote
f.WriteCmd(fmt.Sprintf("git remote add tsuru %s", t.Remote))

View file

@ -0,0 +1,66 @@
package tsuru
import (
"strings"
"testing"
"github.com/drone/drone/shared/build/buildfile"
"github.com/franela/goblin"
)
func Test_Tsuru(t *testing.T) {
g := goblin.Goblin(t)
g.Describe("Tsuru Deploy", func() {
g.It("Should set git.config", func() {
b := new(buildfile.Buildfile)
d := Tsuru{
Remote: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, CmdRevParse)).Equal(true)
g.Assert(strings.Contains(out, CmdGlobalUser)).Equal(true)
g.Assert(strings.Contains(out, CmdGlobalEmail)).Equal(true)
})
g.It("Should add remote", func() {
b := new(buildfile.Buildfile)
d := Tsuru{
Remote: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit remote add tsuru git://foo.com/bar/baz.git\n")).Equal(true)
})
g.It("Should push to remote", func() {
b := new(buildfile.Buildfile)
d := Tsuru{
Remote: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit push tsuru $COMMIT:master\n")).Equal(true)
})
g.It("Should force push to remote", func() {
b := new(buildfile.Buildfile)
d := Tsuru{
Force: true,
Remote: "git://foo.com/bar/baz.git",
}
d.Write(b)
out := b.String()
g.Assert(strings.Contains(out, "\ngit add -A\n")).Equal(true)
g.Assert(strings.Contains(out, "\ngit commit -m 'adding build artifacts'\n")).Equal(true)
g.Assert(strings.Contains(out, "\ngit push tsuru HEAD:master --force\n")).Equal(true)
})
})
}

View file

@ -36,19 +36,6 @@ func Test_NPM(t *testing.T) {
g.Assert(strings.Contains(out, "\nnpm config set")).Equal(false)
})
/*
n := NPM{
Email: "foo@bar.com",
Username: "foo",
Password: "bar",
Force: true,
Registry: "",
Folder: "/path/to/repo",
Tag: "1.0.0",
AlwaysAuth: false,
}
*/
g.It("Should set force", func() {
b := new(buildfile.Buildfile)
n := NPM{

View file

@ -7,14 +7,21 @@ import (
"code.google.com/p/go.net/context"
"github.com/dgrijalva/jwt-go"
"github.com/drone/config"
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/shared/httputil"
"github.com/drone/drone/shared/model"
"github.com/gorilla/securecookie"
)
// secret key used to create jwt
var secret = securecookie.GenerateRandomKey(32)
// random key used to create jwt if none
// provided in the configuration.
var random = securecookie.GenerateRandomKey(32)
var (
secret = config.String("session-secret", string(random))
expires = config.Duration("session-expires", time.Hour*72)
)
// GetUser gets the currently authenticated user for the
// http.Request. The user details will be stored as either
@ -38,7 +45,7 @@ func GenerateToken(c context.Context, r *http.Request, user *model.User) (string
token.Claims["user_id"] = user.ID
token.Claims["audience"] = httputil.GetURL(r)
token.Claims["expires"] = time.Now().UTC().Add(time.Hour * 72).Unix()
return token.SignedString(secret)
return token.SignedString([]byte(*secret))
}
// getUserToken gets the currently authenticated user for the given
@ -56,7 +63,7 @@ func getUserBearer(c context.Context, r *http.Request) *model.User {
fmt.Sscanf(tokenstr, "Bearer %s", &tokenstr)
var token, err = jwt.Parse(tokenstr, func(t *jwt.Token) (interface{}, error) {
return secret, nil
return []byte(*secret), nil
})
if err != nil || !token.Valid {
println("invalid token")