#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
#integration test for file-based cluster manager

#disable the test for now
#exit 0

source setup_env.inc

#create config file for static storage cluster
#./clm_console.sh --configFile $CONFIG_DIR/cluster-12345-cluster-view.json
temp_cluster_view_conf=$VIEW_ROOT/$LOG_DIR_FROM_ROOT/cluster-12345-cluster-view.json
cp -f $VIEW_ROOT/$CONFIG_DIR/cluster-12345-cluster-view.json $temp_cluster_view_conf
$SCRIPT_DIR/cm_driver.py -c clm_console --cmdline_args="--configFile $temp_cluster_view_conf"

#Launch dummy process nodes
dummy_process_1_output=$VIEW_ROOT/$LOG_DIR_FROM_ROOT/dummy_process_1.log
$SCRIPT_DIR/cm_driver.py -c dummy-process -o start --logfile=$dummy_process_1_output -l "integration-test/config/log4j2.properties" --cmdline_args="--configFile $temp_cluster_view_conf --cluster storage-cluster-12345 --host localhost --port 8900"

dummy_process_2_output=$VIEW_ROOT/$LOG_DIR_FROM_ROOT/dummy_process_2.log
$SCRIPT_DIR/cm_driver.py -c dummy-process -o start --logfile=$dummy_process_2_output -l "integration-test/config/log4j2.properties" --cmdline_args="--configFile $temp_cluster_view_conf --cluster storage-cluster-12345 --host localhost --port 8901"

#$SCRIPT_DIR/espresso_driver.py -c dummy-process -o start -l "cluster-manager/test/integration-test/log4j2.properties" --cmdline_props="--configFile /tmp/cluster-12345.cv --cluster storage-cluster-12345 --host localhost --port 8902"

#$SCRIPT_DIR/espresso_driver.py -c dummy-process -o start -l "cluster-manager/test/integration-test/log4j2.properties" --cmdline_props="--configFile /tmp/cluster-12345.cv --cluster storage-cluster-12345 --host localhost --port 8903"

#$SCRIPT_DIR/espresso_driver.py -c dummy-process -o start -l "cluster-manager/test/integration-test/log4j2.properties" --cmdline_props="--configFile /tmp/cluster-12345.cv --cluster storage-cluster-12345 --host localhost --port 8904"

sleep 3

#verify cluster state
#$SCRIPT_DIR/cm_driver.py -c cluster-state-verifier -o start -l "cluster-manager/test/integration-test/log4j2.properties" --cmdline_props="-zkSvr localhost:2188 -cluster storage-cluster-12345"

$SCRIPT_DIR/cm_driver.py -c dummy-process -o stop

#$SCRIPT_DIR/espresso_driver.py -c cluster-manager -o stop

echo == GREP SUCCEED ==
#find ~/EspressoLogs/ -mmin -10 -print | xargs grep succeed
grep succeed $dummy_process_1_output $dummy_process_2_output 

source report_pass_fail.inc

exit $all_stat

