@@ -1736,3 +1736,223 @@ func TestNodeGroupGetOptions(t *testing.T) {
1736
1736
})
1737
1737
}
1738
1738
}
1739
+
1740
+ func TestNodeGroupNodesInstancesStatus (t * testing.T ) {
1741
+ type testCase struct {
1742
+ description string
1743
+ nodeCount int
1744
+ includePendingMachine bool
1745
+ includeDeletingMachine bool
1746
+ includeFailedMachineWithNodeRef bool
1747
+ includeFailedMachineWithoutNodeRef bool
1748
+ includeFailedMachineDeleting bool
1749
+ }
1750
+
1751
+ testCases := []testCase {
1752
+ {
1753
+ description : "standard number of nodes" ,
1754
+ nodeCount : 5 ,
1755
+ },
1756
+ {
1757
+ description : "includes a machine in pending state" ,
1758
+ nodeCount : 5 ,
1759
+ includePendingMachine : true ,
1760
+ },
1761
+ {
1762
+ description : "includes a machine in deleting state" ,
1763
+ nodeCount : 5 ,
1764
+ includeDeletingMachine : true ,
1765
+ },
1766
+ {
1767
+ description : "includes a machine in failed state with nodeRef" ,
1768
+ nodeCount : 5 ,
1769
+ includeFailedMachineWithNodeRef : true ,
1770
+ },
1771
+ {
1772
+ description : "includes a machine in failed state without nodeRef" ,
1773
+ nodeCount : 5 ,
1774
+ includeFailedMachineWithoutNodeRef : true ,
1775
+ },
1776
+ }
1777
+
1778
+ test := func (t * testing.T , tc * testCase , testConfig * testConfig ) {
1779
+ controller , stop := mustCreateTestController (t , testConfig )
1780
+ defer stop ()
1781
+
1782
+ if tc .includePendingMachine {
1783
+ if tc .nodeCount < 1 {
1784
+ t .Fatal ("test cannot pass, deleted machine requires at least 1 machine in machineset" )
1785
+ }
1786
+
1787
+ machine := testConfig .machines [0 ].DeepCopy ()
1788
+ unstructured .RemoveNestedField (machine .Object , "spec" , "providerID" )
1789
+ unstructured .RemoveNestedField (machine .Object , "status" , "nodeRef" )
1790
+
1791
+ if err := updateResource (controller .managementClient , controller .machineInformer , controller .machineResource , machine ); err != nil {
1792
+ t .Fatalf ("unexpected error updating machine, got %v" , err )
1793
+ }
1794
+ }
1795
+
1796
+ if tc .includeDeletingMachine {
1797
+ if tc .nodeCount < 2 {
1798
+ t .Fatal ("test cannot pass, deleted machine requires at least 2 machine in machineset" )
1799
+ }
1800
+
1801
+ machine := testConfig .machines [1 ].DeepCopy ()
1802
+ timestamp := metav1 .Now ()
1803
+ machine .SetDeletionTimestamp (& timestamp )
1804
+
1805
+ if err := updateResource (controller .managementClient , controller .machineInformer , controller .machineResource , machine ); err != nil {
1806
+ t .Fatalf ("unexpected error updating machine, got %v" , err )
1807
+ }
1808
+ }
1809
+
1810
+ if tc .includeFailedMachineWithNodeRef {
1811
+ if tc .nodeCount < 3 {
1812
+ t .Fatal ("test cannot pass, deleted machine requires at least 3 machine in machineset" )
1813
+ }
1814
+
1815
+ machine := testConfig .machines [2 ].DeepCopy ()
1816
+ unstructured .SetNestedField (machine .Object , "node-1" , "status" , "nodeRef" , "name" )
1817
+ unstructured .SetNestedField (machine .Object , "ErrorMessage" , "status" , "errorMessage" )
1818
+
1819
+ if err := updateResource (controller .managementClient , controller .machineInformer , controller .machineResource , machine ); err != nil {
1820
+ t .Fatalf ("unexpected error updating machine, got %v" , err )
1821
+ }
1822
+ }
1823
+
1824
+ if tc .includeFailedMachineWithoutNodeRef {
1825
+ if tc .nodeCount < 4 {
1826
+ t .Fatal ("test cannot pass, deleted machine requires at least 4 machine in machineset" )
1827
+ }
1828
+
1829
+ machine := testConfig .machines [3 ].DeepCopy ()
1830
+ unstructured .RemoveNestedField (machine .Object , "status" , "nodeRef" )
1831
+ unstructured .SetNestedField (machine .Object , "ErrorMessage" , "status" , "errorMessage" )
1832
+
1833
+ if err := updateResource (controller .managementClient , controller .machineInformer , controller .machineResource , machine ); err != nil {
1834
+ t .Fatalf ("unexpected error updating machine, got %v" , err )
1835
+ }
1836
+ }
1837
+
1838
+ if tc .includeFailedMachineDeleting {
1839
+ if tc .nodeCount < 5 {
1840
+ t .Fatal ("test cannot pass, deleted machine requires at least 5 machine in machineset" )
1841
+ }
1842
+
1843
+ machine := testConfig .machines [4 ].DeepCopy ()
1844
+ timestamp := metav1 .Now ()
1845
+ machine .SetDeletionTimestamp (& timestamp )
1846
+ unstructured .SetNestedField (machine .Object , "ErrorMessage" , "status" , "errorMessage" )
1847
+
1848
+ if err := updateResource (controller .managementClient , controller .machineInformer , controller .machineResource , machine ); err != nil {
1849
+ t .Fatalf ("unexpected error updating machine, got %v" , err )
1850
+ }
1851
+ }
1852
+
1853
+ nodegroups , err := controller .nodeGroups ()
1854
+ if err != nil {
1855
+ t .Fatalf ("unexpected error: %v" , err )
1856
+ }
1857
+
1858
+ if l := len (nodegroups ); l != 1 {
1859
+ t .Fatalf ("expected 1 nodegroup, got %d" , l )
1860
+ }
1861
+
1862
+ ng := nodegroups [0 ]
1863
+ instances , err := ng .Nodes ()
1864
+ if err != nil {
1865
+ t .Fatalf ("unexpected error: %v" , err )
1866
+ }
1867
+
1868
+ expectedCount := tc .nodeCount
1869
+ if len (instances ) != expectedCount {
1870
+ t .Errorf ("expected %d nodes, got %d" , expectedCount , len (instances ))
1871
+ }
1872
+
1873
+ // Sort instances by Id for stable comparison
1874
+ sort .Slice (instances , func (i , j int ) bool {
1875
+ return instances [i ].Id < instances [j ].Id
1876
+ })
1877
+
1878
+ for _ , instance := range instances {
1879
+ t .Logf ("instance: %v" , instance )
1880
+ if tc .includePendingMachine && strings .HasPrefix (instance .Id , pendingMachinePrefix ) {
1881
+ if instance .Status == nil || instance .Status .State != cloudprovider .InstanceCreating {
1882
+ t .Errorf ("expected pending machine to have status %v, got %v" , cloudprovider .InstanceCreating , instance .Status )
1883
+ }
1884
+ } else if tc .includeDeletingMachine && strings .HasPrefix (instance .Id , deletingMachinePrefix ) {
1885
+ if instance .Status == nil || instance .Status .State != cloudprovider .InstanceDeleting {
1886
+ t .Errorf ("expected deleting machine to have status %v, got %v" , cloudprovider .InstanceDeleting , instance .Status )
1887
+ }
1888
+ } else if tc .includeFailedMachineWithNodeRef && strings .HasPrefix (instance .Id , failedMachinePrefix ) {
1889
+ if instance .Status != nil {
1890
+ t .Errorf ("expected failed machine with nodeRef to not have status, got %v" , instance .Status )
1891
+ }
1892
+ } else if tc .includeFailedMachineWithoutNodeRef && strings .HasPrefix (instance .Id , failedMachinePrefix ) {
1893
+ if instance .Status == nil || instance .Status .State != cloudprovider .InstanceCreating {
1894
+ t .Errorf ("expected failed machine without nodeRef to have status %v, got %v" , cloudprovider .InstanceCreating , instance .Status )
1895
+ }
1896
+ if instance .Status == nil || instance .Status .ErrorInfo .ErrorClass != cloudprovider .OtherErrorClass {
1897
+ t .Errorf ("expected failed machine without nodeRef to have error class %v, got %v" , cloudprovider .OtherErrorClass , instance .Status .ErrorInfo .ErrorClass )
1898
+ }
1899
+ if instance .Status == nil || instance .Status .ErrorInfo .ErrorCode != "ProvisioningFailed" {
1900
+ t .Errorf ("expected failed machine without nodeRef to have error code %v, got %v" , "ProvisioningFailed" , instance .Status .ErrorInfo .ErrorCode )
1901
+ }
1902
+ } else if tc .includeFailedMachineDeleting && strings .HasPrefix (instance .Id , failedMachinePrefix ) {
1903
+ if instance .Status == nil || instance .Status .State != cloudprovider .InstanceDeleting {
1904
+ t .Errorf ("expected failed machine deleting to have status %v, got %v" , cloudprovider .InstanceDeleting , instance .Status )
1905
+ }
1906
+ if instance .Status == nil || instance .Status .ErrorInfo .ErrorClass != cloudprovider .OtherErrorClass {
1907
+ t .Errorf ("expected failed machine deleting to have error class %v, got %v" , cloudprovider .OtherErrorClass , instance .Status .ErrorInfo .ErrorClass )
1908
+ }
1909
+ if instance .Status == nil || instance .Status .ErrorInfo .ErrorCode != "DeletingFailed" {
1910
+ t .Errorf ("expected failed machine deleting to have error code %v, got %v" , "DeletingFailed" , instance .Status .ErrorInfo .ErrorCode )
1911
+ }
1912
+ }
1913
+ }
1914
+ }
1915
+
1916
+ annotations := map [string ]string {
1917
+ nodeGroupMinSizeAnnotationKey : "1" ,
1918
+ nodeGroupMaxSizeAnnotationKey : "10" ,
1919
+ }
1920
+
1921
+ t .Run ("MachineSet" , func (t * testing.T ) {
1922
+ for _ , tc := range testCases {
1923
+ t .Run (tc .description , func (t * testing.T ) {
1924
+ test (
1925
+ t ,
1926
+ & tc ,
1927
+ createMachineSetTestConfig (
1928
+ RandomString (6 ),
1929
+ RandomString (6 ),
1930
+ RandomString (6 ),
1931
+ tc .nodeCount ,
1932
+ annotations ,
1933
+ nil ,
1934
+ ),
1935
+ )
1936
+ })
1937
+ }
1938
+ })
1939
+
1940
+ t .Run ("MachineDeployment" , func (t * testing.T ) {
1941
+ for _ , tc := range testCases {
1942
+ t .Run (tc .description , func (t * testing.T ) {
1943
+ test (
1944
+ t ,
1945
+ & tc ,
1946
+ createMachineDeploymentTestConfig (
1947
+ RandomString (6 ),
1948
+ RandomString (6 ),
1949
+ RandomString (6 ),
1950
+ tc .nodeCount ,
1951
+ annotations ,
1952
+ nil ,
1953
+ ),
1954
+ )
1955
+ })
1956
+ }
1957
+ })
1958
+ }
0 commit comments