###############################################################################
# 
# Makefile for testing an installed Linux release
# 
###############################################################################
OS = linux

ROOT = UNDEFINED

CIMSERVER_PATH = /usr/bin/

include mak/commands.mak

create_providerlinks:
	@make createlink OBJECT=/usr/share/Pegasus/test/lib64/libSampleInstanceProvider.so.1 LINKNAME=/usr/lib64/Pegasus/providers/libSampleInstanceProvider.so
	@make createlink OBJECT=/usr/share/Pegasus/test/lib64/libSampleMethodProvider.so.1 LINKNAME=/usr/lib64/Pegasus/providers/libSampleMethodProvider.so
	@make createlink OBJECT=/usr/share/Pegasus/test/lib64/libFamilyProvider.so.1 LINKNAME=/usr/lib64/Pegasus/providers/libFamilyProvider.so
	@make createlink OBJECT=/usr/share/Pegasus/test/lib64/libIndicationStressTestConsumer.so.1 LINKNAME=/usr/lib64/Pegasus/providers/libIndicationStressTestConsumer.so
	@make createlink OBJECT=/usr/share/Pegasus/test/lib64/libIndicationStressTestProvider.so.1 LINKNAME=/usr/lib64/Pegasus/providers/libIndicationStressTestProvider.so
	@make createlink OBJECT=/usr/share/Pegasus/test/lib64/libIndicationTestProvider.so.1 LINKNAME=/usr/lib64/Pegasus/providers/libIndicationTestProvider.so

remove_providerlinks:
	rm -f /usr/lib64/Pegasus/providers/libSampleInstanceProvider.so
	rm -f /usr/lib64/Pegasus/providers/libSampleMethodProvider.so
	rm -f /usr/lib64/Pegasus/providers/libFamilyProvider.so
	rm -f /usr/lib64/Pegasus/providers/libIndicationStressTestConsumer.so
	rm -f /usr/lib64/Pegasus/providers/libIndicationStressTestProvider.so
	rm -f /usr/lib64/Pegasus/providers/libIndicationTestProvider.so

prestarttests:
	@make -s prestarttests_internal

prestarttests_internal:
	./bin/TestAbstract
	./bin/TestArray
	./bin/TestAsyncQueue
	./bin/TestBase64
	./bin/TestClassDecl
	./bin/TestDateTime
	./bin/TestFlavor
	./bin/TestFormatter
	./bin/TestHashTable
	./bin/TestInstanceDecl
	./bin/TestList
	./bin/TestLogger
	./bin/TestMethod
	./bin/TestObject
	./bin/TestOperationContext
	./bin/TestParamValue
	./bin/TestParameter
	./bin/TestProperty
	./bin/TestQualifier
	./bin/TestQualifierDecl
	./bin/TestQualifierList
	./bin/TestReference
	./bin/TestResolve
	./bin/TestResponseHandler
	./bin/TestScope
	./bin/TestStack
	./bin/TestStopwatch
	./bin/TestStrToInstName
	./bin/TestString
	./bin/TestTimeValue
	./bin/TestToMof
	./bin/TestTracer
	./bin/TestValidateClass
	./bin/TestValue

poststarttests:
	@make -s poststarttests_internal

poststarttests_internal:
	/usr/share/Pegasus/test/bin/TestClient -local
	/usr/share/Pegasus/test/bin/TestClientOSProvider
	/usr/share/Pegasus/test/bin/TestPegClientInvokeMethod2
	/usr/share/Pegasus/test/bin/TestIndicationStressTest IndicationStressTestClass test/TestProvider setup WQL
	/usr/share/Pegasus/test/bin/TestIndicationStressTest IndicationStressTestClass test/TestProvider run 100 1
	/usr/share/Pegasus/test/bin/TestIndicationStressTest IndicationStressTestClass test/TestProvider cleanup
	/usr/share/Pegasus/test/bin/TestSnmpHandler setup WQL
	/usr/share/Pegasus/test/bin/TestSnmpHandler run 4 2
	/usr/share/Pegasus/test/bin/TestSnmpHandler cleanup
	/usr/share/Pegasus/test/bin/TestSnmpHandler removelog

###############################################################################
# The tests target stops the cimserver, runs the unit tests, starts the
# cimserver, and runs the poststarttests and local TestClient.
# It is necessary to manually run make setupTEST before running make tests,
# and to manually run make unsetupTEST afterward.
# The setupTEST target moves the repository directory to repository.bak, and
# moves the test repository into the repository directory.  The cimserver is
# started, and links are created to the provider libraries.
# Since it may not always be desired to move the repository, the make
# setupTEST step is not done automatically as part of the tests target.
# The unsetupTEST target stops the cimserver, moves the repository to the test
# repository directory, restores the original repository from repository.bak,
# and removes the links to the provider libraries.
# Since in case of failure, it is desired to leave the system in its current
# state to facilitate debugging of the failure, the make unsetupTEST step is
# not done automatically as part of the tests target.
# Note that in case of failure, the original repository remains in
# repository.bak.
###############################################################################
tests:
	@make -i cimstop
	@make prestarttests
	@make runTestSuite CIMSERVER_CONFIG_OPTIONS="" TESTSUITE_CMDS="make@@poststarttests"
	@make runTestSuite CIMSERVER_CONFIG_OPTIONS="enableHttpConnection=false enableHttpsConnection=false" TESTSUITE_CMDS="/usr/share/Pegasus/test/bin/TestClient@@-local"

###############################################################################
# The testsWithSetupAndUnsetup target stops the cimserver, runs the unit tests,
# runs make setupTEST, runs the poststarttests and local TestClient, and runs
# make unsetupTEST.  If poststarttests or local TestClient exits with a
# non-zero status, make unsetupTEST is also run to restore the system to its
# original state.
# It is not necessary to manually run make setupTEST before, nor to run make
# unsetupTEST after, running make testsWithSetupAndUnsetup.
# The setupTEST target moves the repository directory to repository.bak, and
# moves the test repository into the repository directory.  The cimserver is
# started, and links are created to the provider libraries.
# The unsetupTEST target stops the cimserver, moves the repository to the test
# repository directory, restores the original repository from repository.bak,
# and removes the links to the provider libraries.
###############################################################################
testsWithSetupAndUnsetup:
	@make -i cimstop
	@make prestarttests
	@make setupTEST
	@make runTestSuite CIMSERVER_CONFIG_OPTIONS="" TESTSUITE_CMDS="make@@poststarttests" || { make unsetupTEST; exit 1; }
	@make runTestSuite CIMSERVER_CONFIG_OPTIONS="enableHttpConnection=false enableHttpsConnection=false" TESTSUITE_CMDS="/usr/share/Pegasus/test/bin/TestClient@@-local" || { make unsetupTEST; exit 1; }
	@make unsetupTEST

setupTEST:
ifeq ($(wildcard /var/lib/Pegasus/repository.bak),)
	@make -i cimstop
	@mv /var/lib/Pegasus/repository /var/lib/Pegasus/repository.bak
	@mv /var/lib/Pegasus/testrepository /var/lib/Pegasus/repository
	@mv /etc/snmp/snmptrapd.conf /etc/snmp/snmptrapd.conf.bak
	@cp /usr/share/Pegasus/test/snmptrapd.conf /etc/snmp/snmptrapd.conf
	@make cimstart
	@make create_providerlinks
else
	@echo "Setup Aborted: /var/lib/Pegasus/repository.bak already exists"
endif

unsetupTEST:
ifneq ($(wildcard /var/lib/Pegasus/repository/tog-pegasus-testrepository),)
	@make -i cimstop
	@mv /var/lib/Pegasus/repository /var/lib/Pegasus/testrepository
	@mv /var/lib/Pegasus/repository.bak /var/lib/Pegasus/repository
	@mv /etc/snmp/snmptrapd.conf.bak /etc/snmp/snmptrapd.conf
endif
	@make remove_providerlinks

