[Openais] [PATCH 3/7] CTS: add sam tests

Steven Dake sdake at redhat.com
Mon Mar 29 22:39:02 PDT 2010


good for commit

On Tue, 2010-03-30 at 13:30 +1100, Angus Salkeld wrote:
> Add sam tests (basically a copy of test/testsam.c)
> 
> -Angus
> 
> Signed-off-by: Angus Salkeld <asalkeld at redhat.com>
> ---
>  cts/agents/Makefile.am         |    6 +-
>  cts/agents/common_test_agent.h |    9 +
>  cts/agents/confdb_test_agent.c |    5 -
>  cts/agents/sam_test_agent.c    |  417 ++++++++++++++++++++++++++++++++++++++++
>  cts/corosync.py                |   22 ++-
>  cts/corotests.py               |   60 ++++++
>  6 files changed, 511 insertions(+), 8 deletions(-)
>  create mode 100644 cts/agents/sam_test_agent.c
> 
> diff --git a/cts/agents/Makefile.am b/cts/agents/Makefile.am
> index 01dd2ce..be301a3 100644
> --- a/cts/agents/Makefile.am
> +++ b/cts/agents/Makefile.am
> @@ -33,7 +33,7 @@ MAINTAINERCLEANFILES = Makefile.in
>  INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include
>  
>  SOURCES =
> -TEST_AGENTS = cpg_test_agent confdb_test_agent
> +TEST_AGENTS = cpg_test_agent confdb_test_agent sam_test_agent
>  
>  if INSTALL_TESTAGENTS
>  agentdir = $(datadir)/$(PACKAGE)/tests
> @@ -62,6 +62,10 @@ confdb_test_agent_SOURCES = confdb_test_agent.c common_test_agent.c
>  confdb_test_agent_LDADD =  -lconfdb -lcoroipcc ../../exec/coropoll.o
>  confdb_test_agent_LDFLAGS =  -L../../lib 
>  
> +sam_test_agent_SOURCES = sam_test_agent.c common_test_agent.c
> +sam_test_agent_LDADD =  -lsam ../../exec/coropoll.o
> +sam_test_agent_LDFLAGS =  -L../../lib 
> +
>  
>  if BUILD_DARWIN
>  
> diff --git a/cts/agents/common_test_agent.h b/cts/agents/common_test_agent.h
> index fc8983c..b717290 100644
> --- a/cts/agents/common_test_agent.h
> +++ b/cts/agents/common_test_agent.h
> @@ -31,6 +31,15 @@
>   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
>   * THE POSSIBILITY OF SUCH DAMAGE.
>   */
> +
> +
> +
> +#define OK_STR "OK"
> +#define FAIL_STR "FAIL"
> +#define NOT_SUPPORTED_STR "NOT_SUPPORTED"
> +
> +
> +
>  extern int32_t parse_debug;
>  #define HOW_BIG_AND_BUF 4096
>  
> diff --git a/cts/agents/confdb_test_agent.c b/cts/agents/confdb_test_agent.c
> index 6588526..bd5253a 100644
> --- a/cts/agents/confdb_test_agent.c
> +++ b/cts/agents/confdb_test_agent.c
> @@ -51,11 +51,6 @@
>  #include <corosync/confdb.h>
>  #include "common_test_agent.h"
>  
> -
> -#define OK_STR "OK"
> -#define FAIL_STR "FAIL"
> -#define NOT_SUPPORTED_STR "NOT_SUPPORTED"
> -
>  #define INCDEC_VALUE 45
>  
>  confdb_callbacks_t callbacks = {
> diff --git a/cts/agents/sam_test_agent.c b/cts/agents/sam_test_agent.c
> new file mode 100644
> index 0000000..6a96ad1
> --- /dev/null
> +++ b/cts/agents/sam_test_agent.c
> @@ -0,0 +1,417 @@
> +/*
> + * Copyright (c) 2009 Red Hat, Inc.
> + *
> + * All rights reserved.
> + *
> + * Author: Jan Friesse (jfriesse at redhat.com)
> + *
> + * This software licensed under BSD license, the text of which follows:
> + *
> + * Redistribution and use in source and binary forms, with or without
> + * modification, are permitted provided that the following conditions are met:
> + *
> + * - Redistributions of source code must retain the above copyright notice,
> + *   this list of conditions and the following disclaimer.
> + * - Redistributions in binary form must reproduce the above copyright notice,
> + *   this list of conditions and the following disclaimer in the documentation
> + *   and/or other materials provided with the distribution.
> + * - Neither the name of the Red Hat, Inc. nor the names of its
> + *   contributors may be used to endorse or promote products derived from this
> + *   software without specific prior written permission.
> + *
> + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
> + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
> + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
> + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
> + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
> + * THE POSSIBILITY OF SUCH DAMAGE.
> + */
> +
> +/*
> + * Provides test of SAM API
> + */
> +
> +#include <config.h>
> +
> +#include <sys/types.h>
> +#include <stdio.h>
> +#include <stdint.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <signal.h>
> +#include <sys/types.h>
> +#include <sys/socket.h>
> +#include <netinet/in.h>
> +#include <arpa/inet.h>
> +#include <netdb.h>
> +#include <sys/un.h>
> +#include <sys/wait.h>
> +#include <syslog.h>
> +#include <corosync/corotypes.h>
> +#include <corosync/sam.h>
> +#include <corosync/hdb.h>
> +#include "common_test_agent.h"
> +
> +
> +static int test2_sig_delivered = 0;
> +static int test4_hc_cb_count = 0;
> +
> +/*
> + * First test will just register SAM, with policy restart. First instance will
> + * sleep one second, send hc and sleep another 3 seconds. This should force restart.
> + * Second instance will sleep one second, send hc, stop hc and sleep 3 seconds.
> + * Then start hc again and sleep 3 seconds. This should force restart again.
> + * Last instance just calls initialize again. This should end with error.
> + * Then call start, followed by stop and start again. Finally, we will call finalize
> + * twice. One should succeed, second should fail. After this, we will call every function
> + * (none should succeed).
> + */
> +static int test1 (void)
> +{
> +	cs_error_t error;
> +	unsigned int instance_id;
> +	int i;
> +
> +	syslog (LOG_INFO,"%s: initialize\n", __FUNCTION__);
> +	error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
> +	if (error != CS_OK) {
> +		syslog (LOG_ERR, "Can't initialize SAM API. Error %d\n", error);
> +		return 1;
> +	}
> +	syslog (LOG_INFO,"%s: register\n", __FUNCTION__);
> +	error = sam_register (&instance_id);
> +	if (error != CS_OK) {
> +		syslog (LOG_ERR, "Can't register. Error %d\n", error);
> +		return 1;
> +	}
> +
> +	if (instance_id == 1 || instance_id == 2) {
> +		syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
> +		error = sam_start ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
> +			return 1;
> +		}
> +
> +		for (i = 0; i < 10; i++) {
> +			syslog (LOG_INFO,"%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
> +			sleep (1);
> +
> +			syslog (LOG_INFO,"%s iid %d: hc send\n", __FUNCTION__, instance_id);
> +			error = sam_hc_send ();
> +			if (error != CS_OK) {
> +				syslog (LOG_ERR, "Can't send hc. Error %d\n", error);
> +				return 1;
> +			}
> +		}
> +
> +		if (instance_id == 2) {
> +			syslog (LOG_INFO,"%s iid %d: stop\n", __FUNCTION__, instance_id);
> +			error = sam_stop ();
> +
> +			if (error != CS_OK) {
> +				syslog (LOG_ERR, "Can't send hc. Error %d\n", error);
> +				return 1;
> +			}
> +		}
> +
> +		syslog (LOG_INFO,"%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
> +		sleep (3);
> +
> +		syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
> +		error = sam_start ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
> +			return 1;
> +		}
> +
> +		syslog (LOG_INFO,"%s iid %d: sleep 3\n", __FUNCTION__, instance_id);
> +		sleep (3);
> +		return 0;
> +	}
> +
> +	if (instance_id == 3) {
> +		error = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART);
> +		if (error == CS_OK) {
> +			syslog (LOG_ERR, "Can initialize SAM API after initialization");
> +			return 1;
> +		}
> +
> +		error = sam_start ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
> +			return 1;
> +		}
> +		error = sam_stop ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't stop hc. Error %d\n", error);
> +			return 1;
> +		}
> +		error = sam_finalize ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't finalize sam. Error %d\n", error);
> +			return 1;
> +		}
> +		error = sam_finalize ();
> +		if (error == CS_OK) {
> +			syslog (LOG_ERR, "Can finalize sam after finalization!\n");
> +			return 1;
> +		}
> +
> +		if (sam_initialize (2, SAM_RECOVERY_POLICY_RESTART) == CS_OK ||
> +			sam_start () == CS_OK || sam_stop () == CS_OK ||
> +			sam_register (NULL) == CS_OK || sam_hc_send () == CS_OK ||
> +			sam_hc_callback_register (NULL) == CS_OK) {
> +
> +			syslog (LOG_ERR, "Can call one of function after finalization!\n");
> +
> +			return 1;
> +		}
> +
> +		return 0;
> +	}
> +
> +	return 1;
> +}
> +
> +
> +static void test2_signal (int sig) {
> +	syslog (LOG_INFO,"%s\n", __FUNCTION__);
> +
> +	test2_sig_delivered = 1;
> +}
> +
> +/*
> + * This tests recovery policy quit and callback.
> + */
> +static int test2 (void) {
> +	cs_error_t error;
> +	unsigned int instance_id;
> +
> +	syslog (LOG_INFO,"%s: initialize\n", __FUNCTION__);
> +	error = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT);
> +	if (error != CS_OK) {
> +		syslog (LOG_ERR, "Can't initialize SAM API. Error %d\n", error);
> +		return 1;
> +	}
> +	syslog (LOG_INFO,"%s: register\n", __FUNCTION__);
> +	error = sam_register (&instance_id);
> +	if (error != CS_OK) {
> +		syslog (LOG_ERR, "Can't register. Error %d\n", error);
> +		return 1;
> +	}
> +
> +	if (instance_id == 1) {
> +		signal (SIGTERM, test2_signal);
> +
> +		syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
> +		error = sam_start ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
> +			return 1;
> +		}
> +
> +		syslog (LOG_INFO,"%s iid %d: sleep 1\n", __FUNCTION__, instance_id);
> +		sleep (1);
> +
> +		syslog (LOG_INFO,"%s iid %d: hc send\n", __FUNCTION__, instance_id);
> +		error = sam_hc_send ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't send hc. Error %d\n", error);
> +			return 1;
> +		}
> +
> +
> +		syslog (LOG_INFO,"%s iid %d: wait for delivery of signal\n", __FUNCTION__, instance_id);
> +		while (!test2_sig_delivered) {
> +			sleep (1);
> +		}
> +
> +		syslog (LOG_INFO,"%s iid %d: wait for real kill\n", __FUNCTION__, instance_id);
> +
> +		sleep (3);
> +	}
> +
> +	return 1;
> +
> +}
> +
> +/*
> + * Smoke test. Better to turn off coredump ;) This has no time limit, just restart process
> + * when it dies.
> + */
> +static int test3 (void) {
> +	cs_error_t error;
> +	unsigned int instance_id;
> +	int tmp1, tmp2, tmp3;
> +
> +	syslog (LOG_INFO,"%s: initialize\n", __FUNCTION__);
> +	error = sam_initialize (0, SAM_RECOVERY_POLICY_RESTART);
> +	if (error != CS_OK) {
> +		syslog (LOG_ERR, "Can't initialize SAM API. Error %d\n", error);
> +		return 1;
> +	}
> +	syslog (LOG_INFO,"%s: register\n", __FUNCTION__);
> +	error = sam_register (&instance_id);
> +	if (error != CS_OK) {
> +		syslog (LOG_ERR, "Can't register. Error %d\n", error);
> +		return 1;
> +	}
> +
> +	if (instance_id < 100) {
> +		syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
> +		error = sam_start ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
> +			return 1;
> +		}
> +
> +		syslog (LOG_INFO,"%s iid %d: divide by zero\n", __FUNCTION__, instance_id);
> +		tmp2 = rand ();
> +		tmp3 = 0;
> +		tmp1 = tmp2 / tmp3;
> +
> +		return 1;
> +	}
> +
> +	return 0;
> +
> +}
> +
> +static int test4_hc_cb (void)
> +{
> +	syslog (LOG_INFO,"%s %d\n", __FUNCTION__, ++test4_hc_cb_count);
> +
> +	if (test4_hc_cb_count > 10)
> +		return 1;
> +
> +	return 0;
> +}
> +/*
> + * Test event driven healtchecking.
> + */
> +static int test4 (void)
> +{
> +	cs_error_t error;
> +	unsigned int instance_id;
> +
> +	syslog (LOG_INFO,"%s: initialize\n", __FUNCTION__);
> +	error = sam_initialize (100, SAM_RECOVERY_POLICY_RESTART);
> +	if (error != CS_OK) {
> +		syslog (LOG_ERR, "Can't initialize SAM API. Error %d\n", error);
> +		return 1;
> +	}
> +	syslog (LOG_INFO,"%s: register\n", __FUNCTION__);
> +	error = sam_register (&instance_id);
> +	if (error != CS_OK) {
> +		syslog (LOG_ERR, "Can't register. Error %d\n", error);
> +		return 1;
> +	}
> +
> +	if (instance_id == 1) {
> +		syslog (LOG_INFO,"%s iid %d: hc callback register\n", __FUNCTION__, instance_id);
> +		error = sam_hc_callback_register (test4_hc_cb);
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't register hc cb. Error %d\n", error);
> +			return 1;
> +		}
> +
> +
> +		syslog (LOG_INFO,"%s iid %d: start\n", __FUNCTION__, instance_id);
> +		error = sam_start ();
> +		if (error != CS_OK) {
> +			syslog (LOG_ERR, "Can't start hc. Error %d\n", error);
> +			return 1;
> +		}
> +
> +		sleep (2);
> +
> +		syslog (LOG_INFO,"%s iid %d: Failed. Wasn't killed.\n", __FUNCTION__, instance_id);
> +		return 1;
> +	}
> +
> +	if (instance_id == 2) {
> +		return 0;
> +	}
> +
> +	return 1;
> +}
> +
> +static void do_command (int sock, char* func, char*args[], int num_args)
> +{
> +	char response[100];
> +	pid_t pid;
> +	int err;
> +	int stat;
> +	int please_wait = 1;
> +
> +	snprintf (response, 100, "%s", FAIL_STR);
> +
> +	if (parse_debug)
> +		syslog (LOG_INFO,"RPC:%s() called.", func);
> +
> +	pid = fork ();
> +
> +	if (pid == -1) {
> +		syslog (LOG_ERR, "Can't fork\n");
> +		send (sock, response, strlen (response) + 1, 0);
> +		return;
> +	}
> +
> +	if (pid == 0) {
> +		if (strcmp ("test1", func) == 0) {
> +			err = test1 ();
> +		} else if (strcmp ("test2", func) == 0) {
> +			err = test2 ();
> +		} else if (strcmp ("test3", func) == 0) {
> +			err = test3 ();
> +		} else if (strcmp ("test4", func) == 0) {
> +			err = test4 ();
> +		} else {
> +			err = -1;
> +			please_wait = 0;
> +			syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func);
> +			snprintf (response, 100, "%s", NOT_SUPPORTED_STR);
> +		}
> +	}
> +
> +	if (please_wait) {
> +		waitpid (pid, &stat, 0);
> +
> +		if (WEXITSTATUS (stat) == 0) {
> +			snprintf (response, 100, "%s", OK_STR);
> +		} else {
> +			snprintf (response, 100, "%s", FAIL_STR);
> +		}
> +	}
> +	send (sock, response, strlen (response) + 1, 0);
> +}
> +
> +
> +int main (int argc, char *argv[])
> +{
> +	int ret;
> +
> +	openlog (NULL, LOG_CONS|LOG_PID, LOG_DAEMON);
> +	syslog (LOG_ERR, "sam_test_agent STARTING");
> +
> +	parse_debug = 1;
> +	ret = test_agent_run (9036, do_command);
> +	syslog (LOG_ERR, "sam_test_agent EXITING");
> +
> +	return ret;
> +}
> +
> +
> +
> +
> +
> +
> +
> +
> +
> diff --git a/cts/corosync.py b/cts/corosync.py
> index 6962f39..4dc884c 100644
> --- a/cts/corosync.py
> +++ b/cts/corosync.py
> @@ -137,6 +137,7 @@ class corosync_flatiron(ClusterManager):
>              })
>          self.cpg_agent={}
>          self.confdb_agent={}
> +        self.sam_agent={}
>          self.config = CoroConfig ()
>          self.node_to_ip = {}
>          
> @@ -216,6 +217,8 @@ class corosync_flatiron(ClusterManager):
>              self.cpg_agent[node].restart()
>          if self.confdb_agent.has_key(node):
>              self.confdb_agent[node].restart()
> +        if self.sam_agent.has_key(node):
> +            self.sam_agent[node].restart()
>          return ret
>  
>      def StopaCM(self, node):
> @@ -225,8 +228,8 @@ class corosync_flatiron(ClusterManager):
>          self.debug('stoping corosync on : ' + node)
>          if self.cpg_agent.has_key(node):
>              self.cpg_agent[node].stop()
> -        if self.confdb_agent.has_key(node):
> -            self.confdb_agent[node].stop()
> +        if self.sam_agent.has_key(node):
> +            self.sam_agent[node].stop()
>          return ClusterManager.StopaCM(self, node)
>  
>      def test_node_CM(self, node):
> @@ -329,6 +332,8 @@ class TestAgentComponent(ScenarioComponent):
>              self.CM.cpg_agent[node].start()
>              self.CM.confdb_agent[node] = ConfdbTestAgent(node, CM.Env)
>              self.CM.confdb_agent[node].start()
> +            self.CM.sam_agent[node] = SamTestAgent(node, CM.Env)
> +            self.CM.sam_agent[node].start()
>          return 1
>  
>      def TearDown(self, CM):
> @@ -337,6 +342,7 @@ class TestAgentComponent(ScenarioComponent):
>          for node in self.Env["nodes"]:
>              self.CM.cpg_agent[node].stop()
>              self.CM.confdb_agent[node].stop()
> +            self.CM.sam_agent[node].stop()
>  
>  ###################################################################
>  class TestAgent(object):
> @@ -549,3 +555,15 @@ class ConfdbTestAgent(TestAgent):
>      def cpg_local_get(self):
>          return 1
>  
> +###################################################################
> +class SamTestAgent(TestAgent):
> +
> +    def __init__(self, node, Env=None):
> +        TestAgent.__init__(self, "sam_test_agent", node, 9036, env=Env)
> +        self.initialized = False
> +        self.nodeid = None
> +        self.send_recv = True
> +
> +    def cpg_local_get(self):
> +        return 1
> +
> diff --git a/cts/corotests.py b/cts/corotests.py
> index e876602..c356d86 100644
> --- a/cts/corotests.py
> +++ b/cts/corotests.py
> @@ -582,6 +582,62 @@ class ConfdbNotificationTest(CoroTest):
>          else:
>              return self.failure('notification_test failed')
>  
> +###################################################################
> +class SamTest1(CoroTest):
> +    def __init__(self, cm):
> +        CoroTest.__init__(self, cm)
> +        self.name="SamTest1"
> +
> +    def __call__(self, node):
> +        self.incr("calls")
> +        res = self.CM.sam_agent[node].test1()
> +        if 'OK' in res:
> +            return self.success()
> +        else:
> +            return self.failure('sam test 1 failed')
> +
> +###################################################################
> +class SamTest2(CoroTest):
> +    def __init__(self, cm):
> +        CoroTest.__init__(self, cm)
> +        self.name="SamTest2"
> +
> +    def __call__(self, node):
> +        self.incr("calls")
> +        res = self.CM.sam_agent[node].test2()
> +        if 'OK' in res:
> +            return self.success()
> +        else:
> +            return self.failure('sam test 2 failed')
> +
> +###################################################################
> +class SamTest3(CoroTest):
> +    def __init__(self, cm):
> +        CoroTest.__init__(self, cm)
> +        self.name="SamTest3"
> +
> +    def __call__(self, node):
> +        self.incr("calls")
> +        res = self.CM.sam_agent[node].test3()
> +        if 'OK' in res:
> +            return self.success()
> +        else:
> +            return self.failure('sam test 3 failed')
> +
> +###################################################################
> +class SamTest4(CoroTest):
> +    def __init__(self, cm):
> +        CoroTest.__init__(self, cm)
> +        self.name="SamTest4"
> +
> +    def __call__(self, node):
> +        self.incr("calls")
> +        res = self.CM.sam_agent[node].test4()
> +        if 'OK' in res:
> +            return self.success()
> +        else:
> +            return self.failure('sam test 4 failed')
> +
>  
>  GenTestClasses = []
>  GenTestClasses.append(CpgMsgOrderBasic)
> @@ -597,6 +653,10 @@ AllTestClasses.append(ConfdbReplaceTest)
>  AllTestClasses.append(ConfdbIncrementTest)
>  AllTestClasses.append(ConfdbObjectFindTest)
>  AllTestClasses.append(ConfdbNotificationTest)
> +AllTestClasses.append(SamTest1)
> +AllTestClasses.append(SamTest2)
> +AllTestClasses.append(SamTest3)
> +AllTestClasses.append(SamTest4)
>  
>  AllTestClasses.append(ServiceLoadTest)
>  AllTestClasses.append(MemLeakObject)



More information about the Openais mailing list