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,203 @@ 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
+ f"{ self .TEST_BACKUP_DIR } /config1.yaml" ,
713
+ f"{ self .TEST_BACKUP_DIR } /config2.yaml" ,
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 = [[], [f"{ self .TEST_BACKUP_DIR } /config1.yaml" ]]
747
+ mock_copy2 .side_effect = OSError ("Permission denied" )
748
+
749
+ result = restore_netplan_files (
750
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
751
+ )
752
+
753
+ self .assertFalse (result )
754
+
755
+ @patch ("os.path.exists" )
756
+ @patch ("builtins.print" )
757
+ def test_cleanup_backup_not_exists (self , mock_print , mock_exists ):
758
+ """Test cleanup when backup directory doesn't exist."""
759
+ mock_exists .return_value = False
760
+
761
+ result = cleanup_netplan_backup (self .TEST_BACKUP_DIR )
762
+
763
+ self .assertTrue (result )
764
+
765
+ @patch ("os.path.exists" )
766
+ @patch ("shutil.rmtree" )
767
+ @patch ("builtins.print" )
768
+ def test_cleanup_backup_success (
769
+ self , mock_print , mock_rmtree , mock_exists
770
+ ):
771
+ """Test successful cleanup of backup directory."""
772
+ mock_exists .return_value = True
773
+
774
+ result = cleanup_netplan_backup (self .TEST_BACKUP_DIR )
775
+
776
+ self .assertTrue (result )
777
+ mock_rmtree .assert_called_once_with (self .TEST_BACKUP_DIR )
778
+
779
+ @patch ("os.path.exists" )
780
+ @patch ("shutil.rmtree" )
781
+ @patch ("builtins.print" )
782
+ def test_cleanup_backup_error (self , mock_print , mock_rmtree , mock_exists ):
783
+ """Test cleanup when rmtree operation fails."""
784
+ mock_exists .return_value = True
785
+ mock_rmtree .side_effect = OSError ("Permission denied" )
786
+
787
+ result = cleanup_netplan_backup (self .TEST_BACKUP_DIR )
788
+
789
+ self .assertFalse (result )
790
+
791
+ @patch ("os.path.exists" )
792
+ @patch ("glob.glob" )
793
+ @patch ("os.remove" )
794
+ @patch ("builtins.print" )
795
+ def test_restore_netplan_files_remove_error (
796
+ self , mock_print , mock_remove , mock_glob , mock_exists
797
+ ):
798
+ """Test restore when removing existing files fails."""
799
+ mock_exists .return_value = True
800
+ mock_glob .side_effect = [
801
+ [self .TEST_NETPLAN_DIR + "/old1.yaml" ], # Existing files to remove
802
+ [f"{ self .TEST_BACKUP_DIR } /config1.yaml" ], # Backup files
803
+ ]
804
+ mock_remove .side_effect = OSError ("Permission denied" )
805
+ with self .assertRaises (OSError ):
806
+ result = restore_netplan_files (
807
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
808
+ )
809
+
810
+ @patch ("os.path.exists" )
811
+ @patch ("glob.glob" )
812
+ @patch ("os.makedirs" )
813
+ @patch ("builtins.print" )
814
+ def test_restore_netplan_files_makedirs_error (
815
+ self , mock_print , mock_makedirs , mock_glob , mock_exists
816
+ ):
817
+ """Test restore when makedirs operation fails."""
818
+ mock_exists .return_value = True
819
+ mock_glob .side_effect = [[], [f"{ self .TEST_BACKUP_DIR } /config1.yaml" ]]
820
+ mock_makedirs .side_effect = OSError ("Permission denied" )
821
+
822
+ result = restore_netplan_files (
823
+ self .TEST_BACKUP_DIR , self .TEST_NETPLAN_DIR
824
+ )
825
+
826
+ self .assertFalse (result )
0 commit comments