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

SWIG bindings for user_cb_data in repo::DownloadCallbacks, unit tests #1849

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
33 changes: 33 additions & 0 deletions bindings/libdnf5/repo.i
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,40 @@
%include "libdnf5/repo/config_repo.hpp"

%feature("director") DownloadCallbacks;

%typemap(directorin, noblock=1) void * user_cb_data {
$input = SWIG_From_int(static_cast<int>(reinterpret_cast<intptr_t>($1)));
}

%typemap(directorout, noblock=1) void * {
int swig_val;
int swig_res = SWIG_AsVal_int($1, &swig_val);
if (!SWIG_IsOK(swig_res)) {
Swig::DirectorTypeMismatchException::raise(SWIG_ErrorType(SWIG_ArgError(swig_res)), "in output value of type '""int""'");
}
$result = reinterpret_cast<void *>(swig_val);
}

%typemap(in, noblock=1) void * user_cb_data {
{
int swig_val;
int swig_res = SWIG_AsVal_int($input, &swig_val);
if (!SWIG_IsOK(swig_res)) {
Swig::DirectorTypeMismatchException::raise(SWIG_ErrorType(SWIG_ArgError(swig_res)), "in input value of type '""int""'");
}
$1 = reinterpret_cast<void *>(swig_val);
}
}

%typemap(out, noblock=1) void * {
$result = SWIG_From_int(static_cast<int>(reinterpret_cast<intptr_t>($1)));
}

%include "libdnf5/repo/download_callbacks.hpp"
%typemap(directorin) void *;
%typemap(directorout) void * user_cb_data;
%typemap(in) void * user_cb_data;
%typemap(out) void *;
wrap_unique_ptr(DownloadCallbacksUniquePtr, libdnf5::repo::DownloadCallbacks);

%ignore FileDownloadError;
Expand Down
1 change: 1 addition & 0 deletions dnf5.spec
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ BuildRequires: perl(strict)
BuildRequires: perl(Test::More)
BuildRequires: perl(Test::Exception)
BuildRequires: perl(warnings)
BuildRequires: perl(FindBin)
%endif
%endif

Expand Down
53 changes: 38 additions & 15 deletions test/libdnf5/repo/test_package_downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <libdnf5/repo/package_downloader.hpp>
#include <libdnf5/rpm/package_query.hpp>

#include <algorithm>
#include <filesystem>

CPPUNIT_TEST_SUITE_REGISTRATION(PackageDownloaderTest);
Expand All @@ -41,13 +42,15 @@ class DownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
[[maybe_unused]] const char * description,
[[maybe_unused]] double total_to_download) override {
++add_new_download_cnt;
return nullptr;
std::string user_cb_data = std::string("Package: ") + description;
return user_cb_data_container.emplace_back(std::move(user_cb_data)).data();
}

int end([[maybe_unused]] void * user_cb_data, TransferStatus status, const char * msg) override {
++end_cnt;
end_status = status;
end_msg = libdnf5::utils::string::c_to_str(msg);
user_cb_data_array.emplace_back(static_cast<const char *>(user_cb_data));
end_status.emplace_back(status);
end_msg.emplace_back(libdnf5::utils::string::c_to_str(msg));
return 0;
}

Expand All @@ -68,41 +71,59 @@ class DownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
return 0;
}

int add_new_download_cnt = 0;
int end_cnt = 0;
TransferStatus end_status = TransferStatus::ERROR;
std::string end_msg;
std::vector<std::string> user_cb_data_container;

int add_new_download_cnt = 0;
int progress_cnt = 0;
int mirror_failure_cnt = 0;
int end_cnt = 0;

std::vector<const char *> user_cb_data_array;
std::vector<TransferStatus> end_status;
std::vector<std::string> end_msg;
};

void PackageDownloaderTest::test_package_downloader() {
auto repo = add_repo_rpm("rpm-repo1");

libdnf5::rpm::PackageQuery query(base);
query.filter_name("one");
query.filter_version("2");
query.filter_arch("noarch");
CPPUNIT_ASSERT_EQUAL((size_t)1, query.size());
CPPUNIT_ASSERT_EQUAL((size_t)2, query.size());

auto downloader = libdnf5::repo::PackageDownloader(base);

auto cbs_unique_ptr = std::make_unique<DownloadCallbacks>();
auto cbs = cbs_unique_ptr.get();
base.set_download_callbacks(std::move(cbs_unique_ptr));

downloader.add(*query.begin());
for (const auto & package : query) {
downloader.add(package);
}

downloader.download();

CPPUNIT_ASSERT_EQUAL(1, cbs->add_new_download_cnt);
CPPUNIT_ASSERT_EQUAL(1, cbs->end_cnt);
CPPUNIT_ASSERT_EQUAL(DownloadCallbacks::TransferStatus::SUCCESSFUL, cbs->end_status);
CPPUNIT_ASSERT_EQUAL(std::string(""), cbs->end_msg);
std::sort(cbs->user_cb_data_container.begin(), cbs->user_cb_data_container.end());
CPPUNIT_ASSERT_EQUAL(
(std::vector<std::string>{"Package: one-0:1-1.noarch", "Package: one-0:2-1.noarch"}),
cbs->user_cb_data_container);

CPPUNIT_ASSERT_GREATEREQUAL(1, cbs->progress_cnt);
CPPUNIT_ASSERT_EQUAL(2, cbs->add_new_download_cnt);
CPPUNIT_ASSERT_EQUAL(2, cbs->end_cnt);
CPPUNIT_ASSERT_GREATEREQUAL(2, cbs->progress_cnt);
CPPUNIT_ASSERT_EQUAL(0, cbs->mirror_failure_cnt);

std::sort(cbs->user_cb_data_array.begin(), cbs->user_cb_data_array.end(), [](const char * a, const char * b) {
return std::string_view(a).compare(b) < 0;
});
CPPUNIT_ASSERT_EQUAL(cbs->user_cb_data_container[0].c_str(), cbs->user_cb_data_array[0]);
CPPUNIT_ASSERT_EQUAL(cbs->user_cb_data_container[1].c_str(), cbs->user_cb_data_array[1]);

CPPUNIT_ASSERT_EQUAL(
(std::vector<DownloadCallbacks::TransferStatus>{
DownloadCallbacks::TransferStatus::SUCCESSFUL, DownloadCallbacks::TransferStatus::SUCCESSFUL}),
cbs->end_status);
CPPUNIT_ASSERT_EQUAL((std::vector<std::string>{"", ""}), cbs->end_msg);
}

void PackageDownloaderTest::test_package_downloader_temp_files_memory() {
Expand Down Expand Up @@ -136,6 +157,8 @@ void PackageDownloaderTest::test_package_downloader_temp_files_memory() {

CPPUNIT_ASSERT_EQUAL(4, cbs->add_new_download_cnt);
CPPUNIT_ASSERT_EQUAL(4, cbs->end_cnt);
CPPUNIT_ASSERT_GREATEREQUAL(4, cbs->progress_cnt);
CPPUNIT_ASSERT_EQUAL(0, cbs->mirror_failure_cnt);

auto paths_prefix = std::filesystem::path(repo->get_cachedir()) / std::filesystem::path("packages");
const std::vector<std::string> expected = {
Expand Down
2 changes: 1 addition & 1 deletion test/perl5/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ endif()


find_package(Perl REQUIRED)
foreach(MODULE "strict" "Test::More" "Test::Exception" "warnings")
foreach(MODULE "strict" "Test::More" "Test::Exception" "warnings" "FindBin")
message(STATUS "Checking for ${MODULE} Perl module")
execute_process(
COMMAND "${PERL_EXECUTABLE}" -e "require ${MODULE}"
Expand Down
110 changes: 110 additions & 0 deletions test/perl5/libdnf5/BaseTestCase.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Copyright Contributors to the libdnf project.
#
# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
#
# Libdnf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Libdnf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libdnf. If not, see <https://www.gnu.org/licenses/>.

package BaseTestCase;

use strict;
use warnings;

use File::Temp qw(tempdir);
use File::Spec::Functions 'catfile';

use libdnf5::base;
use libdnf5::repo;


my $PROJECT_BINARY_DIR = $ENV{"PROJECT_BINARY_DIR"};
my $PROJECT_SOURCE_DIR = $ENV{"PROJECT_SOURCE_DIR"};

sub new {
my $class = shift;
my $self = {};

$self->{base} = new libdnf5::base::Base();

$self->{tmpdir} = tempdir("libdnf5_perl5_unittest.XXXX", TMPDIR => 1, CLEANUP => 1);

my $config = $self->{base}->get_config();
$config->get_installroot_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $self->{tmpdir}."/installroot");
$config->get_cachedir_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $self->{tmpdir}."/cache");
$config->get_optional_metadata_types_option()->set($libdnf5::conf::Option::Priority_RUNTIME, $libdnf5::conf::OPTIONAL_METADATA_TYPES);

# Prevent loading plugins from host
$config->get_plugins_option()->set(0);

my $vars = $self->{base}->get_vars()->get();
$vars->set("arch", "x86_64");

$self->{base}->setup();

$self->{repo_sack} = $self->{base}->get_repo_sack();
$self->{package_sack} = $self->{base}->get_rpm_package_sack();

return bless ($self, $class);
}

sub tearDown {
my $self = shift;
# shutil.rmtree(self.temp_dir)
}

sub _add_repo {
# Add a repo from `repo_path`.
my $self = shift;
my $repoid = shift;
my $repo_path = shift;
my $load = shift // 1; # True is default

my $repo = $self->{repo_sack}->create_repo($repoid);
$repo->get_config()->get_baseurl_option()->set($libdnf5::conf::Option::Priority_RUNTIME, "file://".$repo_path);
if ($load) {
$self->{repo_sack}->load_repos($libdnf5::repo::Repo::Type_AVAILABLE);
}

return $repo
}

sub add_repo_repomd {
# Add a repo from PROJECT_SOURCE_DIR/test/data/repos-repomd/<repoid>/repodata
my $self = shift;
my $repoid = shift;
my $load = shift // 1; # True is default

my $repo_path = catfile($PROJECT_SOURCE_DIR, "/test/data/repos-repomd", $repoid);
return $self->_add_repo($repoid, $repo_path, $load)
}

sub add_repo_rpm {
# Add a repo from PROJECT_BINARY_DIR/test/data/repos-rpm/<repoid>/repodata
my $self = shift;
my $repoid = shift;
my $load = shift // 1; # True is default

my $repo_path = catfile($PROJECT_BINARY_DIR, "test/data/repos-rpm", $repoid);
return $self->_add_repo($repoid, $repo_path, $load)
}

sub add_repo_solv {
# Add a repo from PROJECT_SOURCE_DIR/test/data/repos-solv/<repoid>.repo
my $self = shift;
my $repoid = shift;

my $repo_path = catfile($PROJECT_SOURCE_DIR, "/test/data/repos-solv", $repoid.".repo");
return $self->{repo_sack}->create_repo_from_libsolv_testcase($repoid, $repo_path);
}

1;
113 changes: 113 additions & 0 deletions test/perl5/libdnf5/repo/test_package_downloader.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Copyright Contributors to the libdnf project.
#
# This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
#
# Libdnf is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Libdnf is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with libdnf. If not, see <https://www.gnu.org/licenses/>.

use strict;
use warnings;

use Test::More;

use FindBin;
use lib "$FindBin::Bin/..";
use BaseTestCase;

{
package PackageDownloadCallbacks;
use base qw(libdnf5::repo::DownloadCallbacks);

sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
$self->{user_cb_data_container} = [];
$self->{start_cnt} = 0;
$self->{progress_cnt} = 0;
$self->{mirror_failure_cnt} = 0;
$self->{end_cnt} = 0;
$self->{user_cb_data_array} = [];
$self->{end_status} = [];
$self->{end_msg} = [];
return bless($self, $class);
}

sub add_new_download {
my ($self, $user_data, $description, $total_to_download) = @_;
$self->{start_cnt}++;
my $user_cb_data = "Package: " . $description;
push(@{$self->{user_cb_data_container}}, $user_cb_data);
return scalar @{$self->{user_cb_data_container}} - 1;
}

sub end {
my ($self, $user_cb_data, $status, $error_message) = @_;
$self->{end_cnt}++;
::ok($user_cb_data>=0 && $user_cb_data<=1, "end: user_cb_data");
push @{$self->{user_cb_data_array}}, $user_cb_data;
push @{$self->{end_status}}, $status;
push @{$self->{end_msg}}, $error_message;
return 0;
}

sub progress {
my ($self, $user_cb_data, $total_to_download, $downloaded) = @_;
$self->{progress_cnt}++;
::ok($user_cb_data>=0 && $user_cb_data<=1, "progress: user_cb_data");
return 0;
}

sub mirror_failure {
my ($self, $user_cb_data, $msg, $url, $metadata) = @_;
$self->{mirror_failure_cnt}++;
::ok($user_cb_data>=0 && $user_cb_data<=1, "mirror_failure: user_cb_data");
return 0;
}
}

my $test = new BaseTestCase();

my $repo = $test->add_repo_rpm("rpm-repo1");

my $query = new libdnf5::rpm::PackageQuery($test->{base});
$query->filter_name(["one"]);
$query->filter_arch(["noarch"]);
is($query->size(), 2, "number of packages in query");

my $downloader = new libdnf5::repo::PackageDownloader($test->{base});

my $cbs = new PackageDownloadCallbacks();
$test->{base}->set_download_callbacks(new libdnf5::repo::DownloadCallbacksUniquePtr($cbs));

my $it = $query->begin();
my $e = $query->end();
while ($it != $e) {
$downloader->add($it->value());
$it->next();
}
$downloader->download();

$cbs = $test->{base}->get_download_callbacks();

is_deeply($cbs->{user_cb_data_container}, ["Package: one-0:1-1.noarch", "Package: one-0:2-1.noarch"]);

is($cbs->{start_cnt}, 2, "start_cnt");
is($cbs->{end_cnt}, 2, "end_cnt");
ok($cbs->{progress_cnt} >= 2, "progress_cnt");
is($cbs->{mirror_failure_cnt}, 0, "mirror_failure_cnt");

is_deeply($cbs->{user_cb_data_array}, [0, 1], "user_cb_data_array");
is_deeply($cbs->{end_status}, [$libdnf5::repo::DownloadCallbacks::TransferStatus_SUCCESSFUL, $libdnf5::repo::DownloadCallbacks::TransferStatus_SUCCESSFUL]);
is_deeply($cbs->{end_status}, [0, 0]);

done_testing();
Loading
Loading