2 # Blackbox tests for mdsearch
4 # Copyright (C) Ralph Boehme 2019
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """Blackbox test for mdsearch"""
27 from http.server import HTTPServer, BaseHTTPRequestHandler
28 from samba.dcerpc import mdssvc
29 from samba.tests import BlackboxTestCase
30 from samba.samba3 import mdscli
31 from samba.logger import get_samba_logger
33 logger = get_samba_logger(name=__name__)
40 class MdssvcHTTPRequestHandler(BaseHTTPRequestHandler):
42 content_length = int(self.headers['content-length'])
43 body = self.rfile.read(content_length)
45 actual_json = json.loads((body))
46 expected_json = json.loads(self.server.json_in)
48 if actual_json != expected_json:
49 logger.error("Bad request, expected:\n%s\nGot:\n%s\n" % (expected_json, actual_json))
54 (expected_json, actual_json))
57 resp = bytes(self.server.json_out, encoding="utf-8")
59 self.send_response(200)
60 self.send_header('content-type', 'application/json; charset=UTF-8')
61 self.send_header('content-length', len(resp))
63 self.wfile.write(resp)
65 class MdfindBlackboxTests(BlackboxTestCase):
70 self.server = HTTPServer(('10.53.57.35', 8080),
71 MdssvcHTTPRequestHandler,
72 bind_and_activate=False)
74 self.t = threading.Thread(target=MdfindBlackboxTests.http_server, args=(self,))
75 self.t.setDaemon(True)
79 self.sharepath = os.environ["LOCAL_PATH"]
81 for file in testfiles:
82 f = open("%s/%s" % (self.sharepath, file), "w")
86 super(BlackboxTestCase, self).tearDown()
87 for file in testfiles:
88 os.remove("%s/%s" % (self.sharepath, file))
90 def http_server(self):
91 self.server.server_bind()
92 self.server.server_activate()
93 self.server.serve_forever()
95 def test_mdsearch(self):
96 """Simple blackbox test for mdsearch"""
98 username = os.environ["USERNAME"]
99 password = os.environ["PASSWORD"]
100 config = os.environ["SMB_CONF_PATH"]
103 "from": 0, "size": 50, "_source": ["path.real"],
106 "query": "(samba*) AND path.real.fulltext:\"%BASEPATH%\""
112 "total" : { "value" : 2},
114 {"_source" : {"path" : {"real" : "%BASEPATH%/foo"}}},
115 {"_source" : {"path" : {"real" : "%BASEPATH%/bar"}}}
120 self.server.json_in = json_in.replace("%BASEPATH%", self.sharepath)
121 self.server.json_out = json_out.replace("%BASEPATH%", self.sharepath)
123 output = self.check_output("mdsearch --configfile=%s -U %s%%%s fileserver spotlight '*==\"samba*\"'" % (config, username, password))
125 actual = output.decode('utf-8').splitlines()
126 self.assertEqual(testfiles, actual)