@@ -43,7 +43,8 @@ func TestMachineDeploymentDefault(t *testing.T) {
4343 ClusterName : "test-cluster" ,
4444 Template : clusterv1.MachineTemplateSpec {
4545 Spec : clusterv1.MachineSpec {
46- Version : "1.19.10" ,
46+ ClusterName : "test-cluster" ,
47+ Version : "1.19.10" ,
4748 Bootstrap : clusterv1.Bootstrap {
4849 DataSecretName : ptr .To ("data-secret" ),
4950 },
@@ -154,7 +155,8 @@ func TestMachineDeploymentReferenceDefault(t *testing.T) {
154155 ClusterName : "test-cluster" ,
155156 Template : clusterv1.MachineTemplateSpec {
156157 Spec : clusterv1.MachineSpec {
157- Version : "1.19.10" ,
158+ ClusterName : "test-cluster" ,
159+ Version : "1.19.10" ,
158160 Bootstrap : clusterv1.Bootstrap {
159161 ConfigRef : & clusterv1.ContractVersionedObjectReference {},
160162 },
@@ -690,6 +692,7 @@ func TestMachineDeploymentClusterNameImmutable(t *testing.T) {
690692 ClusterName : tt .newClusterName ,
691693 Template : clusterv1.MachineTemplateSpec {
692694 Spec : clusterv1.MachineSpec {
695+ ClusterName : tt .newClusterName ,
693696 Bootstrap : clusterv1.Bootstrap {
694697 DataSecretName : ptr .To ("data-secret" ),
695698 },
@@ -703,6 +706,7 @@ func TestMachineDeploymentClusterNameImmutable(t *testing.T) {
703706 ClusterName : tt .oldClusterName ,
704707 Template : clusterv1.MachineTemplateSpec {
705708 Spec : clusterv1.MachineSpec {
709+ ClusterName : tt .oldClusterName ,
706710 Bootstrap : clusterv1.Bootstrap {
707711 DataSecretName : ptr .To ("data-secret" ),
708712 },
@@ -728,6 +732,56 @@ func TestMachineDeploymentClusterNameImmutable(t *testing.T) {
728732 }
729733}
730734
735+ func TestMachineDeploymentClusterNamesEqual (t * testing.T ) {
736+ tests := []struct {
737+ name string
738+ specClusterName string
739+ specTemplateSpecClusterName string
740+ expectErr bool
741+ }{
742+ {
743+ name : "clusterName fields are set to the same value" ,
744+ specClusterName : "foo" ,
745+ specTemplateSpecClusterName : "foo" ,
746+ expectErr : false ,
747+ },
748+ {
749+ name : "clusterName fields are set to different values" ,
750+ specClusterName : "foo" ,
751+ specTemplateSpecClusterName : "bar" ,
752+ expectErr : true ,
753+ },
754+ }
755+
756+ for _ , tt := range tests {
757+ t .Run (tt .name , func (t * testing.T ) {
758+ g := NewWithT (t )
759+
760+ ms := & clusterv1.MachineDeployment {
761+ Spec : clusterv1.MachineDeploymentSpec {
762+ ClusterName : tt .specClusterName ,
763+ Template : clusterv1.MachineTemplateSpec {
764+ Spec : clusterv1.MachineSpec {
765+ ClusterName : tt .specTemplateSpecClusterName ,
766+ Bootstrap : clusterv1.Bootstrap {
767+ DataSecretName : ptr .To ("data-secret" ),
768+ },
769+ },
770+ },
771+ },
772+ }
773+
774+ warnings , err := (& MachineDeployment {}).ValidateCreate (ctx , ms )
775+ if tt .expectErr {
776+ g .Expect (err ).To (HaveOccurred ())
777+ } else {
778+ g .Expect (err ).ToNot (HaveOccurred ())
779+ }
780+ g .Expect (warnings ).To (BeEmpty ())
781+ })
782+ }
783+ }
784+
731785func TestMachineDeploymentTemplateMetadataValidation (t * testing.T ) {
732786 tests := []struct {
733787 name string
0 commit comments