@@ -17,6 +17,7 @@ limitations under the License.
17
17
package controllers
18
18
19
19
import (
20
+ "context"
20
21
"testing"
21
22
"time"
22
23
@@ -1853,6 +1854,115 @@ func TestNodeToMachine(t *testing.T) {
1853
1854
}
1854
1855
}
1855
1856
1857
+ // TODO: We should add a test for the --wait-for-node-deletion flag. This requires error injection
1858
+ // in the fakeclient, which isn't supported yet. There is a very recent issue to track this:
1859
+ // https://github.com/kubernetes-sigs/controller-runtime/issues/1702
1860
+ func TestNodeDeletion (t * testing.T ) {
1861
+ g := NewWithT (t )
1862
+
1863
+ time := metav1 .Now ()
1864
+
1865
+ testCluster := clusterv1.Cluster {
1866
+ ObjectMeta : metav1.ObjectMeta {
1867
+ Name : "test-cluster" ,
1868
+ Namespace : metav1 .NamespaceDefault ,
1869
+ },
1870
+ }
1871
+
1872
+ node := & corev1.Node {
1873
+ ObjectMeta : metav1.ObjectMeta {
1874
+ Name : "test" ,
1875
+ },
1876
+ Spec : corev1.NodeSpec {ProviderID : "test://id-1" },
1877
+ }
1878
+
1879
+ testMachine := clusterv1.Machine {
1880
+ ObjectMeta : metav1.ObjectMeta {
1881
+ Name : "test" ,
1882
+ Namespace : metav1 .NamespaceDefault ,
1883
+ Labels : map [string ]string {
1884
+ clusterv1 .MachineControlPlaneLabelName : "" ,
1885
+ },
1886
+ Annotations : map [string ]string {
1887
+ "machine.cluster.x-k8s.io/exclude-node-draining" : "" ,
1888
+ },
1889
+ Finalizers : []string {clusterv1 .MachineFinalizer },
1890
+ DeletionTimestamp : & time ,
1891
+ },
1892
+ Spec : clusterv1.MachineSpec {
1893
+ ClusterName : "test-cluster" ,
1894
+ InfrastructureRef : corev1.ObjectReference {
1895
+ APIVersion : "infrastructure.cluster.x-k8s.io/v1beta1" ,
1896
+ Kind : "GenericInfrastructureMachine" ,
1897
+ Name : "infra-config1" ,
1898
+ },
1899
+ Bootstrap : clusterv1.Bootstrap {DataSecretName : pointer .StringPtr ("data" )},
1900
+ },
1901
+ Status : clusterv1.MachineStatus {
1902
+ NodeRef : & corev1.ObjectReference {
1903
+ Name : "test" ,
1904
+ },
1905
+ },
1906
+ }
1907
+
1908
+ cpmachine1 := & clusterv1.Machine {
1909
+ ObjectMeta : metav1.ObjectMeta {
1910
+ Name : "cp1" ,
1911
+ Namespace : metav1 .NamespaceDefault ,
1912
+ Labels : map [string ]string {
1913
+ clusterv1 .ClusterLabelName : "test-cluster" ,
1914
+ clusterv1 .MachineControlPlaneLabelName : "" ,
1915
+ },
1916
+ Finalizers : []string {clusterv1 .MachineFinalizer },
1917
+ },
1918
+ Spec : clusterv1.MachineSpec {
1919
+ ClusterName : "test-cluster" ,
1920
+ InfrastructureRef : corev1.ObjectReference {},
1921
+ Bootstrap : clusterv1.Bootstrap {DataSecretName : pointer .StringPtr ("data" )},
1922
+ },
1923
+ Status : clusterv1.MachineStatus {
1924
+ NodeRef : & corev1.ObjectReference {
1925
+ Name : "cp1" ,
1926
+ },
1927
+ },
1928
+ }
1929
+
1930
+ testCases := []struct {
1931
+ waitForDeletion bool
1932
+ resultErr bool
1933
+ }{
1934
+ {
1935
+ waitForDeletion : false ,
1936
+ resultErr : false ,
1937
+ },
1938
+ }
1939
+
1940
+ for _ , tc := range testCases {
1941
+ m := testMachine .DeepCopy ()
1942
+
1943
+ fc := fake .NewClientBuilder ().
1944
+ WithObjects (node , m , cpmachine1 ).
1945
+ Build ()
1946
+ tracker := remote .NewTestClusterCacheTracker (log.NullLogger {}, fc , fakeScheme , client .ObjectKeyFromObject (& testCluster ))
1947
+
1948
+ r := & MachineReconciler {
1949
+ WaitForNodeDeletion : tc .waitForDeletion ,
1950
+ Client : fc ,
1951
+ Tracker : tracker ,
1952
+ }
1953
+
1954
+ _ , err := r .reconcileDelete (context .Background (), & testCluster , m )
1955
+
1956
+ if tc .resultErr {
1957
+ g .Expect (err ).To (HaveOccurred ())
1958
+ } else {
1959
+ g .Expect (err ).NotTo (HaveOccurred ())
1960
+ n := & corev1.Node {}
1961
+ g .Expect (fc .Get (context .Background (), client .ObjectKeyFromObject (node ), n )).NotTo (Succeed ())
1962
+ }
1963
+ }
1964
+ }
1965
+
1856
1966
// adds a condition list to an external object.
1857
1967
func addConditionsToExternal (u * unstructured.Unstructured , newConditions clusterv1.Conditions ) {
1858
1968
existingConditions := clusterv1.Conditions {}
0 commit comments