summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--api/command.go339
-rw-r--r--api/command_loadtest.go357
-rw-r--r--api/command_loadtest_test.go220
-rw-r--r--api/command_logout_test.go2
-rw-r--r--api/command_test.go55
5 files changed, 578 insertions, 395 deletions
diff --git a/api/command.go b/api/command.go
index 2147196fa..8de109846 100644
--- a/api/command.go
+++ b/api/command.go
@@ -278,342 +278,3 @@ func deleteCommand(c *Context, w http.ResponseWriter, r *http.Request) {
c.LogAudit("success")
w.Write([]byte(model.MapToJson(props)))
}
-
-// func loadTestCommand(c *Context, command *model.Command) bool {
-// cmd := cmds["loadTestCommand"]
-
-// // This command is only available when EnableTesting is true
-// if !utils.Cfg.ServiceSettings.EnableTesting {
-// return false
-// }
-
-// if strings.Index(command.Command, cmd) == 0 {
-// if loadTestSetupCommand(c, command) {
-// return true
-// }
-// if loadTestUsersCommand(c, command) {
-// return true
-// }
-// if loadTestChannelsCommand(c, command) {
-// return true
-// }
-// if loadTestPostsCommand(c, command) {
-// return true
-// }
-// if loadTestUrlCommand(c, command) {
-// return true
-// }
-// } else if strings.Index(cmd, command.Command) == 0 {
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Debug Load Testing"})
-// }
-
-// return false
-// }
-
-// func parseRange(command string, cmd string) (utils.Range, bool) {
-// tokens := strings.Fields(strings.TrimPrefix(command, cmd))
-// var begin int
-// var end int
-// var err1 error
-// var err2 error
-// switch {
-// case len(tokens) == 1:
-// begin, err1 = strconv.Atoi(tokens[0])
-// end = begin
-// if err1 != nil {
-// return utils.Range{0, 0}, false
-// }
-// case len(tokens) >= 2:
-// begin, err1 = strconv.Atoi(tokens[0])
-// end, err2 = strconv.Atoi(tokens[1])
-// if err1 != nil || err2 != nil {
-// return utils.Range{0, 0}, false
-// }
-// default:
-// return utils.Range{0, 0}, false
-// }
-// return utils.Range{begin, end}, true
-// }
-
-// func contains(items []string, token string) bool {
-// for _, elem := range items {
-// if elem == token {
-// return true
-// }
-// }
-// return false
-// }
-
-// func loadTestSetupCommand(c *Context, command *model.Command) bool {
-// cmd := cmds["loadTestCommand"] + " setup"
-
-// if strings.Index(command.Command, cmd) == 0 && !command.Suggest {
-// tokens := strings.Fields(strings.TrimPrefix(command.Command, cmd))
-// doTeams := contains(tokens, "teams")
-// doFuzz := contains(tokens, "fuzz")
-
-// numArgs := 0
-// if doTeams {
-// numArgs++
-// }
-// if doFuzz {
-// numArgs++
-// }
-
-// var numTeams int
-// var numChannels int
-// var numUsers int
-// var numPosts int
-
-// // Defaults
-// numTeams = 10
-// numChannels = 10
-// numUsers = 10
-// numPosts = 10
-
-// if doTeams {
-// if (len(tokens) - numArgs) >= 4 {
-// numTeams, _ = strconv.Atoi(tokens[numArgs+0])
-// numChannels, _ = strconv.Atoi(tokens[numArgs+1])
-// numUsers, _ = strconv.Atoi(tokens[numArgs+2])
-// numPosts, _ = strconv.Atoi(tokens[numArgs+3])
-// }
-// } else {
-// if (len(tokens) - numArgs) >= 3 {
-// numChannels, _ = strconv.Atoi(tokens[numArgs+0])
-// numUsers, _ = strconv.Atoi(tokens[numArgs+1])
-// numPosts, _ = strconv.Atoi(tokens[numArgs+2])
-// }
-// }
-// client := model.NewClient(c.GetSiteURL())
-
-// if doTeams {
-// if err := CreateBasicUser(client); err != nil {
-// l4g.Error("Failed to create testing environment")
-// return true
-// }
-// client.LoginByEmail(BTEST_TEAM_NAME, BTEST_USER_EMAIL, BTEST_USER_PASSWORD)
-// environment, err := CreateTestEnvironmentWithTeams(
-// client,
-// utils.Range{numTeams, numTeams},
-// utils.Range{numChannels, numChannels},
-// utils.Range{numUsers, numUsers},
-// utils.Range{numPosts, numPosts},
-// doFuzz)
-// if err != true {
-// l4g.Error("Failed to create testing environment")
-// return true
-// } else {
-// l4g.Info("Testing environment created")
-// for i := 0; i < len(environment.Teams); i++ {
-// l4g.Info("Team Created: " + environment.Teams[i].Name)
-// l4g.Info("\t User to login: " + environment.Environments[i].Users[0].Email + ", " + USER_PASSWORD)
-// }
-// }
-// } else {
-// client.MockSession(c.Session.Token)
-// CreateTestEnvironmentInTeam(
-// client,
-// c.Session.TeamId,
-// utils.Range{numChannels, numChannels},
-// utils.Range{numUsers, numUsers},
-// utils.Range{numPosts, numPosts},
-// doFuzz)
-// }
-// return true
-// } else if strings.Index(cmd, command.Command) == 0 {
-// command.AddSuggestion(&model.SuggestCommand{
-// Suggestion: cmd,
-// Description: "Creates a testing environment in current team. [teams] [fuzz] <Num Channels> <Num Users> <NumPosts>"})
-// }
-
-// return false
-// }
-
-// func loadTestUsersCommand(c *Context, command *model.Command) bool {
-// cmd1 := cmds["loadTestCommand"] + " users"
-// cmd2 := cmds["loadTestCommand"] + " users fuzz"
-
-// if strings.Index(command.Command, cmd1) == 0 && !command.Suggest {
-// cmd := cmd1
-// doFuzz := false
-// if strings.Index(command.Command, cmd2) == 0 {
-// doFuzz = true
-// cmd = cmd2
-// }
-// usersr, err := parseRange(command.Command, cmd)
-// if err == false {
-// usersr = utils.Range{10, 15}
-// }
-// client := model.NewClient(c.GetSiteURL())
-// userCreator := NewAutoUserCreator(client, c.Session.TeamId)
-// userCreator.Fuzzy = doFuzz
-// userCreator.CreateTestUsers(usersr)
-// return true
-// } else if strings.Index(cmd1, command.Command) == 0 {
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd1, Description: "Add a specified number of random users to current team <Min Users> <Max Users>"})
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd2, Description: "Add a specified number of random users with fuzz text to current team <Min Users> <Max Users>"})
-// } else if strings.Index(cmd2, command.Command) == 0 {
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd2, Description: "Add a specified number of random users with fuzz text to current team <Min Users> <Max Users>"})
-// }
-
-// return false
-// }
-
-// func loadTestChannelsCommand(c *Context, command *model.Command) bool {
-// cmd1 := cmds["loadTestCommand"] + " channels"
-// cmd2 := cmds["loadTestCommand"] + " channels fuzz"
-
-// if strings.Index(command.Command, cmd1) == 0 && !command.Suggest {
-// cmd := cmd1
-// doFuzz := false
-// if strings.Index(command.Command, cmd2) == 0 {
-// doFuzz = true
-// cmd = cmd2
-// }
-// channelsr, err := parseRange(command.Command, cmd)
-// if err == false {
-// channelsr = utils.Range{20, 30}
-// }
-// client := model.NewClient(c.GetSiteURL())
-// client.MockSession(c.Session.Token)
-// channelCreator := NewAutoChannelCreator(client, c.Session.TeamId)
-// channelCreator.Fuzzy = doFuzz
-// channelCreator.CreateTestChannels(channelsr)
-// return true
-// } else if strings.Index(cmd1, command.Command) == 0 {
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd1, Description: "Add a specified number of random channels to current team <MinChannels> <MaxChannels>"})
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd2, Description: "Add a specified number of random channels with fuzz text to current team <Min Channels> <Max Channels>"})
-// } else if strings.Index(cmd2, command.Command) == 0 {
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd2, Description: "Add a specified number of random channels with fuzz text to current team <Min Channels> <Max Channels>"})
-// }
-
-// return false
-// }
-
-// func loadTestPostsCommand(c *Context, command *model.Command) bool {
-// cmd1 := cmds["loadTestCommand"] + " posts"
-// cmd2 := cmds["loadTestCommand"] + " posts fuzz"
-
-// if strings.Index(command.Command, cmd1) == 0 && !command.Suggest {
-// cmd := cmd1
-// doFuzz := false
-// if strings.Index(command.Command, cmd2) == 0 {
-// cmd = cmd2
-// doFuzz = true
-// }
-
-// postsr, err := parseRange(command.Command, cmd)
-// if err == false {
-// postsr = utils.Range{20, 30}
-// }
-
-// tokens := strings.Fields(strings.TrimPrefix(command.Command, cmd))
-// rimages := utils.Range{0, 0}
-// if len(tokens) >= 3 {
-// if numImages, err := strconv.Atoi(tokens[2]); err == nil {
-// rimages = utils.Range{numImages, numImages}
-// }
-// }
-
-// var usernames []string
-// if result := <-Srv.Store.User().GetProfiles(c.Session.TeamId); result.Err == nil {
-// profileUsers := result.Data.(map[string]*model.User)
-// usernames = make([]string, len(profileUsers))
-// i := 0
-// for _, userprof := range profileUsers {
-// usernames[i] = userprof.Username
-// i++
-// }
-// }
-
-// client := model.NewClient(c.GetSiteURL())
-// client.MockSession(c.Session.Token)
-// testPoster := NewAutoPostCreator(client, command.ChannelId)
-// testPoster.Fuzzy = doFuzz
-// testPoster.Users = usernames
-
-// numImages := utils.RandIntFromRange(rimages)
-// numPosts := utils.RandIntFromRange(postsr)
-// for i := 0; i < numPosts; i++ {
-// testPoster.HasImage = (i < numImages)
-// testPoster.CreateRandomPost()
-// }
-// return true
-// } else if strings.Index(cmd1, command.Command) == 0 {
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd1, Description: "Add some random posts to current channel <Min Posts> <Max Posts> <Min Images> <Max Images>"})
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd2, Description: "Add some random posts with fuzz text to current channel <Min Posts> <Max Posts> <Min Images> <Max Images>"})
-// } else if strings.Index(cmd2, command.Command) == 0 {
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd2, Description: "Add some random posts with fuzz text to current channel <Min Posts> <Max Posts> <Min Images> <Max Images>"})
-// }
-
-// return false
-// }
-
-// func loadTestUrlCommand(c *Context, command *model.Command) bool {
-// cmd := cmds["loadTestCommand"] + " url"
-
-// if strings.Index(command.Command, cmd) == 0 && !command.Suggest {
-// url := ""
-
-// parameters := strings.SplitN(command.Command, " ", 3)
-// if len(parameters) != 3 {
-// c.Err = model.NewAppError("loadTestUrlCommand", "Command must contain a url", "")
-// return true
-// } else {
-// url = parameters[2]
-// }
-
-// // provide a shortcut to easily access tests stored in doc/developer/tests
-// if !strings.HasPrefix(url, "http") {
-// url = "https://raw.githubusercontent.com/mattermost/platform/master/doc/developer/tests/" + url
-
-// if path.Ext(url) == "" {
-// url += ".md"
-// }
-// }
-
-// var contents io.ReadCloser
-// if r, err := http.Get(url); err != nil {
-// c.Err = model.NewAppError("loadTestUrlCommand", "Unable to get file", err.Error())
-// return false
-// } else if r.StatusCode > 400 {
-// c.Err = model.NewAppError("loadTestUrlCommand", "Unable to get file", r.Status)
-// return false
-// } else {
-// contents = r.Body
-// }
-
-// bytes := make([]byte, 4000)
-
-// // break contents into 4000 byte posts
-// for {
-// length, err := contents.Read(bytes)
-// if err != nil && err != io.EOF {
-// c.Err = model.NewAppError("loadTestUrlCommand", "Encountered error reading file", err.Error())
-// return false
-// }
-
-// if length == 0 {
-// break
-// }
-
-// post := &model.Post{}
-// post.Message = string(bytes[:length])
-// post.ChannelId = command.ChannelId
-
-// if _, err := CreatePost(c, post, false); err != nil {
-// l4g.Error("Unable to create post, err=%v", err)
-// return false
-// }
-// }
-
-// command.Response = model.RESP_EXECUTED
-
-// return true
-// } else if strings.Index(cmd, command.Command) == 0 && strings.Index(command.Command, "/loadtest posts") != 0 {
-// command.AddSuggestion(&model.SuggestCommand{Suggestion: cmd, Description: "Add a post containing the text from a given url to current channel <Url>"})
-// }
-
-// return false
-// }
diff --git a/api/command_loadtest.go b/api/command_loadtest.go
new file mode 100644
index 000000000..eaf0b91b1
--- /dev/null
+++ b/api/command_loadtest.go
@@ -0,0 +1,357 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api
+
+import (
+ "io"
+ "net/http"
+ "path"
+ "strconv"
+ "strings"
+
+ l4g "code.google.com/p/log4go"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+)
+
+var usage = `Mattermost load testing commands to help configure the system
+
+ COMMANDS:
+
+ Setup - Creates a testing environment in current team.
+ /loadtest setup [teams] [fuzz] <Num Channels> <Num Users> <NumPosts>
+
+ Example:
+ /loadtest setup teams fuzz 10 20 50
+
+ Users - Add a specified number of random users with fuzz text to current team.
+ /loadtest users [fuzz] <Min Users> <Max Users>
+
+ Example:
+ /loadtest users fuzz 5 10
+
+ Channels - Add a specified number of random channels with fuzz text to current team.
+ /loadtest channels [fuzz] <Min Channels> <Max Channels>
+
+ Example:
+ /loadtest channels fuzz 5 10
+
+ Posts - Add some random posts with fuzz text to current channel.
+ /loadtest posts [fuzz] <Min Posts> <Max Posts> <Max Images>
+
+ Example:
+ /loadtest posts fuzz 5 10 3
+
+ Url - Add a post containing the text from a given url to current channel.
+ /loadtest url
+
+ Example:
+ /loadtest http://www.example.com/sample_file.md
+
+
+`
+
+type LoadTestProvider struct {
+}
+
+func init() {
+ if !utils.Cfg.ServiceSettings.EnableTesting {
+ RegisterCommandProvider(&LoadTestProvider{})
+ }
+}
+
+func (me *LoadTestProvider) GetCommand() *model.Command {
+ return &model.Command{
+ Trigger: "loadtest",
+ AutoComplete: false,
+ AutoCompleteDesc: "Debug Load Testing",
+ AutoCompleteHint: "help",
+ DisplayName: "loadtest",
+ }
+}
+
+func (me *LoadTestProvider) DoCommand(c *Context, channelId string, message string) *model.CommandResponse {
+
+ // This command is only available when EnableTesting is true
+ // if !utils.Cfg.ServiceSettings.EnableTesting {
+ // return &model.CommandResponse{}
+ // }
+
+ if strings.HasPrefix(message, "setup") {
+ return me.SetupCommand(c, channelId, message)
+ }
+
+ if strings.HasPrefix(message, "users") {
+ return me.UsersCommand(c, channelId, message)
+ }
+
+ if strings.HasPrefix(message, "channels") {
+ return me.ChannelsCommand(c, channelId, message)
+ }
+
+ if strings.HasPrefix(message, "posts") {
+ return me.PostsCommand(c, channelId, message)
+ }
+
+ if strings.HasPrefix(message, "url") {
+ return me.UrlCommand(c, channelId, message)
+ }
+
+ return me.HelpCommand(c, channelId, message)
+}
+
+func (me *LoadTestProvider) HelpCommand(c *Context, channelId string, message string) *model.CommandResponse {
+ return &model.CommandResponse{Text: usage, ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+}
+
+func (me *LoadTestProvider) SetupCommand(c *Context, channelId string, message string) *model.CommandResponse {
+ tokens := strings.Fields(strings.TrimPrefix(message, "setup"))
+ doTeams := contains(tokens, "teams")
+ doFuzz := contains(tokens, "fuzz")
+
+ numArgs := 0
+ if doTeams {
+ numArgs++
+ }
+ if doFuzz {
+ numArgs++
+ }
+
+ var numTeams int
+ var numChannels int
+ var numUsers int
+ var numPosts int
+
+ // Defaults
+ numTeams = 10
+ numChannels = 10
+ numUsers = 10
+ numPosts = 10
+
+ if doTeams {
+ if (len(tokens) - numArgs) >= 4 {
+ numTeams, _ = strconv.Atoi(tokens[numArgs+0])
+ numChannels, _ = strconv.Atoi(tokens[numArgs+1])
+ numUsers, _ = strconv.Atoi(tokens[numArgs+2])
+ numPosts, _ = strconv.Atoi(tokens[numArgs+3])
+ }
+ } else {
+ if (len(tokens) - numArgs) >= 3 {
+ numChannels, _ = strconv.Atoi(tokens[numArgs+0])
+ numUsers, _ = strconv.Atoi(tokens[numArgs+1])
+ numPosts, _ = strconv.Atoi(tokens[numArgs+2])
+ }
+ }
+ client := model.NewClient(c.GetSiteURL())
+
+ if doTeams {
+ if err := CreateBasicUser(client); err != nil {
+ return &model.CommandResponse{Text: "Failed to create testing environment", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+ }
+ client.LoginByEmail(BTEST_TEAM_NAME, BTEST_USER_EMAIL, BTEST_USER_PASSWORD)
+ environment, err := CreateTestEnvironmentWithTeams(
+ client,
+ utils.Range{numTeams, numTeams},
+ utils.Range{numChannels, numChannels},
+ utils.Range{numUsers, numUsers},
+ utils.Range{numPosts, numPosts},
+ doFuzz)
+ if err != true {
+ return &model.CommandResponse{Text: "Failed to create testing environment", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+ } else {
+ l4g.Info("Testing environment created")
+ for i := 0; i < len(environment.Teams); i++ {
+ l4g.Info("Team Created: " + environment.Teams[i].Name)
+ l4g.Info("\t User to login: " + environment.Environments[i].Users[0].Email + ", " + USER_PASSWORD)
+ }
+ }
+ } else {
+ client.MockSession(c.Session.Token)
+ CreateTestEnvironmentInTeam(
+ client,
+ c.Session.TeamId,
+ utils.Range{numChannels, numChannels},
+ utils.Range{numUsers, numUsers},
+ utils.Range{numPosts, numPosts},
+ doFuzz)
+ }
+
+ return &model.CommandResponse{Text: "Creating enviroment...", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+}
+
+func (me *LoadTestProvider) UsersCommand(c *Context, channelId string, message string) *model.CommandResponse {
+ cmd := strings.TrimSpace(strings.TrimPrefix(message, "users"))
+
+ doFuzz := false
+ if strings.Index(cmd, "fuzz") == 0 {
+ doFuzz = true
+ cmd = strings.TrimSpace(strings.TrimPrefix(cmd, "fuzz"))
+ }
+
+ usersr, err := parseRange(cmd, "")
+ if err == false {
+ usersr = utils.Range{2, 5}
+ }
+
+ client := model.NewClient(c.GetSiteURL())
+ userCreator := NewAutoUserCreator(client, c.Session.TeamId)
+ userCreator.Fuzzy = doFuzz
+ userCreator.CreateTestUsers(usersr)
+
+ return &model.CommandResponse{Text: "Adding users...", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+}
+
+func (me *LoadTestProvider) ChannelsCommand(c *Context, channelId string, message string) *model.CommandResponse {
+ cmd := strings.TrimSpace(strings.TrimPrefix(message, "channels"))
+
+ doFuzz := false
+ if strings.Index(cmd, "fuzz") == 0 {
+ doFuzz = true
+ cmd = strings.TrimSpace(strings.TrimPrefix(cmd, "fuzz"))
+ }
+
+ channelsr, err := parseRange(cmd, "")
+ if err == false {
+ channelsr = utils.Range{2, 5}
+ }
+ client := model.NewClient(c.GetSiteURL())
+ client.MockSession(c.Session.Token)
+ channelCreator := NewAutoChannelCreator(client, c.Session.TeamId)
+ channelCreator.Fuzzy = doFuzz
+ channelCreator.CreateTestChannels(channelsr)
+
+ return &model.CommandResponse{Text: "Adding channels...", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+}
+
+func (me *LoadTestProvider) PostsCommand(c *Context, channelId string, message string) *model.CommandResponse {
+ cmd := strings.TrimSpace(strings.TrimPrefix(message, "posts"))
+
+ doFuzz := false
+ if strings.Index(cmd, "fuzz") == 0 {
+ doFuzz = true
+ cmd = strings.TrimSpace(strings.TrimPrefix(cmd, "fuzz"))
+ }
+
+ postsr, err := parseRange(cmd, "")
+ if err == false {
+ postsr = utils.Range{20, 30}
+ }
+
+ tokens := strings.Fields(cmd)
+ rimages := utils.Range{0, 0}
+ if len(tokens) >= 3 {
+ if numImages, err := strconv.Atoi(tokens[2]); err == nil {
+ rimages = utils.Range{numImages, numImages}
+ }
+ }
+
+ var usernames []string
+ if result := <-Srv.Store.User().GetProfiles(c.Session.TeamId); result.Err == nil {
+ profileUsers := result.Data.(map[string]*model.User)
+ usernames = make([]string, len(profileUsers))
+ i := 0
+ for _, userprof := range profileUsers {
+ usernames[i] = userprof.Username
+ i++
+ }
+ }
+
+ client := model.NewClient(c.GetSiteURL())
+ client.MockSession(c.Session.Token)
+ testPoster := NewAutoPostCreator(client, channelId)
+ testPoster.Fuzzy = doFuzz
+ testPoster.Users = usernames
+
+ numImages := utils.RandIntFromRange(rimages)
+ numPosts := utils.RandIntFromRange(postsr)
+ for i := 0; i < numPosts; i++ {
+ testPoster.HasImage = (i < numImages)
+ testPoster.CreateRandomPost()
+ }
+
+ return &model.CommandResponse{Text: "Adding posts...", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+}
+
+func (me *LoadTestProvider) UrlCommand(c *Context, channelId string, message string) *model.CommandResponse {
+ url := strings.TrimSpace(strings.TrimPrefix(message, "url"))
+ if len(url) == 0 {
+ return &model.CommandResponse{Text: "Command must contain a url", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+ }
+
+ // provide a shortcut to easily access tests stored in doc/developer/tests
+ if !strings.HasPrefix(url, "http") {
+ url = "https://raw.githubusercontent.com/mattermost/platform/master/doc/developer/tests/" + url
+
+ if path.Ext(url) == "" {
+ url += ".md"
+ }
+ }
+
+ var contents io.ReadCloser
+ if r, err := http.Get(url); err != nil {
+ return &model.CommandResponse{Text: "Unable to get file", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+ } else if r.StatusCode > 400 {
+ return &model.CommandResponse{Text: "Unable to get file", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+ } else {
+ contents = r.Body
+ }
+
+ bytes := make([]byte, 4000)
+
+ // break contents into 4000 byte posts
+ for {
+ length, err := contents.Read(bytes)
+ if err != nil && err != io.EOF {
+ return &model.CommandResponse{Text: "Encountered error reading file", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+ }
+
+ if length == 0 {
+ break
+ }
+
+ post := &model.Post{}
+ post.Message = string(bytes[:length])
+ post.ChannelId = channelId
+
+ if _, err := CreatePost(c, post, false); err != nil {
+ return &model.CommandResponse{Text: "Unable to create post", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+ }
+ }
+
+ return &model.CommandResponse{Text: "Loading url...", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
+}
+
+func parseRange(command string, cmd string) (utils.Range, bool) {
+ tokens := strings.Fields(strings.TrimPrefix(command, cmd))
+ var begin int
+ var end int
+ var err1 error
+ var err2 error
+ switch {
+ case len(tokens) == 1:
+ begin, err1 = strconv.Atoi(tokens[0])
+ end = begin
+ if err1 != nil {
+ return utils.Range{0, 0}, false
+ }
+ case len(tokens) >= 2:
+ begin, err1 = strconv.Atoi(tokens[0])
+ end, err2 = strconv.Atoi(tokens[1])
+ if err1 != nil || err2 != nil {
+ return utils.Range{0, 0}, false
+ }
+ default:
+ return utils.Range{0, 0}, false
+ }
+ return utils.Range{begin, end}, true
+}
+
+func contains(items []string, token string) bool {
+ for _, elem := range items {
+ if elem == token {
+ return true
+ }
+ }
+ return false
+}
diff --git a/api/command_loadtest_test.go b/api/command_loadtest_test.go
new file mode 100644
index 000000000..4af2a636a
--- /dev/null
+++ b/api/command_loadtest_test.go
@@ -0,0 +1,220 @@
+// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api
+
+import (
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/store"
+ "github.com/mattermost/platform/utils"
+)
+
+func TestLoadTestHelpCommands(t *testing.T) {
+ Setup()
+ // enable testing to use /loadtest but don't save it since we don't want to overwrite config.json
+ enableTesting := utils.Cfg.ServiceSettings.EnableTesting
+ defer func() {
+ utils.Cfg.ServiceSettings.EnableTesting = enableTesting
+ }()
+
+ utils.Cfg.ServiceSettings.EnableTesting = true
+
+ team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
+ team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
+
+ user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
+ user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
+ store.Must(Srv.Store.User().VerifyEmail(user1.Id))
+
+ Client.LoginByEmail(team.Name, user1.Email, "pwd")
+
+ channel := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
+ channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)
+
+ rs := Client.Must(Client.Command(channel.Id, "/loadtest help", false)).Data.(*model.CommandResponse)
+ if !strings.Contains(rs.Text, "Mattermost load testing commands to help") {
+ t.Fatal(rs.Text)
+ }
+
+ time.Sleep(2 * time.Second)
+}
+
+func TestLoadTestSetupCommands(t *testing.T) {
+ Setup()
+ // enable testing to use /loadtest but don't save it since we don't want to overwrite config.json
+ enableTesting := utils.Cfg.ServiceSettings.EnableTesting
+ defer func() {
+ utils.Cfg.ServiceSettings.EnableTesting = enableTesting
+ }()
+
+ utils.Cfg.ServiceSettings.EnableTesting = true
+
+ team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
+ team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
+
+ user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
+ user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
+ store.Must(Srv.Store.User().VerifyEmail(user1.Id))
+
+ Client.LoginByEmail(team.Name, user1.Email, "pwd")
+
+ channel := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
+ channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)
+
+ rs := Client.Must(Client.Command(channel.Id, "/loadtest setup fuzz 1 1 1", false)).Data.(*model.CommandResponse)
+ if rs.Text != "Creating enviroment..." {
+ t.Fatal(rs.Text)
+ }
+
+ time.Sleep(2 * time.Second)
+}
+
+func TestLoadTestUsersCommands(t *testing.T) {
+ Setup()
+ // enable testing to use /loadtest but don't save it since we don't want to overwrite config.json
+ enableTesting := utils.Cfg.ServiceSettings.EnableTesting
+ defer func() {
+ utils.Cfg.ServiceSettings.EnableTesting = enableTesting
+ }()
+
+ utils.Cfg.ServiceSettings.EnableTesting = true
+
+ team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
+ team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
+
+ user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
+ user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
+ store.Must(Srv.Store.User().VerifyEmail(user1.Id))
+
+ Client.LoginByEmail(team.Name, user1.Email, "pwd")
+
+ channel := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
+ channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)
+
+ rs := Client.Must(Client.Command(channel.Id, "/loadtest users fuzz 1 2", false)).Data.(*model.CommandResponse)
+ if rs.Text != "Adding users..." {
+ t.Fatal(rs.Text)
+ }
+
+ time.Sleep(2 * time.Second)
+}
+
+func TestLoadTestChannelsCommands(t *testing.T) {
+ Setup()
+ // enable testing to use /loadtest but don't save it since we don't want to overwrite config.json
+ enableTesting := utils.Cfg.ServiceSettings.EnableTesting
+ defer func() {
+ utils.Cfg.ServiceSettings.EnableTesting = enableTesting
+ }()
+
+ utils.Cfg.ServiceSettings.EnableTesting = true
+
+ team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
+ team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
+
+ user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
+ user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
+ store.Must(Srv.Store.User().VerifyEmail(user1.Id))
+
+ Client.LoginByEmail(team.Name, user1.Email, "pwd")
+
+ channel := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
+ channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)
+
+ rs := Client.Must(Client.Command(channel.Id, "/loadtest channels fuzz 1 2", false)).Data.(*model.CommandResponse)
+ if rs.Text != "Adding channels..." {
+ t.Fatal(rs.Text)
+ }
+
+ time.Sleep(2 * time.Second)
+}
+
+func TestLoadTestPostsCommands(t *testing.T) {
+ Setup()
+ // enable testing to use /loadtest but don't save it since we don't want to overwrite config.json
+ enableTesting := utils.Cfg.ServiceSettings.EnableTesting
+ defer func() {
+ utils.Cfg.ServiceSettings.EnableTesting = enableTesting
+ }()
+
+ utils.Cfg.ServiceSettings.EnableTesting = true
+
+ team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
+ team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
+
+ user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
+ user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
+ store.Must(Srv.Store.User().VerifyEmail(user1.Id))
+
+ Client.LoginByEmail(team.Name, user1.Email, "pwd")
+
+ channel := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
+ channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)
+
+ rs := Client.Must(Client.Command(channel.Id, "/loadtest posts fuzz 2 3 2", false)).Data.(*model.CommandResponse)
+ if rs.Text != "Adding posts..." {
+ t.Fatal(rs.Text)
+ }
+
+ time.Sleep(2 * time.Second)
+}
+
+func TestLoadTestUrlCommands(t *testing.T) {
+ Setup()
+ // enable testing to use /loadtest but don't save it since we don't want to overwrite config.json
+ enableTesting := utils.Cfg.ServiceSettings.EnableTesting
+ defer func() {
+ utils.Cfg.ServiceSettings.EnableTesting = enableTesting
+ }()
+
+ utils.Cfg.ServiceSettings.EnableTesting = true
+
+ team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
+ team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
+
+ user1 := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
+ user1 = Client.Must(Client.CreateUser(user1, "")).Data.(*model.User)
+ store.Must(Srv.Store.User().VerifyEmail(user1.Id))
+
+ Client.LoginByEmail(team.Name, user1.Email, "pwd")
+
+ channel := &model.Channel{DisplayName: "00", Name: "00" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
+ channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)
+
+ command := "/loadtest url "
+ if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.CommandResponse); r.Text != "Command must contain a url" {
+ t.Fatal("/loadtest url with no url should've failed")
+ }
+
+ command = "/loadtest url http://www.hopefullynonexistent.file/path/asdf/qwerty"
+ if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.CommandResponse); r.Text != "Unable to get file" {
+ t.Fatal("/loadtest url with invalid url should've failed")
+ }
+
+ command = "/loadtest url https://raw.githubusercontent.com/mattermost/platform/master/README.md"
+ if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.CommandResponse); r.Text != "Loading url..." {
+ t.Fatal("/loadtest url for README.md should've executed")
+ }
+
+ command = "/loadtest url test-emoticons.md"
+ if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.CommandResponse); r.Text != "Loading url..." {
+ t.Fatal("/loadtest url for test-emoticons.md should've executed")
+ }
+
+ command = "/loadtest url test-emoticons"
+ if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.CommandResponse); r.Text != "Loading url..." {
+ t.Fatal("/loadtest url for test-emoticons should've executed")
+ }
+
+ posts := Client.Must(Client.GetPosts(channel.Id, 0, 5, "")).Data.(*model.PostList)
+ // note that this may make more than 3 posts if files are too long to fit in an individual post
+ if len(posts.Order) < 3 {
+ t.Fatal("/loadtest url made too few posts, perhaps there needs to be a delay before GetPosts in the test?")
+ }
+
+ time.Sleep(2 * time.Second)
+}
diff --git a/api/command_logout_test.go b/api/command_logout_test.go
index 4c8f3e017..86979316b 100644
--- a/api/command_logout_test.go
+++ b/api/command_logout_test.go
@@ -10,7 +10,7 @@ import (
"github.com/mattermost/platform/store"
)
-func TestLogoutCommand(t *testing.T) {
+func TestLogoutTestCommand(t *testing.T) {
Setup()
team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
diff --git a/api/command_test.go b/api/command_test.go
index 1583ac5bb..8243b0ddf 100644
--- a/api/command_test.go
+++ b/api/command_test.go
@@ -197,58 +197,3 @@ func TestDeleteCommand(t *testing.T) {
*utils.Cfg.ServiceSettings.EnableCommands = false
}
-
-// func TestLoadTestUrlCommand(t *testing.T) {
-// Setup()
-
-// // enable testing to use /loadtest but don't save it since we don't want to overwrite config.json
-// enableTesting := utils.Cfg.ServiceSettings.EnableTesting
-// defer func() {
-// utils.Cfg.ServiceSettings.EnableTesting = enableTesting
-// }()
-
-// utils.Cfg.ServiceSettings.EnableTesting = true
-
-// team := &model.Team{DisplayName: "Name", Name: "z-z-" + model.NewId() + "a", Email: "test@nowhere.com", Type: model.TEAM_OPEN}
-// team = Client.Must(Client.CreateTeam(team)).Data.(*model.Team)
-
-// user := &model.User{TeamId: team.Id, Email: model.NewId() + "corey+test@test.com", Nickname: "Corey Hulen", Password: "pwd"}
-// user = Client.Must(Client.CreateUser(user, "")).Data.(*model.User)
-// store.Must(Srv.Store.User().VerifyEmail(user.Id))
-
-// Client.LoginByEmail(team.Name, user.Email, "pwd")
-
-// channel := &model.Channel{DisplayName: "AA", Name: "aa" + model.NewId() + "a", Type: model.CHANNEL_OPEN, TeamId: team.Id}
-// channel = Client.Must(Client.CreateChannel(channel)).Data.(*model.Channel)
-
-// command := "/loadtest url "
-// if _, err := Client.Command(channel.Id, command, false); err == nil {
-// t.Fatal("/loadtest url with no url should've failed")
-// }
-
-// command = "/loadtest url http://www.hopefullynonexistent.file/path/asdf/qwerty"
-// if _, err := Client.Command(channel.Id, command, false); err == nil {
-// t.Fatal("/loadtest url with invalid url should've failed")
-// }
-
-// command = "/loadtest url https://raw.githubusercontent.com/mattermost/platform/master/README.md"
-// if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.Command); r.Response != model.RESP_EXECUTED {
-// t.Fatal("/loadtest url for README.md should've executed")
-// }
-
-// command = "/loadtest url test-emoticons.md"
-// if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.Command); r.Response != model.RESP_EXECUTED {
-// t.Fatal("/loadtest url for test-emoticons.md should've executed")
-// }
-
-// command = "/loadtest url test-emoticons"
-// if r := Client.Must(Client.Command(channel.Id, command, false)).Data.(*model.Command); r.Response != model.RESP_EXECUTED {
-// t.Fatal("/loadtest url for test-emoticons should've executed")
-// }
-
-// posts := Client.Must(Client.GetPosts(channel.Id, 0, 5, "")).Data.(*model.PostList)
-// // note that this may make more than 3 posts if files are too long to fit in an individual post
-// if len(posts.Order) < 3 {
-// t.Fatal("/loadtest url made too few posts, perhaps there needs to be a delay before GetPosts in the test?")
-// }
-// }