1#  Copyright (c) 1997-2021
2#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
3#  Technische Universität Berlin, Germany
4#  https://polymake.org
5#
6#  This program is free software; you can redistribute it and/or modify it
7#  under the terms of the GNU General Public License as published by the
8#  Free Software Foundation; either version 2, or (at your option) any
9#  later version: http://www.gnu.org/licenses/gpl.txt.
10#
11#  This program is distributed in the hope that it will be useful,
12#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14#  GNU General Public License for more details.
15#-------------------------------------------------------------------------------
16
17# Setup and functions necessary for running unit tests involving PolyDB
18#
19#  This file is part of the polymake database interface polyDB.
20#
21#   @author Silke Horn, Andreas Paffenholz
22#   http://www.mathematik.tu-darmstadt.de/~paffenholz
23#
24
25
26REQUIRE
27   polydb.rules
28
29package PolyDB::Test;
30
31# host with a Mongo DB and root user admin/admin, preferably in a docker container without persistent storage
32custom $db_host="localhost";
33
34# port number of test Mongo server, cf. db_host
35custom $db_port=27017;
36
37# test mongo admin auth db, used once to create db admin
38custom $db_mongoadmin_user = "admin";
39
40# test mongo admin auth db, used once to create db admin
41custom $db_mongoadmin_pwd = "admin";
42
43# test mongo admin auth db, used once to create db admin
44custom $db_mongoadmin_auth_db = "admin";
45
46# the name of the database
47custom $db_name = "polydb_data";
48
49# test normal db user
50custom $db_user = "polymake";
51
52# test normal db password
53custom $db_pwd= "database";
54
55# test normal user auth db
56custom $db_auth_db = "admin";
57
58# user name of test admin user
59custom $db_admin_user = "admin";
60
61# password of test admin user
62custom  $db_admin_pwd = "admin";
63
64# password of test admin user
65custom  $db_testsection_name = "TestSection";
66
67# test normal user auth db
68custom $pretty_print_doc = false;
69
70# start docker with
71# docker run --name polydb -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=admin mongo
72
73CONFIGURE {
74   if ($db_host ne "" && $db_port != 0) {
75      defined(eval {
76         MongoDB::MongoClient->new(host => "${db_host}:${db_port}", db_name=> $Test::db_auth_db, username=>$Test::db_admin_user, password=>$Test::db_admin_pwd, socket_timeout_ms => 1000)->database_names
77        })
78      or die <<".";
79Test Mongo instance at ${db_host}:${db_port} not available:
80  $@
81Please start the Mongo server or adjust the settings:
82  include "common::test.rules";
83  set_custom \$PolyDB::Test::db_host="...";
84  set_custom \$PolyDB::Test::db_port=NNN;
85.
86   }
87}
88
89# the default collection name is set to the current testgroup name
90function prepare() {
91   my ($collection_name) = (Cwd::getcwd =~ $filename_re);
92   my $info_collection_name = "Info_${collection_name}";
93
94   local with($Scope->locals) {
95      local $PolyDB::default::db_host = $db_host;
96      local $PolyDB::default::db_port = $db_port;
97      local $PolyDB::default::useSSL = 0;
98
99      local $PolyDB::default::db_user = $db_user;
100      local $PolyDB::default::db_pwd = $db_pwd;
101      local $PolyDB::default::db_auth_db = $db_auth_db;
102
103      local $PolyDB::default::db_section_name = $db_testsection_name;
104      local $PolyDB::default::db_collection_name = $collection_name;
105
106      local $PolyDB::default::pretty_print_doc = $pretty_print_doc;
107   }
108
109   my $client = polyDB(user=>$db_admin_user, password=>$db_admin_pwd);
110
111   my $max_tries = 9;
112   for (my $retry = 0; ; ++$retry) {
113      eval {
114         MongoDB::Collection::drop($client->get_collection($PolyDB::default::db_section_name.".".$collection_name));
115      };
116      last if !$@;
117      die $@ if $retry == $max_tries or $@ !~ /Could not connect to '.*': Connection refused|MongoDB::NetworkTimeout/;
118      warn_print("No connection to Mongo test server, retrying...");
119      sleep min($retry + 1, 5);
120   }
121
122   $client->create_default_user_and_role();
123}
124
125function read_json_for_id($) {
126   my ($id) = @_;
127
128   my $client = Client::get_client();
129   my $db = $client->get_database($PolyDB::default::db_name);
130   my $query = { _id => $id };
131   my $collection = $db->get_collection($PolyDB::default::db_section_name.".".$PolyDB::default::db_collection_name);
132   $collection->find_one($query) // die "No such object in the database.\n";
133}
134
135
136# Local Variables:
137# mode: perl
138# cperl-indent-level:3
139# indent-tabs-mode:nil
140# End:
141