1 // : vi ts=4 sw=4 noet :
3 ==================================================================================
4 Copyright (c) 2021 Nokia
5 Copyright (c) 2021 AT&T Intellectual Property.
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 ==================================================================================
22 Mmemonic: alarm_test.c
23 Abstract: Unit test for common/src/alarm.c functions.
25 Author: E. Scott Daniels
26 Date: 22 February 2021
31 These tests assume there is a dummy process listening on 127.0.0.1:1986; if it's not there
32 the tests still pass, but coverage is reduced because the sends never happen.
34 static int alarm_test( ) {
35 int errors = 0; // number errors found
37 uta_ctx_t* pctx; // tests into rtable functions need a second context
43 ctx->my_name = strdup( "private" );
44 ctx->my_ip = strdup( "30.4.19.86:2750" );
46 endpt = uta_alarm_endpt(); // check defaults are generated
47 if( fail_if_nil( endpt, "alarm endpoint did not generate a string for defaults" ) ) {
50 errors += fail_if_false( strcmp( endpt, "service-ricplt-alarmmanager-rmr:4560" ) == 0, "alarm endpoint default string not expected" );
54 setenv( "ALARM_MGR_SERVICE_NAME", "127.0.0.1", 1 ); // test to ensure digging from env is good too
55 setenv( "ALARM_MGR_SERVICE_PORT", "999", 1 ); // connect should fail
56 endpt = uta_alarm_endpt(); // check defaults are generated
57 uta_alarm( ctx, ALARM_RAISE, 0, "some info for the alarm" ); // this should fail as the service isn't running
59 setenv( "ALARM_MGR_SERVICE_NAME", "127.0.0.1", 1 ); // test to ensure digging from env is good too
60 setenv( "ALARM_MGR_SERVICE_PORT", "1986", 1 );
61 endpt = uta_alarm_endpt(); // check defaults are generated
62 if( fail_if_nil( endpt, "alarm endpoint did not generate a string when name/port are set in env" ) ) {
65 errors += fail_if_false( strcmp( endpt, "127.0.0.1:1986" ) == 0, "alarm endpoint string not expected when values are in env" );
70 // these functions do not return values; driving for coverage and crash testing
71 uta_alarm( ctx, ALARM_RAISE, 0, "some info for the alarm" );
72 uta_alarm( ctx, ALARM_CLEAR, 0, NULL );
73 uta_alarm_send( ctx, NULL ); // ensure nil message doesn't crash us
76 // ------ drive the alarm if dropping function in the route table code --------------------------------
78 pctx = mk_dummy_ctx(); // grab a private context for rt to use
81 These tests don't return anything that we can check; driving just to cover the lines and ensure
82 we don't segfault or something bad like that.
85 alarm_if_drops( ctx, pctx ); // should do nothing; no drops indicated
87 ctx->dcount = 1024; // make it look like we dropped things
88 alarm_if_drops( ctx, pctx ); // should drive the code block to send alarm and put is in dropping mode
90 ctx->dcount = 1028; // make it look like we are still dropping
91 alarm_if_drops( ctx, pctx ); // drive the just reset time to clear block
93 alarm_if_drops( ctx, pctx ); // drive the check to see if past the clear time (it's not) to reset timer
95 fprintf( stderr, "<TEST> pausing 65 seconds before driving last alarm if drops call\n" );
96 sleep( 65 ); // we must pause for longer than the timer so we can drive last block
97 alarm_if_drops( ctx, pctx ); // should appear that we're not dropping and reset the alarm
100 // -------------------------- tidy the house ---------------------------------------------------------
102 free( ctx->my_name );
108 free( pctx->my_name );