Skip to content

Commit

Permalink
Merge pull request #3735 from Martchus/make-addresses-of-internal-cli…
Browse files Browse the repository at this point in the history
…ents-configurable

Make host names used by internal clients configurable
  • Loading branch information
mergify[bot] committed Feb 17, 2021
2 parents 91dd770 + be15e67 commit 7e82dbf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Makefile
Expand Up @@ -33,6 +33,8 @@ unstables := $(shell cat .circleci/unstable_tests.txt | tr '\n' :)
# tests need these environment variables to be unset
OPENQA_BASEDIR =
OPENQA_CONFIG =
OPENQA_SCHEDULER_HOST =
OPENQA_WEB_SOCKETS_HOST =

.PHONY: help
help:
Expand Down
14 changes: 7 additions & 7 deletions lib/OpenQA/Scheduler/Client.pm
@@ -1,4 +1,4 @@
# Copyright (C) 2014-2020 SUSE LLC
# Copyright (C) 2014-2021 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -14,13 +14,14 @@
# with this program; if not, see <http://www.gnu.org/licenses/>.

package OpenQA::Scheduler::Client;
use Mojo::Base -base;
use Mojo::Base -base, -signatures;

use OpenQA::Client;
use OpenQA::Log 'log_warning';
use OpenQA::Utils 'service_port';

has client => sub { OpenQA::Client->new(api => 'localhost') };
has host => sub { $ENV{OPENQA_SCHEDULER_HOST} };
has client => sub { OpenQA::Client->new(api => shift->host // 'localhost') };
has port => sub { service_port('scheduler') };

my $IS_SCHEDULER_ITSELF;
Expand All @@ -44,10 +45,9 @@ sub wakeup {

sub singleton { state $client ||= __PACKAGE__->new }

sub _api {
my ($self, $method) = @_;
my $port = $self->port;
return "http://127.0.0.1:$port/api/$method";
sub _api ($self, $method) {
my ($host, $port) = ($self->host // '127.0.0.1', $self->port);
return "http://$host:$port/api/$method";
}

1;
6 changes: 4 additions & 2 deletions lib/OpenQA/UserAgent.pm
@@ -1,4 +1,4 @@
# Copyright (C) 2018 SUSE LLC
# Copyright (C) 2018-2021 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -43,7 +43,9 @@ sub new {
for my $i (qw(key secret)) {
my $attr = "api$i";
next if $self->$attr;
(my $val = $cfg->val($args{api}, $i)) =~ s/\s+$//; # remove trailing whitespace
my $val = $cfg->val($args{api}, $i);
next unless $val;
$val =~ s/\s+$//; # remove trailing whitespace
$self->$attr($val);
}
last;
Expand Down
14 changes: 7 additions & 7 deletions lib/OpenQA/WebSockets/Client.pm
@@ -1,4 +1,4 @@
# Copyright (C) 2014-2019 SUSE LLC
# Copyright (C) 2014-2021 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -14,14 +14,15 @@
# with this program; if not, see <http://www.gnu.org/licenses/>.

package OpenQA::WebSockets::Client;
use Mojo::Base -base;
use Mojo::Base -base, -signatures;

use Mojo::Server::Daemon;
use Carp 'croak';
use OpenQA::Client;
use OpenQA::Utils 'service_port';

has client => sub { OpenQA::Client->new(api => 'localhost') };
has host => sub { $ENV{OPENQA_WEB_SOCKETS_HOST} };
has client => sub { OpenQA::Client->new(api => shift->host // 'localhost') };
has port => sub { service_port('websocket') };

my $IS_WS_SERVER_ITSELF;
Expand Down Expand Up @@ -59,10 +60,9 @@ sub send_msg {

sub singleton { state $client ||= __PACKAGE__->new }

sub _api {
my ($self, $method) = @_;
my $port = $self->port;
return "http://127.0.0.1:$port/api/$method";
sub _api ($self, $method) {
my ($host, $port) = ($self->host // '127.0.0.1', $self->port);
return "http://$host:$port/api/$method";
}

1;
17 changes: 16 additions & 1 deletion t/31-client.t
@@ -1,4 +1,4 @@
# Copyright (C) 2018-2020 SUSE LLC
# Copyright (C) 2018-2021 SUSE LLC
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -26,6 +26,21 @@ use Test::Warnings ':report_warnings';
use OpenQA::WebAPI;
use OpenQA::Test::Case;
use OpenQA::Script::Client;
use OpenQA::Scheduler::Client;
use OpenQA::WebSockets::Client;
use Mojo::File qw(tempdir);

subtest 'hostnames configurable' => sub {
my $config_dir = tempdir;
$config_dir->child('client.conf')->spurt("[foo]\nkey = fookey\nsome = config\n[bar]\nkey = barkey");
($ENV{OPENQA_CONFIG}, $ENV{OPENQA_SCHEDULER_HOST}, $ENV{OPENQA_WEB_SOCKETS_HOST}) = ($config_dir, qw(foo bar));
my $scheduler_client = OpenQA::Scheduler::Client->new;
is $scheduler_client->host, 'foo', 'scheduler hostname configurable';
is $scheduler_client->client->apikey, 'fookey', 'scheduler hostname passed to client';
my $ws_client = OpenQA::WebSockets::Client->new;
is $ws_client->host, 'bar', 'websockets hostname configurable';
is $ws_client->client->apikey, 'barkey', 'websockets hostname passed to client';
};

subtest 'client instantiation prevented from the daemons itself' => sub {
OpenQA::WebSockets::Client::mark_current_process_as_websocket_server;
Expand Down

0 comments on commit 7e82dbf

Please sign in to comment.