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

Changed add_token.py for issue #4526 #4546

Merged
merged 1 commit into from
Jun 27, 2024

Conversation

ShrishtiKarkera
Copy link
Contributor

Summary

Changes

/microk8s/scripts/wrappers/add_token.py

Testing

The change was tested with a unittest in tests/unit/test_addtoken.py
Tested using mock print assertion with builtins.print

Possible Regressions

Checklist

  • [Yes] Read the contributions page.
  • [Yes] Submitted the CLA form, if you are a first time contributor.
  • [Yes] The introduced changes are covered by unit and/or integration tests.

Notes

Copy link
Contributor

@neoaggelos neoaggelos left a comment

Choose a reason for hiding this comment

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

Thanks a lot @ShrishtiKarkera! This is going in the right direction, and solves the issue at hand. However, there are things that can be improved, please have a look at the comments.

Comment on lines 128 to 131
addr = str(ip) + ":" + str(port) + "/" + str(token) + "/" + str(check)
if addr not in addr_set:
print(f"microk8s join {ip}:{port}/{token}/{check}")
addr_set.add(addr)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is creating a set of IPs we have already "seen". However, the ask here is to simply avoid repeating the default_ip. It should be simpler if you simply check for ip == default_ip in the for loop, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright!
I was thinking if the there were duplicate ips in the all_ip list apart from default ip but its much simpler if the default ip is the only problem.
Thanks!

@mock.patch('builtins.print')
@mock.patch('add_token.get_network_info', return_value=['10.23.53.54', ['10.23.53.54'], str(32)])
def test_add_token(self, addtoken, mock_print):
# self.assertEqual(print_short('t', 'c'), 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this leftover from debugging? do we need it?

from unittest import mock, TestCase
from add_token import print_short

class test_at(TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

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

test classes are typically named with camel-case: TestSomething. Please use a meaningful name for what we test here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad, test_at meant test_addtoken but I'll be more clearer.
Thanks


class test_at(TestCase):
@mock.patch('builtins.print')
@mock.patch('add_token.get_network_info', return_value=['10.23.53.54', ['10.23.53.54'], str(32)])
Copy link
Contributor

Choose a reason for hiding this comment

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

let's be consistent and use '32' as well, instead of str(32)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it.

tests/unit/test_addtoken.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

Changed add_token.py to just check for the default_ip case.
Added another test case with multiple ip addresses.

Copy link
Contributor

@neoaggelos neoaggelos left a comment

Choose a reason for hiding this comment

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

Looks nicer! Did a second pass

@mock.patch('add_token.get_network_info', return_value=['d_ip', ['ip1', 'ip2', 'd_ip'], '4'])
def test_multiple(self, multiple, mock_print):
print_short('t', 'c')
mock_print.assert_called_with('microk8s join ip2:4/t/c')
Copy link
Contributor

Choose a reason for hiding this comment

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

this works, but does not tell the whole story. please make sure that you test all print invocations are as expected, not only the last one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

tests/unit/test_addtoken.py Outdated Show resolved Hide resolved
Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

I couldn't use the inbuilt assertion as it has a limit so I used capsys to check for all output lines.
Please let me know if anything else is required to be done.

Comment on lines 17 to 20
all_outputs[0] == f"microk8s join d_ip:4/t/c"
assert all_outputs[1] == f"microk8s join ip1:4/t/c"
assert all_outputs[2] == f"microk8s join ip2:4/t/c"
assert len(all_outputs) == 3
Copy link
Contributor

Choose a reason for hiding this comment

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

this can be a single assert all_outputs == [cmd1, cmd2, cmd3]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

Removed multiple assertions and added a single assert statement

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

Checked for cla and lint

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

checked for lint and cla

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

checking for lint and cla try 2

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

Lint passes, checking for cla

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

checking for cla

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

Final check

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

Checked all tests

Copy link
Contributor

@neoaggelos neoaggelos left a comment

Choose a reason for hiding this comment

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

Please rebase and cleanup the commit history, then we should be good to merge!

if "Address" in data:
port = data["Address"].rsplit(":")[-1]
except OSError:
pass

Copy link
Contributor

Choose a reason for hiding this comment

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

this is unrelated to the changes of this PR, please revert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure

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'm so sorry, I'll fix this

Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

Removed the join.py commit from the commit history

if "Address" in data:
port = data["Address"].rsplit(":")[-1]
except OSError:
pass

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'm so sorry, I'll fix this

tested with unittest (test_addtoken.py)

tested with unittest (test_addtoken.py)

changed and tested add_token.py canonical#4526

changed and tested add_token.py

checked test_multiple for all outputs

changed assertion in test_multiple()

checked lint and cla

checking for lint and cla

checking for cla

checking for cla

checking for cla

checking final

checking final

final push
Copy link
Contributor Author

@ShrishtiKarkera ShrishtiKarkera left a comment

Choose a reason for hiding this comment

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

Squashed all commits into one

@neoaggelos neoaggelos merged commit 0a11d61 into canonical:master Jun 27, 2024
12 of 14 checks passed
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.

2 participants