system sh/stop_dnodes.sh
system sh/deploy.sh -n dnode1 -i 1
system sh/deploy.sh -n dnode2 -i 2
system sh/deploy.sh -n dnode3 -i 3
system sh/deploy.sh -n dnode4 -i 4
system sh/cfg.sh -n dnode1 -c transPullupInterval -v 1
system sh/cfg.sh -n dnode2 -c transPullupInterval -v 1
system sh/cfg.sh -n dnode3 -c transPullupInterval -v 1
system sh/cfg.sh -n dnode4 -c transPullupInterval -v 1
system sh/exec.sh -n dnode1 -s start
sql connect

print =============== step1: create dnodes
sql create dnode $hostname port 7200
sql create dnode $hostname port 7300
sql create dnode $hostname port 7400

$x = 0
step1: 
	$x = $x + 1
	sleep 1000
	if $x == 5 then
		return -1
	endi
sql select * from information_schema.ins_dnodes
if $data(1)[4] != ready then
  goto step1
endi

print =============== step2: create dnodes - with error
sql_error create mnode on dnode 1;
sql_error create mnode on dnode 2;
sql_error create mnode on dnode 3;
sql_error create mnode on dnode 4;
sql_error create mnode on dnode 5;
sql_error create mnode on dnode 6;

print =============== step3: create mnode 2 and 3
system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s start
system sh/exec.sh -n dnode4 -s start
$x = 0
step3: 
	$x = $x + 1
	sleep 1000
	if $x == 5 then
		return -1
	endi
sql select * from information_schema.ins_dnodes
if $data(2)[4] != ready then
  goto step3
endi
if $data(3)[4] != ready then
  goto step3
endi
if $data(4)[4] != ready then
  goto step3
endi

sql create mnode on dnode 2
sql create mnode on dnode 3

$x = 0
step31: 
	$x = $x + 1
	sleep 1000
	if $x == 50 then
		return -1
	endi
sql select * from information_schema.ins_mnodes
$leaderNum = 0
if $data(1)[2] == leader then
  $leaderNum = 1
endi
if $data(2)[2] == leader then
  $leaderNum = 1
endi
if $data(3)[2] == leader then
  $leaderNum = 1
endi
if  $leaderNum == 0 then
  goto step31
endi

print =============== step4: create dnodes - with error
sql_error create mnode on dnode 1
sql_error create mnode on dnode 2;
sql_error create mnode on dnode 3;
sql_error create mnode on dnode 4;
sql_error create mnode on dnode 5;
sql_error create mnode on dnode 6;

print =============== step5: drop mnodes - with error
sql_error drop mnode on dnode 1
sql_error drop mnode on dnode 4
sql_error drop mnode on dnode 5
sql_error drop mnode on dnode 6

system sh/exec.sh -n dnode2 -s stop 
$x = 0
step5: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_dnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
print ===> $data30 $data31 $data32 $data33 $data34 $data35
if $data(1)[4] != ready then
  goto step5
endi
if $data(2)[4] != offline then
  goto step5
endi
if $data(3)[4] != ready then
  goto step5
endi
if $data(4)[4] != ready then
  goto step5
endi

sql_error drop mnode on dnode 2

system sh/exec.sh -n dnode2 -s start
$x = 0
step51: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_dnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
print ===> $data30 $data31 $data32 $data33 $data34 $data35
if $data(1)[4] != ready then
  goto step51
endi
if $data(2)[4] != ready then
  goto step51
endi
if $data(3)[4] != ready then
  goto step51
endi
if $data(4)[4] != ready then
  goto step51
endi

print =============== step6: stop mnode1 
system sh/exec.sh -n dnode1 -s stop 
# sql_error drop mnode on dnode 1

$x = 0
step61: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_mnodes -x step61
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
$leaderNum = 0
if $data(2)[2] == leader then
  $leaderNum = 1
endi
if $data(3)[2] == leader then
  $leaderNum = 1
endi
if  $leaderNum != 1 then
  goto step61
endi

print =============== step7: start mnode1 and wait it online
system sh/exec.sh -n dnode1 -s start

$x = 0
step71: 
	$x = $x + 1
	sleep 1000
	if $x == 50 then
		return -1
	endi
sql select * from information_schema.ins_dnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
print ===> $data30 $data31 $data32 $data33 $data34 $data35
if $data(1)[4] != ready then
  goto step71
endi
if $data(2)[4] != ready then
  goto step71
endi
if $data(3)[4] != ready then
  goto step71
endi
if $data(4)[4] != ready then
  goto step71
endi

print =============== step8: stop mnode1 and drop it
system sh/exec.sh -n dnode1 -s stop 

$x = 0
step81: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_mnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
$leaderNum = 0
if $data(1)[2] == leader then
  $leaderNum = 1
endi
if $data(2)[2] == leader then
  $leaderNum = 1
endi
if $data(3)[2] == leader then
  $leaderNum = 1
endi
if  $leaderNum != 1 then
  goto step81
endi

print =============== step9: start mnode1 and wait it dropped
print check mnode has leader step9a
$x = 0
step9a: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
print check mnode leader
sql select * from information_schema.ins_mnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
$leaderNum = 0
if $data(1)[2] == leader then
  $leaderNum = 1
endi
if $data(2)[2] == leader then
  $leaderNum = 1
endi
if $data(3)[2] == leader then
  $leaderNum = 1
endi
if  $leaderNum != 1 then
  goto step9a
endi

print start dnode1 step9b
system sh/exec.sh -n dnode1 -s start
$x = 0
step9b: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
print check dnode1 ready	
sql select * from information_schema.ins_dnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
print ===> $data30 $data31 $data32 $data33 $data34 $data35
if $data(1)[4] != ready then
  goto step9b
endi
if $data(2)[4] != ready then
  goto step9b
endi
if $data(3)[4] != ready then
  goto step9b
endi
if $data(4)[4] != ready then
  goto step9b
endi

sleep 4000
print check mnode has leader step9c
$x = 0
step9c: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
print check mnode leader
sql select * from information_schema.ins_mnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
$leaderNum = 0
if $data(1)[2] == leader then
  $leaderNum = 1
endi
if $data(2)[2] == leader then
  $leaderNum = 1
endi
if $data(3)[2] == leader then
  $leaderNum = 1
endi
if  $leaderNum != 1 then
  goto step9c
endi

print drop mnode step9d
sql drop mnode on dnode 1 

$x = 0
step9d: 
	$x = $x + 1
	sleep 1000
	if $x == 20 then
		return -1
	endi
print check mnode leader
sql select * from information_schema.ins_mnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
$leaderNum = 0
if $data(1)[2] == leader then
  $leaderNum = 1
endi
if $data(2)[2] == leader then
  $leaderNum = 1
endi
if $data(3)[2] == leader then
  $leaderNum = 1
endi
if  $leaderNum != 1 then
  goto step9d
endi
if $rows != 2 then
  goto step9d
endi

print =============== stepa: create mnode1 again
sql create mnode on dnode 1
$x = 0
stepa: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_mnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
$leaderNum = 0
if $data(1)[2] == leader then
  $leaderNum = 1
endi
if $data(2)[2] == leader then
  $leaderNum = 1
endi
if $data(3)[2] == leader then
  $leaderNum = 1
endi
if  $leaderNum == 0 then
  goto stepa
endi
if  $leaderNum != 1 then
  return -1
endi

$x = 0
stepb:
	$x = $x + 1
	sleep 1000
	if $x == 10 then
	  print ====> dnode not ready!
		return -1
	endi
sql select * from information_schema.ins_dnodes
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
print ===> $data30 $data31 $data32 $data33 $data34 $data35
if $rows != 4 then
  return -1
endi
if $data(1)[4] != ready then
  goto stepb
endi
if $data(2)[4] != ready then
  goto stepb
endi
if $data(3)[4] != ready then
  goto stepb
endi
if $data(4)[4] != ready then
  goto stepb
endi

system sh/exec.sh -n dnode1 -s stop
system sh/exec.sh -n dnode2 -s stop
system sh/exec.sh -n dnode3 -s stop
system sh/exec.sh -n dnode4 -s stop
