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

Hidden agent id flag #160

Merged
merged 4 commits into from
Dec 23, 2022
Merged

Hidden agent id flag #160

merged 4 commits into from
Dec 23, 2022

Conversation

arbulu89
Copy link
Contributor

@arbulu89 arbulu89 commented Dec 22, 2022

Add a new hidden agent-id flag to have the option to fake the agent id.
This comes handy during development/testing, as you can have predefined IDs, for whatever need you have.

I have added some dependency injection with Afero to manage the filesystem in the GetAgentID function. Not so nice to be passing this value all over the places, but well, it makes testing way better.

PD: Finally, I have changed the horrible testing of the config_test.go file using the teardown 🤦

@arbulu89 arbulu89 force-pushed the hidden-agent-id-flag branch from 599e9e7 to f55935f Compare December 22, 2022 15:04
@arbulu89 arbulu89 force-pushed the hidden-agent-id-flag branch from f55935f to fcdf523 Compare December 23, 2022 10:38
@arbulu89 arbulu89 added the enhancement New feature or request label Dec 23, 2022
Copy link
Contributor

@dottorblaster dottorblaster left a comment

Choose a reason for hiding this comment

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

Looks good, the only thing: maybe having something more explicit like --force-agent-id?

Copy link
Contributor

@dottorblaster dottorblaster left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@CDimonaco CDimonaco left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@nelsonkopliku nelsonkopliku left a comment

Choose a reason for hiding this comment

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

Looks good, even if I am not a fan either of passing afero around, however, that's what we have 😅

I find more compelling the changes that this PR requires to make, at least for me, to the id command.

test/helpers/filesystem.go Show resolved Hide resolved
@@ -12,7 +13,7 @@ func NewAgentIDCmd() *cobra.Command {
Use: "id",
Short: "Print the agent identifier",
RunE: func(cmd *cobra.Command, args []string) error {
agentID, err := agent.GetAgentID()
agentID, err := agent.GetAgentID(afero.NewOsFs())
Copy link
Member

Choose a reason for hiding this comment

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

This PR opens the need to refactor also id command I guess, because as of now it will always get the real agent id.

What I would expect is that when ./trento-agent id is executed it returns either what has been configured in agent's config file or the real agent id.

As an addition I wouldn't expect ./trento-agent id --force-agent-id=blaa, as it would be fancy 😅

Finally we'd need to take care of having the same defaults for both the commands, otherwise in one case viper would complain about not having some expected flags.

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 wouldn't go in this direction. This force-agent-id is a hidden flag for development/test purposes, and it shouldn't be used together with the id command, which is used to get the real id. It is only for the start function, which is the one that it makes sense.
Using ./trento-agent id --force-agent-id=blaa doesn't make any sense.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah true, that's just for test/dev so if that's how it has to be I can live with this little inconsistency 😉

Using ./trento-agent id --force-agent-id=blaa doesn't make any sense.

Yes, we're saying the same about this 😄

)

// MockMachineIDFile mocks the /etc/machine-id file to have a known value
func MockMachineIDFile() afero.Fs {
Copy link
Member

Choose a reason for hiding this comment

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

I find this a bit misleading as we're mocking the whole filesystem, not only the file 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would you call it?
I find the name together with the docstring pretty meaningful, but I wrote msyelf the function, so I'm biased

Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't call it 😜

However, I can think of MockFilesystemWithMachineID, MockFilesystemMachineID, which are by no means any better than MockMachineIDFile at the end 😅

So up to you.

Copy link
Contributor

@rtorrero rtorrero left a comment

Choose a reason for hiding this comment

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

All looks good to me, but maybe considering issuing a warning when force-agent-id is used.

@arbulu89 arbulu89 merged commit 9159f09 into main Dec 23, 2022
@arbulu89 arbulu89 deleted the hidden-agent-id-flag branch December 23, 2022 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

5 participants