Improve server faking in osc unit test framework.

Registered by Tang Chen

There are mainly two problems in the current osc unit framework when it is faking a server:

1. It uses class FakeResource to fake a server, which is a subclass of Object.

That means, if we fake a server like this:

server = FakeResource(......)

we cannnot do this kind of call:

server.start(), server.stop(), server.pause(), ......

This is because it is not a Mock instance. This is also the reason why there is no such test cases in the current osc. So in order to test the methods of a server itself, we need an instance of class Mock to fake the server, instead of class Object.

The solution is make FakeResource inherit from Mock.

One related patch is: https://review.openstack.org/#/c/241924/
(Review in progress, and more similar test cases will be posted as this BP moving on.)

2. It cannot fake and pass more than one servers to the unit test case.

Currently, most of the server commands only take one server as parameter. We have had a BP [1] to improve them to take multiple servers at one time. After this, we need unit tests to test multiple server parameters.

But . For example, in class DeleteServer, it uses a loop to operate on each server:

    def take_action(self, parsed_args):
        ......
        for server in parsed_args.server:
            server_obj = utils.find_resource(
                compute_client.servers, server)
            compute_client.servers.delete(server_obj.id)
        ......

And in class TestServerDelete, it uses class FakeResource to fake the return value for utils.find_resource():

        # This is the return value for utils.find_resource()
        self.servers_mock.get.return_value = fakes.FakeResource(
            None,
            copy.deepcopy(compute_fakes.SERVER),
            loaded=True,
        )

This can fake only one server. If we enter the loop, we can get two same servers.

We can use side_effect in class Mock to solve this problem.

The BP [1] will continuously improve the commands, and this BP will improve the test framework, and continuously add test cases.

[1] https://blueprints.launchpad.net/python-openstackclient/+spec/cmd-with-multi-servers

Blueprint information

Status:
Not started
Approver:
None
Priority:
Undefined
Drafter:
Tang Chen
Direction:
Needs approval
Assignee:
Tang Chen
Definition:
New
Series goal:
None
Implementation:
Unknown
Milestone target:
None

Related branches

Sprints

Whiteboard

Gerrit topic: https://review.openstack.org/#q,topic:bp/osc-unit-test-framework-improvement,n,z

Addressed by: https://review.openstack.org/243977
    Enable FakeResource to fake methods.

Addressed by: https://review.openstack.org/243978
    Introduce random server faking mechanism.

Addressed by: https://review.openstack.org/241924
    Add unit tests for "server pause" command.

Addressed by: https://review.openstack.org/247967
    Use class FakeServer in TestServerCreate.

Addressed by: https://review.openstack.org/247968
    Use class FakeServer in TestServerDelete.

Addressed by: https://review.openstack.org/247969
    Use class FakeServer in TestServerImageCreate.

Addressed by: https://review.openstack.org/247970
    Use class FakeServer in TestServerResize.

Addressed by: https://review.openstack.org/247971
    Remove the old fake server data.

Addressed by: https://review.openstack.org/249559
    Move setup_servers_mock() to class TestServer.

Addressed by: https://review.openstack.org/249672
    Use setup_servers_mock() in the base class in TestServerDelete.

Addressed by: https://review.openstack.org/249673
    Add multiple servers test case to TestServerDelete.

Addressed by: https://review.openstack.org/250176
    Add unit tests for "server unpause" command.

Addressed by: https://review.openstack.org/250177
    Add unit tests for "server lock" command.

Addressed by: https://review.openstack.org/250178
    Add unit tests for "server unlock" command.

Addressed by: https://review.openstack.org/250179
    Add unit tests for "server suspend" command.

Addressed by: https://review.openstack.org/250180
    Add unit tests for "server resume" command.

Addressed by: https://review.openstack.org/250181
    Add unit tests for "server start" command.

Addressed by: https://review.openstack.org/250182
    Add unit tests for "server stop" command.

Addressed by: https://review.openstack.org/250317
    Abstract a helper function for server.xxx() tests.

Addressed by: https://review.openstack.org/251808
    Enable setup_servers_mock() to take attributes param.

Addressed by: https://review.openstack.org/251809
    Add class TestServerList to provide basic unit test for "server list" command.

Addressed by: https://review.openstack.org/254142
    TestServerGeneral: Add test for _format_servers_list_power_state()

Gerrit topic: https://review.openstack.org/#q,topic:formatter-tests-20151207,n,z

Addressed by: https://review.openstack.org/254314
    TestServerGeneral: Add test for _format_servers_list_networks()

Addressed by: https://review.openstack.org/254593
    TestServerGeneral: Add test for _prep_server_detail()

Addressed by: https://review.openstack.org/254860
    Add unit test for TestServerList to test --long option.

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.