File: db_test.rules

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (159 lines) | stat: -rw-r--r-- 5,269 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#  Copyright (c) 1997-2024
#  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 Andreas Paffenholz
#   (c) 2015 - 2023
#   https://polydb.org
#   https://www.mathematik.tu-darmstadt.de/~paffenholz
#

REQUIRE
   main.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";

# polyDB: the version of polyDB used
custom $db_polydb_version = "2.1";

# polyDB: The timeout for waiting to get a socket connection to the db server
custom $db_socket_timeout = 120000;

# polyDB: The color for section names in info output
custom $db_section_color = 'bold red';

# polyDB: The color for collection names in info output
custom $db_collection_color = 'bold yellow';

# polyDB: Whether to use SSL
custom $useSSL = 1;

# polyDB: timeout to block for server selection, in milliseconds. 
custom $serverSelectionTimeout = 0;

# 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 {
          new PolyDB::Client({host => "${db_host}", port=>${db_port}, useSSL=>${useSSL}, db_name=> $Test::db_auth_db, user=>$Test::db_admin_user, password=>$Test::db_admin_pwd, serverSelectionTimeout => 10000, tlsAllowInvalidHostnames=>true, tlsAllowInvalidCertificates=>true})
   })
      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 = $PolyDB::default::db_section_name.".".$collection_name;
      local $PolyDB::default::serverSelectionTimeout = $serverSelectionTimeout;
      
      local $PolyDB::default::pretty_print_doc = $pretty_print_doc;
   }

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

   $client->client->create_default_user_and_role();
}

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

   my $client = Client::get_client();
   my $query = { _id => $id };
   my $collection = $client->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: