Skip to content

Commit

Permalink
refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleyvjoy committed Jun 11, 2024
1 parent ccb21a7 commit 5228aac
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions controllers/acc_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"testing"
"time"

clienttesting "k8s.io/client-go/testing"
"k8s.io/client-go/util/workqueue"

csmv1 "github.com/dell/csm-operator/api/v1"
"github.com/dell/csm-operator/pkg/logger"
deploymentpkg "github.com/dell/csm-operator/pkg/resources/deployment"
"github.com/dell/csm-operator/pkg/utils"
"github.com/dell/csm-operator/tests/shared"
"github.com/dell/csm-operator/tests/shared/clientgoclient"
Expand All @@ -37,6 +39,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"

Expand All @@ -45,6 +48,11 @@ import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

apiv1 "k8s.io/apimachinery/pkg/apis/meta/v1"
configv1 "k8s.io/client-go/applyconfigurations/apps/v1"
corev12 "k8s.io/client-go/applyconfigurations/core/v1"
v1 "k8s.io/client-go/applyconfigurations/meta/v1"
)

var (
Expand Down Expand Up @@ -719,3 +727,101 @@ func (suite *AccControllerTestSuite) debugAccFakeObjects() {
accUnittestLogger.Info("found fake object ", "object", fmt.Sprintf("%#v", o))
}
}

// func TestSyncDeployment(t *testing.T) {
// ctx := context.Background()
// k8sClient := fake.NewSimpleClientset()

// // deploymentParam := &appsv1.Deployment{
// // ObjectMeta: metav1.ObjectMeta{
// // Name: "test-deployment",
// // Namespace: "default",
// // },
// // }

// // applyConfig := deployv1.Deployment(deploymentParam.Name, deploymentParam.Namespace)
// // labels := make(map[string]string, 1)
// labels["*-8-csm"] = "/*-csm"
// deployment := appsv1.DeploymentApplyConfiguration{
// ObjectMetaApplyConfiguration: &v1.ObjectMetaApplyConfiguration{Name: &[]string{"csm"}[0], Namespace: &[]string{"default"}[0]},
// Spec: &appsv1.DeploymentSpecApplyConfiguration{Template: &corev12.PodTemplateSpecApplyConfiguration{
// ObjectMetaApplyConfiguration: &v1.ObjectMetaApplyConfiguration{Labels: labels},
// }},
// }

// //applyConfig.Spec.Template.Labels["csm"] = "test-var"

// // // Initialize the Labels map before setting a value
// // if applyConfig.Spec.Template.Labels == nil {
// // applyConfig.Spec.Template.Labels = make(map[string]string)
// // }
// // applyConfig.Spec.Template.Labels["csm"] = "test"
// // applyConfig = deployv1.Deployment(deploymentParam.Name, deploymentParam.Namespace)
// // applyConfig.WithSpec(deployv1.DeploymentSpec().WithTemplate(deployv1.PodTemplateSpecApplyConfiguration().WithLabels(map[string]string{"csm": "test-var"})))

// err := deployment.SyncDeployment(ctx, *applyConfig, k8sClient, "test-var")
// if err != nil {
// t.Errorf("Unexpected error: %v", err)
// }

// // Simulate an error in Apply
// k8sClient.PrependReactor("patch", "deployments", func(action clienttesting.Action) (bool, runtime.Object, error) {
// return true, nil, fmt.Errorf("forced error")
// })

// err = deployment.SyncDeployment(ctx, *applyConfig, k8sClient, "test-var")
// assert.NotNil(t, err)
// }

// func (suite *AccControllerTestSuite) TestCsmFinalizerErrorScenario() {
// csm := shared.MakeAcc(accName, suite.namespace, accConfigVersion)
// csm.ObjectMeta.Finalizers = []string{"foo"}
// suite.fakeClient.Create(accCtx, &csm)
// sec := shared.MakeSecret(accName+"-creds", suite.namespace, accConfigVersion)
// suite.fakeClient.Create(accCtx, sec)
// reconciler := suite.createAccReconciler()
// updateAccError = true
// _, err := reconciler.Reconcile(accCtx, accReq)
// assert.Error(suite.T(), err)
// updateAccError = false
// suite.deleteAcc(accName)
// }

func TestSyncDeployment(t *testing.T) {
labels := make(map[string]string, 1)
labels["*-8-csm"] = "/*-csm"
deployment := configv1.DeploymentApplyConfiguration{
ObjectMetaApplyConfiguration: &v1.ObjectMetaApplyConfiguration{Name: &[]string{"csm"}[0], Namespace: &[]string{"default"}[0]},
Spec: &configv1.DeploymentSpecApplyConfiguration{Template: &corev12.PodTemplateSpecApplyConfiguration{
ObjectMetaApplyConfiguration: &v1.ObjectMetaApplyConfiguration{Labels: labels},
}},
}
k8sClient := fake.NewSimpleClientset()
//var dep appv2.DeploymentInterface= &Deployment{}

Check failure on line 800 in controllers/acc_controller_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)

csmName := "csm"
var containers = make([]corev1.Container, 0)

Check failure on line 803 in controllers/acc_controller_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
containers = append(containers, corev1.Container{Name: "fake-container", Image: "fake-image"})
create, err := k8sClient.AppsV1().Deployments("default").Create(context.Background(), &appsv1.Deployment{
ObjectMeta: apiv1.ObjectMeta{
Name: csmName,
Namespace: "default",
},
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: apiv1.ObjectMeta{},
Spec: corev1.PodSpec{Containers: containers},
},
},
}, apiv1.CreateOptions{})
assert.NoError(t, err)
assert.NotNil(t, create)

// Simulate an error in Apply
k8sClient.PrependReactor("patch", "deployments", func(action clienttesting.Action) (bool, runtime.Object, error) {

Check warning on line 821 in controllers/acc_controller_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unused-parameter: parameter 'action' seems to be unused, consider removing or renaming it as _ (revive)
return true, nil, fmt.Errorf("fake error")
})

err = deploymentpkg.SyncDeployment(context.Background(), deployment, k8sClient, csmName)
assert.Error(t, err)
}

0 comments on commit 5228aac

Please sign in to comment.