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