-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #822 from bbklab/integration-test
basic Integration test
- Loading branch information
Showing
9 changed files
with
826 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
Oops, something went wrong.