68506c1043588730b5c7878c0cc80f1dbbddfdec
[ric-plt/xapp-frame-cpp.git] / test / config_test.cpp
1 // vim: ts=4 sw=4 noet :
2 /*
3 ==================================================================================
4        Copyright (c) 2020 Nokia
5        Copyright (c) 2020 AT&T Intellectual Property.
6
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
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
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 ==================================================================================
19 */
20
21 /*
22         Mnemonic:       config_test.cpp
23         Abstract:       Unit test to drive the config functions.
24
25         Date:           28 July 2020
26         Author:         E. Scott Daniels
27 */
28
29 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <string.h>
35 #include <time.h>
36
37 #include <string>
38 #include <memory>
39
40 #include "../src/xapp/xapp.hpp"
41
42 //#include "../src/json/jhash.hpp"
43 //#include "../src/json/jhash.cpp"
44
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"
49
50 #include "ut_support.cpp"
51
52 char* data = (char *) "some data to validate in callback";
53 bool    callback_driven = false;
54 int             cb_errors = 0;
55
56
57 /*
58         Notification callback to see that it is driven on change.
59 */
60 void ncb( xapp::Config& c, void* data ) {
61         cb_errors += fail_if( data == NULL, "callback function did not get a good pointer" );
62
63         auto v = c.Get_control_value( "measurement_interval" );
64         cb_errors += fail_if( v != 1000, "measurement value in new file wasn't different" );
65
66         callback_driven = true;
67 }
68
69 int main( int argc, char** argv ) {
70         int             errors = 0;
71
72         set_test_name( "config_test" );
73
74
75         auto x = new Xapp( (char*) "43086", false );
76         if(     fail_if( x == NULL, "could not allocate xapp" )  > 0 ) {
77                 announce_results( 1 );
78                 exit( 1 );
79         }
80
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" );
84
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() );
88
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() );
92
93         s = c->Get_port( "no-interface" );
94         errors += fail_if( ! s.empty(), "did not return empty when get port given an invalid name" );
95
96         s = c->Get_control_str( "no-such-control" );
97         errors += fail_if( ! s.empty(), "expected empty string for missing control got a string" );
98         if( ! s.empty() ) {
99                 fprintf( stderr, "<INFO> unexpected string for no such control name:  %s\n", s.c_str() );
100         }
101
102         auto v = c->Get_control_value( "measurement_interval" );
103         errors += fail_if( v == 0.0, "epxected measurement interval control value not found" );
104
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" );
107
108
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
112
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(  );
116
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() );
120
121         v = c->Get_control_value( "measurement_interval" );
122         errors += fail_if( v == 0.0, "expected measurement interval control value not found" );
123
124         b = c->Get_control_bool( "debug_mode" );
125         errors += fail_if( b == false, "expected debug mode control boolean not found" );
126
127
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" );
132         } else {
133                 errors++;
134         }
135
136
137         // -------------- force callback to drive and test ---------------------------------
138
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
142
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 );
146
147         fprintf( stderr, "<INFO> sleeping to give callback time to be initialsed\n" );
148         sleep( 4 );
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" );
151                 sleep( 3 );
152                 errors += fail_if( callback_driven, "callback was driven when file was deleted/moved away" );
153
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" );
156                         sleep( 3 );
157
158                         errors += fail_if( ! callback_driven, "callback was never executed" );
159                 } else {
160                         fprintf( stderr, "<WARN> attempt to move config file back failed: %s\n", strerror( errno ) );
161                 }
162         } else {
163                 fprintf( stderr, "<WARN> attempt to move config file away failed: %s\n", strerror( errno ) );
164         }
165
166
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
170
171         v = c->Get_control_value( "measurement_interval", 999 );
172         errors += fail_if( v != 999.0, "value from non-existant file wasn't default" );
173
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" );
176
177         b = c->Get_control_bool( "debug_mode", false );
178         errors += fail_if( b, "boolean from non-existant file wasn't default" );
179
180         s = c->Get_port( "rmr-data-out" );
181         errors += fail_if( !s.empty(), "get port from bad jsonfile returned value" );
182
183         // ---------------------------- end housekeeping ---------------------------
184         announce_results( cb_errors + errors );
185         return !!errors;
186 }