From fd229a50234ba8a92c8d538aecf5acd1b37f9f0b Mon Sep 17 00:00:00 2001 From: hmhealey Date: Sat, 21 Nov 2015 12:01:14 -0500 Subject: Added /loadtest url command to load arbitrary files as posts --- api/command.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ api/command_test.go | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) diff --git a/api/command.go b/api/command.go index 50ca41155..db57f0bae 100644 --- a/api/command.go +++ b/api/command.go @@ -4,7 +4,9 @@ package api import ( + "io" "net/http" + "path" "strconv" "strings" "time" @@ -325,6 +327,9 @@ func loadTestCommand(c *Context, command *model.Command) bool { 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"}) } @@ -571,3 +576,71 @@ func loadTestPostsCommand(c *Context, command *model.Command) bool { 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 "}) + } + + return false +} diff --git a/api/command_test.go b/api/command_test.go index 476748c6b..9327850f3 100644 --- a/api/command_test.go +++ b/api/command_test.go @@ -10,6 +10,7 @@ import ( "github.com/mattermost/platform/model" "github.com/mattermost/platform/store" + "github.com/mattermost/platform/utils" ) func TestSuggestRootCommands(t *testing.T) { @@ -184,3 +185,58 @@ func TestEchoCommand(t *testing.T) { t.Fatal("Echo command failed to send") } } + +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.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?") + } +} -- cgit v1.2.3-1-g7c22