Skip to content

Commit 8f04b85

Browse files
authored
Merge pull request #3073 from Ankitasw/awscluster-controller-coverage
Increase unit test coverage for AWSCluster controller
2 parents d4e16e4 + d0d508b commit 8f04b85

10 files changed

+1012
-12
lines changed

controllers/awscluster_controller.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
4242
"sigs.k8s.io/cluster-api-provider-aws/feature"
4343
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope"
44+
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services"
4445
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/ec2"
4546
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/elb"
4647
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/services/instancestate"
@@ -68,9 +69,45 @@ var (
6869
// AWSClusterReconciler reconciles a AwsCluster object.
6970
type AWSClusterReconciler struct {
7071
client.Client
71-
Recorder record.EventRecorder
72-
Endpoints []scope.ServiceEndpoint
73-
WatchFilterValue string
72+
Recorder record.EventRecorder
73+
ec2ServiceFactory func(scope.EC2Scope) services.EC2MachineInterface
74+
networkServiceFactory func(scope.ClusterScope) services.NetworkInterface
75+
elbServiceFactory func(scope.ELBScope) services.ELBInterface
76+
securityGroupFactory func(scope.ClusterScope) services.SecurityGroupInterface
77+
Endpoints []scope.ServiceEndpoint
78+
WatchFilterValue string
79+
}
80+
81+
// getEC2Service factory func is added for testing purpose so that we can inject mocked EC2Service to the AWSClusterReconciler.
82+
func (r *AWSClusterReconciler) getEC2Service(scope scope.EC2Scope) services.EC2MachineInterface {
83+
if r.ec2ServiceFactory != nil {
84+
return r.ec2ServiceFactory(scope)
85+
}
86+
return ec2.NewService(scope)
87+
}
88+
89+
// getELBService factory func is added for testing purpose so that we can inject mocked ELBService to the AWSClusterReconciler.
90+
func (r *AWSClusterReconciler) getELBService(scope scope.ELBScope) services.ELBInterface {
91+
if r.elbServiceFactory != nil {
92+
return r.elbServiceFactory(scope)
93+
}
94+
return elb.NewService(scope)
95+
}
96+
97+
// getNetworkService factory func is added for testing purpose so that we can inject mocked NetworkService to the AWSClusterReconciler.
98+
func (r *AWSClusterReconciler) getNetworkService(scope scope.ClusterScope) services.NetworkInterface {
99+
if r.networkServiceFactory != nil {
100+
return r.networkServiceFactory(scope)
101+
}
102+
return network.NewService(&scope)
103+
}
104+
105+
// getSecurityGroupService factory func is added for testing purpose so that we can inject mocked SecurityGroupService to the AWSClusterReconciler.
106+
func (r *AWSClusterReconciler) getSecurityGroupService(scope scope.ClusterScope) services.SecurityGroupInterface {
107+
if r.securityGroupFactory != nil {
108+
return r.securityGroupFactory(scope)
109+
}
110+
return securitygroup.NewService(&scope, awsSecurityGroupRoles)
74111
}
75112

76113
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsclusters,verbs=get;list;watch;create;update;patch;delete
@@ -159,10 +196,10 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
159196
func (r *AWSClusterReconciler) reconcileDelete(clusterScope *scope.ClusterScope) (reconcile.Result, error) {
160197
clusterScope.Info("Reconciling AWSCluster delete")
161198

162-
ec2svc := ec2.NewService(clusterScope)
163-
elbsvc := elb.NewService(clusterScope)
164-
networkSvc := network.NewService(clusterScope)
165-
sgService := securitygroup.NewService(clusterScope, awsSecurityGroupRoles)
199+
ec2svc := r.getEC2Service(clusterScope)
200+
elbsvc := r.getELBService(clusterScope)
201+
networkSvc := r.getNetworkService(*clusterScope)
202+
sgService := r.getSecurityGroupService(*clusterScope)
166203

167204
if feature.Gates.Enabled(feature.EventBridgeInstanceState) {
168205
instancestateSvc := instancestate.NewService(clusterScope)
@@ -210,10 +247,10 @@ func (r *AWSClusterReconciler) reconcileNormal(clusterScope *scope.ClusterScope)
210247
return reconcile.Result{}, err
211248
}
212249

213-
ec2Service := ec2.NewService(clusterScope)
214-
elbService := elb.NewService(clusterScope)
215-
networkSvc := network.NewService(clusterScope)
216-
sgService := securitygroup.NewService(clusterScope, awsSecurityGroupRoles)
250+
ec2Service := r.getEC2Service(clusterScope)
251+
elbService := r.getELBService(clusterScope)
252+
networkSvc := r.getNetworkService(*clusterScope)
253+
sgService := r.getSecurityGroupService(*clusterScope)
217254

218255
if err := networkSvc.ReconcileNetwork(); err != nil {
219256
clusterScope.Error(err, "failed to reconcile network")

controllers/awscluster_controller_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
1716
package controllers
1817

1918
import (

0 commit comments

Comments
 (0)