-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MM-23900] api/server: accept loadtest.Config and control.Config for …
…agent creation (#223) * api/server: accept loadtest.Config and control.Config for agent creation * api/server: reflect review comments * docs: reflect changes on API update * cmd/ltagent: add log initialization to server command
- Loading branch information
Showing
5 changed files
with
210 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,9 +17,9 @@ import ( | |
"github.com/mattermost/mattermost-load-test-ng/loadtest/control/simulcontroller" | ||
"github.com/mattermost/mattermost-load-test-ng/loadtest/store/memstore" | ||
"github.com/mattermost/mattermost-load-test-ng/loadtest/user/userentity" | ||
"github.com/mattermost/mattermost-load-test-ng/logger" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/mattermost/mattermost-server/v5/mlog" | ||
) | ||
|
||
// API contains information about all load tests. | ||
|
@@ -42,55 +42,74 @@ func writeResponse(w http.ResponseWriter, status int, response *Response) { | |
} | ||
|
||
func (a *API) createLoadAgentHandler(w http.ResponseWriter, r *http.Request) { | ||
var config loadtest.Config | ||
err := json.NewDecoder(r.Body).Decode(&config) | ||
if err != nil { | ||
writeResponse(w, http.StatusBadRequest, &Response{ | ||
Error: err.Error(), | ||
}) | ||
return | ||
var data struct { | ||
LoadTestConfig loadtest.Config | ||
SimpleControllerConfig *simplecontroller.Config `json:",omitempty"` | ||
SimulControllerConfig *simulcontroller.Config `json:",omitempty"` | ||
} | ||
|
||
if err := config.IsValid(); err != nil { | ||
if err := json.NewDecoder(r.Body).Decode(&data); err != nil { | ||
writeResponse(w, http.StatusBadRequest, &Response{ | ||
Error: err.Error(), | ||
Error: fmt.Sprintf("could not read request: %s", err), | ||
}) | ||
return | ||
} | ||
logger.Init(&config.LogSettings) | ||
|
||
agentId := r.FormValue("id") | ||
if a.agents[agentId] != nil { | ||
ltConfig := data.LoadTestConfig | ||
if err := ltConfig.IsValid(); err != nil { | ||
writeResponse(w, http.StatusBadRequest, &Response{ | ||
Error: fmt.Sprintf("load-test agent with id %s already exists", agentId), | ||
Error: fmt.Sprintf("could not validate config: %s", err), | ||
}) | ||
return | ||
} | ||
|
||
var ucConfig control.Config | ||
switch config.UserControllerConfiguration.Type { | ||
var err error | ||
switch ltConfig.UserControllerConfiguration.Type { | ||
case loadtest.UserControllerSimple: | ||
// TODO: pass simplecontroller path appropriately | ||
ucConfig, err = simplecontroller.ReadConfig("") | ||
ucConfig = data.SimpleControllerConfig | ||
if ucConfig == nil { | ||
mlog.Warn("could not read controller config from the request") | ||
ucConfig, err = simplecontroller.ReadConfig("") | ||
} | ||
case loadtest.UserControllerSimulative: | ||
ucConfig, err = simulcontroller.ReadConfig("") | ||
ucConfig = data.SimulControllerConfig | ||
if ucConfig == nil { | ||
mlog.Warn("clould not read controller config from the request") | ||
ucConfig, err = simulcontroller.ReadConfig("") | ||
} | ||
} | ||
if err != nil { | ||
writeResponse(w, http.StatusBadRequest, &Response{ | ||
Error: fmt.Errorf("failed to read controller configuration: %w", err).Error(), | ||
Error: fmt.Sprintf("could not read controller configuration: %s", err), | ||
}) | ||
return | ||
} | ||
if ucConfig != nil { | ||
if err := ucConfig.IsValid(); err != nil { | ||
writeResponse(w, http.StatusBadRequest, &Response{ | ||
Error: fmt.Sprintf("could not validate controller configuration: %s", err), | ||
}) | ||
return | ||
} | ||
} | ||
|
||
agentId := r.FormValue("id") | ||
if a.agents[agentId] != nil { | ||
writeResponse(w, http.StatusBadRequest, &Response{ | ||
Error: fmt.Sprintf("load-test agent with id %s already exists", agentId), | ||
}) | ||
return | ||
} | ||
|
||
newControllerFn := func(id int, status chan<- control.UserStatus) (control.UserController, error) { | ||
ueConfig := userentity.Config{ | ||
ServerURL: config.ConnectionConfiguration.ServerURL, | ||
WebSocketURL: config.ConnectionConfiguration.WebSocketURL, | ||
ServerURL: ltConfig.ConnectionConfiguration.ServerURL, | ||
WebSocketURL: ltConfig.ConnectionConfiguration.WebSocketURL, | ||
Username: fmt.Sprintf("%s-user%d", agentId, id), | ||
Email: fmt.Sprintf("%s-user%[email protected]", agentId, id), | ||
Password: "testPass123$", | ||
} | ||
ue := userentity.New(memstore.New(), ueConfig) | ||
switch config.UserControllerConfiguration.Type { | ||
switch ltConfig.UserControllerConfiguration.Type { | ||
case loadtest.UserControllerSimple: | ||
return simplecontroller.New(id, ue, ucConfig.(*simplecontroller.Config), status) | ||
case loadtest.UserControllerSimulative: | ||
|
@@ -102,12 +121,12 @@ func (a *API) createLoadAgentHandler(w http.ResponseWriter, r *http.Request) { | |
} | ||
} | ||
|
||
lt, err := loadtest.New(&config, newControllerFn) | ||
lt, err := loadtest.New(<Config, newControllerFn) | ||
if err != nil { | ||
writeResponse(w, http.StatusBadRequest, &Response{ | ||
Id: agentId, | ||
Message: "load-test agent creation failed", | ||
Error: err.Error(), | ||
Error: fmt.Sprintf("could not create agent: %s", err), | ||
}) | ||
return | ||
} | ||
|
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
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
Oops, something went wrong.