Changes between Initial Version and Version 1 of Obsolete/Development/Code/TestingComponents


Ignore:
Timestamp:
Apr 23, 2012, 9:25:36 PM (14 years ago)
Author:
munoz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Obsolete/Development/Code/TestingComponents

    v1 v1  
     1= Writing automatic tests for NCM components =
     2
     3'''This is work in progress!! '''
     4
     5[[TOC]]
     6
     7== Introduction ==
     8
     9We lack an automatic test suite. The reason is that many operations we do require root access, and it's very difficult to perform any tests locally. Also, previously it was not possible to load an arbitrary profile and feed the `Configuration` object to the component being tested.
     10
     11In any case, the consequences have been fatal: testing is expensive, as it requires:
     12
     13 1. Writing the component.
     14 1. Preparing a real profile with the component under test.
     15 1. SSH-ing into the test machine.
     16 1. Install the RPM, and do whatever tricks are needed to avoid SPMA from removing our test version.
     17 1. Running the component.
     18 1. Iterate.
     19
     20so we don't test nearly enough. And we don't dare to refactor code, because we cannot be completely sure that we are not breaking some existing use case. And regressions appear.
     21
     22This document proposes a strategy for automatically testing any "decent" Quattor code. Decent Quattor code is code that follows the [Development/Code/CodingStyle Coding style guide]. If you are responsible of a module that is not decent, go and fix it first.
     23
     24== Prerequisites ==
     25
     26For running any tests, you will need:
     27
     28* A check out of `perl-CAF` and `perl-LC` in your `PERL5LIB` environment variable.
     29* If you are writing a component (and most likely you are), a check out of the latest version of CCM, and add it to `PERL5LIB`.
     30* The latest Maven-based build tools ('''not yet released!!! ''').
     31* If you have any mockup profiles, the `panc` executable must be in your `PATH`.
     32
     33== Test layout ==
     34
     35* Tests are scripts with `.t` extension, stored under `src/test/perl`.
     36* Any resources needed for your tests, such as mockup profiles, should be stored in `src/test/resources`.
     37* There must be a smoke test, called `00-load.t` in that directory. Its only mission is to load your module. If you are using a recent enough version of the Maven build tools ('''not yet released'''), it is already there.
     38
     39== Writing a test ==
     40
     41=== Test header ===
     42
     43Obviously, you will be using your component: In addition to that, the canonical way of running tests in Perl is with `Test::Simple` or `Test::More`. You must use one of these (or any standard module that uses one of these). I recommend you to have a look at `Test::Tutorial`, `Test::Simple` and `Test::More` man pages.
     44
     45You want also your test to inspect any commands run and files used by your component. And you need to be sure that your test doesn't break your work station! To ease this, we provide another testing module: `Test::Quattor`. It will disable any dangerous operations for you, will store any `CAF::Process` and `CAF::File*` objects for later verification.
     46
     47Finally, you want an instance of your component. So, your test script will start like this:
     48
     49{{{
     50#!perl
     51use strict;
     52use warnings;
     53use NCM::Component::your_component;
     54use Test::More;
     55use Test::Quattor;
     56
     57my $comp = NCM::Component::your_component->new('your_component');
     58}}}
     59
     60=== Testing smaller functions ===
     61
     62Don't ever start testing your component with the `Configure` method. Instead, test first the behaviour of some if its lower functions. Just create the arguments they will receive from their callers, and call them yourself:
     63
     64{{{
     65#!perl
     66my $tree = {
     67   "users" => { a => 0,
     68                b => 1 }
     69};
     70my $result = $comp->method_that_takes_users($tree);
     71# Run any tests over the returned $result.
     72}}}
     73
     74=== Testing on files ===
     75
     76Any `CAF::FileWriter` or `CAF::FileEditor` objects created by your component are now stored internally by `Test::Quattor`. We can request it to give us each "modified" file with `get_file`, which is exported by default.
     77
     78{{{
     79#!perl
     80# Ensure the file /etc/foo has been opened
     81my $fh = get_file("/etc/foo"); # We have our file object here:
     82ok(defined($fh), "The file was opened");
     83is("$fh", "Some contents", "The file has received the expected contents");
     84is(*$fh->{options}->{mode}, 0700, "The file has the expected permissions");
     85}}}
     86
     87Now, let's imagine that our component was '''editing''' an existing file. In this case, we'll simulate some initial contents for it via the `set_file_contents` function:
     88
     89{{{
     90#!perl
     91set_file_contents("/etc/passwd", "root:x:0:0:root:/root/:/bin/bash\n");
     92# Call the function that will manipulate /etc/passwd here.
     93$fh = get_file("/etc/passwd");
     94}}}
     95
     96=== Tests on commands ===
     97
     98All commands should be run with `CAF::Process`. And we are accessing them with `get_command`:
     99
     100{{{
     101#!perl
     102$ok = $component->function_that_calls_ls($args);
     103ok($ok, "The function was successful");
     104my $cmd = get_command("/bin/ls -lh");
     105ok(defined($cmd), "ls was invoked");
     106}}}
     107
     108Now, this `$cmd` hash contains two elements:
     109* `object` is the `CAF::Process` object that encapsulates the command.
     110* `method` is the name of the method that got executed: `run`, `output`, `execute`...
     111
     112For instance, we want to be sure that this command was `run`, with its output discarded:
     113
     114{{{
     115#!perl
     116is($cmd->{method}, "run", "The correct method was called");
     117}}}
     118
     119=== Testing the `Configure` method ===
     120
     121This method should have almost no logic. But it still deserves some testing. It must receive a valid `EDG::CCM::Configuration` object from a profile. And handling this is done by `Test::Quattor`.