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
system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s start
system sh/exec.sh -n dnode4 -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 == 10 then
		return -1
	endi
sql select * from information_schema.ins_dnodes -x step1
if $data(1)[4] != ready then
  goto step1
endi
if $data(2)[4] != ready then
  goto step1
endi
if $data(3)[4] != ready then
  goto step1
endi

print =============== step2: create mnode 2
sql create mnode on dnode 2
sql create mnode on dnode 3
sql_error create mnode on dnode 4

$leaderExist = 0
$x = 0
step2: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_mnodes -x step2

print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
if $data(1)[2] == leader then
  $leaderExist = 1
endi
if $data(2)[2] == leader then
  $leaderExist = 1
endi
if $data(3)[2] == leader then
  $leaderExist = 1
endi
if  $leaderExist != 1 then
  goto step2
endi

print =============== step3: create user
sql create user user1 PASS 'user1'
sql select * from information_schema.ins_users
if $rows != 2 then 
  return -1
endi

# wait mnode2 mnode3 recv data finish
sleep 1000

print =============== step4: stop dnode1
system sh/exec.sh -n dnode1 -s stop -x SIGKILL

$x = 0
step4: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_mnodes -x step4
print $data(1)[0] $data(1)[1] $data(1)[2] 
print $data(2)[0] $data(2)[1] $data(2)[2] 
print $data(3)[0] $data(3)[1] $data(3)[2] 

sql select * from information_schema.ins_users
if $rows != 2 then 
  return -1
endi

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

print =============== step5: stop dnode1
system sh/exec.sh -n dnode1 -s start
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_mnodes -x step5
print $data(1)[0] $data(1)[1] $data(1)[2] 
print $data(2)[0] $data(2)[1] $data(2)[2] 
print $data(3)[0] $data(3)[1] $data(3)[2] 

sql select * from information_schema.ins_users
if $rows != 2 then 
  return -1
endi

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

print =============== step6: stop dnode1
system sh/exec.sh -n dnode2 -s start
system sh/exec.sh -n dnode3 -s stop

$x = 0
step6: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_mnodes -x step6
print $data(1)[0] $data(1)[1] $data(1)[2] 
print $data(2)[0] $data(2)[1] $data(2)[2] 
print $data(3)[0] $data(3)[1] $data(3)[2] 

sql select * from information_schema.ins_users
if $rows != 2 then 
  return -1
endi

$x = 0
step61: 
	$x = $x + 1
	sleep 1000
	if $x == 10 then
		return -1
	endi
sql select * from information_schema.ins_dnodes -x step61
print ===> $data00 $data01 $data02 $data03 $data04 $data05
print ===> $data10 $data11 $data12 $data13 $data14 $data15
print ===> $data20 $data21 $data22 $data23 $data24 $data25
if $data(1)[4] != ready then
  goto step61
endi
if $data(2)[4] != ready then
  goto step61
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
