ProCurve NAC 800/Test

Материал из Xgu.ru

Перейти к: навигация, поиск
stub.png
Данная страница находится в разработке.
Эта страница ещё не закончена. Информация, представленная здесь, может оказаться неполной или неверной.

Если вы считаете, что её стоило бы доработать как можно быстрее, пожалуйста, скажите об этом.


Как создавать тесты и изменять существующие на ProCurve NAC 800.

NAC 800 использует Python v2.4.1.

Примеры тестов есть на диске NAC 800 CD в каталоге /sampleTests

Шаблон:

#!/usr/bin/python
from BaseClasses.SABase import SABase as SABase

#
# This allows a script to be tested from the command line.
#
if __name__ == '__main__':
	
	import testTemplate
	t = testTemplate.TestTemplate()
	t.processCommandLine()


#
# The class definition. All classes must be derived from the SABase class.
#
class TestTemplate(SABase):

	# 
	# Make up a test id. Just make sure it doesn't match any existing test ids.
	# 
	testId = "TestId"

	# 
	# Make up test name. Just make sure it doesn't match any existing test names.
	#
	testName = "Test Name"

	#
	# Assign the test to an existing group or create a new group.
	# Groups are configured and created in the policies.xml file <group>
	# section (See the Adding new groups section).
	# testGroupId = "TestGroup"
	# This is the HTML that will be displayed in the test properties page
	# in the policy editor.
	#
	testConfig = \
	"""
	<HTML>Test Config HTML</HTML>
	"""
	
	# These are any default values you want to assign to the input parameters
	# in the testConfig HTML.
	#
	defaultConfigValues = {}
	
	# A short summary for the test. This will show up in the description field
	# when editing NAC policies in the management UI.
	# 
	testSummary = \
	"""
	My short description
	"""
	
	# 
	# This is field is unused at the moment.
	# field in the policy editor.
	# 
	testDescription = ''

	# 
	# These are the arguments to run the test. This is displayed in the command
	# line help.
	# 
	testArguments = \
	"""
	My test arguments
	""
	# 
	# All tests must define the runTest method with the self and the debug
	# parameters.
	# 
	def runTest(self,debug=0):
	
		#
		# All tests must call the initialize routine
		#
		self.initTest()

		#
		# Create a hash to store the return results.
		# All tests must fill return a hash with the following keys:
		#
		# status_code - 0 if an unexpected error occurred, 1 if successful
		# result_code - pass, fail or some error
		# result_message - the message to display to the end-user
		#
		returnHash = {}
		returnHash["status_code"] = 1
		returnHash["result_code"] = "pass"
		returnHash["result_message"] = "Some nice text that a user can read here."

		try:
			#
			# Replace 'pass' with your test here. Modify the returnHash accordingly.
			#
			pass
		except:
			#
			# Set the return status when exception occurs
			#
			import sys
			returnHash['status_code'] = 0
			returnHash['result_code'] = "unknown_error"
			returnHash['result_message'] = sys.exc_type, sys.exc_value
			return(returnHash)

		#
		# Always use the doReturn function; this allows superclass to add or modify
		# any items in the returnHash as necessary.
		#
		return(self.doReturn(returnHash))

Use the template, as shown in Шаблон, to create a new test script.

As an example, the new test script is called checkOpenPorts.py, and it fails if any of the specified ports are open on the target host being tested.

Before examining the code, consider the following information about the test scripts:

  • All test scripts contain a self.inputParams hash table that has all input parameters configured through the policy properties HTML. For example, if the testConfig variable for the test is set to:
<input id="myparam" name="myparam" value="">

Then, the self inputParams contains a myparams key that is set to the value of the HTML input element set in the policy editor.

  • All test scripts contain a self.session member variable that is set

by NAC 800 when the test class is instantiated. It contains a reference to a Session object, which is a built-in Python class defined by NAC 800 and is used internally by the BasicTests class described later in this section. However, to retrieve the host name or IP address, use host() method:

self.session.host()

when developing scripts.

  • All tests contain a reference to the BasicTests class called

self.bt. The self.bt class gives you access to commonly used functions for testing endpoints including registry operations and service operations.

See “BasicTests API” on page 13-30 for more information on the BasicTests API. This example does not use this API.

2. figure 3-4 shows the code for the new checkOpenPorts.py test. The file is included on the NAC 800 CD as /sampleTests/ checkOpenPorts.py. Review the code. The comments explain each section of the code.