Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task: Reset config cmd #3496

Merged
merged 22 commits into from
Jul 12, 2022
Merged

Task: Reset config cmd #3496

merged 22 commits into from
Jul 12, 2022

Conversation

pocockn
Copy link
Contributor

@pocockn pocockn commented Jul 4, 2022

Description

Step 1: refactor command _configResetCmd into ./ioctl/newcmd/config/config_reset.go
Step 2: create ./ioctl/newcmd/config/config_reset_test.go to add unit test

Fixes #3468

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • make test

pocockn and others added 6 commits June 9, 2022 15:18
* upstream/master: (24 commits)
  account type with zero init nonce (iotexproject#3387)
  [api] Separate Server and Server Handler (iotexproject#3485)
  [ioctl] Build hdwallet derive command line into new ioctl (iotexproject#3418)
  [ioctl] Build hdwallet create command line into new ioctl (iotexproject#3470)
  [makefile] add go mod tidy (iotexproject#3471)
  [api] update chain metrics (iotexproject#3484)
  remove config.EVMNetworkID() (iotexproject#3460)
  [filedao] remove checkMasterChainDBFile() (iotexproject#3463)
  [api] add crashlog (iotexproject#3456)
  [api] Move generateBlockMeta to grpcserver.go (iotexproject#3303)
  [ioctl] Build action hash command line into new ioctl (iotexproject#3425)
  [ioctl] Build hdwallet export command line into new ioctl (iotexproject#3423)
  [ioctl] Refactor nodereward command in new ioctl (iotexproject#3416)
  [ioctl] Cleanup TestNewNodeDelegateCmd (iotexproject#3421)
  [blockchain] Remove BoltDBDaoOption (iotexproject#3465)
  remove InMemDaoOption (iotexproject#3464)
  [action] add evm london test (iotexproject#3402)
  [ioctl] create main for ioctl/newcmd (iotexproject#3296)
  [ioctl] Build block bucket command line into new ioctl (iotexproject#3386)
  [ioctl] Build hdwallet import command line into new ioctl (iotexproject#3419)
  ...
@pocockn pocockn requested a review from a team as a code owner July 4, 2022 13:29
@codecov
Copy link

codecov bot commented Jul 4, 2022

Codecov Report

Merging #3496 (5488537) into master (a20e489) will decrease coverage by 0.84%.
The diff coverage is 60.24%.

@@            Coverage Diff             @@
##           master    #3496      +/-   ##
==========================================
- Coverage   75.43%   74.59%   -0.85%     
==========================================
  Files         247      252       +5     
  Lines       22845    23133     +288     
==========================================
+ Hits        17233    17255      +22     
- Misses       4685     4951     +266     
  Partials      927      927              
Impacted Files Coverage Δ
blockchain/blockchain.go 0.89% <0.00%> (ø)
config/config.go 94.06% <ø> (+9.31%) ⬆️
actpool/config.go 60.00% <60.00%> (ø)
blockchain/config.go 65.78% <65.78%> (ø)
action/protocol/poll/protocol.go 68.13% <66.66%> (-0.35%) ⬇️
actpool/actpool.go 87.09% <100.00%> (ø)
blockchain/filedao/testing.go 59.71% <100.00%> (ø)
ioctl/client.go 73.61% <100.00%> (+0.32%) ⬆️
ioctl/newcmd/config/config_reset.go 100.00% <100.00%> (ø)
state/factory/factory.go 78.91% <100.00%> (ø)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7b46cdc...5488537. Read the comment docs.

client.EXPECT().Config().Return(config.Config{})
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)

client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(1)

cmd := NewConfigReset(client)
result, err := util.ExecuteCmd(cmd, "reset")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. the failure case with returning error is not simulated or covered
  2. unit test for config.reset should be added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a failure case for the command and a unit test for the reset. I had to pass in the config file name into the command to be able to simulate an error, let me know what you think. Cheers

)

// NewConfigReset resets the config to the default values
func NewConfigReset(client ioctl.Client, defaultConfigFileName string) *cobra.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete argument because user can't know file name and path of defaultConfigFileName.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can get it from client.

  1. add client.ConfigFilePath() in client.go
  2. call it in line23

Comment on lines 19 to 20
Use: "reset",
Short: "Reset config to default",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add Chinese translation like

_nodeCmdShorts = map[config.Language]string{
config.English: "Deal with nodes of IoTeX blockchain",
config.Chinese: "处理IoTeX区块链的节点",
}

then use client.SelectTranslation()

if err != nil {
return errors.Wrap(err, "failed to reset config")
}
cmd.Print("successfully reset config")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmd.Println

Comment on lines 23 to 24
info := newInfo(client.Config(), defaultConfigFileName)
err := info.reset()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. combine in one line: newInfo(client.Config(), defaultConfigFileName).reset()

  2. add unit test TestReset in config_test.go

client.EXPECT().Config().Return(config.Config{}).Times(2)

t.Run("successful config reset", func(t *testing.T) {
cmd := NewConfigReset(client, _defaultConfigFileName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the true file in test because it can delete the real file and can't revert. you can create temp dir using t.TempDir(), then create file under it

// use invalid file name to force error
cmd := NewConfigReset(client, "\x00")
_, err := util.ExecuteCmd(cmd, "reset")
require.Error(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check err with expected error can be better like require.Contains(err.Error(), "expect error")

})
}

func TestConfigReset(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put into config_test.go because reset() is in config.go

Comment on lines 59 to 65
assert.Equal(t, ".", cfg.Wallet)
assert.Equal(t, "", cfg.Endpoint)
assert.Equal(t, true, cfg.SecureConnect)
assert.Equal(t, "English", cfg.Language)
assert.Equal(t, _defaultAnalyserEndpoint, cfg.AnalyserEndpoint)
assert.Equal(t, "iotexscan", cfg.Explorer)
assert.Equal(t, *new(config.Context), cfg.DefaultAccount)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require.Equal

Comment on lines 54 to 55
config.DefaultConfigFile = _defaultConfigFileName
cfg, err := config.LoadConfig()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Don't use global because it may modify the true config data.
  2. use info.loadConfig() and check data with set before by line43~49. I think this should put into TestLoadConfig. it's just unit test.

pocockn added 3 commits July 6, 2022 13:20
…o task/config-reset

* 'task/config-reset' of github.com:pocockn/iotex-core:
  [consensus] remove config.EVMNetworkID() (iotexproject#3491)
Comment on lines 51 to 52
config.DefaultConfigFile = _defaultConfigFileName
cfg, err := config.LoadConfig()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use global because it may modify the true config data.
use info.loadConfig() and check data with set data lin40~46. I think this can put into TestLoadConfig. it's just unit test.

require.NoError(err)
require.Contains(result, "successfully reset config")

defer testutil.CleanupPath("config.default")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for the review, I've made the changes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for the review, I've made the changes

Welcome! 👍

)

// NewConfigReset resets the config to the default values
func NewConfigReset(client ioctl.Client) *cobra.Command {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use NewConfigResetCmd to be consistent with others

@Liuhaai Liuhaai merged commit 58b89f7 into iotexproject:master Jul 12, 2022
pocockn added a commit to pocockn/iotex-core that referenced this pull request Jul 19, 2022
* upstream/master: (28 commits)
  [ioctl] Incorrect conversion between integer types (iotexproject#3522)
  [action] fix incorrect conversion between integer types (iotexproject#3545)
  [test] fix TestLoadBlockchainfromDB (iotexproject#3521)
  [ioctl] correct Chinese usage message (iotexproject#3510)
  fix err not hanled (iotexproject#3509)
  add ReadHeaderTimeout (iotexproject#3539)
  [iotcl] Reset config cmd (iotexproject#3496)
  Update gosec.yaml
  Update ci.yaml
  Update analysis.yaml
  Delete gosec.yaml.bak
  Create gosec.yaml
  [config] move config.ActPool to actpool package refactor (iotexproject#3514)
  Update ci.yaml
  Update analysis.yaml
  Update analysis.yaml
  [config] move config.Chain to blockchain package (iotexproject#3511)
  remove circleci (iotexproject#3498)
  [ioctl] Build block bucketlist command line into new ioctl (iotexproject#3469)
  [config] remove  EVMNetworkID() and SetEVMNetworkID() (iotexproject#3503)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ioctl] build config reset command line into new ioctl
5 participants