DBIO
view release on metacpan or search on metacpan
t/kubernetes/existing-namespace.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use Test::Exception;
use File::Temp ();
# Offline test for the two kinds of namespace DBIO::Test::Kubernetes works in
# (k223).
#
# WHY this is pinned: the module used to assume it owns its namespace -- it
# created one, used fixed object names in it (pod `mysql`, service
# `mysql-svc`), and deleted the whole namespace at the end. An account that is
# confined to one shared namespace can do neither, and the fixed names are the
# lesser problem there. The namespace this was written for already holds a pod
# `mysql` labelled app=mysql behind a LoadBalancer service selecting
# app=mysql, in use by other live suites. A second pod with that label joins
# that service's endpoints and takes a share of its connections; and a cleanup
# that deletes "the namespace", or "everything labelled role=dbio-test-db",
# takes the other databases with it.
#
# The claims, in an existing namespace:
# 1. no Namespace object is created and none is ever deleted;
# 2. names and selector labels are unique per run, and no selector that was
# there before matches a pod of ours;
# 3. cleanup deletes exactly what this run created, by kind and name -- also
# after a deploy that died halfway.
# And the namespace of its own keeps working as before: created, plain names,
# deleted as a whole.
#
# No cluster, no kubeconfig contents, no Kubernetes::REST: new() is the real
# one, handed a fake API through a stand-in for the kubeconfig loader.
my $API;
BEGIN {
package Kubernetes::REST::Kubeconfig;
sub new { bless {}, shift }
sub api { $API }
$INC{'Kubernetes/REST/Kubeconfig.pm'} = __FILE__;
}
{
package FakeCondition;
sub type { $_[0]{type} }
sub status { $_[0]{status} }
package FakeStatus;
sub conditions { $_[0]{conditions} }
package FakePod;
sub status { $_[0]{status} }
package FakeAPI;
sub new {
my ($class, %args) = @_;
return bless { created => [], deleted => [], got => [], %args }, $class;
}
sub new_object {
my ($self, $kind, %args) = @_;
return { kind => $kind, %args };
}
# fail_create_at => N makes the Nth create die, as the API does when a
# name is taken or the quota is full. The object is then NOT in `created`.
sub create {
my ($self, $obj) = @_;
die "create $obj->{kind} failed: 403 Forbidden\n"
if $self->{fail_create_at} && ++$self->{create_calls} == $self->{fail_create_at};
push @{ $self->{created} }, $obj;
return $obj;
}
sub get {
my ($self, $kind, $name, %args) = @_;
push @{ $self->{got} }, { kind => $kind, name => $name, %args };
die $self->{get_error} if $self->{get_error};
t/kubernetes/existing-namespace.t view on Meta::CPAN
my ($api) = @_;
return map { "$_->{kind}/$_->{metadata}{name}" } $api->created;
}
# What the namespace this was written for holds (dev-dbio, read 2026-10-07):
# the pods other suites run against, their labels, and the selectors that are
# live there -- the service selectors, plus the two a tool or a person deletes
# by.
my %FOREIGN_POD_LABELS = (
'dbio-ase' => { app => 'dbio-ase', 'cihq/dbio-db' => 'sybase', role => 'dbio-test-db' },
'dbio-clickhouse-db' => { app => 'dbio-clickhouse-db', 'cihq/dbio-db' => 'clickhouse', role => 'dbio-test-db' },
'dbio-firebird' => { app => 'dbio-firebird', 'cihq/dbio-db' => 'firebird', role => 'dbio-test-db' },
'dbio-mariadb' => { app => 'dbio-mariadb', 'cihq/dbio-db' => 'mariadb', role => 'dbio-test-db' },
'dbio-oracle' => { app => 'dbio-oracle', 'cihq/dbio-db' => 'oracle', role => 'dbio-test-db' },
'dbio-postgresql' => { app => 'dbio-postgresql', 'cihq/dbio-db' => 'postgresql', role => 'dbio-test-db' },
'mssql-dev-0' => { app => 'mssql-dev', role => 'dbio-test-db' },
'mysql' => { app => 'mysql', 'cihq/dbio-db' => 'mysql', role => 'dbio-test-db' },
);
my %FOREIGN_SELECTORS = (
'service dbio-ase-svc' => { app => 'dbio-ase' },
'service dbio-clickhouse-db-svc' => { app => 'dbio-clickhouse-db' },
'service dbio-firebird' => { app => 'dbio-firebird' },
'service dbio-mariadb' => { app => 'dbio-mariadb' },
'service dbio-oracle' => { app => 'dbio-oracle' },
'service dbio-postgresql' => { app => 'dbio-postgresql' },
'service mssql-dev' => { app => 'mssql-dev' },
'service mysql-svc' => { app => 'mysql' },
'role=dbio-test-db' => { role => 'dbio-test-db' },
'cihq/dbio-db=mysql' => { 'cihq/dbio-db' => 'mysql' },
'cihq/dbio-db=postgresql' => { 'cihq/dbio-db' => 'postgresql' },
);
my %FOREIGN_NAMES = map { $_ => 1 } (
keys %FOREIGN_POD_LABELS,
qw( dbio-ase-svc dbio-clickhouse-db-svc dbio-firebird dbio-mariadb
dbio-oracle dbio-postgresql mssql-dev mysql-svc ),
);
# --- existing namespace: nothing created that is not ours to create ---------
subtest 'existing namespace: no Namespace object, names and labels of the run' => sub {
my $k8s = k8s(existing_namespace => 'dev-dbio', databases => [qw(pg mysql)]);
my $api = $k8s->api;
quietly(sub { $k8s->deploy_databases });
is($k8s->namespace, 'dev-dbio', 'works in the namespace it was given');
is(scalar $api->of_kind('Namespace'), 0, 'no Namespace object is created');
my @pods = $api->of_kind('Pod');
my @svcs = $api->of_kind('Service');
is(scalar @pods, 2, 'two pods');
is(scalar @svcs, 2, 'two services');
for my $obj (@pods, @svcs) {
my $name = $obj->{metadata}{name};
is($obj->{metadata}{namespace}, 'dev-dbio', "$obj->{kind} $name is in dev-dbio");
ok(!$FOREIGN_NAMES{$name},
"$obj->{kind} $name does not take a name that is in use there");
like($name, qr/^[a-z][a-z0-9-]{0,62}$/, "$name is a valid object name");
}
# The real harm: one of our pods becoming an endpoint of their service,
# or being swept up by a selector somebody else deletes by.
for my $pod (@pods) {
my $name = $pod->{metadata}{name};
for my $who (sort keys %FOREIGN_SELECTORS) {
ok(!selects($FOREIGN_SELECTORS{$who}, $pod->{metadata}{labels}),
"pod $name is not matched by the selector of: $who");
}
}
# And the other direction: our service selects our pod of that database,
# and no other pod -- not theirs, not our other database's.
for my $i (0 .. $#svcs) {
my $selector = $svcs[$i]{spec}{selector};
my $name = $svcs[$i]{metadata}{name};
ok(scalar(keys %$selector), "service $name has a selector");
is($svcs[$i]{spec}{type}, undef,
"service $name is a plain ClusterIP service (reached by port-forward)");
my @hit = grep { selects($selector, $_->{metadata}{labels}) } @pods;
is_deeply([ map { $_->{metadata}{name} } @hit ], [ $pods[$i]{metadata}{name} ],
"service $name selects exactly its own pod");
ok(!selects($selector, $FOREIGN_POD_LABELS{$_}),
"service $name does not select the pod $_")
for sort keys %FOREIGN_POD_LABELS;
}
};
# --- existing namespace: two runs side by side ------------------------------
subtest 'existing namespace: two runs do not meet' => sub {
my $one = k8s(existing_namespace => 'dev-dbio', databases => [qw(pg mysql)]);
my $two = k8s(existing_namespace => 'dev-dbio', databases => [qw(pg mysql)]);
quietly(sub { $_->deploy_databases }) for $one, $two;
my %names_of_one = map { $_ => 1 } created_kind_name($one->api);
my @shared = grep { $names_of_one{$_} } created_kind_name($two->api);
is_deeply(\@shared, [], 'no object name is used by both runs');
for my $svc ($one->api->of_kind('Service')) {
ok(!selects($svc->{spec}{selector}, $_->{metadata}{labels}),
"service $svc->{metadata}{name} of one run does not select "
. "pod $_->{metadata}{name} of the other")
for $two->api->of_kind('Pod');
}
};
# --- existing namespace: the objects are found again under their names ------
subtest 'existing namespace: readiness and addresses use the created names' => sub {
my $k8s = k8s(existing_namespace => 'dev-dbio', databases => [qw(pg mysql)]);
my $api = $k8s->api;
quietly(sub {
$k8s->deploy_databases;
$k8s->wait_for_ready(timeout => 5, interval => 0);
});
is_deeply(
[ map { "$_->{name} in $_->{namespace}" } $api->got ],
[ map { "$_->{metadata}{name} in dev-dbio" } $api->of_kind('Pod') ],
'wait_for_ready asks for the pods this run created, not for `pg` and `mysql`',
( run in 0.650 second using v1.01-cache-2.11-cpan-eb134014566 )