aboutsummaryrefslogtreecommitdiff
path: root/tests/test_set_config.py
blob: 89b71fca8a10ded59453c735b2079d102d695228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import copy
import importlib.util
import json
import os.path
import sys
from unittest import mock

from oslotest import base


def load_module(name, path):
    module_spec = importlib.util.spec_from_file_location(
        name, path
    )
    module = importlib.util.module_from_spec(module_spec)
    module_spec.loader.exec_module(module)
    return module


# nasty: to import set_config (not a part of the kolla package)
this_dir = os.path.dirname(sys.modules[__name__].__file__)
set_configs_file = os.path.abspath(
    os.path.join(this_dir, '..',
                 'docker', 'base', 'set_configs.py'))

set_configs = load_module('set_configs', set_configs_file)


class LoadFromFile(base.BaseTestCase):

    def test_load_ok(self):
        in_config = json.dumps({'command': '/bin/true',
                                'config_files': {}})

        mo = mock.mock_open(read_data=in_config)
        with mock.patch.object(set_configs, 'open', mo):
            config = set_configs.load_config()
            set_configs.copy_config(config)
            if sys.version_info >= (3, 13):
                calls = [
                 mock.call('/var/lib/kolla/config_files/config.json'),
                 mock.call().__enter__(),
                 mock.call().read(),
                 mock.call().__exit__(None, None, None),
                 mock.call().close(),
                 mock.call('/run_command', 'w+'),
                 mock.call().__enter__(),
                 mock.call().write('/bin/true'),
                 mock.call().__exit__(None, None, None),
                 mock.call().close()
                ]
            else:
                calls = [
                 mock.call('/var/lib/kolla/config_files/config.json'),
                 mock.call().__enter__(),
                 mock.call().read(),
                 mock.call().__exit__(None, None, None),
                 mock.call('/run_command', 'w+'),
                 mock.call().__enter__(),
                 mock.call().write('/bin/true'),
                 mock.call().__exit__(None, None, None)
                ]

            self.assertEqual(calls, mo.mock_calls)

    @mock.patch('os.path.exists', return_value=True)
    def test_load_yaml_ok(self, mock_exists):
        in_config = (
            'command: /bin/true\n'
            'config_files: {}\n'
        )

        mo = mock.mock_open(read_data=in_config)
        with mock.patch.object(set_configs, 'open', mo):
            config = set_configs.load_config()
            self.assertEqual(config['command'], '/bin/true')

        mock_exists.assert_called_with(
            '/var/lib/kolla/config_files/config.yaml')

    @mock.patch('os.path.exists', return_value=True)
    def test_load_yaml_invalid(self, mock_exists):
        in_config = '{'

        mo = mock.mock_open(read_data=in_config)
        with mock.patch.object(set_configs, 'open', mo):
            self.assertRaises(set_configs.InvalidConfig,
                              set_configs.load_config)

    @mock.patch('os.path.exists', return_value=False)
    def test_load_json_when_no_yaml(self, mock_exists):
        in_config = json.dumps({'command': '/bin/true',
                                'config_files': {}})

        mo = mock.mock_open(read_data=in_config)
        with mock.patch.object(set_configs, 'open', mo):
            config = set_configs.load_config()
            self.assertEqual(config['command'], '/bin/true')

        mock_exists.assert_called_with(
            '/var/lib/kolla/config_files/config.yaml')

    @mock.patch('os.path.exists', return_value=True)
    def test_load_yaml_full_config(self, mock_exists):
        in_config = (
            'command: kolla_toolbox\n'
            'config_files:\n'
            '- dest: /etc/rabbitmq/rabbitmq-env.conf\n'
            '  source: /var/lib/kolla/config_files/rabbitmq-env.conf\n'
            '  owner: rabbitmq\n'
            '  perm: \'0600\'\n'
            'permissions:\n'
            '- path: /var/log/kolla/ansible.log\n'
            '  owner: ansible:kolla\n'
            '  perm: \'0664\'\n'
        )

        mo = mock.mock_open(read_data=in_config)
        with mock.patch.object(set_configs, 'open', mo):
            config = set_configs.load_config()

        self.assertEqual(config['command'], 'kolla_toolbox')
        self.assertEqual(len(config['config_files']), 1)
        self.assertEqual(config['config_files'][0]['owner'], 'rabbitmq')
        self.assertEqual(config['permissions'][0]['perm'], '0664')


FAKE_CONFIG_FILES = [
    set_configs.ConfigFile(
        '/var/lib/kolla/config_files/bar.conf',
        '/foo/bar.conf', 'user1', '0644')
]

FAKE_CONFIG_FILE = FAKE_CONFIG_FILES[0]


class ConfigFileTest(base.BaseTestCase):

    @mock.patch('os.path.lexists', return_value=False)
    def test_delete_path_not_exists(self, mock_lexists):

        config_file = copy.deepcopy(FAKE_CONFIG_FILE)
        config_file._delete_path(config_file.dest)

        mock_lexists.assert_called_with(config_file.dest)

    @mock.patch('os.path.lexists', return_value=True)
    @mock.patch('os.path.isdir', return_value=True)
    @mock.patch('shutil.rmtree')
    def test_delete_path_exist_dir(self,
                                   mock_rmtree,
                                   mock_isdir,
                                   mock_lexists):

        config_file = copy.deepcopy(FAKE_CONFIG_FILE)
        config_file._delete_path(config_file.dest)

        mock_lexists.assert_called_with(config_file.dest)
        mock_isdir.assert_called_with(config_file.dest)
        mock_rmtree.assert_called_with(config_file.dest)

    @mock.patch('os.path.lexists', return_value=True)
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch('os.remove')
    def test_delete_path_exist_file(self,
                                    mock_remove,
                                    mock_isdir,
                                    mock_lexists):

        config_file = copy.deepcopy(FAKE_CONFIG_FILE)
        config_file._delete_path(config_file.dest)

        mock_lexists.assert_called_with(config_file.dest)
        mock_isdir.assert_called_with(config_file.dest)
        mock_remove.assert_called_with(config_file.dest)

    @mock.patch('shutil.copystat')
    @mock.patch('os.stat')
    @mock.patch('os.chown')
    def test_set_properties_from_file(self,
                                      mock_chown,
                                      mock_stat,
                                      mock_copystat):

        stat_result = mock.MagicMock()
        mock_stat.return_value = stat_result

        config_file = copy.deepcopy(FAKE_CONFIG_FILE)
        config_file._set_properties_from_file(config_file.source,
                                              config_file.dest)

        mock_copystat.assert_called_with(config_file.source, config_file.dest)
        mock_stat.assert_called_with(config_file.source)
        mock_chown.assert_called_with(config_file.dest, stat_result.st_uid,
                                      stat_result.st_gid)

    @mock.patch('os.chmod')
    @mock.patch.object(set_configs, 'handle_permissions')
    def test_set_properties_from_conf(self,
                                      mock_handle_permissions,
                                      mock_chmod):

        config_file = copy.deepcopy(FAKE_CONFIG_FILE)
        config_file._set_properties_from_conf(config_file.dest)
        mock_handle_permissions.assert_called_with({'permissions':
                                                    [{'owner': 'user1',
                                                      'path': config_file.dest,
                                                      'perm': '0644'}]})

    @mock.patch('glob.glob', return_value=[])
    def test_copy_no_source_not_optional(self,
                                         mock_glob):

        config_file = copy.deepcopy(FAKE_CONFIG_FILE)

        self.assertRaises(set_configs.MissingRequiredSource,
                          config_file.copy)

    @mock.patch('glob.glob', return_value=[])
    def test_copy_no_source_optional(self, mock_glob):

        config_file = copy.deepcopy(FAKE_CONFIG_FILE)
        config_file.optional = True

        config_file.copy()

        mock_glob.assert_called_with(config_file.source)

    @mock.patch.object(set_configs.ConfigFile, '_copy_file')
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch.object(set_configs.ConfigFile, '_create_parent_dirs')
    @mock.patch.object(set_configs.ConfigFile, '_delete_path')
    @mock.patch('glob.glob')
    def test_copy_one_source_file(self, mock_glob, mock_delete_path,
                                  mock_create_parent_dirs, mock_isdir,
                                  mock_copy_file):
        config_file = copy.deepcopy(FAKE_CONFIG_FILE)

        mock_glob.return_value = [config_file.source]

        config_file.copy()

        mock_glob.assert_called_with(config_file.source)
        mock_delete_path.assert_called_with(config_file.dest)
        mock_create_parent_dirs.assert_called_with(config_file.dest)
        mock_isdir.assert_called_with(config_file.source)
        mock_copy_file.assert_called_with(config_file.source,
                                          config_file.dest)

    @mock.patch.object(set_configs.ConfigFile, '_merge_directories')
    @mock.patch('os.path.isdir', return_value=True)
    @mock.patch.object(set_configs.ConfigFile, '_create_parent_dirs')
    @mock.patch.object(set_configs.ConfigFile, '_delete_path')
    @mock.patch('glob.glob')
    def test_copy_one_source_dir(self, mock_glob, mock_delete_path,
                                 mock_create_parent_dirs, mock_isdir,
                                 mock_merge_directories):
        config_file = copy.deepcopy(FAKE_CONFIG_FILE)

        mock_glob.return_value = [config_file.source]

        config_file.copy()

        mock_glob.assert_called_with(config_file.source)
        mock_delete_path.assert_called_with(config_file.dest)
        mock_create_parent_dirs.assert_called_with(config_file.dest)
        mock_merge_directories.assert_called_with(config_file.source,
                                                  config_file.dest)

    @mock.patch.object(set_configs.ConfigFile, '_copy_file')
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch.object(set_configs.ConfigFile, '_create_parent_dirs')
    @mock.patch.object(set_configs.ConfigFile, '_delete_path')
    @mock.patch('glob.glob')
    def test_copy_glob_source_file(self, mock_glob, mock_delete_path,
                                   mock_create_parent_dirs, mock_isdir,
                                   mock_copy_file):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar.*', '/foo/', 'user1', '0644')

        mock_glob.return_value = ['/var/lib/kolla/config_files/bar.conf',
                                  '/var/lib/kolla/config_files/bar.yml']

        config_file.copy()

        mock_glob.assert_called_with(config_file.source)

        self.assertEqual(mock_delete_path.mock_calls,
                         [mock.call('/foo/bar.conf'),
                          mock.call('/foo/bar.yml')])
        self.assertEqual(mock_create_parent_dirs.mock_calls,
                         [mock.call('/foo/bar.conf'),
                          mock.call('/foo/bar.yml')])
        self.assertEqual(mock_isdir.mock_calls,
                         [mock.call('/var/lib/kolla/config_files/bar.conf'),
                          mock.call('/var/lib/kolla/config_files/bar.yml')])
        self.assertEqual(mock_copy_file.mock_calls,
                         [mock.call('/var/lib/kolla/config_files/bar.conf',
                                    '/foo/bar.conf'),
                          mock.call('/var/lib/kolla/config_files/bar.yml',
                                    '/foo/bar.yml')])

    @mock.patch.object(set_configs.ConfigFile, '_cmp_file')
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch('glob.glob')
    def test_check_glob_source_file(self, mock_glob, mock_isdir,
                                    mock_cmp_file):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar.*', '/foo/', 'user1', '0644')

        mock_glob.return_value = ['/var/lib/kolla/config_files/bar.conf',
                                  '/var/lib/kolla/config_files/bar.yml']
        mock_cmp_file.return_value = True

        config_file.check()

        self.assertEqual(mock_isdir.mock_calls,
                         [mock.call('/var/lib/kolla/config_files/bar.conf'),
                          mock.call('/var/lib/kolla/config_files/bar.yml')])
        self.assertEqual(mock_cmp_file.mock_calls,
                         [mock.call('/var/lib/kolla/config_files/bar.conf',
                                    '/foo/bar.conf'),
                          mock.call('/var/lib/kolla/config_files/bar.yml',
                                    '/foo/bar.yml')])

    @mock.patch.object(set_configs.ConfigFile, '_cmp_file')
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch('glob.glob')
    def test_check_glob_source_file_no_equal(self, mock_glob, mock_isdir,
                                             mock_cmp_file):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar.*', '/foo/', 'user1', '0644')

        mock_glob.return_value = ['/var/lib/kolla/config_files/bar.conf',
                                  '/var/lib/kolla/config_files/bar.yml']
        mock_cmp_file.side_effect = [True, False]

        self.assertRaises(set_configs.ConfigFileBadState,
                          config_file.check)

        self.assertEqual(mock_isdir.mock_calls,
                         [mock.call('/var/lib/kolla/config_files/bar.conf'),
                          mock.call('/var/lib/kolla/config_files/bar.yml')])
        self.assertEqual(mock_cmp_file.mock_calls,
                         [mock.call('/var/lib/kolla/config_files/bar.conf',
                                    '/foo/bar.conf'),
                          mock.call('/var/lib/kolla/config_files/bar.yml',
                                    '/foo/bar.yml')])

    @mock.patch.object(set_configs.ConfigFile, '_cmp_file')
    @mock.patch.object(set_configs.ConfigFile, '_cmp_dir')
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch('glob.glob')
    def test_check_source_dir(self, mock_glob, mock_isdir, mock_cmp_dir,
                              mock_cmp_file):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644')

        mock_glob.return_value = ['/var/lib/kolla/config_files/bar']
        mock_isdir.return_value = True
        mock_cmp_dir.return_value = True

        config_file.check()

        mock_isdir.assert_called_once_with('/var/lib/kolla/config_files/bar')
        mock_cmp_dir.assert_called_once_with(
            '/var/lib/kolla/config_files/bar', '/foo')
        mock_cmp_file.assert_not_called()

    @mock.patch.object(set_configs.ConfigFile, '_cmp_file')
    @mock.patch.object(set_configs.ConfigFile, '_cmp_dir')
    @mock.patch('os.path.isdir', return_value=False)
    @mock.patch('glob.glob')
    def test_check_source_dir_no_equal(self, mock_glob, mock_isdir,
                                       mock_cmp_dir, mock_cmp_file):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644')

        mock_glob.return_value = ['/var/lib/kolla/config_files/bar']
        mock_isdir.return_value = True
        mock_cmp_dir.return_value = False

        self.assertRaises(set_configs.ConfigFileBadState,
                          config_file.check)

        mock_isdir.assert_called_once_with('/var/lib/kolla/config_files/bar')
        mock_cmp_dir.assert_called_once_with(
            '/var/lib/kolla/config_files/bar', '/foo')
        mock_cmp_file.assert_not_called()

    @mock.patch('grp.getgrgid', autospec=True)
    @mock.patch('pwd.getpwuid', autospec=True)
    @mock.patch('os.stat', autospec=True)
    @mock.patch('builtins.open', new_callable=mock.mock_open)
    @mock.patch('os.path.exists', autospec=True)
    def test_cmp_file_opens_both_files_rb(self, mock_os_exists, mock_open,
                                          mock_os_stat, mock_pwd_getpwuid,
                                          mock_grp_getgrgid):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644')

        mock_os_exists.return_value = True
        mock_os_stat.return_value.st_mode = int('0o100644', 8)
        mock_pwd_getpwuid.return_value.pw_name = 'user1'
        mock_grp_getgrgid.return_value.gr_name = 'user1'

        self.assertIs(True,
                      config_file._cmp_file('/fake/file1', '/fake/file2'))

        self.assertEqual([mock.call('/fake/file1', 'rb'),
                          mock.call('/fake/file2', 'rb')],
                         mock_open.call_args_list)

    @mock.patch('glob.glob')
    def test_check_non_optional_src_file_not_exists(self,
                                                    mock_glob):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644')

        mock_glob.return_value = []

        self.assertRaises(set_configs.MissingRequiredSource,
                          config_file.check)

    @mock.patch('glob.glob')
    def test_check_optional_src_file_not_exists(self,
                                                mock_glob):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644',
            optional=True)
        mock_glob.return_value = []

        self.assertIsNone(config_file.check())

    @mock.patch('glob.glob')
    @mock.patch('os.path.isdir')
    @mock.patch.object(set_configs.ConfigFile, '_cmp_file')
    def test_check_raises_config_bad_state(self,
                                           mock_cmp_file,
                                           mock_isdir,
                                           mock_glob):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644',
            optional=True)
        mock_cmp_file.return_value = False
        mock_isdir.return_value = False
        mock_glob.return_value = ['/var/lib/kolla/config_files/bar']

        self.assertRaises(set_configs.ConfigFileBadState, config_file.check)

    @mock.patch('os.path.exists', autospec=True)
    def test_cmp_file_optional_src_exists_dest_no_exists(self, mock_os_exists):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644',
            optional=True)

        def fake_exists(path):
            if path == '/var/lib/kolla/config_files/bar':
                return True
            return False

        mock_os_exists.side_effect = fake_exists

        self.assertIs(False,
                      config_file._cmp_file('/var/lib/kolla/config_files/bar',
                                            '/foo'))

    @mock.patch('os.path.exists', autospec=True)
    def test_cmp_file_optional_src_no_exists_dest_exists(self, mock_os_exists):
        config_file = set_configs.ConfigFile(
            '/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644',
            optional=True)

        def fake_exists(path):
            if path == '/var/lib/kolla/config_files/bar':
                return False
            return True

        mock_os_exists.side_effect = fake_exists

        self.assertIs(False,
                      config_file._cmp_file('/var/lib/kolla/config_files/bar',
                                            '/foo'))

    @mock.patch('os.makedirs')
    @mock.patch('os.path.exists')
    @mock.patch('builtins.open', new_callable=mock.mock_open,
                read_data='{"foo": "bar"}')
    def test_get_defaults_state_exist(
            self, mock_open, mock_exists, mock_makedirs):
        """Test get_defaults_state() when the default state file exists.

        This test mocks the behavior of the function when the default state
        file exists. It ensures that:
        - The directory for Kolla defaults is created if needed.
        - The state file is opened and read successfully.
        - The correct state data is returned as a dictionary.

        Mocks:
        - os.makedirs: Ensures the directory is created.
        - os.path.exists: Simulates that the state file exists.
        - open: Mocks the file opening and reading, returning a sample JSON
                content.
        """

        # Simulate that the state file exists
        mock_exists.side_effect = lambda \
            path: path == set_configs.KOLLA_DEFAULTS_STATE

        result = set_configs.get_defaults_state()

        # Check that the directory creation was called
        mock_makedirs.assert_called_once_with(set_configs.KOLLA_DEFAULTS,
                                              exist_ok=True)

        # Verify that the function checked if the state file exists
        mock_exists.assert_called_once_with(set_configs.KOLLA_DEFAULTS_STATE)

        # Verify that the state file was opened for reading
        mock_open.assert_called_once_with(
            set_configs.KOLLA_DEFAULTS_STATE, 'r')

        # Validate the result is as expected
        self.assertEqual(result, {"foo": "bar"})

    @mock.patch('os.makedirs')
    @mock.patch('os.path.exists', return_value=False)
    def test_get_defaults_state_not_exist(self, mock_exists, mock_makedirs):
        """Test get_defaults_state() when the default state file doesn't exist.

        This test simulates the scenario where the default state file is
        missing.
        It verifies that:
        - The directory for Kolla defaults is created if needed.
        - The state file is checked but not found.
        - An empty dictionary is returned since the state file is missing.

        Mocks:
        - os.makedirs: Ensures the directory is created.
        - os.path.exists: Simulates that the state file does not exist.
        """

        # Simulate that the file does not exist
        mock_exists.side_effect = lambda path: False

        result = set_configs.get_defaults_state()

        # Check that the directory creation was called
        mock_makedirs.assert_called_once_with(set_configs.KOLLA_DEFAULTS,
                                              exist_ok=True)
        # Verify that the function checked if the state file exists
        mock_exists.assert_called_once_with(set_configs.KOLLA_DEFAULTS_STATE)

        # Result should be an empty dictionary since the state file is missing
        self.assertEqual(result, {})

    @mock.patch('builtins.open', new_callable=mock.mock_open)
    @mock.patch('json.dump')
    def test_set_defaults_state(self, mock_json_dump, mock_open):
        """Test set_defaults_state() to ensure proper saving of the state.

        This test verifies that the provided state is correctly saved as a JSON
        file with proper indentation. It checks:
        - The state file is opened for writing.
        - The provided state dictionary is dumped into the file in JSON format
          with indentation for readability.

        Mocks:
        - open: Mocks the file opening for writing.
        - json.dump: Mocks the JSON dumping process.
        """

        state = {"foo": "bar"}

        set_configs.set_defaults_state(state)

        # Ensure the state file is opened for writing
        mock_open.assert_called_once_with(
            set_configs.KOLLA_DEFAULTS_STATE, 'w')

        # Check that the JSON state is dumped with proper indentation
        mock_json_dump.assert_called_once_with(state, mock_open(), indent=4)

    @mock.patch.object(set_configs, 'set_defaults_state')
    @mock.patch.object(set_configs, 'ConfigFile')
    @mock.patch('os.path.exists')
    @mock.patch.object(set_configs, 'get_defaults_state', return_value={})
    def test_handle_defaults_state_not_exist_config_exist(
            self, mock_get_defaults_state, mock_exists, mock_config_file,
            mock_set_defaults_state):
        """Test handle_defaults() when no existing default config is present.

        This test simulates the case where no prior default configuration file
        exists and a new configuration file needs to be backed up. It verifies:
        - The current default state is retrieved (empty in this case).
        - The configuration file exists, and a backup is created for it.
        - The new state is saved after processing the configuration.

        Mocks:
        - get_defaults_state: Returns an empty state.
        - os.path.exists: Simulates that the source file exists.
        - ConfigFile: Mocks the behavior of the ConfigFile class for file
          copying.
        - set_defaults_state: Ensures the new state is saved.
        """

        config = {
            'config_files': [
                {
                    'source': '/source/file', 'dest': '/dest/file'
                 }
            ]
        }

        # Simulate the file exists
        mock_exists.return_value = True

        copy = {
            'source': '/dest/file',
            'dest': set_configs.KOLLA_DEFAULTS + '/dest/file',
            'preserve_properties': True
        }

        expected_state = {
            '/dest/file': copy
        }

        # Create a mock instance of ConfigFile
        mock_config_file_instance = mock_config_file.return_value
        mock_config_file_instance.copy = mock.MagicMock()

        # Call the function being tested
        set_configs.handle_defaults(config)

        # Check that the directory creation was called
        mock_get_defaults_state.assert_called_once()

        # Verify that the ConfigFile was instantiated correctly
        mock_config_file.assert_called_once_with(**copy)

        # Ensure the copy method was called for the ConfigFile instance
        mock_config_file_instance.copy.assert_called_once()

        # Check that the updated state was saved
        mock_set_defaults_state.assert_called_once_with(expected_state)

    @mock.patch.object(set_configs, 'set_defaults_state')
    @mock.patch.object(set_configs, 'ConfigFile')
    @mock.patch('os.path.exists')
    @mock.patch.object(set_configs, 'get_defaults_state', return_value={})
    def test_handle_defaults_state_not_exist_config_not_exist(
            self, mock_get_defaults_state, mock_exists, mock_config_file,
            mock_set_defaults_state):
        """Test handle_defaults() with no config file and no default state.

        This test simulates the scenario where the configuration file does not
        exist, and no existing default state is present. It verifies:
        - The current default state is retrieved (empty).
        - Since the configuration file doesn't exist, no backup is made.
        - The state is updated accordingly.

        Mocks:
        - get_defaults_state: Returns an empty state.
        - os.path.exists: Simulates that the source
                          exists (in /var/lib/kolla/config/) but the
                          destination does not
                          (real destination where file should be copied to).
        - ConfigFile: Ensures no ConfigFile instance is created since no backup
                      is needed.
        - set_defaults_state: Ensures the updated state is saved.
        """

        config = {
            'config_files': [
                {
                    'source': '/source/file', 'dest': '/dest/file'
                 }
            ]
        }

        # Simulate source exists but dest does not
        def mock_exists_side_effect(path):
            if path == '/source/file':
                return True  # Source exists
            return False  # Destination does not exist

        mock_exists.side_effect = mock_exists_side_effect

        copy = {
            'source': '/dest/file',
            'dest': None,
            'preserve_properties': True
        }

        expected_state = {
            '/dest/file': copy
        }

        # Create a mock instance of ConfigFile
        mock_config_file_instance = mock_config_file.return_value
        mock_config_file_instance.copy = mock.MagicMock()

        # Call the function being tested
        set_configs.handle_defaults(config)

        # Ensure the current default state was retrieved
        mock_get_defaults_state.assert_called_once()

        # Verify that ConfigFile was not instantiated (because
        # dest doesn't exist - nothing to backup)
        mock_config_file.assert_not_called()

        # Check that the updated state was saved
        mock_set_defaults_state.assert_called_once_with(expected_state)

    @mock.patch.object(set_configs, 'set_defaults_state')
    @mock.patch.object(set_configs, 'ConfigFile')
    @mock.patch('os.remove')
    @mock.patch('shutil.rmtree')
    @mock.patch('os.path.isfile')
    @mock.patch('os.path.exists')
    @mock.patch.object(set_configs, 'get_defaults_state', return_value={
        "/dest/file": {
            "source": "/dest/file",
            "preserve_properties": True,
            "dest": None
        }
    })
    def test_handle_defaults_state_exist_config_exist_is_file(
            self, mock_get_defaults_state, mock_exists, mock_isfile,
            mock_rmtree, mock_remove, mock_config_file,
            mock_set_defaults_state):
        """Test handle_defaults() when the configuration exists and is a file.

        This test simulates the scenario where a configuration file already
        exists.
        It verifies:
        - The current default state is retrieved.
        - The destination is identified as a file.
        - The file is removed.
        - The updated state is saved correctly after the file is handled.

        Mocks:
        - get_defaults_state: Returns an existing state.
        - os.path.exists: Simulates the destination file exists.
        - os.path.isfile: Ensures the destination is identified as a file.
        - os.remove: Ensures the file is removed.
        - set_defaults_state: Ensures the state is saved after processing.
        """

        config = {
            'config_files': [
                {
                    'source': '/source/file', 'dest': '/dest/file'
                 }
            ]
        }

        # Simulate that destination exists and is a file
        mock_exists.side_effect = lambda path: path == "/dest/file"
        mock_isfile.side_effect = lambda path: path == "/dest/file"

        # Expected state after handling defaults
        expected_state = {
            "/dest/file": {
                "source": "/dest/file",
                "preserve_properties": True,
                "dest": None
            }
        }

        # Call the function being tested
        set_configs.handle_defaults(config)

        # Ensure the current default state was retrieved
        mock_get_defaults_state.assert_called_once()

        # Verify that the file check was performed
        mock_isfile.assert_called_once_with("/dest/file")

        # Ensure the file is removed since it exists
        mock_remove.assert_called_once_with("/dest/file")

        # Ensure rmtree was not called since it's a file, not a directory
        mock_rmtree.assert_not_called()

        # Verify that the updated state was saved
        mock_set_defaults_state.assert_called_once_with(expected_state)

    @mock.patch.object(set_configs, 'set_defaults_state')
    @mock.patch.object(set_configs, 'ConfigFile')
    @mock.patch('os.remove')
    @mock.patch('shutil.rmtree')
    @mock.patch('os.path.isfile')
    @mock.patch('os.path.exists')
    @mock.patch.object(set_configs, 'get_defaults_state', return_value={
        "/dest/file": {
            "source": "/dest/file",
            "preserve_properties": True,
            "dest": None
        }
    })
    def test_handle_defaults_state_exist_config_exist_is_dir(
            self, mock_get_defaults_state, mock_exists, mock_isfile,
            mock_rmtree, mock_remove, mock_config_file,
            mock_set_defaults_state):
        """Test handle_defaults() when the conf file exists and is a directory.

        This test simulates the scenario where the configuration exists as
        a directory.
        It verifies:
        - The current default state is retrieved.
        - The configuration is identified as a directory.
        - The configuration directory is removed using shutil.rmtree().
        - The updated state is saved correctly after handling the directory.

        Mocks:
        - get_defaults_state: Returns an existing state.
        - os.path.exists: Simulates the destination directory exists.
        - os.path.isfile: Ensures the destination is not a file
          (it’s a directory).
        - shutil.rmtree: Ensures the directory is removed.
        - ConfigFile: Mocks the ConfigFile handling.
        - set_defaults_state: Ensures the state is saved after processing.
        """

        config = {
            'config_files': [
                {
                    'source': '/source/file', 'dest': '/dest/file'
                 }
            ]
        }

        # Simulate that destination exists and is a file
        mock_exists.side_effect = lambda path: path == "/dest/file"
        # Simulate that destination exists, but it's a directory, not a file
        mock_isfile.side_effect = lambda path: False

        # Expected state after handling defaults
        expected_state = {
            "/dest/file": {
                "source": "/dest/file",
                "preserve_properties": True,
                "dest": None
            }
        }

        # Create a mock instance of ConfigFile
        mock_config_file_instance = mock_config_file.return_value
        mock_config_file_instance.copy = mock.MagicMock()

        # Call the function being tested
        set_configs.handle_defaults(config)

        # Ensure the current default state was retrieved
        mock_get_defaults_state.assert_called_once()

        # Verify that a file check was performed
        mock_isfile.assert_called_once_with("/dest/file")

        # Check that os.remove was called for the existing file
        mock_remove.assert_not_called()

        # Since it's a directory, ensure rmtree was called to remove it
        mock_rmtree.assert_called_once_with("/dest/file")

        # Verify that the updated state was saved
        mock_set_defaults_state.assert_called_once_with(expected_state)

    @mock.patch.object(set_configs, 'set_defaults_state')
    @mock.patch.object(set_configs, 'ConfigFile')
    @mock.patch('os.path.exists')
    @mock.patch.object(set_configs, 'get_defaults_state', return_value={
        "/dest/file": {
            "source": "/source/file",
            "dest": "/dest/file",
            "preserve_properties": True
        }
    })
    def test_handle_defaults_state_exist_config_restored(
            self, mock_get_defaults_state, mock_exists, mock_config_file,
            mock_set_defaults_state):
        """Test handle_defaults() when dest is not None in the default state.

        This test simulates the case where the destination in the default state
        is not None, meaning a swap of source and destination is required.
        It verifies:
        - The current default state is retrieved.
        - The source and destination are swapped in the ConfigFile
          and restored.
        - The state is updated correctly after processing.

        Mocks:
        - get_defaults_state: Returns the current state.
        - ConfigFile: Mocks the behavior of ConfigFile with swapped
          source/dest.
        - set_defaults_state: Ensures the state is saved after the swap and
          processing.
        """

        # Configuration input (irrelevant in this case, as we're
        # only focusing on state)
        # Everything else is covered by other tests
        config = {}

        copy = {
            "source": "/dest/file",
            "dest": "/source/file",  # Swapped source and dest
            "preserve_properties": True
        }

        # Expected state after swapping source and dest
        expected_state = {
            "/dest/file": {
                "source": "/dest/file",
                "dest": "/source/file",  # Swapped source and dest
                "preserve_properties": True
            }
        }

        # Create a mock instance of ConfigFile
        mock_config_file_instance = mock_config_file.return_value
        mock_config_file_instance.copy = mock.MagicMock()

        # Call the function being tested
        set_configs.handle_defaults(config)

        # Ensure the current default state was retrieved
        mock_get_defaults_state.assert_called_once()

        # Verify that ConfigFile was instantiated with the swapped
        # source and dest
        mock_config_file.assert_called_once_with(**copy)

        # Ensure the copy method was called for the ConfigFile instance
        mock_config_file_instance.copy.assert_called_once()

        # Ensure the copy method was called on the ConfigFile instance
        mock_config_file_instance.copy.assert_called_once()

        # Verify that the updated state was saved
        mock_set_defaults_state.assert_called_once_with(expected_state)


class ValidateConfigDirectoriesTest(base.BaseTestCase):

    def test_validate_config_directories_missing_path(self):
        config = {
            'command': '/bin/true',
            'directories': [{'owner': 'myuser', 'perm': '0755'}]
        }
        self.assertRaises(set_configs.InvalidConfig,
                          set_configs.validate_config, config)

    def test_validate_config_directories_missing_owner(self):
        config = {
            'command': '/bin/true',
            'directories': [{'path': '/var/log/foo', 'perm': '0755'}]
        }
        self.assertRaises(set_configs.InvalidConfig,
                          set_configs.validate_config, config)

    def test_validate_config_directories_missing_perm(self):
        config = {
            'command': '/bin/true',
            'directories': [{'path': '/var/log/foo', 'owner': 'myuser'}]
        }
        self.assertRaises(set_configs.InvalidConfig,
                          set_configs.validate_config, config)

    def test_validate_config_directories_valid(self):
        config = {
            'command': '/bin/true',
            'directories': [
                {'path': '/var/log/foo', 'owner': 'myuser', 'perm': '0755'}
            ]
        }
        set_configs.validate_config(config)


class CreateDirectoriesTest(base.BaseTestCase):

    @mock.patch.object(set_configs, 'handle_permissions')
    @mock.patch('os.makedirs')
    @mock.patch('os.path.exists', return_value=False)
    def test_create_directory_not_exists(self, mock_exists, mock_makedirs,
                                         mock_handle_permissions):
        config = {
            'directories': [
                {'path': '/var/log/foo', 'owner': 'myuser', 'perm': '0755'}
            ]
        }
        set_configs.create_directories(config)

        mock_makedirs.assert_called_once_with('/var/log/foo', exist_ok=True)
        mock_handle_permissions.assert_called_once_with(
            {'permissions': [
                {'path': '/var/log/foo', 'owner': 'myuser', 'perm': '0755'}
            ]}
        )

    @mock.patch.object(set_configs, 'handle_permissions')
    @mock.patch('os.makedirs')
    @mock.patch('os.path.exists', return_value=True)
    def test_create_directory_already_exists(self, mock_exists, mock_makedirs,
                                             mock_handle_permissions):
        config = {
            'directories': [
                {'path': '/var/log/foo', 'owner': 'myuser', 'perm': '0755'}
            ]
        }
        set_configs.create_directories(config)

        mock_makedirs.assert_called_once_with('/var/log/foo', exist_ok=True)
        mock_handle_permissions.assert_called_once_with(
            {'permissions': [
                {'path': '/var/log/foo', 'owner': 'myuser', 'perm': '0755'}
            ]}
        )

    @mock.patch.object(set_configs, 'handle_permissions')
    @mock.patch('os.makedirs')
    @mock.patch('os.path.exists', return_value=False)
    def test_create_directories_no_directories_key(
            self, mock_exists, mock_makedirs, mock_handle_permissions):
        config = {'command': '/bin/true'}
        set_configs.create_directories(config)

        mock_exists.assert_not_called()
        mock_makedirs.assert_not_called()
        mock_handle_permissions.assert_not_called()

    @mock.patch.object(set_configs, 'handle_permissions')
    @mock.patch('os.makedirs')
    @mock.patch('os.path.exists', return_value=False)
    def test_create_multiple_directories(self, mock_exists, mock_makedirs,
                                         mock_handle_permissions):
        config = {
            'directories': [
                {'path': '/var/log/foo', 'owner': 'user1', 'perm': '0755'},
                {'path': '/var/log/bar', 'owner': 'user2', 'perm': '0750'},
            ]
        }
        set_configs.create_directories(config)

        self.assertEqual(mock_makedirs.call_args_list,
                         [mock.call('/var/log/foo', exist_ok=True),
                          mock.call('/var/log/bar', exist_ok=True)])
        self.assertEqual(mock_handle_permissions.call_count, 2)


class ExecuteConfigCheckStateMismatchTest(base.BaseTestCase):

    @mock.patch.object(set_configs, 'get_defaults_state')
    def test_execute_config_check_raises_state_mismatch(
        self, mock_get_defaults_state
    ):
        """Test execute_config_check() when state has extra config file.

        This test simulates the scenario where the state file contains
        a destination that no longer exists in config.json. It verifies:
        - get_defaults_state() returns a state with an extra entry.
        - execute_config_check() raises StateMismatch when config.json
          omits a tracked destination.
        """
        config = {
            "command": "/bin/true",
            "config_files": [
                {
                    "source": "/etc/foo/foo.conf",
                    "dest": "/etc/foo/foo.conf",
                    "owner": "user1",
                    "perm": "0644"
                }
            ]
        }

        mock_get_defaults_state.return_value = {
            "/etc/foo/foo.conf": {
                "source": "/etc/foo/foo.conf",
                "preserve_properties": True,
                "dest": "/etc/kolla/defaults/etc/foo/foo.conf"
            },
            "/etc/old/obsolete.conf": {
                "source": "/etc/old/obsolete.conf",
                "preserve_properties": True,
                "dest": "/etc/kolla/defaults/etc/old/obsolete.conf"
            }
        }

        self.assertRaises(set_configs.StateMismatch,
                          set_configs.execute_config_check,
                          config)