66 * Copyright (c) 2021 by Cisco Systems, Inc.
77 *------------------------------------------------------------------
88 */
9+ #include < fstream>
910#include < mutex>
1011#include < string>
1112#include < sys/stat.h>
@@ -71,8 +72,6 @@ const std::vector<std::string> generated_services = {
7172 " test.service" , /* A single instance test service
7273 to test dependency creation */
7374 " test.timer" , /* A timer service */
74- " midplane-network-npu.service" , /* A midplane network service for smart switch NPU*/
75- " midplane-network-dpu.service" , /* A midplane network service for smart switch DPU*/
7675 " database.service" , /* A database service*/
7776 " database@.service" , /* A database service for multi instances */
7877};
@@ -225,15 +224,15 @@ class SsgMainTest : public SsgFunctionTest {
225224 bool find_string_in_file (std::string str,
226225 std::string file_name) {
227226 bool found = false ;
228- std::string line;
227+ std::string line;
229228
230- std::ifstream file (TEST_UNIT_FILE_PREFIX + file_name);
231- while (getline (file, line) && !found) {
232- if (str == line) {
233- found = true ;
234- break ;
235- }
229+ std::ifstream file (TEST_OUTPUT_DIR + file_name);
230+ while (getline (file, line) && !found) {
231+ if (str == line) {
232+ found = true ;
233+ break ;
236234 }
235+ }
237236 return found;
238237 }
239238
@@ -263,6 +262,18 @@ class SsgMainTest : public SsgFunctionTest {
263262 }
264263 }
265264
265+ void validate_output_dependency_list_ignore_multi_instance (
266+ std::vector<std::string> strs,
267+ std::string target,
268+ bool expected_result) {
269+ for (std::string str : strs) {
270+ bool finished = false ;
271+ EXPECT_EQ (find_string_in_file (str, target),
272+ expected_result)
273+ << " Error validating " + str + " in " + target;
274+ }
275+ }
276+
266277 /* This function validates if unit file paths in the provided
267278 * list strs exists or not as per expected_result. The unit files
268279 * should exist if expected_result is true.
@@ -302,7 +313,16 @@ class SsgMainTest : public SsgFunctionTest {
302313 * This function validates the generated dependencies in a Unit File.
303314 */
304315 void validate_depedency_in_unit_file (const SsgMainConfig &cfg) {
305- std::string test_service = " test.service" ;
316+ std::string test_service = " test.service.d/multi-asic-dependencies.conf" ;
317+
318+ if (IS_SINGLE_ASIC (cfg.num_asics ) && cfg.num_dpus == 0 ) {
319+ /* Nothing in this section will apply to single asic, as the file
320+ * won't be created at all.
321+ */
322+ validate_output_dependency_list (common_dependency_list,
323+ test_service, false , cfg.num_asics );
324+ return ;
325+ }
306326
307327 /* Validate Unit file dependency creation for multi instance
308328 * services. These entries should be present for multi asic
@@ -317,21 +337,10 @@ class SsgMainTest : public SsgFunctionTest {
317337 * Despite the split, the final result remains equivalent.
318338 */
319339 if (cfg.num_dpus > 0 ) {
320- /* Validate Unit file dependency creation for single instance
321- * services. These entries should not be present for multi asic
322- * system but present for single asic system.
323- */
324- validate_output_dependency_list (single_asic_dependency_list_split,
325- test_service, IS_SINGLE_ASIC (cfg.num_asics ), cfg.num_asics );
326- validate_output_dependency_list (npu_dependency_list,
327- " midplane-network-npu.service" , true , cfg.num_dpus );
328- } else {
329- /* Validate Unit file dependency creation for single instance
330- * services. These entries should not be present for multi asic
331- * system but present for single asic system.
332- */
333- validate_output_dependency_list (single_asic_dependency_list,
334- test_service, IS_SINGLE_ASIC (cfg.num_asics ), cfg.num_asics );
340+ for (int i = 0 ; i < cfg.num_dpus ; i++) {
341+ validate_output_dependency_list_ignore_multi_instance (npu_dependency_list,
342+ " database@dpu" + std::to_string (i) + " .service.d/ordering.conf" , true );
343+ }
335344 }
336345
337346 /* Validate Unit file dependency creation for single instance
@@ -355,8 +364,6 @@ class SsgMainTest : public SsgFunctionTest {
355364 test_target, true , cfg.num_asics );
356365 validate_output_unit_files (npu_service_list,
357366 test_target, cfg.is_smart_switch_npu , cfg.num_dpus );
358- validate_output_unit_files (npu_network_service_list,
359- " network" , cfg.is_smart_switch_npu , cfg.num_dpus );
360367 validate_output_unit_files (dpu_service_list,
361368 test_target, cfg.is_smart_switch_dpu , cfg.num_dpus );
362369 validate_output_unit_files (dpu_network_service_list,
@@ -374,19 +381,26 @@ class SsgMainTest : public SsgFunctionTest {
374381
375382 checked_service_list.insert (checked_service_list.end (), common_service_list.begin (), common_service_list.end ());
376383 if (cfg.num_dpus > 0 ) {
377- checked_service_list.insert (checked_service_list.end (), npu_service_list .begin (), npu_service_list .end ());
384+ checked_service_list.insert (checked_service_list.end (), npu_service_list_for_environment_variables .begin (), npu_service_list_for_environment_variables .end ());
378385 }
379386 if (cfg.num_asics > 1 ) {
380387 checked_service_list.insert (checked_service_list.end (), multi_asic_service_list.begin (), multi_asic_service_list.end ());
381388 }
382389
383390 for (const auto &target: checked_service_list) {
384- if (find_string_in_file (" [Service]" , target)) {
385- for (const auto & item : env_vars) {
386- std::string str = " Environment=\" " + item.first + " =" + item.second + " \" " ;
387- EXPECT_EQ (find_string_in_file (str, target), true )
388- << " Error validating " + str + " in " + target;
391+ if (!target.ends_with (" .service" )) {
392+ continue ;
393+ }
394+
395+ for (const auto & item : env_vars) {
396+ std::string str = " Environment=\" " + item.first + " =" + item.second + " \" " ;
397+ auto target_unit = target;
398+ if (is_multi_instance (target)) {
399+ /* insert instance id in string */
400+ target_unit = (boost::format{target_unit} % " " ).str ();
389401 }
402+ EXPECT_EQ (find_string_in_file (str, target_unit + " .d/environment.conf" ), true )
403+ << " Error validating " + str + " in " + target_unit;
390404 }
391405 }
392406 }
@@ -466,14 +480,6 @@ class SsgMainTest : public SsgFunctionTest {
466480 /* Save global variables before running tests */
467481 virtual void SetUp () {
468482 SsgFunctionTest::SetUp ();
469- // Create /dev/null symlink for simulation disabled service
470- std::vector<std::string> disabled_service;
471- disabled_service.insert (disabled_service.end (), npu_network_service_list.begin (), npu_network_service_list.end ());
472- disabled_service.insert (disabled_service.end (), dpu_network_service_list.begin (), dpu_network_service_list.end ());
473- for (const auto &service : disabled_service) {
474- fs::create_symlink (" /dev/null" , TEST_ETC_NETWORK + service);
475- }
476- fs::create_symlink (" /dev/null" , TEST_ETC_SYSTEM + " systemd-networkd.service" );
477483 }
478484
479485 /* Restore global vars */
@@ -488,7 +494,7 @@ class SsgMainTest : public SsgFunctionTest {
488494 static const std::vector<std::string> common_service_list;
489495 static const std::vector<std::string> non_smart_switch_service_list;
490496 static const std::vector<std::string> npu_service_list;
491- static const std::vector<std::string> npu_network_service_list ;
497+ static const std::vector<std::string> npu_service_list_for_environment_variables ;
492498 static const std::vector<std::string> dpu_service_list;
493499 static const std::vector<std::string> dpu_network_service_list;
494500 static const std::vector<std::string> single_asic_dependency_list;
@@ -541,17 +547,11 @@ SsgMainTest::non_smart_switch_service_list = {
541547const std::vector<std::string>
542548SsgMainTest::npu_service_list = {
543549 " database@dpu%1%.service" ,
544- " midplane-network-npu.service" ,
545550};
546551
547- /* Systemd service Unit file list for Smart Switch NPU. */
548552const std::vector<std::string>
549- SsgMainTest::npu_network_service_list = {
550- " bridge-midplane.netdev" ,
551- " bridge-midplane.network" ,
552- " dummy-midplane.netdev" ,
553- " dummy-midplane.network" ,
554- " midplane-network-npu.network" ,
553+ SsgMainTest::npu_service_list_for_environment_variables = {
554+ " database@%1%.service" ,
555555};
556556
557557/* Systemd service Unit file list for Smart Switch DPU. */
@@ -609,7 +609,8 @@ SsgMainTest::common_dependency_list = {
609609
610610const std::vector<std::string>
611611SsgMainTest::npu_dependency_list = {
612- " Before=database@dpu%1%.service" ,
612+ " Requires=systemd-networkd-wait-online@bridge-midplane.service" ,
613+ " After=systemd-networkd-wait-online@bridge-midplane.service" ,
613614};
614615
615616/* Test get functions for global vasr*/
0 commit comments