don't stop believing

hecate 실습 (geth) 2 본문

Python/hecate & tmux

hecate 실습 (geth) 2

Tongchun 2018. 9. 20. 19:31

terminal based application (cli tool) 테스트에 사용하려고 templit을 만들어 봤습니다.


참고해서 사용하면 좋을 것 같습니다.


# coding=utf-8

import hecate.runner as r
from hecate.hecate import Runner, AbnormalExit
import sys, os
import time, datetime
import signal
import unittest

Runner.print_on_exit = False

class TestUtils():

	def getDateStrings(self):
		# 날짜를 확인하기 위해 여러 형태로 날자 데이터를 불러옵니다.
		i = datetime.datetime.now()
		dt = datetime.datetime(i.year, i.month, i.day, i.hour, i.minute, i.second)
		ts = time.mktime(dt.timetuple())

		# 날짜 데이터를 이용해 로그파일 이름을 지정합니다.
		logFileName = datetime.datetime.fromtimestamp(int(ts)).strftime('%Y%m%d%H%M%S') + ".log"

		return (logFileName)

	def getAppearByEnding(self, first):
		# list에서 모든 공백을 제거하고 가장 마지막에서 찾습니다.
		f = list(filter(None, first.splitlines()))
		c = len(f) - 1
		if f[c] == ">": del f[-1]
		return f[-1]


	def getAppearByReverse(self, first, command):
		# list의 순서를 역순으로 변경하고 index로 찾습니다.
		screenReverseArray = first.splitlines()[::-1]
		searchindex = screenReverseArray.index("> " + command)
		return (searchindex, screenReverseArray[searchindex - 1])
		

	def writeLog(self, logFileName, searchIndex, writeData):
		# 로그 파일에 Testing 시작을 기록합니다.

		with open(logFileName, 'at') as f:
			if writeData == "Start":
				data = "------------ Testing Start -------------" + "\n"
			elif writeData == "Finish":
				data = "------------ Testing Finish ------------" + "\n"
			else:
				data = "[IDX:" + str(searchIndex) + "][" + str(datetime.datetime.now()) + "] " + writeData

			print(data)
			f.write(data)


class TestingGeth(unittest.TestCase):
	
	def test_geth(self):
		
		t = TestUtils()

		rows, columns = os.popen('stty size', 'r').read().split()
		logFileName = t.getDateStrings()
		testingLines = 1000
		avgSleepTime = 0.5

		t.writeLog(logFileName, testingLines, 'Start')

		with Runner(	"/usr/local/bin/geth", 
						"--networkid" ,"45", 
						"--nodiscover", 
						"--maxpeers", "0", 
						"--datadir", "/Users/tongchunkim/Documents/Test_Ethereum/data_testnet", 
						"console", 
						"2>>", "/Users/tongchunkim/Documents/Test_Ethereum/data_testnet/geth.log", 
						width=columns, height=testingLines) as h:

			h.await_text("Welcome to the Geth JavaScript console!", 5)

			# 로그파일 확인을 위해 같은 테스트를 10번 반복합니다.
			for i in range(0, 10):

				# coinbase를 확인합니다.
				command = "eth.coinbase"
				h.write(command)
				h.press("Enter")
				time.sleep(avgSleepTime)

				searchindex, value = t.getAppearByReverse(h.screenshot(), command)

				data = command + ": " + value
				t.writeLog(logFileName, searchindex, data)

				# 명령에 대한 결과값을 assert 명령으로 처리할 수 있습니다.
				self.assertEqual(value, '"0x78ce083531ff41d26c7002efcf4eec1fd11deaa0"')

				# blockNumber를 확인합니다.
				command = "eth.blockNumber"
				h.write(command)
				h.press("Enter")
				time.sleep(avgSleepTime)

				searchindex, value = t.getAppearByReverse(h.screenshot(), command)

				data = command + ": " + value
				t.writeLog(logFileName, searchindex, data)

				assert 0 < int(value), "BlockNumber is less then 0."

			command = "exit"
			h.write(command)
			h.press("Enter")

			searchindex, value = t.getAppearByReverse(h.screenshot(), command)

			data = command + ": " + value
			t.writeLog(logFileName, searchindex, data)

			h.await_exit()


		# 테스트를 종료합니다.
		t.writeLog(logFileName, 0, 'Finish')
	

if __name__== "__main__":
	unittest.main()


'Python > hecate & tmux' 카테고리의 다른 글

tmux 설치 (on CentOS, Ubuntu)  (0) 2018.10.15
hecate 실습 (geth) 1  (0) 2018.09.17
hecate python module 설치와 기본 사용 (on Mac)  (0) 2018.09.16
Comments