20
20
import unittest
21
21
from subprocess import CalledProcessError
22
22
from unittest .mock import patch , call , MagicMock
23
+ from pathlib import Path
24
+ import shutil
23
25
24
26
from checkbox_support .helpers .retry import mock_retry
25
27
42
44
perform_ping_test ,
43
45
parser_args ,
44
46
main ,
47
+ restore_netplan_files ,
48
+ backup_netplan_files ,
49
+ cleanup_netplan_backup ,
45
50
)
46
51
47
52
@@ -606,6 +611,9 @@ def test_main_open_no_aps_found(
606
611
"TestSSID" : {"Chan" : "11" , "Freq" : "2462" , "Signal" : "80" },
607
612
},
608
613
)
614
+ @patch ("wifi_nmcli_test.backup_netplan_files" )
615
+ @patch ("wifi_nmcli_test.restore_netplan_files" )
616
+ @patch ("wifi_nmcli_test.cleanup_netplan_backup" )
609
617
@patch ("wifi_nmcli_test.open_connection" , return_value = 0 )
610
618
@patch (
611
619
"wifi_nmcli_test.sys.argv" ,
@@ -616,5 +624,209 @@ def test_main_open_aps_found(
616
624
list_aps_mock ,
617
625
get_nm_activate_connection_mock ,
618
626
mock_open_connection ,
627
+ mock_cleanup_back ,
628
+ mock_rest_back ,
629
+ mock_cr_back ,
619
630
):
620
631
main ()
632
+
633
+
634
+ class TestNetplanBackupFunctions (unittest .TestCase ):
635
+ def setUp (self ):
636
+ self .TEST_BACKUP_DIR = "/tmp/test_netplan_backup"
637
+ self .TEST_NETPLAN_DIR = "/tmp/etc/netplan"
638
+ """Set up test fixtures before each test method."""
639
+ Path (self .TEST_BACKUP_DIR ).mkdir (parents = True , exist_ok = True )
640
+ Path (self .TEST_NETPLAN_DIR ).mkdir (parents = True , exist_ok = True )
641
+
642
+ def tearDown (self ):
643
+ """Clean up after each test method."""
644
+ # Clean up test directories
645
+ shutil .rmtree (Path (self .TEST_BACKUP_DIR ), ignore_errors = True )
646
+ shutil .rmtree (Path (self .TEST_NETPLAN_DIR ), ignore_errors = True )
647
+
648
+ @patch ("glob.glob" )
649
+ @patch ("builtins.print" )
650
+ def test_backup_netplan_files_no_files_found (self , mock_print , mock_glob ):
651
+ """Test backup when no YAML files are found."""
652
+ mock_glob .return_value = []
653
+
654
+ result = backup_netplan_files (
655
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
656
+ )
657
+ self .assertTrue (result )
658
+
659
+ @patch ("os.chown" )
660
+ @patch ("os.stat" )
661
+ @patch ("glob.glob" )
662
+ @patch ("shutil.copy2" )
663
+ @patch ("pathlib.Path.mkdir" )
664
+ @patch ("builtins.print" )
665
+ def test_backup_netplan_files_success (
666
+ self ,
667
+ mock_print ,
668
+ mock_mkdir ,
669
+ mock_copy2 ,
670
+ mock_glob ,
671
+ mock_stat ,
672
+ mock_chown ,
673
+ ):
674
+ """Test successful backup of netplan files."""
675
+ mock_glob .return_value = [
676
+ self .TEST_NETPLAN_DIR + "/config1.yaml" ,
677
+ self .TEST_NETPLAN_DIR + "/config2.yaml" ,
678
+ ]
679
+
680
+ result = backup_netplan_files (
681
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
682
+ )
683
+
684
+ self .assertTrue (result )
685
+ self .assertEqual (mock_copy2 .call_count , 2 )
686
+ mock_mkdir .assert_called_once_with (parents = True , exist_ok = True )
687
+
688
+ @patch ("os.chown" )
689
+ @patch ("os.stat" )
690
+ @patch ("os.path.exists" )
691
+ @patch ("glob.glob" )
692
+ @patch ("os.remove" )
693
+ @patch ("os.makedirs" )
694
+ @patch ("shutil.copy2" )
695
+ @patch ("builtins.print" )
696
+ def test_restore_netplan_files_success (
697
+ self ,
698
+ mock_print ,
699
+ mock_copy2 ,
700
+ mock_makedirs ,
701
+ mock_remove ,
702
+ mock_glob ,
703
+ mock_exists ,
704
+ mock_stat ,
705
+ mock_chown ,
706
+ ):
707
+ """Test successful restore of netplan files."""
708
+ mock_exists .return_value = True
709
+ mock_glob .side_effect = [
710
+ [self .TEST_NETPLAN_DIR + "/old1.yaml" ], # Existing files to remove
711
+ [
712
+ "{}/config1.yaml" .format (self .TEST_BACKUP_DIR ),
713
+ "{}/config2.yaml" .format (self .TEST_BACKUP_DIR ),
714
+ ], # Backup files
715
+ ]
716
+
717
+ result = restore_netplan_files (
718
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
719
+ )
720
+
721
+ self .assertTrue (result )
722
+ mock_remove .assert_called_once_with (
723
+ self .TEST_NETPLAN_DIR + "/old1.yaml"
724
+ )
725
+ self .assertEqual (mock_copy2 .call_count , 2 )
726
+
727
+ @patch ("os.chown" )
728
+ @patch ("os.stat" )
729
+ @patch ("os.path.exists" )
730
+ @patch ("glob.glob" )
731
+ @patch ("os.remove" )
732
+ @patch ("shutil.copy2" )
733
+ @patch ("builtins.print" )
734
+ def test_restore_netplan_files_copy_error (
735
+ self ,
736
+ mock_print ,
737
+ mock_copy2 ,
738
+ mock_remove ,
739
+ mock_glob ,
740
+ mock_exists ,
741
+ mock_stat ,
742
+ mock_chown ,
743
+ ):
744
+ """Test restore when copy operation fails."""
745
+ mock_exists .return_value = True
746
+ mock_glob .side_effect = [
747
+ [],
748
+ ["{}/config1.yaml" .format (self .TEST_BACKUP_DIR )],
749
+ ]
750
+ mock_copy2 .side_effect = OSError ("Permission denied" )
751
+
752
+ result = restore_netplan_files (
753
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
754
+ )
755
+
756
+ self .assertFalse (result )
757
+
758
+ @patch ("os.path.exists" )
759
+ @patch ("builtins.print" )
760
+ def test_cleanup_backup_not_exists (self , mock_print , mock_exists ):
761
+ """Test cleanup when backup directory doesn't exist."""
762
+ mock_exists .return_value = False
763
+
764
+ result = cleanup_netplan_backup (self .TEST_BACKUP_DIR )
765
+
766
+ self .assertTrue (result )
767
+
768
+ @patch ("os.path.exists" )
769
+ @patch ("shutil.rmtree" )
770
+ @patch ("builtins.print" )
771
+ def test_cleanup_backup_success (
772
+ self , mock_print , mock_rmtree , mock_exists
773
+ ):
774
+ """Test successful cleanup of backup directory."""
775
+ mock_exists .return_value = True
776
+
777
+ result = cleanup_netplan_backup (self .TEST_BACKUP_DIR )
778
+
779
+ self .assertTrue (result )
780
+ mock_rmtree .assert_called_once_with (self .TEST_BACKUP_DIR )
781
+
782
+ @patch ("os.path.exists" )
783
+ @patch ("shutil.rmtree" )
784
+ @patch ("builtins.print" )
785
+ def test_cleanup_backup_error (self , mock_print , mock_rmtree , mock_exists ):
786
+ """Test cleanup when rmtree operation fails."""
787
+ mock_exists .return_value = True
788
+ mock_rmtree .side_effect = OSError ("Permission denied" )
789
+
790
+ result = cleanup_netplan_backup (self .TEST_BACKUP_DIR )
791
+
792
+ self .assertFalse (result )
793
+
794
+ @patch ("os.path.exists" )
795
+ @patch ("glob.glob" )
796
+ @patch ("os.remove" )
797
+ @patch ("builtins.print" )
798
+ def test_restore_netplan_files_remove_error (
799
+ self , mock_print , mock_remove , mock_glob , mock_exists
800
+ ):
801
+ """Test restore when removing existing files fails."""
802
+ mock_exists .return_value = True
803
+ mock_glob .side_effect = [
804
+ [self .TEST_NETPLAN_DIR + "/old1.yaml" ], # Existing files to remove
805
+ ["{}/config1.yaml" .format (self .TEST_BACKUP_DIR )], # Backup files
806
+ ]
807
+ mock_remove .side_effect = OSError ("Permission denied" )
808
+ with self .assertRaises (OSError ):
809
+ result = restore_netplan_files (
810
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
811
+ )
812
+
813
+ @patch ("os.path.exists" )
814
+ @patch ("glob.glob" )
815
+ @patch ("os.makedirs" )
816
+ @patch ("builtins.print" )
817
+ def test_restore_netplan_files_makedirs_error (
818
+ self , mock_print , mock_makedirs , mock_glob , mock_exists
819
+ ):
820
+ """Test restore when makedirs operation fails."""
821
+ mock_exists .return_value = True
822
+ mock_glob .side_effect = [
823
+ [],
824
+ ["{}/config1.yaml" .format (self .TEST_BACKUP_DIR )],
825
+ ]
826
+ mock_makedirs .side_effect = OSError ("Permission denied" )
827
+
828
+ result = restore_netplan_files (
829
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
830
+ )
831
+
832
+ self .assertFalse (result )
0 commit comments