[ovirt-devel] Integration tests future (and very nice alternative for the DAO fixture file)

Roman Mohr rmohr at redhat.com
Wed Apr 13 13:22:09 UTC 2016


Hi all,

In [1] you can find some patches which are meant to improve the test
writing experience in ovirt-engine.

They provide the following things:

  A) Domain Object builders which can be used for creating and/or
persisting domain objects [2]
  B) DAO testing without writing fixtures because of the builders
  C) Integration testing for commands in conjunction with a real database
Arquillian, injectable commands and the builders [3]

# How to run what?

A) In normal unit tests just create a new instance of a builder and use it.
This should help us to get rid of all the small createDefaultVm(),
createHostWithX() helper methods in our tests.

B) In dao tests just inject them and go ahead. The advantage of not using
the fixture file is that we can now set up clean scenarios for every test
in a setup method. See example 2 below on how easy it is to set up a new
cluster.

C) Arquillian integration tests need to be marked with
"@Category(IntegrationTest.class)" and can inherit from
TransactionalTestBase. The @Category annotation makes sure that the
integration tests are only run when

    mvn clean verify -DskipITs=false

is invoked. Note that these tests are then executed in the integration test
phase of maven. For them we use the maven-failsafe-plugin[5] which will
also make sure that the testing database is up to date. See [4] for more
details.

# Examples

1) Add a running VM to a host, persist everything to the database and load
all VMs which are running on the host:

    VDS host = vdsBuilder.cluster(persistedCluster).persist();
    vmBuilder.host(host).up().persist();
    List<VM> vms = vmDao.getAllRunningForVds(host.getId());

2) Add 10 hosts with 1 GB of RAM to a cluster, persist the hosts to the
database in a DAO test:

    public class MyHostDaoTest extends BaseDaoTestCase {

        @Inject
        private VdsBuilder vdsBuilder;

        @Test
        public void createHosts() {
            VdsBuilder builder =
vdsBuilder.cluster(persistedCluster).physicalMemory(1000);
            for (int x =0; x < 10; x++){
                builder.id(Guid.newGuid()).persist();
        }
    }
}

3) Full integration test with arquillian and the database

    @Category(IntegrationTest.class)
    public class VmDaoIntegrationTest extends TransactionalTestBase {

        @Inject
        VmDao vmDao;

        private final Guid VM1_GUID =
Guid.createGuidFromString("0fe4bc81-5999-4ab6-80f8-7a4a2d4bfacd");

        @Deployment
        public static JavaArchive deploy(){
            return createDeployment();
        }

        @Test
        public void shouldFailOnExistingEntity() {

vmBuilder.id(VM1_GUID).cluster(clusterBuilder.reset().persist()).persist();
            // This uses assertThat from assertj:
            assertThat(vmDao.get(VM1_GUID)).isNotNull();
        }
    }

4) Using the builders in a normal unit test without a database:

    VM vm = new VmBuilder().id(Guid.newGuid()).up().build();


# How to add your own Domain objects?

There are just a few simple rules:

1) Your builder should extend org.ovirt.engine.core.builder.AbstractBuilder

2) Make sure that you only access DAOs injected into the builder during
#prePersist() and #persist(). This allows to use the #build() method also
without injections

3) #prePersist() should set all fields which are necessary to suffice
database constraints. The fields should only be set if they are not already
set before by the builder. When following this rule we can always persist
new objects to the database by simply calling myBuilder.reset().persist().

4) Mark your builder with @Repository to make them useable for our Spring
DAO tests and our Arquillian integration tests.

So have a look at the patches at [1] and let me know what you think about
them.

Best Regards and happy testing,

Roman

[1] https://gerrit.ovirt.org/#/q/topic:integration
[2] https://gerrit.ovirt.org/#/c/47008/17
[3] https://gerrit.ovirt.org/#/c/47007/10
[4] https://gerrit.ovirt.org/#/c/47008/17
[5] https://maven.apache.org/surefire/maven-failsafe-plugin/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ovirt.org/pipermail/devel/attachments/20160413/9e530418/attachment-0001.html>


More information about the Devel mailing list