[icinga-checkins] icinga.org: icinga-core/mfriedrich/fixes: Fixed deferred sorting of comments and downtimes. Added tests to prove sorting

git at icinga.org git at icinga.org
Sun Jan 10 08:40:18 CET 2010


Module: icinga-core
Branch: mfriedrich/fixes
Commit: dad43d20b92e23a3c24b90e83b6a6f1407b1337a
URL:    https://git.icinga.org/?p=icinga-core.git;a=commit;h=dad43d20b92e23a3c24b90e83b6a6f1407b1337a

Author: Ton Voon <ton.voon at opsera.com>
Date:   Thu Jan  7 01:03:01 2010 +0000

Fixed deferred sorting of comments and downtimes. Added tests to prove sorting

---

 cgi/extinfo.c                    |    1 +
 common/comments.c                |    6 +-
 common/downtime.c                |    6 +-
 t-tap/Makefile.in                |    8 +++-
 t-tap/test_xsddefault.c          |  102 ++++++++++++++++++++++++++++++++++++++
 t/660status-downtimes-comments.t |    9 ++-
 t/bin/generate_downtimes         |    2 +-
 7 files changed, 123 insertions(+), 11 deletions(-)

diff --git a/cgi/extinfo.c b/cgi/extinfo.c
index 94a2b9d..bcb19cd 100644
--- a/cgi/extinfo.c
+++ b/cgi/extinfo.c
@@ -2596,6 +2596,7 @@ void display_comments(int type){
 	printf("<TR CLASS='comment'><TH CLASS='comment'>Entry Time</TH><TH CLASS='comment'>Author</TH><TH CLASS='comment'>Comment</TH><TH CLASS='comment'>Comment ID</TH><TH CLASS='comment'>Persistent</TH><TH CLASS='comment'>Type</TH><TH CLASS='comment'>Expires</TH><TH CLASS='comment'>Actions</TH></TR>\n");
 
 	/* check all the comments to see if they apply to this host or service */
+	/* Comments are displayed in the order they are read from the status.dat file */
 	for(temp_comment=get_first_comment_by_host(host_name);temp_comment!=NULL;temp_comment=get_next_comment_by_host(host_name,temp_comment)){
 
 		display_comment=FALSE;
diff --git a/common/comments.c b/common/comments.c
index 9710bac..aa9f2d9 100644
--- a/common/comments.c
+++ b/common/comments.c
@@ -546,8 +546,8 @@ int add_comment(int comment_type, int entry_type, char *host_name, char *svc_des
         }
 
 static int comment_compar(const void *p1, const void *p2){
-	comment *c1 = (comment *)p1;
-	comment *c2 = (comment *)p2;
+	comment *c1 = *(comment **)p1;
+	comment *c2 = *(comment **)p2;
 	return (c1->comment_id < c2->comment_id) ? -1 : (c1->comment_id - c2->comment_id);
 	}
 
@@ -572,7 +572,7 @@ int sort_comments(void){
 		return ERROR;
 
 	qsort((void *)array, i, sizeof(*array), comment_compar);
-	last_comment = array[0];
+	comment_list = last_comment = array[0];
 	for (i=1; i<unsorted_comments;i++){
 		last_comment->next = array[i];
 		last_comment = last_comment->next;
diff --git a/common/downtime.c b/common/downtime.c
index b3ae720..5e428fb 100644
--- a/common/downtime.c
+++ b/common/downtime.c
@@ -973,8 +973,8 @@ int add_downtime(int downtime_type, char *host_name, char *svc_description, time
         }
 
 static int downtime_compar(const void *p1, const void *p2){
-	scheduled_downtime *d1 = (scheduled_downtime *)p1;
-	scheduled_downtime *d2 = (scheduled_downtime *)p2;
+	scheduled_downtime *d1 = *(scheduled_downtime **)p1;
+	scheduled_downtime *d2 = *(scheduled_downtime **)p2;
 	return (d1->start_time < d2->start_time) ? -1 : (d1->start_time - d2->start_time);
 	}
 
@@ -999,7 +999,7 @@ int sort_downtime(void){
 		return ERROR;
 
 	qsort((void *)array, i, sizeof(*array), downtime_compar);
-	last_downtime = array[0];
+	scheduled_downtime_list = last_downtime = array[0];
 	for (i=1; i<unsorted_downtime;i++){
 		last_downtime->next = array[i];
 		last_downtime = last_downtime->next;
diff --git a/t-tap/Makefile.in b/t-tap/Makefile.in
index 80cb322..e17eb7f 100644
--- a/t-tap/Makefile.in
+++ b/t-tap/Makefile.in
@@ -6,12 +6,15 @@ SRC_COMMON=../common
 SRC_INCLUDE=../include
 SRC_XDATA=../xdata
 SRC_BASE=../base
+SRC_CGI=../cgi
 
 CC=@CC@
 CFLAGS=@CFLAGS@ @DEFS@ -DNSCORE -I../include -I../tap/src
 TAPOBJ=../tap/src/tap.o
 
-TESTS = test_logging test_events test_timeperiods test_icinga_config
+TESTS = test_logging test_events test_timeperiods test_icinga_config test_xsddefault
+
+test_xsddefault_OBJS = ${SRC_CGI}/xstatusdata-cgi.o ${SRC_CGI}/cgiutils.o ${SRC_CGI}/comments-cgi.o ${SRC_CGI}/objects-cgi.o ${SRC_CGI}/skiplist.o ${SRC_CGI}/statusdata-cgi.o ${SRC_CGI}/downtime-cgi.o ${SRC_CGI}/xobjects-cgi.o
 
 LDFLAGS=@LDFLAGS@
 LIBS=@LIBS@
@@ -83,6 +86,9 @@ test_icinga_config: test_icinga_config.c $(SRC_BASE)/utils.o ${SRC_BASE}/config.
 test_timeperiods: test_timeperiods.c $(SRC_BASE)/utils.o ${SRC_BASE}/config.o ${SRC_BASE}/macros-base.o ${SRC_BASE}/objects-base.o ${SRC_BASE}/skiplist.o ${SRC_BASE}/xobjects-base.o 
 	$(CC) $(CFLAGS) -o $@ test_timeperiods.c $(SRC_BASE)/utils.o ${SRC_BASE}/config.o ${SRC_BASE}/macros-base.o ${SRC_BASE}/objects-base.o ${SRC_BASE}/skiplist.o ${SRC_BASE}/xobjects-base.o $(TAPOBJ) $(BROKER_LDFLAGS) $(LDFLAGS) $(MATHLIBS) $(SOCKETLIBS) $(THREADLIBS) $(BROKERLIBS) $(LIBS)
 
+test_xsddefault: test_xsddefault.c ${test_xsddefault_OBJS}
+	$(CC) $(CFLAGS) -o $@ test_xsddefault.c $(TAPOBJ) ${test_xsddefault_OBJS}
+
 test: $(TESTS)
 	HARNESS_PERL=./test_each.t perl -MTest::Harness -e '$$Test::Harness::switches=""; runtests(@ARGV)' $(TESTS)
 
diff --git a/t-tap/test_xsddefault.c b/t-tap/test_xsddefault.c
new file mode 100644
index 0000000..f7a019e
--- /dev/null
+++ b/t-tap/test_xsddefault.c
@@ -0,0 +1,102 @@
+/*****************************************************************************
+ *
+ * test_xsddefault.c - Test configuration loading
+ *
+ * Program: Nagios Core Testing
+ * License: GPL
+ * Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors
+ * Copyright (c) 1999-2009 Ethan Galstad
+ *
+ * First Written:   06-01-2010, based on test_nagios_config.c
+ *
+ * Description:
+ *
+ * Tests Nagios status file reading
+ *
+ * License:
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ *****************************************************************************/
+
+/* Need these to get CGI mode */
+#undef NSCORE
+#define NSCGI 1
+#include "../include/config.h"
+#include "../include/common.h"
+#include "../include/locations.h"
+#include "../include/statusdata.h"
+#include "../include/comments.h"
+#include "../include/downtime.h"
+#include "../include/macros.h"
+#include "../include/skiplist.h"
+#include "tap.h"
+
+extern comment *comment_list;
+extern scheduled_downtime *scheduled_downtime_list;
+
+int main(int argc, char **argv){
+	int result;
+	int c;
+	int last_id;
+	time_t last_time;
+	comment *temp_comment;
+	scheduled_downtime *temp_downtime;
+
+	plan_tests(7);
+
+	chdir("../t");
+
+	ok( system("cat var/status.dat > var/status-generated.dat")==0, "New status.dat file");
+	ok( system("bin/generate_downtimes 10 >> var/status-generated.dat")==0, "Generated 10 downtimes");
+
+	result = xsddefault_read_status_data("etc/cgi-with-generated-status.cfg", 0);
+	ok(result==OK, "Read cgi status data okay");
+
+	temp_comment = comment_list;
+	last_id=0;
+	c=0;
+	result=OK;
+	while(temp_comment!=NULL){
+		c++;
+		if(temp_comment->comment_id <= last_id) {
+			result=ERROR;
+			break;
+		}
+		last_id=temp_comment->comment_id;
+		temp_comment=temp_comment->next;
+	}
+	ok(c==12, "Got %d comments - expected 12", c);
+	ok(result==OK, "All comments in order");
+
+	temp_downtime = scheduled_downtime_list;
+	last_time=0;
+	c=0;
+	result=OK;
+	while(temp_downtime!=NULL){
+		c++;
+		if(temp_downtime->start_time < last_time) {
+			result=ERROR;
+			break;
+		}
+		last_time=temp_downtime->start_time;
+		temp_downtime=temp_downtime->next;
+	}
+	ok(c==20, "Got %d downtimes - expected 20", c);
+	ok(result==OK, "All downtimes in order");
+
+	return exit_status();
+	}
+
+
diff --git a/t/660status-downtimes-comments.t b/t/660status-downtimes-comments.t
index 5981a55..54d91a9 100644
--- a/t/660status-downtimes-comments.t
+++ b/t/660status-downtimes-comments.t
@@ -26,7 +26,7 @@ my $iteration = 1;
 my $iterations_max = shift @ARGV || 5;
 my $copies;
 
-plan tests => 4 * $iterations_max;
+plan tests => 3 * $iterations_max;
 
 while($iteration <= $iterations_max) {
 	$copies = (10)**($iteration);
@@ -39,9 +39,12 @@ while($iteration <= $iterations_max) {
 	my $duration = time-$start;
 	like( $output, "/This service has $num_comments comments associated with it/", "Found $num_comments comments in HTML output from status.dat. Took $duration seconds" );
 
+	# This test is invalid - the comments displayed are in the order they are read
+	# As the test status.dat generator is in a random order, the output will also be in the same
+	# random order
 	# Check that the comments ids are sorted
-	$output = `NAGIOS_CGI_CONFIG=etc/cgi-with-generated-status.cfg REQUEST_METHOD=GET REMOTE_USER=nagiosadmin QUERY_STRING="type=2&host=host1&service=Dummy+service" $extinfo_cgi`;
-	check_decrementing_comment_ids();
+	#$output = `NAGIOS_CGI_CONFIG=etc/cgi-with-generated-status.cfg REQUEST_METHOD=GET REMOTE_USER=nagiosadmin QUERY_STRING="type=2&host=host1&service=Dummy+service" $extinfo_cgi`;
+	#check_decrementing_comment_ids();
 
 	$iteration++;
 }
diff --git a/t/bin/generate_downtimes b/t/bin/generate_downtimes
index b9476f3..3ba7f3e 100755
--- a/t/bin/generate_downtimes
+++ b/t/bin/generate_downtimes
@@ -13,7 +13,7 @@ my $servicecomment_id_start=1024;
 my $servicecomment_ids = {};
 
 my $downtime_id = 2596;
-my $starttime = 9000000000;
+my $starttime = 1920000000;
 my $downtime_template = <<EOF;
 servicecomment {
     host_name=$hostname





More information about the icinga-checkins mailing list