@@ -35,12 +35,11 @@ func newFakeCluster(t *testing.T, cluster *clusterv1alpha1.Cluster) *fakeCluster
3535 conf : cluster ,
3636 },
3737 }
38-
39- c .fakeEnvironment = mocks .NewMockEnvironment (c .ctrl )
4038 c .fakeProvider = mocks .NewMockProvider (c .ctrl )
4139 c .fakeTarmak = mocks .NewMockTarmak (c .ctrl )
4240 c .fakeConfig = mocks .NewMockConfig (c .ctrl )
43- c .Cluster .environment = c .fakeEnvironment
41+ c .fakeEnvironment = mocks .NewMockEnvironment (c .ctrl )
42+ c .environment = c .fakeEnvironment
4443
4544 // setup custom logger
4645 logger := logrus .New ()
@@ -65,13 +64,26 @@ func newFakeCluster(t *testing.T, cluster *clusterv1alpha1.Cluster) *fakeCluster
6564 return c
6665}
6766
67+ func newFakeHub (t * testing.T ) * fakeCluster {
68+ return & fakeCluster {
69+ ctrl : gomock .NewController (t ),
70+ Cluster : & Cluster {
71+ conf : & clusterv1alpha1.Cluster {
72+ Type : clusterv1alpha1 .ClusterTypeHub ,
73+ },
74+ },
75+ }
76+ }
77+
6878func TestCluster_NewMinimalClusterMulti (t * testing.T ) {
6979 clusterConfig := config .NewClusterMulti ("multi" , "cluster" )
7080 config .ApplyDefaults (clusterConfig )
7181 clusterConfig .Location = "my-region"
7282 c := newFakeCluster (t , nil )
7383 defer c .Finish ()
7484
85+ c .fakeEnvironment .EXPECT ().Hub ().AnyTimes ().Return (newFakeHub (t ))
86+
7587 // fake two clusters
7688 c .fakeEnvironment .EXPECT ().Name ().Return ("multi" ).AnyTimes ()
7789 c .fakeConfig .EXPECT ().Clusters ("multi" ).Return ([]* clusterv1alpha1.Cluster {
@@ -225,6 +237,8 @@ func TestCluster_ValidateClusterInstancePoolTypesHub(t *testing.T) {
225237 c := newFakeCluster (t , nil )
226238 defer c .Finish ()
227239
240+ c .fakeEnvironment .EXPECT ().Hub ().AnyTimes ().Return (newFakeHub (t ))
241+
228242 var err error
229243 c .Cluster , err = NewFromConfig (c .fakeEnvironment , clusterConfig )
230244 if err != nil {
@@ -261,6 +275,8 @@ func TestCluster_ValidateClusterInstancePoolsMulti(t *testing.T) {
261275 c := newFakeCluster (t , nil )
262276 defer c .Finish ()
263277
278+ c .fakeEnvironment .EXPECT ().Hub ().AnyTimes ().Return (newFakeHub (t ))
279+
264280 var err error
265281 c .Cluster , err = NewFromConfig (c .fakeEnvironment , clusterConfig )
266282 if err != nil {
@@ -408,6 +424,87 @@ func tryInstancePoolCount(c *fakeCluster, singleTypes, multiTypes []string, t *t
408424 c .conf = baseConfig .DeepCopy ()
409425}
410426
427+ func TestClusterValidateSubnetsIgnore (t * testing.T ) {
428+ clusterConfig := config .NewClusterMulti ("multi" , "cluster" )
429+ config .ApplyDefaults (clusterConfig )
430+ clusterConfig .Location = "my-region"
431+ c := newFakeCluster (t , & clusterv1alpha1.Cluster {
432+ Type : clusterv1alpha1 .ClusterTypeClusterSingle ,
433+ })
434+ defer c .Finish ()
435+
436+ if err := c .validateSubnets (); err != nil {
437+ t .Errorf ("unexpected error: %v" , err )
438+ }
439+
440+ c = newFakeCluster (t , & clusterv1alpha1.Cluster {
441+ Type : clusterv1alpha1 .ClusterTypeHub ,
442+ })
443+ defer c .Finish ()
444+
445+ if err := c .validateSubnets (); err != nil {
446+ t .Errorf ("unexpected error: %v" , err )
447+ }
448+ }
449+
450+ func TestClusterValidateSubnetsMulti (t * testing.T ) {
451+ clusterConfig := config .NewClusterMulti ("multi" , "cluster" )
452+ config .ApplyDefaults (clusterConfig )
453+ clusterConfig .Location = "my-region"
454+ c := newFakeCluster (t , & clusterv1alpha1.Cluster {
455+ Type : clusterv1alpha1 .ClusterTypeClusterMulti ,
456+ })
457+ defer c .Finish ()
458+
459+ superZones := []string {
460+ "zone-1" ,
461+ "zone-2" ,
462+ "zone-3" ,
463+ }
464+
465+ subZones := []string {
466+ "zone-1" ,
467+ "zone-2" ,
468+ }
469+
470+ hub := newFakeCluster (t , & clusterv1alpha1.Cluster {
471+ Type : clusterv1alpha1 .ClusterTypeHub ,
472+ InstancePools : instancePoolsWithZones (superZones ),
473+ })
474+ c .fakeEnvironment .EXPECT ().Hub ().Times (4 ).Return (hub )
475+ c .conf .InstancePools = instancePoolsWithZones (subZones )
476+ if err := c .validateSubnets (); err != nil {
477+ t .Errorf ("unexpected error: %v" , err )
478+ }
479+
480+ c .conf .InstancePools = instancePoolsWithZones (superZones )
481+ if err := c .validateSubnets (); err != nil {
482+ t .Errorf ("unexpected error: %v" , err )
483+ }
484+
485+ hub = newFakeCluster (t , & clusterv1alpha1.Cluster {
486+ Type : clusterv1alpha1 .ClusterTypeHub ,
487+ InstancePools : instancePoolsWithZones (subZones ),
488+ })
489+ c .fakeEnvironment .EXPECT ().Hub ().Times (2 ).Return (hub )
490+ if err := c .validateSubnets (); err == nil {
491+ t .Errorf ("expected error due to hub not including zone, got=none" )
492+ }
493+
494+ }
495+
496+ func instancePoolsWithZones (zones []string ) []clusterv1alpha1.InstancePool {
497+ pool := clusterv1alpha1.InstancePool {}
498+
499+ for _ , z := range zones {
500+ pool .Subnets = append (pool .Subnets , & clusterv1alpha1.Subnet {
501+ Zone : z ,
502+ })
503+ }
504+
505+ return []clusterv1alpha1.InstancePool {pool }
506+ }
507+
411508/*
412509func testDefaultClusterConfig() *config.Cluster {
413510 return &config.Cluster{
0 commit comments