File: 09-lockapi.t

package info (click to toggle)
os-autoinst 4.5.1527308405.8b586d5-4.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,688 kB
  • sloc: perl: 10,424; cpp: 1,527; python: 217; makefile: 211; sh: 71; xml: 11
file content (135 lines) | stat: -rwxr-xr-x 4,289 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl

use strict;
use warnings;
use Test::More;
use Test::MockModule;
use Test::Warnings;

BEGIN {
    unshift @INC, '..';
}

use lockapi;

# mock api_call return value
my $api_call_return;
my %locks;
my %action = (
    method => undef,
    action => undef,
    params => undef,
);

package ua_return;

sub new { my $t = shift; return bless {res => @_}, $t; }
sub code { return shift->{res} }
1;

package main;
# simulate responses from openQA WebUI or overridden by $api_call_return
sub fake_api_call {
    my ($method, $action, $params, $expected_codes) = @_;
    %action = (
        method => $method,
        action => $action,
        params => $params
    );
    return ua_return->new($api_call_return);
}

# monkey-patch mmap::api_call
my $mod = new Test::MockModule('lockapi');
$mod->mock(api_call => \&fake_api_call);

sub check_action {
    my ($method, $action, $params) = @_;
    my $res = 0;
    $res++ if ($method eq $action{method});
    $res++ if ($action eq $action{action});
    #     return unless($params

    %action = (
        method => undef,
        action => undef,
        params => undef,
    );
    return $res;
}

eval { mutex_create; };
ok($@, 'missing create name catched');
eval { mutex_try_lock; };
ok($@, 'missing try lock name catched');
eval { mutex_lock; };
ok($@, 'missing lock name catched');
eval { mutex_unlock; };
ok($@, 'missing unlock name catched');

# check successful ops
$api_call_return = 200;
ok(mutex_create('lock1'), 'mutex created');
ok(check_action('POST', 'mutex', {name => 'lock1'}), 'mutex_create request valid');

ok(mutex_lock('lock1'), 'mutex locked');
ok(check_action('POST', 'mutex/lock1', {action => 'lock'}), 'mutex_lock request valid');

ok(mutex_try_lock('lock1'), 'mutex locked');
ok(check_action('POST', 'mutex/lock1', {action => 'lock'}), 'mutex_lock request valid');

ok(mutex_unlock('lock1'), 'lock unlocked');
ok(check_action('POST', 'mutex/lock1', {action => 'unlock'}), 'mutex_unlock request valid');

# check unsuccessful ops
$api_call_return = 409;
ok(!mutex_create('lock1'), 'mutex not created');
ok(check_action('POST', 'mutex', {name => 'lock1'}), 'mutex_create request valid');

# instead of mutex_lock test mutex_try_lock to avoid block
ok(!mutex_try_lock('lock1'), 'mutex not locked');
ok(check_action('POST', 'mutex/lock1', {action => 'lock'}), 'mutex_lock request valid');

ok(!mutex_unlock('lock1'), 'lock not unlocked');
ok(check_action('POST', 'mutex/lock1', {action => 'unlock'}), 'mutex_unlock request valid');



# barriers testing
$api_call_return = 200;
eval { barrier_create; };
ok($@, 'missing create name catched');
eval { barrier_create('barrier1'); };
ok($@, 'missing create tasks catched');
eval { barrier_wait; };
ok($@, 'missing wait name catched');
eval { barrier_destroy; };
ok($@, 'missing destroy name catched');

ok(barrier_create('barrier1', 3), 'barrier created');
ok(check_action('POST', 'barrier', {name => 'barrier1', tasks => 3}), 'barrier create request valid');

ok(barrier_wait('barrier1'), 'registered for waiting and released immideately');
ok(check_action('POST', 'barrier/barrier1', undef), 'barrier wait request valid');

ok(barrier_wait {name => 'barrier1'}, 'registered for waiting and released immediately');
ok(check_action('POST', 'barrier/barrier1', undef), 'barrier wait request valid');

ok(barrier_wait({name => 'barrier1', check_dead_job => 1}), 'registered for waiting and destroy barrier if one of the jobs die');
ok(check_action('POST', 'barrier/barrier1', {check_dead_job => 1}), 'barrier wait request valid with check_dead_job');

ok(barrier_destroy('barrier1'), 'barrier destroyed');
ok(check_action('DELETE', 'barrier/barrier1', undef), 'barrier destroy request valid');

$api_call_return = 409;
ok(!barrier_create('barrier1', 3), 'barrier not created');
ok(check_action('POST', 'barrier', {name => 'barrier1', tasks => 3}), 'barrier create request valid');

# instead of barrier_wait test barrier_try_wait to avoid block
ok(!barrier_try_wait('barrier1'), 'registered for waiting and waiting');
ok(check_action('POST', 'barrier/barrier1', undef), 'barrier wait request valid');

ok(!barrier_destroy('barrier1'), 'barrier not destroyed');
ok(check_action('DELETE', 'barrier/barrier1', undef), 'barrier destroy request valid');

done_testing;