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