1 // vim: ts=4 sw=4 noet :
3 ==================================================================================
4 Copyright (c) 2020 Nokia
5 Copyright (c) 2020 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 Mnemonic: config_test.cpp
23 Abstract: Unit test to drive the config functions.
26 Author: E. Scott Daniels
40 #include "../src/xapp/xapp.hpp"
42 //#include "../src/json/jhash.hpp"
43 //#include "../src/json/jhash.cpp"
45 #include "../src/config/config.hpp" // include things directly under test
46 #include "../src/config/config_cb.hpp"
47 #include "../src/config/config.cpp"
48 #include "../src/config/config_cb.cpp"
50 #include "ut_support.cpp"
52 char* data = (char *) "some data to validate in callback";
53 bool callback_driven = false;
58 Notification callback to see that it is driven on change.
60 void ncb( xapp::Config& c, void* data ) {
61 cb_errors += fail_if( data == NULL, "callback function did not get a good pointer" );
63 auto v = c.Get_control_value( "measurement_interval" );
64 cb_errors += fail_if( v != 1000, "measurement value in new file wasn't different" );
66 callback_driven = true;
69 int main( int argc, char** argv ) {
72 set_test_name( "config_test" );
75 auto x = new Xapp( (char*) "43086", false );
76 if( fail_if( x == NULL, "could not allocate xapp" ) > 0 ) {
77 announce_results( 1 );
81 fprintf( stderr, "<INFO> sussing info from config1.json\n" );
82 auto c = new xapp::Config( "config1.json" );
83 errors += fail_if( c == NULL, "unable to allocate a config with alternate name" );
85 auto s = c->Get_control_str( "ves_collector_address" );
86 errors += fail_if( s.empty(), "expected control string not found" );
87 fprintf( stderr, "<INFO> collector address string var: %s\n", s.c_str() );
89 s = c->Get_port( "rmr-data-out" );
90 errors += fail_if( s.empty(), "expected port string not found" );
91 fprintf( stderr, "<INFO> port string var: %s\n", s.c_str() );
93 s = c->Get_port( "no-interface" );
94 errors += fail_if( ! s.empty(), "did not return empty when get port given an invalid name" );
96 s = c->Get_control_str( "no-such-control" );
97 errors += fail_if( ! s.empty(), "expected empty string for missing control got a string" );
99 fprintf( stderr, "<INFO> unexpected string for no such control name: %s\n", s.c_str() );
102 auto v = c->Get_control_value( "measurement_interval" );
103 errors += fail_if( v == 0.0, "epxected measurement interval control value not found" );
105 auto b = c->Get_control_bool( "debug_mode" );
106 errors += fail_if( b == false, "epxected debug mode control boolean not found or had wrong value" );
109 // ----- test sussing path and using default name ----------------------------------
110 fprintf( stderr, "<INFO> sussing info from default (no name)\n" );
111 c = new xapp::Config( ); // drive for coverage
113 fprintf( stderr, "<INFO> sussing info from default (env var == ./config1.json)\n" );
114 setenv( (char *) "XAPP_DESCRIPTOR_PATH", "./config1.json", 1 ); // this var name is bad; it's not a path, but fname
115 c = new xapp::Config( );
117 s = c->Get_control_str( "ves_collector_address" );
118 errors += fail_if( s.empty(), "expected collector address control string not found" );
119 fprintf( stderr, "<INFO> string var: %s\n", s.c_str() );
121 v = c->Get_control_value( "measurement_interval" );
122 errors += fail_if( v == 0.0, "expected measurement interval control value not found" );
124 b = c->Get_control_bool( "debug_mode" );
125 errors += fail_if( b == false, "expected debug mode control boolean not found" );
128 auto cs = c->Get_contents();
129 if( fail_if( cs.empty(), "get contents returned an empty string" ) == 0 ) {
130 fprintf( stderr, "<INFO> contents from file: %s\n", cs.c_str() );
131 fprintf( stderr, "<INFO> ---------------------\n" );
137 // -------------- force callback to drive and test ---------------------------------
139 fprintf( stderr, "<INFO> load config-file.json for listener coverage testing\n" );
140 c = new xapp::Config( "config-file.json" ); // set filname with out leading path
141 c->Set_callback( ncb, data ); // for coverage in listener
143 fprintf( stderr, "<INFO> load ./config-file.json for callback testing\n" );
144 c = new xapp::Config( "./config-file.json" );
145 c->Set_callback( ncb, data );
147 fprintf( stderr, "<INFO> sleeping to give callback time to be initialsed\n" );
149 if( rename( (char *) "./config-file.json", (char *) "./tmp-config.json" ) == 0 ) { // rename (should not cause callback)
150 fprintf( stderr, "<INFO> file moved; sleeping a bit\n" );
152 errors += fail_if( callback_driven, "callback was driven when file was deleted/moved away" );
154 if( rename( (char *) "./tmp-config.json", (char *) "./config-file.json" ) == 0 ) { // put it back to drive callback
155 fprintf( stderr, "<INFO> sleeping to give callback time to be driven\n" );
158 errors += fail_if( ! callback_driven, "callback was never executed" );
160 fprintf( stderr, "<WARN> attempt to move config file back failed: %s\n", strerror( errno ) );
163 fprintf( stderr, "<WARN> attempt to move config file away failed: %s\n", strerror( errno ) );
167 // ----- force errors where we can -----------------------------------------
168 fprintf( stderr, "<INFO> json parse errors expected to be reported now\n" );
169 c = new xapp::Config( "not-there.json" ); // json parse will fail
171 v = c->Get_control_value( "measurement_interval", 999 );
172 errors += fail_if( v != 999.0, "value from non-existant file wasn't default" );
174 s = c->Get_control_str( "ves_collector_address", "no-value" );
175 errors += fail_if( s.compare( "no-value" ) != 0, "string from non-existant file wasn't default" );
177 b = c->Get_control_bool( "debug_mode", false );
178 errors += fail_if( b, "boolean from non-existant file wasn't default" );
180 s = c->Get_port( "rmr-data-out" );
181 errors += fail_if( !s.empty(), "get port from bad jsonfile returned value" );
183 // ---------------------------- end housekeeping ---------------------------
184 announce_results( cb_errors + errors );