test_docker_databases_provider.py 7.8 KB
Newer Older
1
2
3
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

Samuel GAIST's avatar
Samuel GAIST committed
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
###################################################################################
#                                                                                 #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/               #
# Contact: beat.support@idiap.ch                                                  #
#                                                                                 #
# Redistribution and use in source and binary forms, with or without              #
# modification, are permitted provided that the following conditions are met:     #
#                                                                                 #
# 1. Redistributions of source code must retain the above copyright notice, this  #
# list of conditions and the following disclaimer.                                #
#                                                                                 #
# 2. Redistributions in binary form must reproduce the above copyright notice,    #
# this list of conditions and the following disclaimer in the documentation       #
# and/or other materials provided with the distribution.                          #
#                                                                                 #
# 3. Neither the name of the copyright holder nor the names of its contributors   #
# may be used to endorse or promote products derived from this software without   #
# specific prior written permission.                                              #
#                                                                                 #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE    #
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL      #
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR      #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER      #
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   #
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   #
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.            #
#                                                                                 #
###################################################################################
35
36
37
38
39
40
41
42


# Tests for experiment execution

import os
import sys

import logging
Samuel GAIST's avatar
Samuel GAIST committed
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
logger = logging.getLogger(__name__)

import unittest
import simplejson
import multiprocessing
import tempfile
import shutil
import zmq
from time import time
from time import sleep

from beat.backend.python.scripts import databases_provider
from beat.backend.python.database import Database
from beat.backend.python.data import RemoteDataSource
from beat.backend.python.data import RemoteException

from ..dock import Host
61
from ..utils import find_free_port
62
63
64
65
66

from . import prefix
from . import tmp_prefix


Samuel GAIST's avatar
Samuel GAIST committed
67
# ----------------------------------------------------------
68
69
70


CONFIGURATION = {
Samuel GAIST's avatar
Samuel GAIST committed
71
72
73
74
75
76
77
78
79
80
81
    "queue": "queue",
    "inputs": {
        "in_data": {
            "set": "double",
            "protocol": "double",
            "database": "integers_db/1",
            "output": "a",
            "path": "ec/89/e5/6e161d2cb012ef6ac8acf59bf453a6328766f90dc9baba9eb14ea23c55",
            "endpoint": "a",
            "hash": "ec89e56e161d2cb012ef6ac8acf59bf453a6328766f90dc9baba9eb14ea23c55",
            "channel": "integers",
82
83
        }
    },
Samuel GAIST's avatar
Samuel GAIST committed
84
85
86
87
88
89
90
91
92
    "algorithm": "user/integers_echo/1",
    "parameters": {},
    "environment": {"name": "Python 2.7", "version": "1.2.0"},
    "outputs": {
        "out_data": {
            "path": "20/61/b6/2df3c3bedd5366f4a625c5d87ffbf5a26007c46c456e9abf21b46c6681",
            "endpoint": "out_data",
            "hash": "2061b62df3c3bedd5366f4a625c5d87ffbf5a26007c46c456e9abf21b46c6681",
            "channel": "integers",
93
94
        }
    },
Samuel GAIST's avatar
Samuel GAIST committed
95
96
    "nb_slots": 1,
    "channel": "integers",
97
98
99
}


Samuel GAIST's avatar
Samuel GAIST committed
100
# ----------------------------------------------------------
101
102
103
104
105
106
107
108


class TestDatabasesProvider(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.host = Host(raise_on_errors=False)

    def setUp(self):
Samuel GAIST's avatar
Samuel GAIST committed
109
110
111
        if sys.platform == "darwin":
            self.working_dir = tempfile.mkdtemp(prefix=__name__, dir="/tmp")
            self.cache_root = tempfile.mkdtemp(prefix=__name__, dir="/tmp")
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
        else:
            self.working_dir = tempfile.mkdtemp(prefix=__name__)
            self.cache_root = tempfile.mkdtemp(prefix=__name__)

        self.data_source = None
        self.client_context = None
        self.client_socket = None
        self.databases_container = None

    def tearDown(self):
        self.stop_databases_provider()

        shutil.rmtree(self.working_dir)
        shutil.rmtree(self.cache_root)

        self.working_dir = None
        self.cache_root = None
        self.data_source = None

        if self.client_socket is not None:
            self.client_socket.setsockopt(zmq.LINGER, 0)
            self.client_socket.close()
            self.client_context.destroy()

        self.host.teardown()

    def start_databases_provider(self, configuration):
Samuel GAIST's avatar
Samuel GAIST committed
139
        with open(os.path.join(self.working_dir, "configuration.json"), "wb") as f:
140
            data = simplejson.dumps(configuration, indent=4)
Samuel GAIST's avatar
Samuel GAIST committed
141
            f.write(data.encode("utf-8"))
142

Samuel GAIST's avatar
Samuel GAIST committed
143
        working_prefix = os.path.join(self.working_dir, "prefix")
144
145
146
        if not os.path.exists(working_prefix):
            os.makedirs(working_prefix)

Samuel GAIST's avatar
Samuel GAIST committed
147
        input_name, input_cfg = list(configuration["inputs"].items())[0]
148

Samuel GAIST's avatar
Samuel GAIST committed
149
        database = Database(prefix, input_cfg["database"])
150
151
        database.export(working_prefix)

Samuel GAIST's avatar
Samuel GAIST committed
152
153
        view = database.view(input_cfg["protocol"], input_cfg["set"])
        view.index(os.path.join(self.cache_root, input_cfg["path"]))
154

155
        port = find_free_port()
156
        cmd = [
Samuel GAIST's avatar
Samuel GAIST committed
157
158
159
160
161
            "databases_provider",
            "--debug",
            "0.0.0.0:%i" % port,
            "/beat/prefix",
            "/beat/cache",
162
163
        ]

Samuel GAIST's avatar
Samuel GAIST committed
164
        databases_environment = self.host.db2docker([input_cfg["database"]])
165

Samuel GAIST's avatar
Samuel GAIST committed
166
167
168
        self.databases_container = self.host.create_container(
            databases_environment, cmd
        )
169

Samuel GAIST's avatar
Samuel GAIST committed
170
171
172
173
174
        self.databases_container.add_volume(self.working_dir, "/beat/prefix")
        self.databases_container.add_volume(
            "/tmp", os.path.join("/beat/datasets", input_cfg["database"])
        )
        self.databases_container.add_volume(self.cache_root, "/beat/cache")
175

176
        self.databases_container.add_port(port, port, host_address=self.host.ip)
177
178
179
180
181

        self.host.start(self.databases_container)

        self.client_context = zmq.Context()
        self.client_socket = self.client_context.socket(zmq.PAIR)
Samuel GAIST's avatar
Samuel GAIST committed
182
        self.client_socket.connect("tcp://{}:{}".format(self.host.ip, port))
183

Samuel GAIST's avatar
Samuel GAIST committed
184
185
186
        dataformat_name = database.set(input_cfg["protocol"], input_cfg["set"])[
            "outputs"
        ][input_cfg["output"]]
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

        self.data_source = RemoteDataSource()
        self.data_source.setup(self.client_socket, input_name, dataformat_name, prefix)

    def stop_databases_provider(self):
        if self.databases_container is not None:
            self.host.kill(self.databases_container)
            self.host.wait(self.databases_container)
            self.databases_container = None

    def test_success(self):
        self.start_databases_provider(CONFIGURATION)

        self.assertEqual(len(self.data_source), 9)

        for i in range(0, 9):
            (data, start_index, end_index) = self.data_source[i]
            self.assertEqual(start_index, i)
            self.assertEqual(end_index, i)
            self.assertEqual(data.value, i + 1)