cosmos-sdk/cosmovisor/args_test.go

173 lines
4.3 KiB
Go
Raw Normal View History

package cosmovisor
import (
"fmt"
feat(cosmovisor): strict boolean argument parsing (#10018) ## Description Closes: #10017 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-08-30 08:17:12 -07:00
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/suite"
)
type argsTestSuite struct {
suite.Suite
}
func TestArgsTestSuite(t *testing.T) {
suite.Run(t, new(argsTestSuite))
}
func (s *argsTestSuite) TestConfigPaths() {
cases := map[string]struct {
cfg Config
upgradeName string
expectRoot string
expectGenesis string
expectUpgrade string
}{
"simple": {
cfg: Config{Home: "/foo", Name: "myd"},
upgradeName: "bar",
expectRoot: fmt.Sprintf("/foo/%s", rootName),
expectGenesis: fmt.Sprintf("/foo/%s/genesis/bin/myd", rootName),
expectUpgrade: fmt.Sprintf("/foo/%s/upgrades/bar/bin/myd", rootName),
},
"handle space": {
cfg: Config{Home: "/longer/prefix/", Name: "yourd"},
upgradeName: "some spaces",
expectRoot: fmt.Sprintf("/longer/prefix/%s", rootName),
expectGenesis: fmt.Sprintf("/longer/prefix/%s/genesis/bin/yourd", rootName),
expectUpgrade: "/longer/prefix/cosmovisor/upgrades/some%20spaces/bin/yourd",
},
}
for _, tc := range cases {
s.Require().Equal(tc.cfg.Root(), filepath.FromSlash(tc.expectRoot))
s.Require().Equal(tc.cfg.GenesisBin(), filepath.FromSlash(tc.expectGenesis))
s.Require().Equal(tc.cfg.UpgradeBin(tc.upgradeName), filepath.FromSlash(tc.expectUpgrade))
}
}
// Test validate
func (s *argsTestSuite) TestValidate() {
relPath := filepath.Join("testdata", "validate")
absPath, err := filepath.Abs(relPath)
s.Require().NoError(err)
testdata, err := filepath.Abs("testdata")
s.Require().NoError(err)
cases := map[string]struct {
cfg Config
valid bool
}{
"happy": {
cfg: Config{Home: absPath, Name: "bind"},
valid: true,
},
"happy with download": {
cfg: Config{Home: absPath, Name: "bind", AllowDownloadBinaries: true},
valid: true,
},
"missing home": {
cfg: Config{Name: "bind"},
valid: false,
},
"missing name": {
cfg: Config{Home: absPath},
valid: false,
},
"relative path": {
cfg: Config{Home: relPath, Name: "bind"},
valid: false,
},
"no upgrade manager subdir": {
cfg: Config{Home: testdata, Name: "bind"},
valid: false,
},
"no such dir": {
cfg: Config{Home: filepath.FromSlash("/no/such/dir"), Name: "bind"},
valid: false,
},
}
for _, tc := range cases {
err := tc.cfg.validate()
if tc.valid {
s.Require().NoError(err)
} else {
s.Require().Error(err)
}
}
}
func (s *argsTestSuite) TestEnsureBin() {
relPath := filepath.Join("testdata", "validate")
absPath, err := filepath.Abs(relPath)
s.Require().NoError(err)
cfg := Config{Home: absPath, Name: "dummyd"}
s.Require().NoError(cfg.validate())
s.Require().NoError(EnsureBinary(cfg.GenesisBin()))
cases := map[string]struct {
upgrade string
hasBin bool
}{
"proper": {"chain2", true},
"no binary": {"nobin", false},
"not executable": {"noexec", false},
"no directory": {"foobarbaz", false},
}
for _, tc := range cases {
err := EnsureBinary(cfg.UpgradeBin(tc.upgrade))
if tc.hasBin {
s.Require().NoError(err)
} else {
s.Require().Error(err)
}
}
}
feat(cosmovisor): strict boolean argument parsing (#10018) ## Description Closes: #10017 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-08-30 08:17:12 -07:00
func (s *argsTestSuite) TestBooleanOption() {
require := s.Require()
name := "COSMOVISOR_TEST_VAL"
check := func(def, expected, isErr bool, msg string) {
v, err := booleanOption(name, def)
if isErr {
require.Error(err)
return
}
require.NoError(err)
require.Equal(expected, v, msg)
}
os.Setenv(name, "")
check(true, true, false, "should correctly set default value")
check(false, false, false, "should correctly set default value")
os.Setenv(name, "wrong")
check(true, true, true, "should error on wrong value")
os.Setenv(name, "truee")
check(true, true, true, "should error on wrong value")
os.Setenv(name, "false")
check(true, false, false, "should handle false value")
check(false, false, false, "should handle false value")
os.Setenv(name, "faLSe")
check(true, false, false, "should handle false value case not sensitive")
check(false, false, false, "should handle false value case not sensitive")
os.Setenv(name, "true")
check(true, true, false, "should handle true value")
check(false, true, false, "should handle true value")
os.Setenv(name, "TRUE")
check(true, true, false, "should handle true value case not sensitive")
check(false, true, false, "should handle true value case not sensitive")
}