diff --git a/defaults/json.go b/defaults/json.go index 887e7b4af..f62285483 100644 --- a/defaults/json.go +++ b/defaults/json.go @@ -10,19 +10,24 @@ import ( // This function will try to read from given path, if it is empty will try // fallback path. If it fails on fallback, it will set value to it's defaults func ReadFromJSON(path, fallbackPath string, value interface{}) error { + if err := Set(value); err != nil { + return err + } + if path != "" { if err := readJSON(path, &value); err != nil { - return err + return fmt.Errorf("failed to read from path %s: %w", path, err) } return nil } - if err := readJSON(fallbackPath, value); err == nil { + // If the fallback path doesn't exist, return. + if _, err := os.Stat(fallbackPath); err != nil && os.IsNotExist(err) { return nil } - if err := Set(value); err != nil { - return err + if err := readJSON(fallbackPath, value); err != nil { + return fmt.Errorf("failed to read from fallback path %s: %w", fallbackPath, err) } return nil diff --git a/defaults/json_test.go b/defaults/json_test.go new file mode 100644 index 000000000..f01264e1a --- /dev/null +++ b/defaults/json_test.go @@ -0,0 +1,42 @@ +package defaults + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type testCFG struct { + Setting string `default:"hi"` + Another int `default:"1"` +} + +func TestReadFromJSON(t *testing.T) { + cfg := testCFG{} + f, err := os.CreateTemp("", "loadtest") + require.NoError(t, err) + defer os.Remove(f.Name()) // clean up + + // Ensuring that a bad config throws an error + _, err = f.Write([]byte(`{"setting": "hello" "another": 1}`)) + require.NoError(t, err) + require.NoError(t, f.Close()) + + require.Error(t, ReadFromJSON("", f.Name(), &cfg)) + + cfg = testCFG{} + f1, err := os.CreateTemp("", "loadtest") + require.NoError(t, err) + defer os.Remove(f1.Name()) // clean up + + // Ensuring default values get correctly overridden + _, err = f1.Write([]byte(`{"setting": "hello"}`)) + require.NoError(t, err) + require.NoError(t, f1.Close()) + + require.NoError(t, ReadFromJSON("", f1.Name(), &cfg)) + assert.Equal(t, 1, cfg.Another) + assert.Equal(t, "hello", cfg.Setting) +} diff --git a/loadtest/user/userentity/helper_test.go b/loadtest/user/userentity/helper_test.go index 773c1504a..b62a18daa 100644 --- a/loadtest/user/userentity/helper_test.go +++ b/loadtest/user/userentity/helper_test.go @@ -8,10 +8,17 @@ import ( "github.com/mattermost/mattermost-load-test-ng/defaults" "github.com/mattermost/mattermost-load-test-ng/loadtest/store/memstore" + "github.com/mattermost/mattermost-load-test-ng/logger" "github.com/stretchr/testify/require" ) +type ratesDistribution struct { + Rate float64 `default:"1.0" validate:"range:[0,)"` + Percentage float64 `default:"1.0" validate:"range:[0,1]"` +} +type userControllerType string + type config struct { ConnectionConfiguration struct { ServerURL string `default:"http://localhost:8065" validate:"url"` @@ -19,6 +26,32 @@ type config struct { AdminEmail string `default:"sysadmin@sample.mattermost.com" validate:"email"` AdminPassword string `default:"Sys@dmin-sample1" validate:"notempty"` } + UserControllerConfiguration struct { + Type userControllerType `default:"simulative" validate:"oneof:{simple,simulative,noop,cluster,generative}"` + RatesDistribution []ratesDistribution `default_len:"1"` + ServerVersion string + } + InstanceConfiguration struct { + NumTeams int64 `default:"2" validate:"range:[0,]"` + NumChannels int64 `default:"10" validate:"range:[0,]"` + NumPosts int64 `default:"0" validate:"range:[0,]"` + NumReactions int64 `default:"0" validate:"range:[0,]"` + NumAdmins int64 `default:"0" validate:"range:[0,]"` + PercentReplies float64 `default:"0.5" validate:"range:[0,1]"` + PercentRepliesInLongThreads float64 `default:"0.05" validate:"range:[0,1]"` + PercentUrgentPosts float64 `default:"0.001" validate:"range:[0,1]"` + PercentPublicChannels float64 `default:"0.2" validate:"range:[0,1]"` + PercentPrivateChannels float64 `default:"0.1" validate:"range:[0,1]"` + PercentDirectChannels float64 `default:"0.6" validate:"range:[0,1]"` + PercentGroupChannels float64 `default:"0.1" validate:"range:[0,1]"` + } + UsersConfiguration struct { + UsersFilePath string + InitialActiveUsers int `default:"0" validate:"range:[0,$MaxActiveUsers]"` + MaxActiveUsers int `default:"2000" validate:"range:(0,]"` + AvgSessionsPerUser int `default:"1" validate:"range:[1,]"` + } + LogSettings logger.Settings } type TestHelper struct {