File: db_test.rules

package info (click to toggle)
polymake 4.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 31,528 kB
  • sloc: cpp: 152,204; perl: 43,222; javascript: 30,700; ansic: 2,937; java: 2,654; python: 641; sh: 244; xml: 117; makefile: 62
file content (140 lines) | stat: -rw-r--r-- 4,531 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#  Copyright (c) 1997-2020
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://polymake.org
#
#  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 the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program 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.
#-------------------------------------------------------------------------------

# Setup and functions necessary for running unit tests involving PolyDB
#
#  This file is part of the polymake database interface polyDB.
#
#   @author Silke Horn, Andreas Paffenholz
#   http://www.mathematik.tu-darmstadt.de/~paffenholz
#


REQUIRE
   polydb.rules

package PolyDB::Test;

# host with a Mongo DB and root user admin/admin, preferably in a docker container without persistent storage
custom $db_host="localhost";

# port number of test Mongo server, cf. db_host
custom $db_port=27017;

# test mongo admin auth db, used once to create db admin
custom $db_mongoadmin_user = "admin";

# test mongo admin auth db, used once to create db admin
custom $db_mongoadmin_pwd = "admin";

# test mongo admin auth db, used once to create db admin
custom $db_mongoadmin_auth_db = "admin";

# the name of the database
custom $db_name = "polydb_data";

# test normal db user
custom $db_user = "polymake";

# test normal db password
custom $db_pwd= "database";

# test normal user auth db
custom $db_auth_db = "admin";

# user name of test admin user
custom $db_admin_user = "admin";

# password of test admin user
custom  $db_admin_pwd = "admin";

# password of test admin user
custom  $db_testsection_name = "TestSection";

# test normal user auth db
custom $pretty_print_doc = false;

# start docker with
# docker run --name polydb -d -p 27017:27017 -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=admin mongo

CONFIGURE {
   if ($db_host ne "" && $db_port != 0) {
      defined(eval {
         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
        })
      or die <<".";
Test Mongo instance at ${db_host}:${db_port} not available:
  $@
Please start the Mongo server or adjust the settings:
  include "common::test.rules";
  set_custom \$PolyDB::Test::db_host="...";
  set_custom \$PolyDB::Test::db_port=NNN;
.
   }
}

# the default collection name is set to the current testgroup name
function prepare() {
   my ($collection_name) = (Cwd::getcwd =~ $filename_re);
   my $info_collection_name = "Info_${collection_name}";

   local with($Scope->locals) {
      local $PolyDB::default::db_host = $db_host;
      local $PolyDB::default::db_port = $db_port;
      local $PolyDB::default::useSSL = 0;
      
      local $PolyDB::default::db_user = $db_user;
      local $PolyDB::default::db_pwd = $db_pwd;
      local $PolyDB::default::db_auth_db = $db_auth_db;
      
      local $PolyDB::default::db_section_name = $db_testsection_name;
      local $PolyDB::default::db_collection_name = $collection_name;

      local $PolyDB::default::pretty_print_doc = $pretty_print_doc;
   }

   my $client = polyDB(user=>$db_admin_user, password=>$db_admin_pwd);

   my $max_tries = 9;
   for (my $retry = 0; ; ++$retry) {
      eval {
         MongoDB::Collection::drop($client->get_collection($PolyDB::default::db_section_name.".".$collection_name));
      };
      last if !$@;
      die $@ if $retry == $max_tries or $@ !~ /Could not connect to '.*': Connection refused|MongoDB::NetworkTimeout/;
      warn_print("No connection to Mongo test server, retrying...");
      sleep min($retry + 1, 5);
   }

   $client->create_default_user_and_role();
}

function read_json_for_id($) {
   my ($id) = @_;

   my $client = Client::get_client();
   my $db = $client->get_database($PolyDB::default::db_name);
   my $query = { _id => $id };
   my $collection = $db->get_collection($PolyDB::default::db_section_name.".".$PolyDB::default::db_collection_name);
   $collection->find_one($query) // die "No such object in the database.\n";
}


# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: