-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #539 from codingben/CNV-43604
test(CNV-43604): add disk-uploader unit tests
- Loading branch information
Showing
79 changed files
with
5,974 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
modules/disk-uploader/pkg/certificate/certificate_suite_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package certificate_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestCertificate(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Certificate Suite") | ||
} |
117 changes: 117 additions & 0 deletions
117
modules/disk-uploader/pkg/certificate/certificate_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package certificate_test | ||
|
||
import ( | ||
"context" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/golang/mock/gomock" | ||
|
||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
v1beta1 "kubevirt.io/api/export/v1beta1" | ||
kubevirtfake "kubevirt.io/client-go/generated/kubevirt/clientset/versioned/fake" | ||
"kubevirt.io/client-go/kubecli" | ||
|
||
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-uploader/pkg/certificate" | ||
) | ||
|
||
var _ = Describe("Certificate", func() { | ||
const ( | ||
namespace = "test-namespace" | ||
name = "test-vmexport" | ||
) | ||
|
||
var ( | ||
vmExportClient *kubevirtfake.Clientset | ||
virtClient kubecli.KubevirtClient | ||
) | ||
|
||
BeforeEach(func() { | ||
ctrl := gomock.NewController(GinkgoT()) | ||
vmExportClient = kubevirtfake.NewSimpleClientset() | ||
|
||
kubecli.GetKubevirtClientFromClientConfig = kubecli.GetMockKubevirtClientFromClientConfig | ||
kubecli.MockKubevirtClientInstance = kubecli.NewMockKubevirtClient(ctrl) | ||
kubecli.MockKubevirtClientInstance.EXPECT().VirtualMachineExport(namespace).Return(vmExportClient.ExportV1beta1().VirtualMachineExports(namespace)).AnyTimes() | ||
|
||
virtClient, _ = kubecli.GetKubevirtClientFromClientConfig(nil) | ||
}) | ||
|
||
Describe("GetCertificateFromVirtualMachineExport", func() { | ||
It("should return the certificate content", func() { | ||
_, err := vmExportClient.ExportV1beta1().VirtualMachineExports(namespace).Create(context.Background(), | ||
&v1beta1.VirtualMachineExport{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Status: &v1beta1.VirtualMachineExportStatus{ | ||
Links: &v1beta1.VirtualMachineExportLinks{ | ||
Internal: &v1beta1.VirtualMachineExportLink{ | ||
Cert: "test-cert-content", | ||
}, | ||
}, | ||
}, | ||
}, | ||
metav1.CreateOptions{}, | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
cert, err := certificate.GetCertificateFromVirtualMachineExport(virtClient, namespace, name) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(cert).To(Equal("test-cert-content")) | ||
}) | ||
|
||
It("should return not found error", func() { | ||
_, err := certificate.GetCertificateFromVirtualMachineExport(virtClient, namespace, "test") | ||
Expect(err).To(MatchError(errors.IsNotFound, "errors.IsNotFound")) | ||
}) | ||
|
||
It("should return an error when no links found", func() { | ||
_, err := vmExportClient.ExportV1beta1().VirtualMachineExports(namespace).Create(context.Background(), | ||
&v1beta1.VirtualMachineExport{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Status: &v1beta1.VirtualMachineExportStatus{ | ||
Links: nil, | ||
}, | ||
}, | ||
metav1.CreateOptions{}, | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
cert, err := certificate.GetCertificateFromVirtualMachineExport(virtClient, namespace, name) | ||
Expect(err).To(MatchError("no links found in VirtualMachineExport status")) | ||
Expect(cert).To(BeEmpty()) | ||
}) | ||
|
||
It("should return an error when no certificate found", func() { | ||
_, err := vmExportClient.ExportV1beta1().VirtualMachineExports(namespace).Create(context.Background(), | ||
&v1beta1.VirtualMachineExport{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Status: &v1beta1.VirtualMachineExportStatus{ | ||
Links: &v1beta1.VirtualMachineExportLinks{ | ||
Internal: &v1beta1.VirtualMachineExportLink{ | ||
Cert: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
metav1.CreateOptions{}, | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
cert, err := certificate.GetCertificateFromVirtualMachineExport(virtClient, namespace, name) | ||
Expect(err).To(MatchError("no certificate found in VirtualMachineExport status")) | ||
Expect(cert).To(BeEmpty()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package parse_test | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/kubevirt/kubevirt-tekton-tasks/modules/disk-uploader/pkg/parse" | ||
) | ||
|
||
var _ = Describe("CLIOptions", func() { | ||
const ( | ||
expectedExportSourceKind = "vm" | ||
expectedExportSourceNamespace = "test-namespace" | ||
expectedExportSourceName = "test-vmexport" | ||
expectedVolumeName = "test-volume" | ||
expectedImageDestination = "quay.io/kubevirt/example" | ||
) | ||
|
||
DescribeTable("Init return correct assertion errors", func(expectedErrMessage string, options *parse.CLIOptions) { | ||
Expect(options.Init()).To(MatchError(expectedErrMessage)) | ||
}, | ||
Entry("no export-source-kind", "export-source-kind param has to be specified", | ||
&parse.CLIOptions{}), | ||
Entry("no export-source-name", "export-source-name param has to be specified", | ||
&parse.CLIOptions{ExportSourceKind: expectedExportSourceKind}), | ||
Entry("no volume-name", "volume-name param has to be specified", | ||
&parse.CLIOptions{ExportSourceKind: expectedExportSourceKind, ExportSourceName: expectedExportSourceName}), | ||
Entry("no image-destination", "image-destination param has to be specified", | ||
&parse.CLIOptions{ExportSourceKind: expectedExportSourceKind, ExportSourceName: expectedExportSourceName, VolumeName: expectedVolumeName}), | ||
) | ||
|
||
Context("valid cli options", func() { | ||
It("should succeed with yaml output", func() { | ||
options := &parse.CLIOptions{ | ||
ExportSourceKind: expectedExportSourceKind, | ||
ExportSourceNamespace: expectedExportSourceNamespace, | ||
ExportSourceName: expectedExportSourceName, | ||
VolumeName: expectedVolumeName, | ||
ImageDestination: expectedImageDestination, | ||
PushTimeout: 60, | ||
Debug: true, | ||
} | ||
Expect(options.Init()).To(Succeed()) | ||
}) | ||
|
||
It("Init should trim spaces", func() { | ||
options := &parse.CLIOptions{ | ||
ExportSourceKind: " " + expectedExportSourceKind + " ", | ||
ExportSourceNamespace: " " + expectedExportSourceNamespace + " ", | ||
ExportSourceName: " " + expectedExportSourceName + " ", | ||
VolumeName: " " + expectedVolumeName + " ", | ||
ImageDestination: " " + expectedImageDestination + " ", | ||
PushTimeout: 60, | ||
Debug: true, | ||
} | ||
|
||
Expect(options.Init()).To(Succeed()) | ||
Expect(options.ExportSourceKind).To(Equal(expectedExportSourceKind), "ExportSourceKind should equal") | ||
Expect(options.ExportSourceNamespace).To(Equal(expectedExportSourceNamespace), "ExportSourceNamespace should equal") | ||
Expect(options.ExportSourceName).To(Equal(expectedExportSourceName), "ExportSourceName should equal") | ||
Expect(options.VolumeName).To(Equal(expectedVolumeName), "VolumeName should equal") | ||
Expect(options.ImageDestination).To(Equal(expectedImageDestination), "ImageDestination should equal") | ||
}) | ||
|
||
It("GetDebugLevel should return correct debug level when Debug is true", func() { | ||
options := &parse.CLIOptions{Debug: true} | ||
Expect(options.GetDebugLevel()).To(Equal(zapcore.DebugLevel), "level should equal") | ||
}) | ||
|
||
It("GetDebugLevel should return correct info level when Debug is false", func() { | ||
options := &parse.CLIOptions{Debug: false} | ||
Expect(options.GetDebugLevel()).To(Equal(zapcore.InfoLevel), "level should equal") | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.