Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

[1LP][RFR] Create test_import_tag and implement fixtures and functions it needs #10162

Merged
merged 11 commits into from
Jun 19, 2020

Conversation

prichard77
Copy link
Contributor

@prichard77 prichard77 commented Jun 3, 2020

Purpose or Intent

  • Adding test for importing tags from csv file to cover BZ 1792185.

PRT Run

{{ pytest: --use-provider vsphere65-nested cfme/tests/configure/test_tag.py::test_import_tag }}

@prichard77 prichard77 changed the title [WIP]Begin work on test_import_tag and create navigation and view for impo… [WIP] Create test_import_tag and implement fixtures and functions it needs Jun 5, 2020
@prichard77 prichard77 changed the title [WIP] Create test_import_tag and implement fixtures and functions it needs [WIPTEST] Create test_import_tag and implement fixtures and functions it needs Jun 5, 2020
@prichard77
Copy link
Contributor Author

I still might have some debugging to do in the csv file cleanup, but wanted to get the review started.

@dajoRH dajoRH added WIP-testing and removed WIP labels Jun 5, 2020
@@ -124,6 +124,30 @@ def test_map_tagging_crud(appliance, category, soft_assert):
.format(map_tag_entity.label))


@pytest.fixture
def csv_tag_file(create_vm, category, tag):
file_name = 'test_import_' + fauxfactory.gen_alphanumeric(4) + ".csv"
Copy link
Member

@mshriver mshriver Jun 5, 2020

Choose a reason for hiding this comment

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

I highly recommend using tempfile for this:
https://docs.python.org/3.7/library/tempfile.html

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@prichard77 prichard77 changed the title [WIPTEST] Create test_import_tag and implement fixtures and functions it needs [RFR] Create test_import_tag and implement fixtures and functions it needs Jun 5, 2020
@dajoRH dajoRH removed the WIP-testing label Jun 5, 2020
@@ -124,6 +126,29 @@ def test_map_tagging_crud(appliance, category, soft_assert):
.format(map_tag_entity.label))


@pytest.fixture
def csv_tag_file(create_vm, category, tag):
temp_file = tempfile.NamedTemporaryFile(suffix=".csv")
Copy link
Member

Choose a reason for hiding this comment

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

So, you can use tempfiles's context manager here to make this even simpler.

with tempfile.NamedTemporaryFile(suffix='.csv') as temp_file:
    tempfile.write(csv_data)
    yield temp_file.name

Note this is yielding the file name, not the file object, which is what the test actually needs anyway.

After the yield, the context manager exists and the file is deleted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated as suggested. I was on the fence about returning the object or just name. I relaize I just use the name, but was thinking I might want to access the object if I used this fixture for another test. Using it in another test seems quite unlikely now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

getting error trying to use tempfile: TypeError: expected str, bytes or os.PathLike object, not _TemporaryFileWrapper . Investigating.

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 was unable to get the shortened version to work. Tried a few things but was getting errors or creating empty files. Moving on to the asserts for the vm tag.


Args:
file_name: Name of .csv file containing tag data.
By default file must be at cfme/tests/configure/
Copy link
Member

Choose a reason for hiding this comment

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

Why must the file be at this location? tempfile should take care of the file location, and it shouldn't matter where the file is for the import in the UI.

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 the comment. his is where I was seeing the framework put the file. I guess it handles this location so we don't need to care about it.

@pytest.fixture
def csv_tag_file(create_vm, category, tag):
temp_file = tempfile.NamedTemporaryFile(suffix=".csv")
csv_data = f'name,category,entry\n{create_vm.name},{category.display_name},{tag.display_name}'
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't you be using a random alphanumeric for the tag, instead of trying to import a tag that matches an existing one?

The tag fixture that you're using here is a random tag that has already been created on the category.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. The import is to add the existing tag value to a VM. If the tag or category or VM do not exist, the upload fails. the terms here are confusing between a category, entry, tag etc. . I believe i have the bahavior ad it is in the BZ.

Copy link
Member

Choose a reason for hiding this comment

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

Can you add to the docblock for the test case the description that you're importing tag mappings to a VM, and not just tag definitions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Bugzilla:
1792185
"""
category.import_tag_from_file(csv_tag_file.name)
Copy link
Member

Choose a reason for hiding this comment

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

I think some additional assertions are necessary here - you have the tag and category display names, you should be able to instantiate a Tag and assert that it exists.

Look at something like:

assert category.tags.instantiate(display_name=tag.display_name).exists

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the tag does not exist the import will not work. The BZ issue was that an error gets displayed after "Upload" is clicked but before the apply. We are covering the BZ here. I guess I could add an assert that the tag exists on the VM?

Copy link
Member

Choose a reason for hiding this comment

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

gotcha, that makes more sense - please update the docblock with more detailed description of what's being tested.

Also correct about the additional assertion of the tag on the VM. You should be able to call tag in create_vm.get_tags, as the tag has an __eq__ implementation.

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 put the assert against the vm tags in the category.import_tag_from_file() method.
I could have put it in the test case itself, but chose the method.
If I put it in the testcase I wouldn't need to pass down the extra 2 args to import_tag_from_file and could leave more flexibility in other tests to check results for an already existing tag (just comparing the display name strings) without having to create the tag object.
But how much more tagging tests are we going to write? And if we do write more, couldn't I just move the assert out then?

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 moved the validation of the tag on the VM out into the testcase.

@dajoRH
Copy link
Contributor

dajoRH commented Jun 8, 2020

I detected some fixture changes in commit 0438283

The local fixture csv_tag_file is used in the following files:

  • cfme/tests/configure/test_tag.py
    • test_import_tag

Please, consider creating a PRT run to make sure your fixture changes do not break existing usage 😃

@prichard77 prichard77 requested a review from mshriver June 9, 2020 16:19
Copy link
Contributor

@john-dupuy john-dupuy left a comment

Choose a reason for hiding this comment

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

One question, otherwise LGTM 👍

@john-dupuy john-dupuy changed the title [RFR] Create test_import_tag and implement fixtures and functions it needs [1LP][RFR] Create test_import_tag and implement fixtures and functions it needs Jun 17, 2020
@jawatts jawatts merged commit 5521cd8 into ManageIQ:master Jun 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants