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

[ioctl] Build action command line into new ioctl #3472

Merged
merged 28 commits into from
Jul 16, 2022

Conversation

LuckyPigeon
Copy link
Contributor

Description

Refactor action command in new ioctl, with the following note.

  • Use client interface to construct the Cobra command.
  • Output package is deprecated, replace it with errors package.
  • Replace fmt.Println with cmd.Println
  • Refactor unit test to cover the modification.

Type of change

Please delete options that are not relevant.

  • Code refactor or improvement

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
  • TestNewActionHashCmd

Test Configuration:

  • Firmware version: Ubuntu 21.04
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@codecov
Copy link

codecov bot commented Jun 27, 2022

Codecov Report

Merging #3472 (e3c34b6) into master (a20e489) will decrease coverage by 1.42%.
The diff coverage is 38.00%.

@@            Coverage Diff             @@
##           master    #3472      +/-   ##
==========================================
- Coverage   75.43%   74.01%   -1.43%     
==========================================
  Files         247      253       +6     
  Lines       22845    23339     +494     
==========================================
+ Hits        17233    17274      +41     
- Misses       4685     5135     +450     
- Partials      927      930       +3     
Impacted Files Coverage Δ
blockchain/blockchain.go 0.89% <0.00%> (ø)
blockchain/pubsubmanager.go 0.00% <0.00%> (ø)
config/config.go 94.06% <ø> (+9.31%) ⬆️
ioctl/newcmd/alias/alias.go 0.00% <0.00%> (ø)
ioctl/newcmd/bc/bc.go 37.50% <0.00%> (+0.46%) ⬆️
ioctl/util/util.go 40.00% <0.00%> (ø)
ioctl/newcmd/action/action.go 20.62% <20.62%> (ø)
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%) ⬇️
... and 35 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 c1866c0...e3c34b6. Read the comment docs.

@LuckyPigeon LuckyPigeon marked this pull request as ready for review June 27, 2022 16:15
@LuckyPigeon LuckyPigeon requested a review from a team as a code owner June 27, 2022 16:15
@LuckyPigeon
Copy link
Contributor Author

LuckyPigeon commented Jun 27, 2022

@huof6829
Need help! Unit test for SendAction and Execute (Execute is depends on SendAction`), I couldn't find the correct private key for the signer. Here's my current code.

func TestSendAction(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
response := &iotexapi.SendActionResponse{}
tx := &action.Execution{}
elp := (&action.EnvelopeBuilder{}).
SetNonce(0).
SetGasPrice(unit.ConvertIotxToRau(1100000)).
SetGasLimit(100).
SetAction(tx).Build()
client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(2)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {})
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {})
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil)
apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(response, nil)
t.Run("sends signed action to blockchain", func(t *testing.T) {
cmd := NewActionCmd(client)
err := SendAction(client, cmd, elp, "test")
require.Error(err) // This should be NoError, but couldn't design a correct signer with public key.
})
}

@huof6829
Copy link
Contributor

@huof6829 Need help! Unit test for SendAction and Execute (Execute is depends on SendAction`), I couldn't find the correct private key for the signer. Here's my current code.

func TestSendAction(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
response := &iotexapi.SendActionResponse{}
tx := &action.Execution{}
elp := (&action.EnvelopeBuilder{}).
SetNonce(0).
SetGasPrice(unit.ConvertIotxToRau(1100000)).
SetGasLimit(100).
SetAction(tx).Build()
client.EXPECT().SelectTranslation(gomock.Any()).Return("mockTranslationString", config.English).Times(2)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Do(func(_ func(*string, string, string, string)) {})
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Do(func(_ func(*bool, string, bool, string)) {})
client.EXPECT().APIServiceClient().Return(apiServiceClient, nil)
apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(response, nil)
t.Run("sends signed action to blockchain", func(t *testing.T) {
cmd := NewActionCmd(client)
err := SendAction(client, cmd, elp, "test")
require.Error(err) // This should be NoError, but couldn't design a correct signer with public key.
})
}

I fixed. Pls check it. argument singer is generated from keystore which is created first. so the private key can be exported correctly.

@LuckyPigeon
Copy link
Contributor Author

@huof6829 Thanks for the help!

@huof6829
Copy link
Contributor

You can refer to action.go in #3444. it's the same file

@LuckyPigeon
Copy link
Contributor Author

@huof6829
When I write test for action.go, the command won't catch my flag args, even I've already gave args by the following command.

_, err := util.ExecuteCmd(cmd, "--signer", "test")

Did I miss understand something?
My code is in the following commit.

@huof6829
Copy link
Contributor

--signer is not registered in NewActionCmd() . you can catch flag after registered it in NewActionCmd before ExecuteCmd.

ioctl/newcmd/action/action.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action_test.go Show resolved Hide resolved
},
}
)

Copy link
Contributor

Choose a reason for hiding this comment

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

Is TestSendAction removed ?

Copy link
Contributor

@huof6829 huof6829 Jul 7, 2022

Choose a reason for hiding this comment

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

If codes is too large. we can split to small prs: one or two TestXxx per pr. What do you think of it ? @LuckyPigeon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TestSendAction has removed temporily, since it isn't completed yet.
I think sperate PRs will be better. TestSendRaw and TestSigner are alike, they can be in same PR. TestSendAction, TestExecute and TestRead and similar, they can be in a PR.

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'll open another PR for the rest 3 test case.

Copy link
Contributor

Choose a reason for hiding this comment

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

ioctl/newcmd/action/action_test.go Outdated Show resolved Hide resolved
ioctl/newcmd/action/action_test.go Outdated Show resolved Hide resolved
@huof6829
Copy link
Contributor

@LuckyPigeon This pr just finish TestSendRaw and TestSigner. Other Testxxx is another pr. Thanks.

@Liuhaai Liuhaai merged commit 7783ced into iotexproject:master Jul 16, 2022
pocockn added a commit to pocockn/iotex-core that referenced this pull request Jul 16, 2022
…task/config-get

* 'task/config-get' of github.com:pocockn/iotex-core:
  [ioctl] Build action command line into new ioctl (iotexproject#3472)
  fix potential file inclusion via variable (iotexproject#3549)
pocockn added a commit to pocockn/iotex-core that referenced this pull request Jul 20, 2022
…task/set-config

* 'task/set-config' of github.com:pocockn/iotex-core:
  [test] Disable workingset cache in the benchmark test (iotexproject#3558)
  [pkg] fix  deferring unsafe method "Close" on type "*os.File" (iotexproject#3548)
  [action] Refactor handleTransfer() (iotexproject#3557)
  Add MinVersion in tls.Config (iotexproject#3562)
  [ioctl] Modify file permission as 0600 (iotexproject#3563)
  [httputil] add ReadHeaderTimeout (iotexproject#3550)
  [staking] unexport namespace (iotexproject#3551)
  move chanid metrics to chainservice (iotexproject#3544)
  [ioctl] fix log entries created from user input (iotexproject#3546)
  add log in rolldposctx (iotexproject#3553)
  fix uncontrolled data used in path expression (iotexproject#3547)
  [api] impl. TestGrpcServer_GetServerMeta (iotexproject#3559)
  [ioctl] Build action command line into new ioctl (iotexproject#3472)
  fix potential file inclusion via variable (iotexproject#3549)
@LuckyPigeon LuckyPigeon mentioned this pull request Aug 4, 2022
11 tasks
pocockn added a commit to pocockn/iotex-core that referenced this pull request Jun 3, 2023
* upstream/master: (45 commits)
  Task: Get config cmd (iotexproject#3552)
  [ioctl] fix  Errors unhandled (iotexproject#3567)
  fix dir permission and file inclusion (iotexproject#3566)
  [test] Disable workingset cache in the benchmark test (iotexproject#3558)
  [pkg] fix  deferring unsafe method "Close" on type "*os.File" (iotexproject#3548)
  [action] Refactor handleTransfer() (iotexproject#3557)
  Add MinVersion in tls.Config (iotexproject#3562)
  [ioctl] Modify file permission as 0600 (iotexproject#3563)
  [httputil] add ReadHeaderTimeout (iotexproject#3550)
  [staking] unexport namespace (iotexproject#3551)
  move chanid metrics to chainservice (iotexproject#3544)
  [ioctl] fix log entries created from user input (iotexproject#3546)
  add log in rolldposctx (iotexproject#3553)
  fix uncontrolled data used in path expression (iotexproject#3547)
  [api] impl. TestGrpcServer_GetServerMeta (iotexproject#3559)
  [ioctl] Build action command line into new ioctl (iotexproject#3472)
  fix potential file inclusion via variable (iotexproject#3549)
  [ioctl] Incorrect conversion between integer types (iotexproject#3522)
  [action] fix incorrect conversion between integer types (iotexproject#3545)
  [test] fix TestLoadBlockchainfromDB (iotexproject#3521)
  ...
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.

4 participants