Skip to content

Commit

Permalink
fixup! Config: Fix config version downgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Jan 20, 2025
1 parent 82174cf commit c95defc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func main() {
usage(fs)
os.Exit(3)
}
version = -1
version = versions.LatestVersion
}
if data, err = versions.Manager.Deploy(context.Background(), data, version); err != nil {
fatal("Unable to " + cmd + " config; Error: " + err.Error())
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ func (c *Config) readConfig(d io.Reader) error {
}
}

if j, err = versions.Manager.Deploy(context.Background(), j, -1); err != nil {
if j, err = versions.Manager.Deploy(context.Background(), j, versions.LatestVersion); err != nil {
return err
}

Expand Down
8 changes: 5 additions & 3 deletions config/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/thrasher-corp/gocryptotrader/common"
)

const LatestVersion = -1

Check failure on line 29 in config/versions/versions.go

View workflow job for this annotation

GitHub Actions / lint

exported: exported const LatestVersion should have comment or be unexported (revive)

var (
errMissingVersion = errors.New("missing version")
errVersionIncompatible = errors.New("version does not implement ConfigVersion or ExchangeVersion")
Expand Down Expand Up @@ -59,7 +61,7 @@ type manager struct {
var Manager = &manager{}

// Deploy upgrades or downgrades the config between versions
// version param -1 defaults to the latest version
// version param LatestVersion defaults to the latest version
// Prints an error an exits if the config file version or version param is not registered
func (m *manager) Deploy(ctx context.Context, j []byte, version int) ([]byte, error) {
if err := m.checkVersions(); err != nil {
Expand All @@ -72,7 +74,7 @@ func (m *manager) Deploy(ctx context.Context, j []byte, version int) ([]byte, er
}

target := latest
if version != -1 {
if version != LatestVersion {
target = version
}

Expand All @@ -83,7 +85,7 @@ func (m *manager) Deploy(ctx context.Context, j []byte, version int) ([]byte, er
current := int(current64)
switch {
case errors.Is(err, jsonparser.KeyPathNotFoundError):
current = -1
current = LatestVersion
case err != nil:
return j, fmt.Errorf("%w `version`: %w", common.ErrGettingField, err)
case target == current:
Expand Down
20 changes: 10 additions & 10 deletions config/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@ import (
func TestDeploy(t *testing.T) {
t.Parallel()
m := manager{}
_, err := m.Deploy(context.Background(), []byte(``), -1)
_, err := m.Deploy(context.Background(), []byte(``), LatestVersion)
assert.ErrorIs(t, err, errNoVersions)

m.registerVersion(1, &TestVersion1{})
_, err = m.Deploy(context.Background(), []byte(``), -1)
_, err = m.Deploy(context.Background(), []byte(``), LatestVersion)
require.ErrorIs(t, err, errVersionIncompatible)

m = manager{}

m.registerVersion(0, &Version0{})
_, err = m.Deploy(context.Background(), []byte(`not an object`), -1)
_, err = m.Deploy(context.Background(), []byte(`not an object`), LatestVersion)
require.ErrorIs(t, err, jsonparser.KeyPathNotFoundError, "Must throw the correct error trying to add version to bad json")
require.ErrorIs(t, err, common.ErrSettingField, "Must throw the correct error trying to add version to bad json")
require.ErrorContains(t, err, "version", "Must throw the correct error trying to add version to bad json")

_, err = m.Deploy(context.Background(), []byte(`{"version":"not an int"}`), -1)
_, err = m.Deploy(context.Background(), []byte(`{"version":"not an int"}`), LatestVersion)
require.ErrorIs(t, err, common.ErrGettingField, "Must throw the correct error trying to get version from bad json")

in := []byte(`{"version":0,"exchanges":[{"name":"Juan"}]}`)
j, err := m.Deploy(context.Background(), in, -1)
j, err := m.Deploy(context.Background(), in, LatestVersion)
require.NoError(t, err)
assert.Equal(t, string(in), string(j))

m.registerVersion(1, &Version1{})
j, err = m.Deploy(context.Background(), in, -1)
j, err = m.Deploy(context.Background(), in, LatestVersion)
require.NoError(t, err)
assert.Contains(t, string(j), `"version": 1`)

_, err = m.Deploy(context.Background(), j, 2)
assert.ErrorIs(t, err, errTargetVersion, "Downgrade to a unregistered version should not be allowed")

m.versions = append(m.versions, &TestVersion2{ConfigErr: true, ExchErr: false})
_, err = m.Deploy(context.Background(), j, -1)
_, err = m.Deploy(context.Background(), j, LatestVersion)
require.ErrorIs(t, err, errUpgrade)

m.versions[len(m.versions)-1] = &TestVersion2{ConfigErr: false, ExchErr: true}
_, err = m.Deploy(context.Background(), in, -1)
_, err = m.Deploy(context.Background(), in, LatestVersion)
require.Implements(t, (*ExchangeVersion)(nil), m.versions[1])
require.ErrorIs(t, err, errUpgrade)

Expand All @@ -58,7 +58,7 @@ func TestDeploy(t *testing.T) {
assert.Contains(t, string(j2), `"version": 0`, "Explicit downgrade should work correctly")

m.versions = m.versions[:1]
_, err = m.Deploy(context.Background(), j, -1)
_, err = m.Deploy(context.Background(), j, LatestVersion)
assert.ErrorIs(t, err, errConfigVersion, "Config version ahead of latest version should error")

_, err = m.Deploy(context.Background(), j, 0)
Expand All @@ -70,7 +70,7 @@ func TestDeploy(t *testing.T) {
func TestExchangeDeploy(t *testing.T) {
t.Parallel()
m := manager{}
_, err := m.Deploy(context.Background(), []byte(``), -1)
_, err := m.Deploy(context.Background(), []byte(``), LatestVersion)
assert.ErrorIs(t, err, errNoVersions)

v := &TestVersion2{}
Expand Down

0 comments on commit c95defc

Please sign in to comment.