Kubernetes-REST

 view release on metacpan or  search on metacpan

t/12_patch.t  view on Meta::CPAN

#!/usr/bin/env perl
# Tests for the Kubernetes Patch API using mock data.
#
# Run:
#   prove -l t/12_patch.t

use strict;
use warnings;
use Test::More;
use Test::Exception;
use FindBin;
use lib "$FindBin::Bin/lib";
use lib "$FindBin::Bin/../lib";

use Test::Kubernetes::Mock qw(mock_api);
use Kubernetes::REST;
use Kubernetes::REST::Server;
use Kubernetes::REST::AuthToken;
use IO::K8s;

# Build a mock API with programmable responses
my $mock_io = Test::Kubernetes::Mock::IO->new;

my $api = Kubernetes::REST->new(
    server      => Kubernetes::REST::Server->new(endpoint => 'http://mock.local'),
    credentials => Kubernetes::REST::AuthToken->new(token => 'MockToken'),
    resource_map_from_cluster => 0,
    io          => $mock_io,
);

# Simulated patched deployment response
my $deploy_response = {
    apiVersion => 'apps/v1',
    kind       => 'Deployment',
    metadata   => {
        name            => 'my-app',
        namespace       => 'default',
        resourceVersion => '200',
        uid             => 'uid-deploy-1',
        labels          => {
            app => 'my-app',
            env => 'staging',
        },
        annotations => {
            'demo/patched' => 'true',
        },
    },
    spec => {
        replicas => 5,
        selector => { matchLabels => { app => 'my-app' } },
        template => {
            metadata => { labels => { app => 'my-app' } },
            spec => {
                containers => [{
                    name  => 'nginx',
                    image => 'nginx:1.27',
                }],
            },
        },
    },
};

# Register mock responses for PATCH requests
$mock_io->add_response('PATCH', '/apis/apps/v1/namespaces/default/deployments/my-app', $deploy_response);

# Simulated patched namespace response (cluster-scoped)
my $ns_response = {
    apiVersion => 'v1',
    kind       => 'Namespace',
    metadata   => {
        name            => 'test-ns',
        resourceVersion => '300',
        uid             => 'uid-ns-1',
        labels          => {
            env      => 'test',
            new_label => 'added',
        },
    },
    spec   => { finalizers => ['kubernetes'] },
    status => { phase => 'Active' },
};
$mock_io->add_response('PATCH', '/api/v1/namespaces/test-ns', $ns_response);

# === Test 1: Strategic merge patch (default) by class + name ===
subtest 'strategic merge patch by name' => sub {



( run in 1.564 second using v1.01-cache-2.11-cpan-524268b4103 )