Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

t: Extract helper for client user agent initialization #3414

Merged
merged 4 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ aliases:
export PERL5OPT="$COVEROPT,-db,cover_db_${CIRCLE_JOB}"
echo PERL5OPT="$PERL5OPT"
make test-$CIRCLE_JOB
no_output_timeout: 30m
no_output_timeout: 50m

- &git_token_authentication
name: "Prepare auth token"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ test-unstable:

.PHONY: test-fullstack
test-fullstack:
$(MAKE) test-with-database FULLSTACK=1 TIMEOUT_M=20 PROVE_ARGS="$$HARNESS t/full-stack.t" RETRY=3
$(MAKE) test-with-database FULLSTACK=1 TIMEOUT_M=15 PROVE_ARGS="$$HARNESS t/full-stack.t" RETRY=3

.PHONY: test-scheduler
test-scheduler:
Expand Down
9 changes: 2 additions & 7 deletions t/09-job_clone.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ use OpenQA::Utils;
use Mojo::File 'tempdir';
use OpenQA::Jobs::Constants;
use OpenQA::Script::CloneJob;
use OpenQA::Test::Client 'client';
use OpenQA::Test::Database;
use OpenQA::Test::Utils qw(create_webapi stop_service);
use OpenQA::Test::TimeLimit '50';
use Test::Mojo;
use Test::Warnings ':report_warnings';

OpenQA::Test::Database->new->create(fixtures_glob => '01-jobs.pl');
my $t = Test::Mojo->new('OpenQA::WebAPI');
# XXX: https://github.com/kraih/mojo/issues/598
my $app = $t->app;
$t->ua(
OpenQA::Client->new(apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02')->ioloop(Mojo::IOLoop->singleton));
$t->app($app);

my $t = client(Test::Mojo->new('OpenQA::WebAPI'));
my $mojoport = Mojo::IOLoop::Server->generate_port;
my $host = "localhost:$mojoport";
my $webapi = create_webapi($mojoport, sub { });
Expand Down
15 changes: 4 additions & 11 deletions t/23-amqp.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use Mojo::IOLoop;

use FindBin;
use lib "$FindBin::Bin/lib";
use OpenQA::Client;
use OpenQA::Jobs::Constants;
use OpenQA::Test::Client 'client';
use OpenQA::Test::Database;
use OpenQA::Test::TimeLimit '24';
use Test::MockModule;
Expand All @@ -48,17 +48,10 @@ my $tempdir = tempdir;
$ENV{OPENQA_CONFIG} = $tempdir;
path($ENV{OPENQA_CONFIG})->make_path->child("openqa.ini")->spurt(@conf);

my $t = Test::Mojo->new('OpenQA::WebAPI');

# XXX: Test::Mojo loses its app when setting a new ua
# https://github.com/kraih/mojo/issues/598
my $app = $t->app;
$t->ua(
OpenQA::Client->new(apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02')->ioloop(Mojo::IOLoop->singleton));
$t->app($app);
my $t = client(Test::Mojo->new('OpenQA::WebAPI'));

# create a parent group
my $schema = $app->schema;
my $schema = $t->app->schema;
my $parent_groups = $schema->resultset('JobGroupParents');
$parent_groups->create(
{
Expand Down Expand Up @@ -273,7 +266,7 @@ $publisher_mock->redefine(
# we need an instance of the plugin now. I can't find a documented
# way to access the one that's already loaded...
my $amqp = OpenQA::WebAPI::Plugin::AMQP->new;
$amqp->register($app);
$amqp->register($t->app);

subtest 'amqp_publish call without headers' => sub {
$amqp->publish_amqp('some.topic', 'some message');
Expand Down
5 changes: 2 additions & 3 deletions t/27-websockets.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use POSIX;
use FindBin;
use lib ("$FindBin::Bin/lib", "../lib", "lib");
use OpenQA::Test::TimeLimit '24';
use OpenQA::Client;
use OpenQA::Jobs::Constants;
use OpenQA::WebSockets;
use OpenQA::WebSockets::Model::Status;
use OpenQA::Constants 'WEBSOCKET_API_VERSION';
use OpenQA::Test::Client 'client';
use OpenQA::Test::Database;
use OpenQA::Test::FakeWebSocketTransaction;
use Test::Output;
Expand All @@ -45,8 +45,7 @@ subtest 'Authentication' => sub {
$t->get_ok('/')->status_is(200)->json_is({name => $app->defaults('appname')});
local $t->app->config->{no_localhost_auth} = 0;
$t->get_ok('/')->status_is(403)->json_is({error => 'Not authorized'});
$t->ua(OpenQA::Client->new(apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02')
->ioloop(Mojo::IOLoop->singleton))->app($app);
client($t);
$t->get_ok('/')->status_is(200)->json_is({name => $app->defaults('appname')});
}
qr/auth by user: percival/, 'auth logged';
Expand Down
13 changes: 3 additions & 10 deletions t/31-client_archive.t
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,12 @@ use Test::Mojo;
use OpenQA::Client::Archive;
use Mojo::File qw(tempdir path);
use OpenQA::Test::Case;
use OpenQA::Test::Client 'client';

# init test case
OpenQA::Test::Case->new->init_data(fixtures_glob => '01-jobs.pl 03-users.pl 05-job_modules.pl');
my $t = Test::Mojo->new('OpenQA::WebAPI');

# XXX: Test::Mojo loses it's app when setting a new ua
# https://github.com/kraih/mojo/issues/598
my $app = $t->app;
$t->ua(
OpenQA::Client->new(apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02')->ioloop(Mojo::IOLoop->singleton));
$t->app($app);

my $t = client(Test::Mojo->new('OpenQA::WebAPI'));
my $destination = tempdir;

subtest 'OpenQA::Client:Archive tests' => sub {
my $jobid = 99938;
my $limit = 1024 * 1024;
Expand Down
41 changes: 14 additions & 27 deletions t/32-openqa_client.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ use Test::Most;

use FindBin;
use lib "$FindBin::Bin/lib", "../lib", "lib";
use OpenQA::Client;
use Test::Mojo;
use Mojo::File qw(tempfile path);
use OpenQA::Client;
use OpenQA::Events;
use OpenQA::Test::Case;
use OpenQA::Test::Client 'client';
use OpenQA::Test::TimeLimit '240';

OpenQA::Test::Case->new->init_data(fixtures_glob => '01-jobs.pl 02-workers.pl 03-users.pl');
Expand All @@ -32,25 +31,15 @@ my $chunk_size = 10000000;
# allow up to 200MB - videos mostly
$ENV{MOJO_MAX_MESSAGE_SIZE} = 207741824;

my $t = Test::Mojo->new('OpenQA::WebAPI');

OpenQA::Events->singleton->on(
'chunk_upload.end' => sub {
Devel::Cover::report() if Devel::Cover->can('report');
});

# XXX: Test::Mojo loses it's app when setting a new ua
# https://github.com/kraih/mojo/issues/598
my $app = $t->app;

sub new_client {
OpenQA::Client->new(apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02')->ioloop(Mojo::IOLoop->singleton);
}

my $client = new_client;
$t->ua($client);
$t->app($app);
my $base_url = $t->ua->server->url->to_string;
my @client_args = (apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02');
my $t = client(Test::Mojo->new('OpenQA::WebAPI'), @client_args);
my $client = $t->ua;
my $base_url = $client->server->url->to_string;
$client->base_url($base_url);
my $jobs = $t->app->schema->resultset('Jobs');
$jobs->find(99963)->update({state => 'running'});
Expand Down Expand Up @@ -229,10 +218,8 @@ subtest 'upload failures' => sub {
};

subtest 'upload internal errors' => sub {
my $client = new_client;
my $client = client($t, @client_args)->ua;
$client->base_url($base_url);
$t->ua($client);
$t->app($app);

my $chunkdir = 't/data/openqa/share/factory/tmp/other/00099963-hdd_image6.xml.CHUNKS/';
my $rp = "t/data/openqa/share/factory/other/00099963-hdd_image6.xml";
Expand All @@ -256,28 +243,28 @@ subtest 'upload internal errors' => sub {
};

subtest 'detecting local webui' => sub {
my $client = new_client()->base_url('http://openqa-staging-1.qa.suse.de');
my $client = $t->ua->base_url('http://openqa-staging-1.qa.suse.de');
ok !$client->upload->is_local, 'not a local webui';

$client = new_client()->base_url('http://localhost');
$client = $t->ua->base_url('http://localhost');
ok $client->upload->is_local, 'local webui';

$client = new_client()->base_url('http://127.0.0.1');
$client = $t->ua->base_url('http://127.0.0.1');
ok $client->upload->is_local, 'local webui';

$client = new_client()->base_url('http://127.0.0.1:3000');
$client = $t->ua->base_url('http://127.0.0.1:3000');
ok $client->upload->is_local, 'local webui';

$client = new_client()->base_url('http://[::1]');
$client = $t->ua->base_url('http://[::1]');
ok $client->upload->is_local, 'local webui';

$client = new_client()->base_url('http://[::1]:3000');
$client = $t->ua->base_url('http://[::1]:3000');
ok $client->upload->is_local, 'local webui';

$client = new_client()->base_url('http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:3000');
$client = $t->ua->base_url('http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:3000');
ok !$client->upload->is_local, 'not a local webui';

$client = new_client()->base_url('http://openqa-staging-1.qa.suse.de:3000');
$client = $t->ua->base_url('http://openqa-staging-1.qa.suse.de:3000');
ok !$client->upload->is_local, 'not a local webui';
};

Expand Down
18 changes: 4 additions & 14 deletions t/api/02-assets.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,11 @@ use Test::Mojo;
use Test::Warnings qw(:all :report_warnings);
use OpenQA::Test::TimeLimit '16';
use OpenQA::Test::Case;
use OpenQA::Client;
use OpenQA::Test::Client 'client';
use Mojo::IOLoop;

OpenQA::Test::Case->new->init_data(fixtures_glob => '01-jobs.pl 03-users.pl');

my $t = Test::Mojo->new('OpenQA::WebAPI');

# XXX: Test::Mojo loses it's app when setting a new ua
# https://github.com/kraih/mojo/issues/598
my $app = $t->app;
$t->ua(OpenQA::Client->new(apikey => 'ARTHURKEY01', apisecret => 'EXCALIBUR')->ioloop(Mojo::IOLoop->singleton));
$t->app($app);
my $t = client(Test::Mojo->new('OpenQA::WebAPI'), apikey => 'ARTHURKEY01', apisecret => 'EXCALIBUR');

sub la {
return unless $ENV{HARNESS_IS_VERBOSE};
Expand Down Expand Up @@ -157,11 +150,8 @@ $t->post_ok('/api/v1/assets/cleanup')->status_is(200)->json_is('/status' => 'ok'
->json_is('/gru_id' => undef);
is($gru_tasks->count, 1, 'no further task if one was already enqueued');

# switch to operator (percival) and try some modifications
$app = $t->app;
$t->ua(
OpenQA::Client->new(apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02')->ioloop(Mojo::IOLoop->singleton));
$t->app($app);
# switch to operator (default client) and try some modifications
client($t);

# test delete operation
$t->delete_ok('/api/v1/assets/' . ($listing->[1]->{id} + 1))->status_is(403, 'asset deletion forbidden for operator');
Expand Down
11 changes: 2 additions & 9 deletions t/api/02-iso-download.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ use Test::Mojo;
use Test::Warnings;
use OpenQA::Test::TimeLimit '50';
use OpenQA::Test::Case;
use OpenQA::Client;
use OpenQA::Test::Client 'client';
use Mojo::IOLoop;

use OpenQA::Utils 'locate_asset';

OpenQA::Test::Case->new->init_data(fixtures_glob => '01-jobs.pl 03-users.pl 04-products.pl');

my $t = Test::Mojo->new('OpenQA::WebAPI');
my $t = client(Test::Mojo->new('OpenQA::WebAPI'));

# Allow Devel::Cover to collect stats for background jobs
$t->app->minion->on(
Expand All @@ -43,13 +43,6 @@ $t->app->minion->on(
});
});

# XXX: Test::Mojo loses it's app when setting a new ua
# https://github.com/kraih/mojo/issues/598
my $app = $t->app;
$t->ua(
OpenQA::Client->new(apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02')->ioloop(Mojo::IOLoop->singleton));
$t->app($app);

my $gru_tasks = $t->app->schema->resultset('GruTasks');
my $gru_dependencies = $t->app->schema->resultset('GruDependencies');
my $test_suites = $t->app->schema->resultset('TestSuites');
Expand Down
24 changes: 7 additions & 17 deletions t/api/02-iso.t
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ use Test::Mojo;
use Test::Warnings ':report_warnings';
use OpenQA::Test::TimeLimit '560';
use OpenQA::Test::Case;
use OpenQA::Client;
use OpenQA::Test::Client 'client';
use OpenQA::Schema::Result::ScheduledProducts;
use Mojo::IOLoop;

OpenQA::Test::Case->new->init_data(fixtures_glob => '01-jobs.pl 03-users.pl 04-products.pl');

my $t = Test::Mojo->new('OpenQA::WebAPI');
my $t = client(Test::Mojo->new('OpenQA::WebAPI'));

# Allow Devel::Cover to collect stats for background jobs
$t->app->minion->on(
Expand All @@ -42,13 +41,6 @@ $t->app->minion->on(
});
});

# XXX: Test::Mojo loses it's app when setting a new ua
# https://github.com/kraih/mojo/issues/598
my $app = $t->app;
$t->ua(
OpenQA::Client->new(apikey => 'PERCIVALKEY02', apisecret => 'PERCIVALSECRET02')->ioloop(Mojo::IOLoop->singleton));
$t->app($app);

my $schema = $t->app->schema;
my $job_templates = $schema->resultset('JobTemplates');
my $test_suites = $schema->resultset('TestSuites');
Expand Down Expand Up @@ -382,9 +374,7 @@ is($res->json->{count}, 5, '5 new jobs created (two twice for both machine types
# can not do as operator
$t->delete_ok("/api/v1/isos/$iso")->status_is(403);
# switch to admin and continue
$app = $t->app;
$t->ua(OpenQA::Client->new(apikey => 'ARTHURKEY01', apisecret => 'EXCALIBUR')->ioloop(Mojo::IOLoop->singleton));
$t->app($app);
client($t, apikey => 'ARTHURKEY01', apisecret => 'EXCALIBUR');
$t->delete_ok("/api/v1/isos/$iso")->status_is(200);
# now the jobs should be gone
$t->get_ok('/api/v1/jobs/$newid')->status_is(404);
Expand All @@ -393,7 +383,7 @@ subtest 'jobs belonging to important builds are not cancelled by new iso post' =
$t->get_ok('/api/v1/jobs/99963')->status_is(200);
is($t->tx->res->json->{job}->{state}, 'running', 'job in build 0091 running');
my $tag = 'tag:0091:important';
$t->app->schema->resultset("JobGroups")->find(1001)->comments->create({text => $tag, user_id => 99901});
$schema->resultset("JobGroups")->find(1001)->comments->create({text => $tag, user_id => 99901});
$res = schedule_iso({%iso, _OBSOLETE => 1});
is($res->json->{count}, 10, '10 jobs created');
my $example = $res->json->{ids}->[9];
Expand All @@ -410,7 +400,7 @@ subtest 'jobs belonging to important builds are not cancelled by new iso post' =
is(scalar @jobs, 21, 'only the important jobs, jobs from the current build and the important build are scheduled');
# now test with a VERSION-BUILD format tag
$tag = 'tag:13.1-0093:important';
$t->app->schema->resultset("JobGroups")->find(1001)->comments->create({text => $tag, user_id => 99901});
$schema->resultset("JobGroups")->find(1001)->comments->create({text => $tag, user_id => 99901});
$res = schedule_iso({%iso, BUILD => '0094', _OBSOLETE => 1});
$t->get_ok('/api/v1/jobs?state=scheduled');
@jobs = @{$t->tx->res->json->{jobs}};
Expand Down Expand Up @@ -474,7 +464,7 @@ sub add_opensuse_test {
for my $key (keys %settings) {
push(@mapped_settings, {key => $key, value => $settings{$key}}) if $key ne 'MACHINE';
}
$t->app->schema->resultset('TestSuites')->create(
$schema->resultset('TestSuites')->create(
{
name => $name,
settings => \@mapped_settings
Expand All @@ -487,7 +477,7 @@ sub add_opensuse_test {
$param->{name} = $job_template_name if $job_template_name;
for my $machine (@{$settings{MACHINE}}) {
$param->{machine} = {name => $machine};
$t->app->schema->resultset('JobTemplates')->create($param);
$schema->resultset('JobTemplates')->create($param);
}
}

Expand Down
13 changes: 5 additions & 8 deletions t/api/03-auth.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ use Mojo::URL;
use Mojo::Util qw(encode hmac_sha1_sum);
use OpenQA::Test::TimeLimit '20';
use OpenQA::Test::Case;
use OpenQA::Client;
use OpenQA::Test::Client 'client';

OpenQA::Test::Case->new->init_data((fixtures_glob => '01-jobs.pl 03-users.pl'));

my $t = Test::Mojo->new('OpenQA::WebAPI');

# XXX: Test::Mojo loses it's app when setting a new ua
# https://github.com/kraih/mojo/issues/598
my $app = $t->app;
$t->ua(OpenQA::Client->new()->ioloop(Mojo::IOLoop->singleton));
$t->app($app);

# we don't want to *actually* delete any assets when we're testing
# whether we're allowed to or not, so let's mock that out
my $mock_asset_remove_callcount = 0;
Expand All @@ -58,6 +51,8 @@ subtest 'authentication routes for plugins' => sub {
$ensure_operator->put('/operator_plugin' => sub { shift->render(text => 'API operator plugin works!') });
};

client($t, apikey => undef, apisecret => undef);

subtest 'access limiting for non authenticated users' => sub() {
$t->get_ok('/api/v1/jobs')->status_is(200);
$t->get_ok('/api/v1/products')->status_is(200);
Expand All @@ -78,6 +73,8 @@ subtest 'access limiting for non authenticated users' => sub() {
$t->put_ok('/api/v1/operator_plugin')->status_is(403);
};

client($t);

subtest 'access limiting for authenticated users but not operators nor admins' => sub() {
$t->ua->apikey('LANCELOTKEY01');
$t->ua->apisecret('MANYPEOPLEKNOW');
Expand Down
Loading