harness-drone/server/badge_test.go

146 lines
4.2 KiB
Go
Raw Normal View History

2015-04-23 21:56:26 +00:00
package server
import (
2015-04-28 21:42:09 +00:00
"encoding/xml"
2015-04-23 21:56:26 +00:00
"net/http"
2015-04-28 21:42:09 +00:00
"net/url"
"strings"
2015-04-23 21:56:26 +00:00
"testing"
2015-04-28 21:42:09 +00:00
"time"
2015-04-23 21:56:26 +00:00
"github.com/drone/drone/common"
2015-04-28 21:42:09 +00:00
"github.com/drone/drone/common/ccmenu"
"github.com/drone/drone/datastore"
2015-05-01 04:20:59 +00:00
"github.com/drone/drone/datastore/mock"
"github.com/drone/drone/server/recorder"
2015-04-23 21:56:26 +00:00
. "github.com/franela/goblin"
"github.com/gin-gonic/gin"
)
2015-04-28 21:57:56 +00:00
func TestBadge(t *testing.T) {
2015-04-23 21:56:26 +00:00
g := Goblin(t)
g.Describe("Badge", func() {
var ctx gin.Context
2015-04-28 21:42:09 +00:00
owner := "Freya"
name := "Hello-World"
fullName := owner + "/" + name
repo := &common.Repo{Owner: owner, Name: name, FullName: fullName}
2015-04-23 21:56:26 +00:00
g.BeforeEach(func() {
2015-04-28 21:42:09 +00:00
ctx = gin.Context{Engine: gin.Default()}
url, _ := url.Parse("http://drone.local/badges/" + fullName)
ctx.Request = &http.Request{URL: url}
ctx.Set("repo", repo)
2015-04-23 21:56:26 +00:00
})
g.AfterEach(func() {
})
2015-05-01 04:20:59 +00:00
cycleStateTester := func(expector gin.HandlerFunc, handle gin.HandlerFunc, validator func(state string, w *recorder.ResponseRecorder)) {
2015-04-28 21:42:09 +00:00
for idx, state := range []string{"", common.StateError, common.StateFailure, common.StateKilled, common.StatePending, common.StateRunning, common.StateSuccess} {
2015-05-01 04:20:59 +00:00
w := recorder.NewResponseRecorder()
2015-04-28 21:42:09 +00:00
ctx.Writer = w
repo.Last = &common.Build{
Started: time.Now().UTC().Unix(),
Finished: time.Now().UTC().Unix(),
Number: idx,
State: state,
}
ctx.Set("repo", repo)
2015-04-23 21:56:26 +00:00
2015-04-28 21:42:09 +00:00
if expector != nil {
expector(&ctx)
}
2015-04-23 21:56:26 +00:00
2015-04-28 21:42:09 +00:00
handle(&ctx)
2015-04-23 21:56:26 +00:00
2015-04-28 21:42:09 +00:00
validator(state, w)
}
}
g.It("should provide SVG response", func() {
{
// 1. verify no "last" build
2015-05-01 04:20:59 +00:00
w := recorder.NewResponseRecorder()
2015-04-28 21:42:09 +00:00
ctx.Writer = w
ctx.Request.URL.Path += "/status.svg"
2015-04-23 21:56:26 +00:00
GetBadge(&ctx)
2015-04-28 21:42:09 +00:00
2015-04-23 21:56:26 +00:00
g.Assert(w.Status()).Equal(200)
2015-04-28 21:42:09 +00:00
g.Assert(w.HeaderMap.Get("content-type")).Equal("image/svg+xml")
g.Assert(strings.Contains(w.Body.String(), ">none")).IsTrue()
2015-04-23 21:56:26 +00:00
}
2015-04-28 21:42:09 +00:00
// 2. verify a variety of "last" build states
2015-05-01 04:20:59 +00:00
cycleStateTester(nil, GetBadge, func(state string, w *recorder.ResponseRecorder) {
2015-04-28 21:42:09 +00:00
g.Assert(w.Status()).Equal(200)
g.Assert(w.HeaderMap.Get("content-type")).Equal("image/svg+xml")
// this may be excessive, but does effectively verify behavior
switch state {
case common.StateSuccess:
g.Assert(strings.Contains(w.Body.String(), ">success")).IsTrue()
case common.StatePending, common.StateRunning:
g.Assert(strings.Contains(w.Body.String(), ">started")).IsTrue()
case common.StateError, common.StateKilled:
g.Assert(strings.Contains(w.Body.String(), ">error")).IsTrue()
case common.StateFailure:
g.Assert(strings.Contains(w.Body.String(), ">failure")).IsTrue()
default:
g.Assert(strings.Contains(w.Body.String(), ">none")).IsTrue()
}
})
2015-04-23 21:56:26 +00:00
})
2015-04-28 21:42:09 +00:00
g.It("should provide CCTray response", func() {
{
// 1. verify no "last" build
2015-05-01 04:20:59 +00:00
w := recorder.NewResponseRecorder()
2015-04-28 21:42:09 +00:00
ctx.Writer = w
ctx.Request.URL.Path += "/cc.xml"
ds := new(mocks.Datastore)
ctx.Set("datastore", ds)
2015-04-23 21:56:26 +00:00
2015-04-28 21:42:09 +00:00
ds.On("BuildLast", fullName).Return(nil, datastore.ErrKeyNotFound).Once()
GetCC(&ctx)
g.Assert(w.Status()).Equal(404)
}
// 2. verify a variety of "last" build states
cycleStateTester(func(c *gin.Context) {
repo := ToRepo(c)
ds := new(mocks.Datastore)
ctx.Set("datastore", ds)
ds.On("BuildLast", fullName).Return(repo.Last, nil).Once()
},
GetCC,
2015-05-01 04:20:59 +00:00
func(state string, w *recorder.ResponseRecorder) {
2015-04-28 21:42:09 +00:00
g.Assert(w.Status()).Equal(200)
v := ccmenu.CCProjects{}
xml.Unmarshal(w.Body.Bytes(), &v)
switch state {
case common.StateSuccess:
g.Assert(v.Project.Activity).Equal("Sleeping")
g.Assert(v.Project.LastBuildStatus).Equal("Success")
case common.StatePending, common.StateRunning:
g.Assert(v.Project.Activity).Equal("Building")
g.Assert(v.Project.LastBuildStatus).Equal("Unknown")
case common.StateError, common.StateKilled:
g.Assert(v.Project.Activity).Equal("Sleeping")
g.Assert(v.Project.LastBuildStatus).Equal("Exception")
case common.StateFailure:
g.Assert(v.Project.Activity).Equal("Sleeping")
g.Assert(v.Project.LastBuildStatus).Equal("Failure")
default:
g.Assert(v.Project.Activity).Equal("Sleeping")
g.Assert(v.Project.LastBuildStatus).Equal("Unknown")
}
})
})
2015-04-23 21:56:26 +00:00
})
}