containerized-data-importer/pkg/controller/transfer/controller_test.go
Edu Gómez Escandell 0e750262a3
Enable autoformatting linters (#3179)
* Enable gofmt linter

From the docs:

> Gofmt checks whether code was gofmt-ed. By default this tool runs with
> -s option to check for code simplification.

https://golangci-lint.run/usage/linters/#gofmt

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Run gomft on the project

Ran this command after adding the gofmt linter:

	golangci-lint run ./... --fix

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Enable whitespace linter

From the docs:
> Whitespace is a linter that checks for unnecessary newlines at the
> start and end of functions, if, for, etc.

https://golangci-lint.run/usage/linters/#whitespace

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Run whitespace on the project

Ran this command after adding the whitespace linter:

	golangci-lint run ./... --fix

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Enable GCI linter

Per the docs:

> Gci controls Go package import order and makes it always deterministic.

https://golangci-lint.run/usage/linters/#gci

NOTE: I noticed that many files separate their imports in a particular
way, so I set the linter to enforce this standard.

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

* Run GCI on the project

Ran this command after adding the GCI linter:

	golangci-lint run ./... --fix

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>

---------

Signed-off-by: Edu Gómez Escandell <egomez@redhat.com>
2024-04-24 13:52:22 +02:00

74 lines
2.4 KiB
Go

package transfer_test
import (
"context"
"reflect"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
cdiv1 "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"kubevirt.io/containerized-data-importer/pkg/client/clientset/versioned/scheme"
"kubevirt.io/containerized-data-importer/pkg/common"
"kubevirt.io/containerized-data-importer/pkg/controller/transfer"
)
func rr(name string) reconcile.Request {
return reconcile.Request{NamespacedName: types.NamespacedName{Name: name}}
}
func getResource(c client.Client, ns, name string, obj client.Object) error {
p := reflect.ValueOf(obj).Elem()
p.Set(reflect.Zero(p.Type()))
return c.Get(context.TODO(), types.NamespacedName{Namespace: ns, Name: name}, obj)
}
func checkCompleteFalse(ot *cdiv1.ObjectTransfer, m, r string) {
Expect(ot.Status.Conditions).To(HaveLen(1))
cond := ot.Status.Conditions[0]
Expect(cond.Type).To(Equal(cdiv1.ObjectTransferConditionComplete))
Expect(cond.Status).To(Equal(corev1.ConditionFalse))
Expect(cond.Message).To(Equal(m))
Expect(cond.Reason).To(Equal(r))
Expect(cond.LastHeartbeatTime.Unix()).ToNot(BeZero())
Expect(cond.LastTransitionTime.Unix()).ToNot(BeZero())
}
func checkCompleteTrue(ot *cdiv1.ObjectTransfer) {
Expect(ot.Status.Conditions).To(HaveLen(1))
cond := ot.Status.Conditions[0]
Expect(cond.Type).To(Equal(cdiv1.ObjectTransferConditionComplete))
Expect(cond.Status).To(Equal(corev1.ConditionTrue))
Expect(cond.Message).To(Equal("Transfer complete"))
Expect(cond.Reason).To(Equal(""))
Expect(cond.LastHeartbeatTime.Unix()).ToNot(BeZero())
Expect(cond.LastTransitionTime.Unix()).ToNot(BeZero())
}
func createReconciler(objects ...client.Object) *transfer.ObjectTransferReconciler {
s := scheme.Scheme
_ = corev1.AddToScheme(s)
_ = cdiv1.AddToScheme(s)
cl := fake.NewClientBuilder().WithScheme(s).WithObjects(objects...).WithStatusSubresource(objects...).Build()
return &transfer.ObjectTransferReconciler{
Client: cl,
Scheme: s,
Log: logf.Log.WithName("transfer-controller-test"),
Recorder: record.NewFakeRecorder(10),
InstallerLabels: map[string]string{
common.AppKubernetesPartOfLabel: "testing",
common.AppKubernetesVersionLabel: "v0.0.0-tests",
},
}
}