@@ -22,6 +22,7 @@ const { TMP_PATH, create_redirect_file, create_config_dir,
22
22
const config_root = path . join ( TMP_PATH , 'config_root_nc_upgrade_manager_test' ) ;
23
23
const config_fs = new ConfigFS ( config_root ) ;
24
24
const hostname = os . hostname ( ) ;
25
+ const mock_higher_version_than_pkg_version = pkg . version + '.1' ;
25
26
26
27
const dummy_upgrade_script_1 =
27
28
`/* Copyright (C) 2024 NooBaa */
@@ -57,6 +58,7 @@ module.exports = {
57
58
description: 'dummy upgrade script file 1'
58
59
};
59
60
` ;
61
+
60
62
const old_expected_system_json = {
61
63
[ hostname ] : {
62
64
current_version : '5.17.0' ,
@@ -72,25 +74,87 @@ const old_expected_system_json = {
72
74
73
75
const old_expected_system_json_has_config_directory = {
74
76
[ hostname ] : {
75
- current_version : '5.18.1' ,
77
+ current_version : mock_higher_version_than_pkg_version ,
76
78
upgrade_history : {
77
79
successful_upgrades : [ {
78
80
timestamp : 1724687496424 ,
79
- from_version : '5.18.0' ,
80
- to_version : '5.18.1'
81
+ from_version : pkg . version ,
82
+ to_version : mock_higher_version_than_pkg_version
81
83
} ]
82
84
} ,
83
85
} ,
84
86
config_directory : {
85
- config_dir_version : '1.0.0' ,
86
- upgrade_package_version : '5.18.0' ,
87
+ config_dir_version : config_fs . config_dir_version ,
88
+ upgrade_package_version : pkg . version ,
87
89
phase : CONFIG_DIR_PHASES . CONFIG_DIR_UNLOCKED ,
88
90
upgrade_history : {
89
91
successful_upgrades : [ {
90
92
timestamp : 1724687496424 ,
91
93
completed_scripts : [ ] ,
92
94
package_from_version : '5.17.0' ,
93
- package_to_version : '5.18.0'
95
+ package_to_version : pkg . version
96
+ } ]
97
+ }
98
+ }
99
+ } ;
100
+
101
+ const failed_expected_system_json_has_config_directory = {
102
+ [ hostname ] : {
103
+ current_version : pkg . version ,
104
+ upgrade_history : {
105
+ successful_upgrades : [ {
106
+ timestamp : 1724687496424 ,
107
+ from_version : pkg . version ,
108
+ to_version : pkg . version
109
+ } ]
110
+ } ,
111
+ } ,
112
+ config_directory : {
113
+ config_dir_version : '0.0.0' ,
114
+ upgrade_package_version : '5.17.0' ,
115
+ phase : CONFIG_DIR_PHASES . CONFIG_DIR_UNLOCKED ,
116
+ upgrade_history : {
117
+ last_failure : {
118
+ 'timestamp' : 1714687496424 ,
119
+ 'running_host' : hostname ,
120
+ 'completed_scripts' : [ ] ,
121
+ 'config_dir_from_version' : '0.0.0' ,
122
+ 'config_dir_to_version' : '1.0.0' ,
123
+ 'package_from_version' : '5.17.0' ,
124
+ 'package_to_version' : '5.18.0' ,
125
+ 'error' : new Error ( 'this is a last failure error' ) . stack
126
+ } ,
127
+ successful_upgrades : [ { // mock successful upgrade
128
+ timestamp : 1724687496424 ,
129
+ completed_scripts : [ ] ,
130
+ package_from_version : '5.16.0' ,
131
+ package_to_version : '5.17.0'
132
+ } ]
133
+ }
134
+ }
135
+ } ;
136
+
137
+ const newer_expected_system_json_has_config_directory = {
138
+ [ hostname ] : {
139
+ current_version : mock_higher_version_than_pkg_version ,
140
+ upgrade_history : {
141
+ successful_upgrades : [ {
142
+ timestamp : 1724687496424 ,
143
+ from_version : pkg . version ,
144
+ to_version : mock_higher_version_than_pkg_version
145
+ } ]
146
+ } ,
147
+ } ,
148
+ config_directory : {
149
+ config_dir_version : config_fs . config_dir_version + '.1' ,
150
+ upgrade_package_version : mock_higher_version_than_pkg_version ,
151
+ phase : CONFIG_DIR_PHASES . CONFIG_DIR_UNLOCKED ,
152
+ upgrade_history : {
153
+ successful_upgrades : [ {
154
+ timestamp : 1724687496424 ,
155
+ completed_scripts : [ ] ,
156
+ package_from_version : '5.17.0' ,
157
+ package_to_version : mock_higher_version_than_pkg_version
94
158
} ]
95
159
}
96
160
}
@@ -105,7 +169,9 @@ const old_expected_system_json_no_successful_upgrades = {
105
169
}
106
170
} ;
107
171
108
- const current_expected_system_json = {
172
+ // config_directory property is missing because this system.json info is before the config directory upgrade
173
+ // and after the RPM upgrade
174
+ const system_json_only_rpm_upgraded = {
109
175
[ hostname ] : {
110
176
current_version : pkg . version ,
111
177
config_dir_version : config_fs . config_dir_version ,
@@ -191,10 +257,10 @@ describe('nc upgrade manager - upgrade RPM', () => {
191
257
} , TEST_TIMEOUT ) ;
192
258
193
259
it ( 'upgrade rpm - nothing to upgrade - no changes in system.json' , async ( ) => {
194
- await config_fs . create_system_config_file ( JSON . stringify ( current_expected_system_json ) ) ;
260
+ await config_fs . create_system_config_file ( JSON . stringify ( system_json_only_rpm_upgraded ) ) ;
195
261
await nc_upgrade_manager . update_rpm_upgrade ( config_fs ) ;
196
262
const system_data_after_upgrade_run = await config_fs . get_system_config_file ( ) ;
197
- expect ( current_expected_system_json ) . toStrictEqual ( system_data_after_upgrade_run ) ;
263
+ expect ( system_json_only_rpm_upgraded ) . toStrictEqual ( system_data_after_upgrade_run ) ;
198
264
} ) ;
199
265
200
266
it ( 'upgrade rpm - nothing to upgrade - no changes in system.json - no successful upgrades' , async ( ) => {
@@ -254,7 +320,7 @@ describe('nc upgrade manager - upgrade config directory', () => {
254
320
} ) ;
255
321
256
322
it ( 'config_directory_defaults - hostname from_version exists - 5.17.0' , ( ) => {
257
- const system_data = current_expected_system_json ;
323
+ const system_data = system_json_only_rpm_upgraded ;
258
324
const config_dir_defaults = nc_upgrade_manager . config_directory_defaults ( system_data ) ;
259
325
assert_config_dir_defaults ( config_dir_defaults , system_data ) ;
260
326
} ) ;
@@ -302,7 +368,7 @@ describe('nc upgrade manager - upgrade config directory', () => {
302
368
} ) ;
303
369
304
370
it ( '_verify_config_dir_upgrade - host current_version > new_version should upgrade RPM' , async ( ) => {
305
- const newer_version = pkg . version + '.1' ;
371
+ const newer_version = mock_higher_version_than_pkg_version ;
306
372
const system_data = { [ hostname ] : { current_version : newer_version } , other_hostname : { current_version : pkg . version } } ;
307
373
const expected_err_msg = `config dir upgrade can not be started until all expected hosts have the expected version=${ pkg . version } , host=${ hostname } host's current_version=${ newer_version } ` ;
308
374
await expect ( nc_upgrade_manager . _verify_config_dir_upgrade ( system_data , pkg . version , [ hostname , 'other_hostname' ] ) )
@@ -318,7 +384,7 @@ describe('nc upgrade manager - upgrade config directory', () => {
318
384
} ) ;
319
385
320
386
it ( '_verify_config_dir_upgrade - fail on mismatch expected_version' , async ( ) => {
321
- const expected_version = pkg . version + '.1' ;
387
+ const expected_version = mock_higher_version_than_pkg_version ;
322
388
const system_data = { [ hostname ] : { current_version : pkg . version , other_hostname : { current_version : pkg . version } } } ;
323
389
const nc_upgrade_manager_higher_version = new NCUpgradeManager ( config_fs ) ;
324
390
const expected_err_msg = `config dir upgrade can not be started - the host's package version=${ pkg . version } does not match the user's expected version=${ expected_version } ` ;
@@ -673,6 +739,101 @@ describe('nc upgrade manager - upgrade config directory', () => {
673
739
assert_upgrade_finish_or_fail_data ( system_json_data , expected_data ) ;
674
740
} ) ;
675
741
} ) ;
742
+
743
+ describe ( 'nc upgrade manager - upgrade_config_dir' , ( ) => {
744
+ beforeEach ( async ( ) => {
745
+ await create_config_dir ( config . NSFS_NC_DEFAULT_CONF_DIR ) ;
746
+ await create_config_dir ( config_root ) ;
747
+ await create_redirect_file ( config_fs , config_root ) ;
748
+ } ) ;
749
+
750
+ afterEach ( async ( ) => {
751
+ await clean_config_dir ( config_fs , config_root ) ;
752
+ } , TEST_TIMEOUT ) ;
753
+
754
+ it ( 'upgrade_config_dir - empty system_data - should fail' , async ( ) => {
755
+ const expected_version = pkg . version ;
756
+ const hosts_list = [ hostname ] ;
757
+ const expected_err_msg = 'system does not exist' ;
758
+ await expect ( nc_upgrade_manager . upgrade_config_dir ( expected_version , hosts_list ) )
759
+ . rejects . toThrow ( expected_err_msg ) ;
760
+ } ) ;
761
+
762
+ it ( 'upgrade_config_dir - config_directory property is missing in system.json' , async ( ) => {
763
+ const system_data = _ . cloneDeep ( system_json_only_rpm_upgraded ) ;
764
+ const expected_version = pkg . version ;
765
+ const hosts_list = [ hostname ] ;
766
+ await config_fs . create_system_config_file ( JSON . stringify ( system_data ) ) ;
767
+ const res = await nc_upgrade_manager . upgrade_config_dir ( expected_version , hosts_list ) ;
768
+ const system_json_data = await config_fs . get_system_config_file ( ) ;
769
+ const expected_data = {
770
+ ...system_data ,
771
+ config_directory : {
772
+ phase : CONFIG_DIR_PHASES . CONFIG_DIR_UNLOCKED ,
773
+ config_dir_version : config_fs . config_dir_version ,
774
+ upgrade_package_version : expected_version ,
775
+ upgrade_history : {
776
+ successful_upgrades : [ res ]
777
+ }
778
+ }
779
+ } ;
780
+ assert_upgrade_finish_or_fail_data ( system_json_data , expected_data ) ;
781
+ } ) ;
782
+
783
+ it ( 'upgrade_config_dir - system.json and package.json have the same version' , async ( ) => {
784
+ const system_data = _ . cloneDeep ( old_expected_system_json_has_config_directory ) ;
785
+ const expected_version = pkg . version ;
786
+ const hosts_list = [ hostname ] ;
787
+ await config_fs . create_system_config_file ( JSON . stringify ( system_data ) ) ;
788
+ const expected_msg = { "message" : "config_dir_version on system.json and config_fs.config_dir_version match, nothing to upgrade" } ;
789
+ const res = await nc_upgrade_manager . upgrade_config_dir ( expected_version , hosts_list ) ;
790
+ expect ( res ) . toStrictEqual ( expected_msg ) ;
791
+ } ) ;
792
+
793
+ it ( 'upgrade_config_dir - config_dir_version in system.json is newer than the hosts current config_dir_version' , async ( ) => {
794
+ const system_data = _ . cloneDeep ( newer_expected_system_json_has_config_directory ) ;
795
+ const expected_version = pkg . version ;
796
+ const hosts_list = [ hostname ] ;
797
+ await config_fs . create_system_config_file ( JSON . stringify ( system_data ) ) ;
798
+ const expected_err_msg = 'attempt to run old container version with newer server version' ;
799
+ await expect ( nc_upgrade_manager . upgrade_config_dir ( expected_version , hosts_list ) )
800
+ . rejects . toThrow ( expected_err_msg ) ;
801
+ } ) ;
802
+
803
+ it ( 'upgrade_config_dir - host RPM is not upgraded to the expectced version' , async ( ) => {
804
+ const system_data = _ . cloneDeep ( old_expected_system_json ) ;
805
+ const expected_version = pkg . version ;
806
+ const hosts_list = [ hostname ] ;
807
+ await config_fs . create_system_config_file ( JSON . stringify ( system_data ) ) ;
808
+ const expected_err_msg = `config dir upgrade can not be started until all expected hosts have the expected version=${ pkg . version } , host=${ hostname } host's current_version=${ system_data [ hostname ] . current_version } ` ;
809
+ await expect ( nc_upgrade_manager . upgrade_config_dir ( expected_version , hosts_list ) )
810
+ . rejects . toThrow ( expected_err_msg ) ;
811
+ } ) ;
812
+
813
+ it ( 'upgrade_config_dir - old successful upgrades is not empty' , async ( ) => {
814
+ const system_data = _ . cloneDeep ( failed_expected_system_json_has_config_directory ) ;
815
+ const expected_version = pkg . version ;
816
+ const hosts_list = [ hostname ] ;
817
+ await config_fs . create_system_config_file ( JSON . stringify ( system_data ) ) ;
818
+ const res = await nc_upgrade_manager . upgrade_config_dir ( expected_version , hosts_list ) ;
819
+ const system_json_data = await config_fs . get_system_config_file ( ) ;
820
+ const expected_data = {
821
+ ...system_data ,
822
+ config_directory : {
823
+ phase : CONFIG_DIR_PHASES . CONFIG_DIR_UNLOCKED ,
824
+ config_dir_version : config_fs . config_dir_version ,
825
+ upgrade_package_version : expected_version ,
826
+ upgrade_history : {
827
+ successful_upgrades : [
828
+ res ,
829
+ ...failed_expected_system_json_has_config_directory . config_directory . upgrade_history . successful_upgrades ,
830
+ ]
831
+ }
832
+ }
833
+ } ;
834
+ assert_upgrade_finish_or_fail_data ( system_json_data , expected_data ) ;
835
+ } ) ;
836
+ } ) ;
676
837
} ) ;
677
838
678
839
/**
0 commit comments