def set_data_replace_reserv_var(): """ 置換処理のテストに必要なデータをDBに登録 """ now = datetime.datetime.now(pytz.timezone('UTC')) trace_id = EventsRequestCommon.generate_trace_id(now) rhdm_response_action = None action_history = None try: with transaction.atomic(): # イベントリクエスト events_request = EventsRequest( trace_id=trace_id, request_type_id=1, rule_type_id=1, request_reception_time=now, request_user='******', request_server='pytest_server', event_to_time=now, event_info='{"EVENT_INFO":["置換テスト"]}', status='1', status_update_id='pytest_id', retry_cnt=0, last_update_timestamp=now, last_update_user='******') events_request.save(force_insert=True) except Exception as e: print(e) return events_request
def test_generate_trace_id_ok(): """ トレースID生成テスト ※正常系 """ trace_id = EventsRequestCommon.generate_trace_id() assert len(trace_id) == 55
def set_test_regist_data(): """ テストで使用するデータの作成 """ now = datetime.datetime.now(pytz.timezone('UTC')) trace_id = EventsRequestCommon.generate_trace_id(now) response_id = 1 act_his = ActionHistory(response_id=response_id, trace_id=trace_id, rule_type_id=1, rule_type_name='pytest_ruletable', rule_name='pytest_rule', execution_order=1, action_start_time=now, action_type_id=1, status=2106, status_detail=0, status_update_id='pytest_id', retry_flag=False, retry_status=None, retry_status_detail=None, action_retry_count=0, last_act_user='******', last_update_timestamp=now, last_update_user='******') act_his.save(force_insert=True) now = datetime.datetime.now(pytz.timezone('UTC')) user = User( user_id=Cstobj.DB_OASE_USER, user_name='unittest_procedure', login_id='', mail_address='', password='', disp_mode_id=DISP_MODE.DEFAULT, lang_mode_id=LANG_MODE.JP, password_count=0, password_expire=datetime.datetime.now(pytz.timezone('UTC')), last_update_user='******', last_update_timestamp=datetime.datetime.now(pytz.timezone('UTC')), ) user.save(force_insert=True)
def eventsrequest(request): """ [メソッド概要] テストリクエスト実行時のリクエストを処理する """ now = datetime.datetime.now(pytz.timezone('UTC')) trace_id = EventsRequestCommon.generate_trace_id() resp_json = {} result = False msg = '' logger.system_log('LOSI13001', trace_id) try: ######################################### # リクエストのチェック ######################################### # メソッドのチェック if not request or request.method == 'GET': msg = 'Invalid request. Must be POST. Not GET.' logger.user_log('LOSM13001', trace_id) raise Exception(msg) # フォーマットのチェック try: json_str = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: msg = 'Invalid request format. Must be JSON.' logger.user_log('LOSM13005', trace_id) raise Exception(msg) if json_str[EventsRequestCommon.KEY_REQTYPE] in [1, '1']: # 1:プロダクション環境 # プロダクション環境 json_str['traceid'] = trace_id json_str = json.dumps(json_str) # 初回の設定読込で落ちるのめんどくさいのでここに置いとく _rabbitMQ_conf() # RabbitMQへ送信 mq_lock.acquire() _produce(json_str) mq_lock.release() result = True msg = 'Accept request.' elif json_str[EventsRequestCommon.KEY_REQTYPE] in [2, '2']: # 2:ステージング環境 # ステージング環境 user = User.objects.get(user_id=1) # キーのチェック err_code = EventsRequestCommon.check_events_request_key(json_str) if err_code != EventsRequestCommon.REQUEST_OK: err_keyname = '' if err_code == EventsRequestCommon.REQUEST_ERR_RULETYPE_KEY: err_keyname = EventsRequestCommon.KEY_RULETYPE elif err_code == EventsRequestCommon.REQUEST_ERR_REQTYPE_KEY: err_keyname = EventsRequestCommon.KEY_REQTYPE elif err_code == EventsRequestCommon.REQUEST_ERR_DATETIME_KEY: err_keyname = EventsRequestCommon.KEY_EVENTTIME elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_KEY: err_keyname = EventsRequestCommon.KEY_EVENTINFO msg = 'Invalid request.' logger.user_log('LOSM13002', err_keyname, trace_id) raise Exception(msg) # ルール情報の取得 reqtypeid = json_str[EventsRequestCommon.KEY_REQTYPE] ruletablename = json_str[EventsRequestCommon.KEY_RULETYPE] ruletypeid = RuleType.objects.get( rule_type_name=ruletablename).rule_type_id evinfo_length = DataObject.objects.filter( rule_type_id=ruletypeid).values('label').distinct().count() # イベント情報のチェック err_code = EventsRequestCommon.check_events_request_len( json_str, evinfo_length) if err_code != EventsRequestCommon.REQUEST_OK: if err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_TYPE: msg = 'Unmatch, Number of event information elements.' logger.user_log('LOSM13003', trace_id, ruletypeid, 0, evinfo_length) raise Exception(msg) elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_LENGTH: msg = 'Unmatch, Number of event information elements.' logger.user_log( 'LOSM13003', trace_id, ruletypeid, len(json_str[EventsRequestCommon.KEY_EVENTINFO]), evinfo_length) raise Exception(msg) raise Exception() ######################################### # リクエストをDBに保存 ######################################### # DB登録用に成形 evinfo_str = '' rset = DataObject.objects.filter( rule_type_id=ruletypeid).order_by('data_object_id') label_list = [] conditional_expression_id_list = [] for a in rset: if a.label not in label_list: label_list.append(a.label) conditional_expression_id_list.append( a.conditional_expression_id) for rs, v in zip(conditional_expression_id_list, json_str[EventsRequestCommon.KEY_EVENTINFO]): if evinfo_str: evinfo_str += ',' # 条件式がリストの場合 if rs in (13, 14): if not isinstance(v, list): evinfo_str += '%s' % (v) else: temp_val = '[' for i, val in enumerate(v): if i > 0: temp_val += ',' temp_val += '"%s"' % (val) temp_val += ']' evinfo_str += '%s' % (temp_val) # 条件式がリスト以外の場合 else: evinfo_str += '"%s"' % (v) evinfo_str = '{"EVENT_INFO":[%s]}' % (evinfo_str) event_dt = json_str[EventsRequestCommon.KEY_EVENTTIME] event_dt = TimeConversion.get_time_conversion_utc( event_dt, 'Asia/Tokyo') json_data = { 'trace_id': trace_id, 'request_type_id': reqtypeid, 'rule_type_id': ruletypeid, 'request_reception_time': now, 'request_user': '******', 'request_server': 'OASE Web', 'event_to_time': event_dt, 'event_info': evinfo_str, 'status': defs.UNPROCESS, 'status_update_id': '', 'retry_cnt': 0, 'last_update_timestamp': now, 'last_update_user': user.user_name, } # バリデーションチェック oters = EventsRequestSerializer(data=json_data) result_valid = oters.is_valid() # バリデーションエラー if result_valid == False: msg = '%s' % oters.errors logger.user_log('LOSM13004', trace_id, msg) # 正常の場合はDB保存 else: oters.save() result = True msg = 'Accept request.' else: # 不明なリクエスト種別 msg = 'Invalid request type.' logger.user_log('LOSM13023', trace_id, json_str[EventsRequestCommon.KEY_REQTYPE]) except Exception as e: if not msg: msg = 'Unexpected error.' logger.system_log('LOSM13013', trace_id, traceback.format_exc()) # レスポンス情報の作成 resp_json = { 'result': result, 'msg': msg, 'trace_id': trace_id, } resp_json = json.dumps(resp_json, ensure_ascii=False) logger.system_log('LOSI13002', trace_id, result, msg) # 応答 return HttpResponse(resp_json)
def bulk_eventsrequest(request): """ [メソッド概要] 一括用のリクエストを処理する """ resp_json = {} result = False rule_type_id_list = {} label_count_list = {} logger.system_log('LOSI13023') try: ######################################### # リクエストのチェック ######################################### # メソッドのチェック if not request or request.method == 'GET': logger.system_log('LOSM13025') raise Exception() # フォーマットのチェック try: json_str = json.loads(request.body.decode('UTF-8')) except json.JSONDecodeError: logger.system_log('LOSM13026') raise Exception() for data in json_str['request']: # キーのチェック err_code = EventsRequestCommon.check_events_request_key(data) if err_code != EventsRequestCommon.REQUEST_OK: err_keyname = '' if err_code == EventsRequestCommon.REQUEST_ERR_RULETYPE_KEY: err_keyname = EventsRequestCommon.KEY_RULETYPE elif err_code == EventsRequestCommon.REQUEST_ERR_REQTYPE_KEY: err_keyname = EventsRequestCommon.KEY_REQTYPE elif err_code == EventsRequestCommon.REQUEST_ERR_DATETIME_KEY: err_keyname = EventsRequestCommon.KEY_EVENTTIME elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_KEY: err_keyname = EventsRequestCommon.KEY_EVENTINFO logger.system_log('LOSM13027', err_keyname) raise Exception() # ルール情報の取得 reqtypeid = data[EventsRequestCommon.KEY_REQTYPE] ruletablename = data[EventsRequestCommon.KEY_RULETYPE] if ruletablename not in rule_type_id_list: rule_type_id_list.update({ruletablename: 0}) label_count_list.update({ruletablename: 0}) rset = RuleType.objects.filter( rule_type_name=ruletablename).values( 'rule_type_id', 'rule_type_name', 'label_count') for rs in rset: rule_type_id_list.update( {rs['rule_type_name']: rs['rule_type_id']}) label_count_list.update( {rs['rule_type_name']: rs['label_count']}) if ruletablename in rule_type_id_list: ruletypeid = rule_type_id_list[ruletablename] evinfo_length = label_count_list[ruletablename] # イベント情報のチェック err_code = EventsRequestCommon.check_events_request_len( data, evinfo_length) if err_code != EventsRequestCommon.REQUEST_OK: if err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_TYPE: logger.system_log('LOSM13028', ruletypeid, 0, evinfo_length) raise Exception() elif err_code == EventsRequestCommon.REQUEST_ERR_EVINFO_LENGTH: logger.system_log( 'LOSM13028', ruletypeid, len(data[EventsRequestCommon.KEY_EVENTINFO]), evinfo_length) raise Exception() raise Exception() trace_id = EventsRequestCommon.generate_trace_id() data['traceid'] = trace_id data = json.dumps(data) _rabbitMQ_conf() # RabbitMQへ送信 mq_lock.acquire() _produce(data) mq_lock.release() result = True except Exception as e: logger.system_log('LOSM13029', traceback.format_exc()) # レスポンス情報の作成 resp_json = { 'result': result, } resp_json = json.dumps(resp_json, ensure_ascii=False) logger.system_log('LOSI13024', result) # 応答 return HttpResponse(resp_json)
def handle(self, *args, **options): random.seed() try: # パラメーター取得 self.now = datetime.datetime.now(pytz.timezone('UTC')) self.host = options['host'] self.port = options['port'] self.rule_ids = self.get_target_ruleid_list(options['rule']) self.req_type = options['reqtype'] self.count = options['reqcnt'] self.repeat = options['repeat'] self.interval = options['interval'] self.data_objs = {} # パラメーターチェック if len(self.rule_ids) <= 0: raise Exception('送信対象のルール種別がありません') if self.req_type not in [0, 1, 2]: raise Exception('リクエスト種別が不正です') if self.count == 0: self.count = random.randint(1, 100) if self.count < 0: self.count = 1 if self.count > 100: self.count = 100 if self.repeat < 0: self.repeat = 0 if self.interval <= 0: self.interval = 1 if self.repeat == 0: self.interval = 0 # ルール別データオブジェクト取得 self.get_data_objects(options['filename']) # プロダクション向けのリクエストを送信する可能性がある場合は確認 if self.req_type in [0, 1]: while True: choice = input( "プロダクション向けのリクエストを送信しようとしています。\n実行しても、よろしいですか? (y/n)\n" ).lower() if choice in ['y', 'yes']: break elif choice in ['n', 'no']: return # リクエスト送信 total_cnt = 0 for repeat_cnt in range(self.repeat + 1): events_request_list = [] for req_cnt in range(self.count): rule_type_id = random.choice(self.rule_ids) req_type_id = self.req_type if req_type_id == 0: req_type_id = random.randint(1, 2) evinfo_str = '' evinfo_str = random.choice(self.data_objs[rule_type_id]) evinfo_str = '{"EVENT_INFO":[%s]}' % (evinfo_str) evreq_obj = EventsRequest( trace_id=EventsRequestCommon.generate_trace_id(), request_type_id=req_type_id, rule_type_id=rule_type_id, request_reception_time=self.now, request_user='******', request_server='TestCommand', event_to_time=self.now, event_info=evinfo_str, status=defs.UNPROCESS, status_update_id='', retry_cnt=0, last_update_timestamp=self.now, last_update_user='******') events_request_list.append(evreq_obj) if len(events_request_list) > 0: EventsRequest.objects.bulk_create(events_request_list) if self.interval > 0: time.sleep(self.interval) except Exception as e: print(e)
def set_data_action_stop(action_stop_interval, action_stop_count, exec_order): """ 抑止回数/抑止間隔のテストに必要なデータをDBに登録 """ now = datetime.datetime.now(pytz.timezone('UTC')) trace_id = EventsRequestCommon.generate_trace_id(now) rhdm_response_action = None action_history = None try: with transaction.atomic(): # アクション種別 action_type = ActionType(driver_type_id=1, disuse_flag='0', last_update_timestamp=now, last_update_user='******') action_type.save(force_insert=True) # イベントリクエスト events_request = EventsRequest(trace_id=trace_id, request_type_id=1, rule_type_id=1, request_reception_time=now, request_user='******', request_server='pytest_server', event_to_time=now, event_info='{"EVENT_INFO":["1"]}', status='3', status_update_id='pytest_id', retry_cnt=0, last_update_timestamp=now, last_update_user='******') events_request.save(force_insert=True) # ルールマッチング結果詳細 rhdm_response_action = RhdmResponseAction( response_id=1, rule_name='pytest_rule', execution_order=exec_order, action_type_id=action_type.pk, action_parameter_info= '{"ACTION_PARAMETER_INFO": ["SERVER_LIST=localhost", "ITA_NAME=ITA176", "SYMPHONY_CLASS_ID=1"]}', action_pre_info='', action_retry_interval=1, action_retry_count=1, action_stop_interval=action_stop_interval, action_stop_count=action_stop_count, last_update_timestamp=now, last_update_user='******', ) rhdm_response_action.save(force_insert=True) # アクション履歴 action_history = ActionHistory( response_id=rhdm_response_action.response_id, trace_id=trace_id, rule_type_id=events_request.rule_type_id, rule_type_name='pytest_ruletable', rule_name=rhdm_response_action.rule_name, execution_order=rhdm_response_action.execution_order, action_start_time=now, action_type_id=rhdm_response_action.action_type_id, status=2, status_detail=0, status_update_id='pytest_id', retry_flag=False, retry_status=None, retry_status_detail=None, action_retry_count=0, last_act_user='******', last_update_timestamp=now, last_update_user='******', ) action_history.save(force_insert=True) except Exception as e: print(e) return trace_id, rhdm_response_action, action_history