Skip to content

Commit

Permalink
Merge pull request #822 from bbklab/integration-test
Browse files Browse the repository at this point in the history
basic Integration test
  • Loading branch information
bbklab authored Aug 1, 2017
2 parents 720630f + bf3e482 commit aa7f495
Show file tree
Hide file tree
Showing 9 changed files with 826 additions and 33 deletions.
34 changes: 3 additions & 31 deletions integration-test/init_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package main

import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"testing"

log "github.com/Sirupsen/logrus"
"gopkg.in/check.v1"
check "gopkg.in/check.v1"
)

func init() {
s, err := newApiSuite()
if err != nil {
log.Fatalln(err)
log.Fatal(err)
}

check.Suite(s)
Expand Down Expand Up @@ -47,27 +43,3 @@ func newApiSuite() (*ApiSuite, error) {
SwanHost: swanHost,
}, nil
}

func (s *ApiSuite) sendRequest(method, uri string, data io.Reader) (code int, body []byte, err error) {
req, err := http.NewRequest(method, "http://"+s.SwanHost+uri, data)
if err != nil {
return -1, nil, err
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return -1, nil, err
}
defer resp.Body.Close()

bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -1, nil, err
}

return resp.StatusCode, bs, nil
}

func (s *ApiSuite) bind(data []byte, val interface{}) error {
return json.Unmarshal(data, &val)
}
138 changes: 138 additions & 0 deletions integration-test/swan_api_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package main

import (
"fmt"
"net/http"
"regexp"
"strings"
"time"

check "gopkg.in/check.v1"

"github.com/Dataman-Cloud/swan/types"
)

func (s *ApiSuite) TestCreateApp(c *check.C) {
// Purge
//
err := s.purge(time.Second*30, c)
c.Assert(err, check.IsNil)
fmt.Println("TestCreateApp() purged")

// New Create App
//
ver := demoVersion().setName("demo").setCount(10).setCPU(0.01).setMem(5).Get()
id := s.createApp(ver, c)
err = s.waitApp(id, types.OpStatusNoop, time.Second*30, c)
c.Assert(err, check.IsNil)
fmt.Println("TestCreateApp() created")

// verify app
app := s.inspectApp(id, c)
c.Assert(app.Name, check.Equals, "demo")
c.Assert(app.TaskCount, check.Equals, 10)
c.Assert(app.VersionCount, check.Equals, 1)
c.Assert(len(app.Version), check.Equals, 1)

// verify app versions
vers := s.listAppVersions(id, c)
c.Assert(len(vers), check.Equals, 1)
c.Assert(vers[0].CPUs, check.Equals, 0.01)
c.Assert(vers[0].Mem, check.Equals, float64(5))
c.Assert(vers[0].Instances, check.Equals, int32(10))
c.Assert(vers[0].RunAs, check.Equals, app.RunAs)

// verify app tasks
tasks := s.listAppTasks(id, c)
c.Assert(len(tasks), check.Equals, 10)
for _, task := range tasks {
c.Assert(task.Version, check.Equals, vers[0].ID)
}

// Remove
//
err = s.removeApp(id, time.Second*10, c)
c.Assert(err, check.IsNil)
fmt.Println("TestCreateApp() removed")
}

func (s *ApiSuite) TestCreateInvalidApp(c *check.C) {
// Purge
//
err := s.purge(time.Second*30, c)
c.Assert(err, check.IsNil)
fmt.Println("TestCreateInvalidApp() purged")

// Invalid Create Request
//

// invalid Content-Type
req, err := s.newRawReq("POST", "/v1/apps", "....")
c.Assert(err, check.IsNil)
code, body, err := s.sendRawRequest(req)
c.Assert(err, check.IsNil)
c.Assert(code, check.Equals, http.StatusBadRequest)
c.Log(string(body))
match, _ := regexp.MatchString("must be.*application/json", string(body))
c.Assert(match, check.Equals, true)
fmt.Println("TestCreateInvalidApp() illegal content type verified")

// invalid App Name
var invalidNames = [][2]string{
[2]string{"de..mo", "character.*not allowed"},
[2]string{"<**$", "character.*not allowed"},
[2]string{" ", "character.*not allowed"},
[2]string{strings.Repeat("x", 64), "appName empty or too long"},
[2]string{"", "appName empty or too long"},
}
for _, pairs := range invalidNames {
name, errmsg := pairs[0], pairs[1]
ver := demoVersion().setName(name).Get()
code, body, err := s.rawCreateApp(ver)
c.Assert(err, check.IsNil)
c.Assert(code, check.Equals, http.StatusBadRequest)
c.Log(string(body))
match, _ = regexp.MatchString(errmsg, string(body))
c.Assert(match, check.Equals, true)
}
fmt.Println("TestCreateInvalidApp() illegal app name verified")

// invalid RunAs
var invalidRunAses = [][2]string{
[2]string{"bbk.", "character.*not allowed"},
[2]string{"<**$", "character.*not allowed"},
[2]string{" ", "character.*not allowed"},
[2]string{"", "runAs should not empty"},
}
for _, pairs := range invalidRunAses {
runas, errmsg := pairs[0], pairs[1]
ver := demoVersion().setRunAs(runas).Get()
code, body, err := s.rawCreateApp(ver)
c.Assert(err, check.IsNil)
c.Assert(code, check.Equals, http.StatusBadRequest)
c.Log(string(body))
match, _ = regexp.MatchString(errmsg, string(body))
c.Assert(match, check.Equals, true)
}
fmt.Println("TestCreateInvalidApp() illegal app runAs verified")

// invalid image
ver := demoVersion().setImage("").Get()
code, body, err = s.rawCreateApp(ver)
c.Assert(err, check.IsNil)
c.Assert(code, check.Equals, http.StatusBadRequest)
c.Log(string(body))
match, _ = regexp.MatchString("image field required", string(body))
c.Assert(match, check.Equals, true)
fmt.Println("TestCreateInvalidApp() illegal app image verified")

// invalid instances
ver = demoVersion().setCount(0).Get()
code, body, err = s.rawCreateApp(ver)
c.Assert(err, check.IsNil)
c.Assert(code, check.Equals, http.StatusBadRequest)
c.Log(string(body))
match, _ = regexp.MatchString("should greater than 0", string(body))
c.Assert(match, check.Equals, true)
fmt.Println("TestCreateInvalidApp() illegal app count verified")
}
20 changes: 20 additions & 0 deletions integration-test/swan_api_leader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"net/http"

check "gopkg.in/check.v1"

"github.com/Dataman-Cloud/swan/types"
)

func (s *ApiSuite) TestGetLeader(c *check.C) {
code, body, err := s.sendRequest("GET", "/v1/leader", nil)
c.Assert(err, check.IsNil)
c.Assert(code, check.Equals, http.StatusOK)

var leader = new(types.Leader)
err = s.bind(body, leader)
c.Assert(err, check.IsNil)
c.Assert(leader.Leader, check.Not(check.Equals), "")
}
14 changes: 14 additions & 0 deletions integration-test/swan_api_ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"net/http"

check "gopkg.in/check.v1"
)

func (s *ApiSuite) TestPing(c *check.C) {
code, body, err := s.sendRequest("GET", "/ping", nil)
c.Assert(err, check.IsNil)
c.Assert(code, check.Equals, http.StatusOK)
c.Assert(string(body), check.Equals, `"pong"`)
}
113 changes: 113 additions & 0 deletions integration-test/swan_api_rollback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"fmt"
"time"

check "gopkg.in/check.v1"

"github.com/Dataman-Cloud/swan/types"
)

func (s *ApiSuite) TestRollBackApp(c *check.C) {
// Purge
//
err := s.purge(time.Second*30, c)
c.Assert(err, check.IsNil)
fmt.Println("TestUpdateApp() purged")

// New Create App
//
ver := demoVersion().setName("demo").setCount(3).setCPU(0.01).setMem(5).Get()
id := s.createApp(ver, c)
err = s.waitApp(id, types.OpStatusNoop, time.Second*30, c)
c.Assert(err, check.IsNil)
fmt.Println("TestUpdateApp() created")

// verify app
app := s.inspectApp(id, c)
c.Assert(app.Name, check.Equals, "demo")
c.Assert(app.TaskCount, check.Equals, 3)
c.Assert(app.VersionCount, check.Equals, 1)
c.Assert(len(app.Version), check.Equals, 1)

// verify app versions
vers := s.listAppVersions(id, c)
c.Assert(len(vers), check.Equals, 1)
c.Assert(vers[0].CPUs, check.Equals, 0.01)
c.Assert(vers[0].Mem, check.Equals, float64(5))
c.Assert(vers[0].Instances, check.Equals, int32(3))
c.Assert(vers[0].RunAs, check.Equals, app.RunAs)

// verify app tasks
tasks := s.listAppTasks(id, c)
c.Assert(len(tasks), check.Equals, 3)
for _, task := range tasks {
c.Assert(task.Version, check.Equals, vers[0].ID)
}

// Update App
//
newVer := demoVersion().setName("demo").setCount(3).setCPU(0.02).setMem(10).Get()
s.updateApp(id, newVer, c)
err = s.waitApp(id, types.OpStatusNoop, time.Second*120, c)
c.Assert(err, check.IsNil)
fmt.Println("TestUpdateApp() updated")

// verify app
app = s.inspectApp(id, c)
c.Assert(app.Name, check.Equals, "demo")
c.Assert(app.TaskCount, check.Equals, 3)
c.Assert(app.VersionCount, check.Equals, 2)
c.Assert(len(app.Version), check.Equals, 1)

// verify app versions
vers = s.listAppVersions(id, c)
c.Assert(len(vers), check.Equals, 2)
c.Assert(vers[0].CPUs, check.Equals, 0.02)
c.Assert(vers[0].Mem, check.Equals, float64(10))
c.Assert(vers[0].Instances, check.Equals, int32(3))
c.Assert(vers[0].RunAs, check.Equals, app.RunAs)

// verify app tasks
tasks = s.listAppTasks(id, c)
c.Assert(len(tasks), check.Equals, 3)
for _, task := range tasks {
c.Assert(task.Version, check.Equals, vers[0].ID)
}

// Roll Back App
//
s.rollbackApp(id, "", c) // rollback to previous version
err = s.waitApp(id, types.OpStatusNoop, time.Second*120, c)
c.Assert(err, check.IsNil)
fmt.Println("TestUpdateApp() rolled back")

// verify app
app = s.inspectApp(id, c)
c.Assert(app.Name, check.Equals, "demo")
c.Assert(app.TaskCount, check.Equals, 3)
c.Assert(app.VersionCount, check.Equals, 2)
c.Assert(len(app.Version), check.Equals, 1)

// verify app versions
vers = s.listAppVersions(id, c)
c.Assert(len(vers), check.Equals, 2)
c.Assert(vers[1].CPUs, check.Equals, 0.01)
c.Assert(vers[1].Mem, check.Equals, float64(5))
c.Assert(vers[1].Instances, check.Equals, int32(3))
c.Assert(vers[1].RunAs, check.Equals, app.RunAs)

// verify app tasks
tasks = s.listAppTasks(id, c)
c.Assert(len(tasks), check.Equals, 3)
for _, task := range tasks {
c.Assert(task.Version, check.Equals, vers[1].ID)
}

// Remove
//
err = s.removeApp(id, time.Second*10, c)
c.Assert(err, check.IsNil)
fmt.Println("TestUpdateApp() removed")
}
Loading

0 comments on commit aa7f495

Please sign in to comment.