Skip to content

Commit

Permalink
MM-54314: Return the error correctly from config parse failure (#649)
Browse files Browse the repository at this point in the history
https://mattermost.atlassian.net/browse/MM-54314

Co-authored-by: Alejandro García Montoro <[email protected]>
  • Loading branch information
agnivade and agarciamontoro authored Oct 14, 2023
1 parent 30a3c75 commit b616ffa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
13 changes: 9 additions & 4 deletions defaults/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions defaults/json_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
33 changes: 33 additions & 0 deletions loadtest/user/userentity/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,50 @@ 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"`
WebSocketURL string `default:"ws://localhost:8065" validate:"url"`
AdminEmail string `default:"[email protected]" 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 {
Expand Down

0 comments on commit b616ffa

Please sign in to comment.