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 collector address 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" );
108 auto cs = c->Get_contents();
109 if( fail_if( cs.empty(), "get contents returned an empty string" ) == 0 ) {
110 fprintf( stderr, "<INFO> contents from file: %s\n", cs.c_str() );
111 fprintf( stderr, "<INFO> ---------------------\n" );
117 // ----- test sussing path and using default name ----------------------------------
118 fprintf( stderr, "<INFO> sussing info from default (no env XAPP_DESCRIPTOR_PATH)\n" );
119 unsetenv( "XAPP_DESCRIPTOR_PATH" );
120 c = new xapp::Config( ); // no env XAPP_DESCRIPTOR_PATH; assume the config-file.json is in the same directory
121 errors += fail_if( c->Get_contents().empty(), "no env XAPP_DESCRIPTOR_PATH : expected default config-file.json not found" );
123 fprintf( stderr, "<INFO> sussing info using XAPP_DESCRIPTOR_PATH as a directory without trailing slash\n" );
124 setenv( (char *) "XAPP_DESCRIPTOR_PATH", ".", 1 ); // this env var is a directory path
125 c = new xapp::Config( );
126 errors += fail_if( c->Get_contents().empty(), "env XAPP_DESCRIPTOR_PATH=. : expected default config-file.json not found" );
128 fprintf( stderr, "<INFO> sussing info using XAPP_DESCRIPTOR_PATH as a directory with trailing slash\n" );
129 setenv( (char *) "XAPP_DESCRIPTOR_PATH", "./", 1 ); // this env var is a directory path with trailing slash
130 c = new xapp::Config( );
131 errors += fail_if( c->Get_contents().empty(), "env XAPP_DESCRIPTOR_PATH=./ : expected default config-file.json not found" );
133 fprintf( stderr, "<INFO> sussing info from XAPP_DESCRIPTOR_PATH as filename\n" );
134 setenv( (char *) "XAPP_DESCRIPTOR_PATH", "./config1.json", 1 ); // this var name is ok; it's an fname
135 c = new xapp::Config( );
136 errors += fail_if( c->Get_contents().empty(), "XAPP_DESCRIPTOR_PATH as fname : expected config1.json not found" );
139 // -------------- force callback to drive and test ---------------------------------
141 fprintf( stderr, "<INFO> load config-file.json for listener coverage testing\n" );
142 c = new xapp::Config( "config-file.json" ); // set filname with out leading path
143 c->Set_callback( ncb, data ); // for coverage in listener
145 fprintf( stderr, "<INFO> load ./config-file.json for callback testing\n" );
146 c = new xapp::Config( "./config-file.json" );
147 c->Set_callback( ncb, data );
149 fprintf( stderr, "<INFO> sleeping to give callback time to be initialsed\n" );
151 if( rename( (char *) "./config-file.json", (char *) "./tmp-config.json" ) == 0 ) { // rename (should not cause callback)
152 fprintf( stderr, "<INFO> file moved; sleeping a bit\n" );
154 errors += fail_if( callback_driven, "callback was driven when file was deleted/moved away" );
156 if( rename( (char *) "./tmp-config.json", (char *) "./config-file.json" ) == 0 ) { // put it back to drive callback
157 fprintf( stderr, "<INFO> sleeping to give callback time to be driven\n" );
160 errors += fail_if( ! callback_driven, "callback was never executed" );
162 fprintf( stderr, "<WARN> attempt to move config file back failed: %s\n", strerror( errno ) );
165 fprintf( stderr, "<WARN> attempt to move config file away failed: %s\n", strerror( errno ) );
169 // ----- force errors where we can -----------------------------------------
170 fprintf( stderr, "<INFO> json parse errors expected to be reported now\n" );
171 c = new xapp::Config( "not-there.json" ); // json parse will fail
173 v = c->Get_control_value( "measurement_interval", 999 );
174 errors += fail_if( v != 999.0, "value from non-existant file wasn't default" );
176 s = c->Get_control_str( "ves_collector_address", "no-value" );
177 errors += fail_if( s.compare( "no-value" ) != 0, "string from non-existant file wasn't default" );
179 b = c->Get_control_bool( "debug_mode", false );
180 errors += fail_if( b, "boolean from non-existant file wasn't default" );
182 s = c->Get_port( "rmr-data-out" );
183 errors += fail_if( !s.empty(), "get port from bad jsonfile returned value" );
185 // ---------------------------- end housekeeping ---------------------------
186 announce_results( cb_errors + errors );