Skip to content

Commit

Permalink
Merge pull request #225 from nunnatsa/improve-linter
Browse files Browse the repository at this point in the history
Add the ginkgolinter and fix findings
  • Loading branch information
k8s-ci-robot authored Feb 15, 2023
2 parents 54ac76b + c5ff039 commit 1c6ec37
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 192 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with: # TODO: remove this when the deprecated function will be removed (issue #85)
args: --exclude SA1019 --timeout=5m
args: --exclude SA1019 --timeout=5m -E ginkgolinter

check-gen:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ goimports:
linter:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# todo remove the exclude parameter when issue #85 is resolved
golangci-lint run --exclude SA1019
golangci-lint run --timeout=5m -E ginkgolinter --exclude SA1019

.PHONY: sanity
sanity: linter goimports test
3 changes: 1 addition & 2 deletions api/v1alpha1/kubevirtmachinetemplate_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ var _ = Describe("Template Validation", func() {
})

It("should not return error", func() {
err := tests.newTemplate.ValidateUpdate(tests.oldTemplate)
Ω(err).ShouldNot(HaveOccurred())
Ω(tests.newTemplate.ValidateUpdate(tests.oldTemplate)).Should(Succeed())
})
})
Context("Template comparison with errors", func() {
Expand Down
26 changes: 8 additions & 18 deletions clusterkubevirtadm/cmd/credentials/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ var _ = Describe("test credentials common function", func() {
client := fake.NewSimpleClientset()
cmdCtx.Client = client

err := ensureNamespace(context.Background(), cmdCtx, clientOperationCreate)
Expect(err).ToNot(HaveOccurred())
Expect(ensureNamespace(context.Background(), cmdCtx, clientOperationCreate)).To(Succeed())

ns, err := client.CoreV1().Namespaces().Get(context.Background(), namespaceName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expand All @@ -44,8 +43,7 @@ var _ = Describe("test credentials common function", func() {
client := fake.NewSimpleClientset(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespaceName}})
cmdCtx.Client = client

err := ensureNamespace(context.Background(), cmdCtx, clientOperationApply)
Expect(err).ToNot(HaveOccurred())
Expect(ensureNamespace(context.Background(), cmdCtx, clientOperationApply)).To(Succeed())

ns, err := client.CoreV1().Namespaces().Get(context.Background(), namespaceName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -73,9 +71,7 @@ var _ = Describe("test credentials common function", func() {
client := fake.NewSimpleClientset(&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespaceName}})
cmdCtx.Client = client

err := ensureServiceAccount(context.Background(), cmdCtx)

Expect(err).ToNot(HaveOccurred())
Expect(ensureServiceAccount(context.Background(), cmdCtx)).To(Succeed())
})

It("should do nothing if the serviceAccount is already exist", func() {
Expand All @@ -95,9 +91,7 @@ var _ = Describe("test credentials common function", func() {
)
cmdCtx.Client = client

err := ensureServiceAccount(context.Background(), cmdCtx)

Expect(err).ToNot(HaveOccurred())
Expect(ensureServiceAccount(context.Background(), cmdCtx)).To(Succeed())
})
})

Expand All @@ -115,8 +109,7 @@ var _ = Describe("test credentials common function", func() {
)
cmdCtx.Client = client

err := createOrUpdateRole(context.Background(), cmdCtx, clientOperationCreate)
Expect(err).ToNot(HaveOccurred())
Expect(createOrUpdateRole(context.Background(), cmdCtx, clientOperationCreate)).To(Succeed())

roles, err := client.RbacV1().Roles(namespaceName).List(context.Background(), metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -150,8 +143,7 @@ var _ = Describe("test credentials common function", func() {
)
cmdCtx.Client = client

err := createOrUpdateRole(context.Background(), cmdCtx, clientOperationCreate)
Expect(err).To(HaveOccurred())
Expect(createOrUpdateRole(context.Background(), cmdCtx, clientOperationCreate)).ToNot(Succeed())
})

It("should update the role if it is already exist, with different values", func() {
Expand All @@ -172,8 +164,7 @@ var _ = Describe("test credentials common function", func() {
)
cmdCtx.Client = client

err := createOrUpdateRole(context.Background(), cmdCtx, clientOperationApply)
Expect(err).ToNot(HaveOccurred())
Expect(createOrUpdateRole(context.Background(), cmdCtx, clientOperationApply)).To(Succeed())

roles, err := client.RbacV1().Roles(namespaceName).List(context.Background(), metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -206,8 +197,7 @@ var _ = Describe("test credentials common function", func() {
)
cmdCtx.Client = client

err := ensureRoleBinding(context.Background(), cmdCtx)
Expect(err).ToNot(HaveOccurred())
Expect(ensureRoleBinding(context.Background(), cmdCtx)).To(Succeed())

roleBindings, err := client.RbacV1().RoleBindings(namespaceName).List(context.Background(), metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
Expand Down
64 changes: 32 additions & 32 deletions controllers/kubevirtmachine_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ var _ = Describe("reconcile a kubevirt machine", func() {
// should expect VM to be created with expected name
vm := &kubevirtv1.VirtualMachine{}
vmKey := client.ObjectKey{Namespace: kubevirtMachine.Namespace, Name: kubevirtMachine.Name}
err = fakeClient.Get(gocontext.Background(), vmKey, vm)
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(gocontext.Background(), vmKey, vm)).To(Succeed())

// Should expect kubevirt machine is still not ready
Expect(machineContext.KubevirtMachine.Status.Ready).To(BeFalse())
Expand All @@ -369,8 +368,9 @@ var _ = Describe("reconcile a kubevirt machine", func() {
machineBootstrapSecretReferenceName := machineContext.Machine.Spec.Bootstrap.DataSecretName
machineBootstrapSecretReferenceKey := client.ObjectKey{Namespace: machineContext.Machine.GetNamespace(), Name: *machineBootstrapSecretReferenceName + "-userdata"}
bootstrapDataSecret := &corev1.Secret{}
err = fakeClient.Get(gocontext.Background(), machineBootstrapSecretReferenceKey, bootstrapDataSecret)
Expect(err).NotTo(HaveOccurred())
Expect(
fakeClient.Get(gocontext.Background(), machineBootstrapSecretReferenceKey, bootstrapDataSecret),
).To(Succeed())
Expect(bootstrapDataSecret.Data).To(HaveKeyWithValue("userdata", []byte("shell-script")))
Expect(bootstrapDataSecret.Labels).To(HaveLen(1))
Expect(bootstrapDataSecret.Labels).To(HaveKeyWithValue("hello", "world"))
Expand Down Expand Up @@ -474,17 +474,17 @@ var _ = Describe("reconcile a kubevirt machine", func() {
// should expect VM to be created with expected name
vm := &kubevirtv1.VirtualMachine{}
vmKey := client.ObjectKey{Namespace: kubevirtMachine.Namespace, Name: kubevirtMachine.Name}
err = fakeClient.Get(gocontext.Background(), vmKey, vm)
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(gocontext.Background(), vmKey, vm)).To(Succeed())

machineBootstrapSecretReferenceName := machineContext.Machine.Spec.Bootstrap.DataSecretName
machineBootstrapSecretReferenceKey := client.ObjectKey{Namespace: machineContext.Machine.GetNamespace(), Name: *machineBootstrapSecretReferenceName + "-userdata"}
infraClusterClient, _, err := infraClusterMock.GenerateInfraClusterClient(kubevirtMachine.Spec.InfraClusterSecretRef, kubevirtMachine.Namespace, machineContext.Context)
Expect(err).NotTo(HaveOccurred())

bootstrapDataSecret := &corev1.Secret{}
err = infraClusterClient.Get(gocontext.Background(), machineBootstrapSecretReferenceKey, bootstrapDataSecret)
Expect(err).NotTo(HaveOccurred())
Expect(
infraClusterClient.Get(gocontext.Background(), machineBootstrapSecretReferenceKey, bootstrapDataSecret),
).To(Succeed())

Expect(bootstrapUserDataSecret.Data["userdata"]).To(Equal([]byte("shell-script")))
})
Expand Down Expand Up @@ -518,7 +518,7 @@ var _ = Describe("reconcile a kubevirt machine", func() {
err = fakeClient.Get(gocontext.Background(), vmKey, vm)
Expect(err).To(HaveOccurred())

Expect(machineContext.Machine.ObjectMeta.Finalizers).To(HaveLen(0))
Expect(machineContext.Machine.ObjectMeta.Finalizers).To(BeEmpty())
})

It("should create KubeVirt VM with externally managed cluster and no ssh key", func() {
Expand Down Expand Up @@ -549,8 +549,7 @@ var _ = Describe("reconcile a kubevirt machine", func() {
// should expect VM to be created with expected name
vm := &kubevirtv1.VirtualMachine{}
vmKey := client.ObjectKey{Namespace: kubevirtMachine.Namespace, Name: kubevirtMachine.Name}
err = fakeClient.Get(gocontext.Background(), vmKey, vm)
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(gocontext.Background(), vmKey, vm)).To(Succeed())

// Should expect kubevirt machine is still not ready
Expect(machineContext.KubevirtMachine.Status.Ready).To(BeFalse())
Expand All @@ -560,8 +559,9 @@ var _ = Describe("reconcile a kubevirt machine", func() {
machineBootstrapSecretReferenceName := machineContext.Machine.Spec.Bootstrap.DataSecretName
machineBootstrapSecretReferenceKey := client.ObjectKey{Namespace: kubevirtMachine.Namespace, Name: *machineBootstrapSecretReferenceName + "-userdata"}
bootstrapDataSecret := &corev1.Secret{}
err = fakeClient.Get(gocontext.Background(), machineBootstrapSecretReferenceKey, bootstrapDataSecret)
Expect(err).NotTo(HaveOccurred())
Expect(
fakeClient.Get(gocontext.Background(), machineBootstrapSecretReferenceKey, bootstrapDataSecret),
).To(Succeed())
Expect(bootstrapDataSecret.Data).To(HaveKeyWithValue("userdata", []byte("shell-script")))
Expect(bootstrapDataSecret.Labels).To(HaveLen(1))
Expect(bootstrapDataSecret.Labels).To(HaveKeyWithValue("hello", "world"))
Expand Down Expand Up @@ -595,8 +595,7 @@ var _ = Describe("reconcile a kubevirt machine", func() {
// should expect VM to be created with expected name
vm := &kubevirtv1.VirtualMachine{}
vmKey := client.ObjectKey{Namespace: customNamespace, Name: kubevirtMachine.Name}
err = fakeClient.Get(gocontext.Background(), vmKey, vm)
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(gocontext.Background(), vmKey, vm)).To(Succeed())

// Should expect kubevirt machine is still not ready
Expect(machineContext.KubevirtMachine.Status.Ready).To(BeFalse())
Expand All @@ -606,8 +605,7 @@ var _ = Describe("reconcile a kubevirt machine", func() {
machineBootstrapSecretReferenceName := machineContext.Machine.Spec.Bootstrap.DataSecretName
machineBootstrapSecretReferenceKey := client.ObjectKey{Namespace: customNamespace, Name: *machineBootstrapSecretReferenceName + "-userdata"}
bootstrapDataSecret := &corev1.Secret{}
err = fakeClient.Get(gocontext.Background(), machineBootstrapSecretReferenceKey, bootstrapDataSecret)
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(gocontext.Background(), machineBootstrapSecretReferenceKey, bootstrapDataSecret)).To(Succeed())
Expect(bootstrapDataSecret.Data).To(HaveKeyWithValue("userdata", []byte("shell-script")))
Expect(bootstrapDataSecret.Labels).To(HaveLen(1))
Expect(bootstrapDataSecret.Labels).To(HaveKeyWithValue("hello", "world"))
Expand Down Expand Up @@ -654,8 +652,7 @@ var _ = Describe("reconcile a kubevirt machine", func() {
// should expect VM to be created with expected name
vm := &kubevirtv1.VirtualMachine{}
vmKey := client.ObjectKey{Namespace: kubevirtMachine.Namespace, Name: kubevirtMachine.Name}
err = fakeClient.Get(gocontext.Background(), vmKey, vm)
Expect(err).NotTo(HaveOccurred())
Expect(fakeClient.Get(gocontext.Background(), vmKey, vm)).To(Succeed())

Expect(machineContext.KubevirtMachine.Status.Ready).To(BeTrue())
Expect(*machineContext.KubevirtMachine.Spec.ProviderID).To(Equal("kubevirt://" + kubevirtMachineName))
Expand Down Expand Up @@ -766,8 +763,9 @@ var _ = Describe("reconcile a kubevirt machine", func() {
Expect(err).ShouldNot(HaveOccurred())

newKubevirtMachine := &infrav1.KubevirtMachine{}
err = kubevirtMachineReconciler.Client.Get(machineContext, kubevirtMachineKey, newKubevirtMachine)
Expect(err).ShouldNot(HaveOccurred())
Expect(
kubevirtMachineReconciler.Client.Get(machineContext, kubevirtMachineKey, newKubevirtMachine),
).To(Succeed())

conditions := newKubevirtMachine.GetConditions()
Expect(conditions[1].Type).To(Equal(infrav1.VMProvisionedCondition))
Expand Down Expand Up @@ -795,8 +793,7 @@ var _ = Describe("reconcile a kubevirt machine", func() {
Expect(err).ShouldNot(HaveOccurred())

newKubevirtMachine := &infrav1.KubevirtMachine{}
err = kubevirtMachineReconciler.Client.Get(machineContext, kubevirtMachineKey, newKubevirtMachine)
Expect(err).ShouldNot(HaveOccurred())
Expect(kubevirtMachineReconciler.Client.Get(machineContext, kubevirtMachineKey, newKubevirtMachine)).To(Succeed())

conditions := newKubevirtMachine.GetConditions()
Expect(conditions[1].Type).To(Equal(infrav1.VMProvisionedCondition))
Expand Down Expand Up @@ -1121,10 +1118,11 @@ var _ = Describe("updateNodeProviderID", func() {
Expect(out).To(Equal(ctrl.Result{}))
workloadClusterNode := &corev1.Node{}
workloadClusterNodeKey := client.ObjectKey{Namespace: kubevirtMachine.Namespace, Name: kubevirtMachine.Name}
err = fakeWorkloadClusterClient.Get(machineContext, workloadClusterNodeKey, workloadClusterNode)
Expect(err).NotTo(HaveOccurred())
Expect(
fakeWorkloadClusterClient.Get(machineContext, workloadClusterNodeKey, workloadClusterNode),
).To(Succeed())
Expect(workloadClusterNode.Spec.ProviderID).To(Equal(expectedProviderId))
Expect(kubevirtMachine.Status.NodeUpdated).To(Equal(true))
Expect(kubevirtMachine.Status.NodeUpdated).To(BeTrue())
})

It("GenerateWorkloadClusterClient failure", func() {
Expand All @@ -1136,10 +1134,11 @@ var _ = Describe("updateNodeProviderID", func() {
Expect(out).To(Equal(ctrl.Result{RequeueAfter: 10 * time.Second}))
workloadClusterNode := &corev1.Node{}
workloadClusterNodeKey := client.ObjectKey{Namespace: kubevirtMachine.Namespace, Name: kubevirtMachine.Name}
err = fakeWorkloadClusterClient.Get(machineContext, workloadClusterNodeKey, workloadClusterNode)
Expect(err).NotTo(HaveOccurred())
Expect(
fakeWorkloadClusterClient.Get(machineContext, workloadClusterNodeKey, workloadClusterNode),
).To(Succeed())
Expect(workloadClusterNode.Spec.ProviderID).NotTo(Equal(expectedProviderId))
Expect(kubevirtMachine.Status.NodeUpdated).To(Equal(false))
Expect(kubevirtMachine.Status.NodeUpdated).To(BeFalse())
})

It("Node doesn't exist", func() {
Expand All @@ -1151,9 +1150,10 @@ var _ = Describe("updateNodeProviderID", func() {
Expect(out).To(Equal(ctrl.Result{RequeueAfter: 10 * time.Second}))
workloadClusterNode := &corev1.Node{}
workloadClusterNodeKey := client.ObjectKey{Namespace: kubevirtMachine.Namespace, Name: kubevirtMachine.Name}
err = fakeWorkloadClusterClient.Get(machineContext, workloadClusterNodeKey, workloadClusterNode)
Expect(err).NotTo(HaveOccurred())
Expect(
fakeWorkloadClusterClient.Get(machineContext, workloadClusterNodeKey, workloadClusterNode),
).To(Succeed())
Expect(workloadClusterNode.Spec.ProviderID).NotTo(Equal(expectedProviderId))
Expect(kubevirtMachine.Status.NodeUpdated).To(Equal(false))
Expect(kubevirtMachine.Status.NodeUpdated).To(BeFalse())
})
})
5 changes: 3 additions & 2 deletions controllers/vmi_eviction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ var _ = Describe("Test VMI Controller", func() {

// check that the VMI was not deleted
readVMI := &kubevirtv1.VirtualMachineInstance{}
err = fakeClient.Get(gocontext.TODO(), client.ObjectKey{Namespace: clusterNamespace, Name: nodeName}, readVMI)
Expect(err).ShouldNot(HaveOccurred())
Expect(
fakeClient.Get(gocontext.TODO(), client.ObjectKey{Namespace: clusterNamespace, Name: nodeName}, readVMI),
).To(Succeed())
Expect(readVMI).ToNot(BeNil())
})
})
Expand Down
Loading

0 comments on commit 1c6ec37

Please sign in to comment.