2019-08-17 11:36:55 +02:00
#
2020-07-04 23:16:40 +02:00
# Blackbox tests for mdsearch
2019-08-17 11:36:55 +02:00
#
# Copyright (C) Ralph Boehme 2019
#
# 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 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
2020-07-04 23:16:40 +02:00
""" Blackbox test for mdsearch """
2019-08-17 11:36:55 +02:00
import os
import time
import threading
import logging
import json
from http . server import HTTPServer , BaseHTTPRequestHandler
from samba . dcerpc import mdssvc
from samba . tests import BlackboxTestCase
from samba . samba3 import mdscli
from samba . logger import get_samba_logger
logger = get_samba_logger ( name = __name__ )
testfiles = [
" foo " ,
" bar " ,
]
class MdssvcHTTPRequestHandler ( BaseHTTPRequestHandler ) :
def do_POST ( self ) :
content_length = int ( self . headers [ ' content-length ' ] )
body = self . rfile . read ( content_length )
actual_json = json . loads ( ( body ) )
expected_json = json . loads ( self . server . json_in )
if actual_json != expected_json :
logger . error ( " Bad request, expected: \n %s \n Got: \n %s \n " % ( expected_json , actual_json ) )
self . send_error ( 400 ,
" Bad request " ,
" Expected: %s \n "
" Got: %s \n " %
( expected_json , actual_json ) )
return
resp = bytes ( self . server . json_out , encoding = " utf-8 " )
self . send_response ( 200 )
self . send_header ( ' content-type ' , ' application/json; charset=UTF-8 ' )
self . send_header ( ' content-length ' , len ( resp ) )
self . end_headers ( )
self . wfile . write ( resp )
class MdfindBlackboxTests ( BlackboxTestCase ) :
def setUp ( self ) :
2023-11-28 16:38:22 +13:00
super ( ) . setUp ( )
2019-08-17 11:36:55 +02:00
2019-01-23 22:29:19 +01:00
self . server = HTTPServer ( ( ' 10.53.57.35 ' , 8080 ) ,
2019-08-17 11:36:55 +02:00
MdssvcHTTPRequestHandler ,
bind_and_activate = False )
self . t = threading . Thread ( target = MdfindBlackboxTests . http_server , args = ( self , ) )
self . t . setDaemon ( True )
self . t . start ( )
time . sleep ( 1 )
2023-06-17 13:53:27 +02:00
self . sharepath = os . environ [ " LOCAL_PATH " ]
2019-08-17 11:36:55 +02:00
for file in testfiles :
f = open ( " %s / %s " % ( self . sharepath , file ) , " w " )
f . close ( )
def tearDown ( self ) :
2023-11-30 13:22:18 +13:00
super ( ) . tearDown ( )
2019-08-17 11:36:55 +02:00
for file in testfiles :
os . remove ( " %s / %s " % ( self . sharepath , file ) )
def http_server ( self ) :
self . server . server_bind ( )
self . server . server_activate ( )
self . server . serve_forever ( )
2020-07-04 23:16:40 +02:00
def test_mdsearch ( self ) :
""" Simple blackbox test for mdsearch """
2019-08-17 11:36:55 +02:00
username = os . environ [ " USERNAME " ]
password = os . environ [ " PASSWORD " ]
config = os . environ [ " SMB_CONF_PATH " ]
json_in = r ''' {
2023-04-20 17:27:20 +02:00
" from " : 0 , " size " : 50 , " _source " : [ " path.real " ] ,
2019-08-17 11:36:55 +02:00
" query " : {
" query_string " : {
" query " : " (samba*) AND path.real.fulltext: \" % BASEPATH % \" "
}
}
} '''
json_out = ''' {
" hits " : {
" total " : { " value " : 2 } ,
" hits " : [
{ " _source " : { " path " : { " real " : " % BASEPATH % /foo " } } } ,
{ " _source " : { " path " : { " real " : " % BASEPATH % /bar " } } }
]
}
} '''
self . server . json_in = json_in . replace ( " % BASEPATH % " , self . sharepath )
self . server . json_out = json_out . replace ( " % BASEPATH % " , self . sharepath )
2021-04-14 11:44:51 +02:00
output = self . check_output ( " mdsearch --configfile= %s -U %s %% %s fileserver spotlight ' *== \" samba* \" ' " % ( config , username , password ) )
2019-08-17 11:36:55 +02:00
actual = output . decode ( ' utf-8 ' ) . splitlines ( )
2023-06-17 13:53:27 +02:00
self . assertEqual ( testfiles , actual )