4
4
'use strict' ;
5
5
6
6
const path = require ( 'path' ) ;
7
+ const _ = require ( 'lodash' ) ;
7
8
const mocha = require ( 'mocha' ) ;
8
9
const sinon = require ( 'sinon' ) ;
9
10
const assert = require ( 'assert' ) ;
@@ -19,7 +20,7 @@ const NSFSHealth = require('../../manage_nsfs/health').NSFSHealth;
19
20
const { get_process_fs_context } = require ( '../../util/native_fs_utils' ) ;
20
21
const { ManageCLIError } = require ( '../../manage_nsfs/manage_nsfs_cli_errors' ) ;
21
22
const { TYPES , DIAGNOSE_ACTIONS , ACTIONS } = require ( '../../manage_nsfs/manage_nsfs_constants' ) ;
22
- const { TMP_PATH , create_fs_user_by_platform, delete_fs_user_by_platform, exec_manage_cli } = require ( '../system_tests/test_utils' ) ;
23
+ const { TMP_PATH , create_fs_user_by_platform, delete_fs_user_by_platform, exec_manage_cli, TEST_TIMEOUT } = require ( '../system_tests/test_utils' ) ;
23
24
const { CONFIG_DIR_PHASES } = require ( '../../sdk/config_fs' ) ;
24
25
25
26
const tmp_fs_path = path . join ( TMP_PATH , 'test_nc_health' ) ;
@@ -56,12 +57,13 @@ const connection_from_file = {
56
57
name : "http_notif" ,
57
58
} ;
58
59
59
- mocha . describe ( 'nsfs nc health' , function ( ) {
60
+ const config_root = path . join ( tmp_fs_path , 'config_root_nsfs_health' ) ;
61
+ const config_fs = new ConfigFS ( config_root ) ;
62
+ const root_path = path . join ( tmp_fs_path , 'root_path_nsfs_health/' ) ;
63
+ const config_root_invalid = path . join ( tmp_fs_path , 'config_root_nsfs_health_invalid' ) ;
64
+ const tmp_lifecycle_logs_dir_path = path . join ( TMP_PATH , 'test_lifecycle_logs' ) ;
60
65
61
- const config_root = path . join ( tmp_fs_path , 'config_root_nsfs_health' ) ;
62
- const config_fs = new ConfigFS ( config_root ) ;
63
- const root_path = path . join ( tmp_fs_path , 'root_path_nsfs_health/' ) ;
64
- const config_root_invalid = path . join ( tmp_fs_path , 'config_root_nsfs_health_invalid' ) ;
66
+ mocha . describe ( 'nsfs nc health' , function ( ) {
65
67
let Health ;
66
68
67
69
mocha . before ( async ( ) => {
@@ -693,6 +695,85 @@ mocha.describe('nsfs nc health', function() {
693
695
} ) ;
694
696
} ) ;
695
697
698
+
699
+ mocha . describe ( 'health - lifecycle' , function ( ) {
700
+ const Health = new NSFSHealth ( { config_root, config_fs, lifecycle : true } ) ;
701
+ const orig_lifecycle_logs_dir = config . NC_LIFECYCLE_LOGS_DIR ;
702
+
703
+ mocha . before ( async ( ) => {
704
+ await fs_utils . create_fresh_path ( config_root ) ;
705
+ config . NC_LIFECYCLE_LOGS_DIR = tmp_lifecycle_logs_dir_path ;
706
+ } ) ;
707
+
708
+ mocha . after ( async ( ) => {
709
+ fs_utils . folder_delete ( config_root ) ;
710
+ config . NC_LIFECYCLE_LOGS_DIR = orig_lifecycle_logs_dir ;
711
+ } ) ;
712
+
713
+ mocha . beforeEach ( async ( ) => {
714
+ await config_fs . create_config_json_file ( JSON . stringify ( { NC_LIFECYCLE_LOGS_DIR : tmp_lifecycle_logs_dir_path } ) ) ;
715
+ } ) ;
716
+
717
+ mocha . afterEach ( async ( ) => {
718
+ fs_utils . folder_delete ( tmp_lifecycle_logs_dir_path ) ;
719
+ await config_fs . delete_config_json_file ( ) ;
720
+ } ) ;
721
+
722
+ mocha . it ( 'Health lifecycle - lifecycle worker never ran on host' , async function ( ) {
723
+ try {
724
+ await nb_native ( ) . fs . readdir ( config_fs . fs_context , tmp_lifecycle_logs_dir_path ) ;
725
+ assert . fail ( 'tmp_lifecycle_logs_dir_path should not exist' ) ;
726
+ } catch ( err ) {
727
+ assert . equal ( err . code , 'ENOENT' ) ;
728
+ }
729
+ const health_status = await Health . nc_nsfs_health ( ) ;
730
+ const empty_lifecycle_health_status = { } ;
731
+ assert_lifecycle_status ( health_status , empty_lifecycle_health_status , false ) ;
732
+ } ) ;
733
+
734
+ mocha . it ( 'Health lifecycle - after 1 run of the lifecycle worker' , async function ( ) {
735
+ await exec_manage_cli ( TYPES . LIFECYCLE , '' , { config_root, disable_service_validation : true , disable_runtime_validation : true } , true ) ;
736
+ const lifecycle_log_entries = await nb_native ( ) . fs . readdir ( config_fs . fs_context , tmp_lifecycle_logs_dir_path ) ;
737
+ assert . strictEqual ( lifecycle_log_entries . length , 1 ) ;
738
+ const log_file_path = path . join ( tmp_lifecycle_logs_dir_path , lifecycle_log_entries [ 0 ] . name ) ;
739
+ const lifecycle_log_json = await config_fs . get_config_data ( log_file_path , { silent_if_missing : true } ) ;
740
+ const health_status = await Health . nc_nsfs_health ( ) ;
741
+ assert_lifecycle_status ( health_status , lifecycle_log_json , false ) ;
742
+ } ) ;
743
+
744
+ mocha . it ( 'Health lifecycle - should report on timeout error' , async function ( ) {
745
+ await config_fs . update_config_json_file ( JSON . stringify ( {
746
+ NC_LIFECYCLE_TIMEOUT_MS : 1 ,
747
+ NC_LIFECYCLE_LOGS_DIR : tmp_lifecycle_logs_dir_path
748
+ } ) ) ;
749
+ await exec_manage_cli ( TYPES . LIFECYCLE , '' , { config_root, disable_service_validation : true , disable_runtime_validation : true } , true ) ;
750
+ const lifecycle_log_entries = await nb_native ( ) . fs . readdir ( config_fs . fs_context , tmp_lifecycle_logs_dir_path ) ;
751
+ assert . strictEqual ( lifecycle_log_entries . length , 1 ) ;
752
+ const log_file_path = path . join ( tmp_lifecycle_logs_dir_path , lifecycle_log_entries [ 0 ] . name ) ;
753
+ const lifecycle_log_json = await config_fs . get_config_data ( log_file_path , { silent_if_missing : true } ) ;
754
+ const health_status = await Health . nc_nsfs_health ( ) ;
755
+ assert_lifecycle_status ( health_status , lifecycle_log_json , true ) ;
756
+ } ) ;
757
+
758
+ mocha . it ( 'Health lifecycle - run lifecycle 3 times - should report on latest run' , async function ( ) {
759
+ this . timeout ( TEST_TIMEOUT ) ; // eslint-disable-line no-invalid-this
760
+ let latest_lifecycle ;
761
+ for ( let i = 0 ; i < 3 ; i ++ ) {
762
+ latest_lifecycle = await exec_manage_cli ( TYPES . LIFECYCLE , '' , { config_root, disable_service_validation : true , disable_runtime_validation : true } , true ) ;
763
+ }
764
+ const parsed_res_latest_lifecycle = JSON . parse ( latest_lifecycle ) ;
765
+ const lifecycle_log_entries = await nb_native ( ) . fs . readdir ( config_fs . fs_context , tmp_lifecycle_logs_dir_path ) ;
766
+ assert . strictEqual ( lifecycle_log_entries . length , 3 ) ;
767
+ const latest_log_file_path = _ . maxBy ( lifecycle_log_entries , entry => entry . name ) ;
768
+ const is_latest = latest_log_file_path . name . endsWith ( parsed_res_latest_lifecycle . response . reply . lifecycle_run_times . run_lifecycle_start_time + '.json' ) ;
769
+ assert . equal ( is_latest , true ) ;
770
+ const log_file_path = path . join ( tmp_lifecycle_logs_dir_path , latest_log_file_path . name ) ;
771
+ const lifecycle_log_json = await config_fs . get_config_data ( log_file_path , { silent_if_missing : true } ) ;
772
+ const health_status = await Health . nc_nsfs_health ( ) ;
773
+ assert_lifecycle_status ( health_status , lifecycle_log_json , false ) ;
774
+ } ) ;
775
+ } ) ;
776
+
696
777
/**
697
778
* assert_config_dir_status asserts config directory status
698
779
* @param {Object } health_status
@@ -723,6 +804,21 @@ function assert_upgrade_status(actual_upgrade_status, expected_upgrade_status) {
723
804
assert . deepStrictEqual ( actual_upgrade_status_res , expected_upgrade_status_res ) ;
724
805
}
725
806
807
+ /**
808
+ * assert_lifecycle_status asserts the lifecycle status
809
+ * @param {Object } health_status
810
+ * @param {Object } lifecycle_log_json
811
+ * @param {Boolean } is_error_expected
812
+ * @returns {Void }
813
+ */
814
+ function assert_lifecycle_status ( health_status , lifecycle_log_json , is_error_expected = false ) {
815
+ assert . deepStrictEqual ( health_status . checks . latest_lifecycle_run_status . total_stats , lifecycle_log_json . total_stats ) ;
816
+ assert . deepStrictEqual ( health_status . checks . latest_lifecycle_run_status . lifecycle_run_times ,
817
+ lifecycle_log_json . lifecycle_run_times ) ;
818
+ const err_value = is_error_expected ? lifecycle_log_json . errors : undefined ;
819
+ assert . deepStrictEqual ( health_status . checks . latest_lifecycle_run_status . errors , err_value ) ;
820
+ }
821
+
726
822
/**
727
823
* restore_health_if_needed restores health obj functions if needed
728
824
* @param {* } health_obj
@@ -740,7 +836,8 @@ function restore_health_if_needed(health_obj) {
740
836
* the value is an array of responses by the order of their call
741
837
* @param {Object } Health
742
838
* @param {{get_endpoint_response?: Object[], get_service_state?: Object[],
743
- * get_system_config_file?: Object[], get_service_memory_usage?: Object[]}} mock_function_responses
839
+ * get_system_config_file?: Object[], get_service_memory_usage?: Object[],
840
+ * get_lifecycle_health_status?: Object, get_latest_lifecycle_run_status?: Object}} mock_function_responses
744
841
*/
745
842
function set_mock_functions ( Health , mock_function_responses ) {
746
843
for ( const mock_function_name of Object . keys ( mock_function_responses ) ) {
0 commit comments