qa: refactor and add tests
Change-Id: Ie135d25cca7229b6d713fa29ba0c05439930c542 Signed-off-by: Loic Dachary <loic@dachary.org>