diff --git a/pkg/controller/provider/container/ovirt/BUILD.bazel b/pkg/controller/provider/container/ovirt/BUILD.bazel index f4657f4aa..592934ffe 100644 --- a/pkg/controller/provider/container/ovirt/BUILD.bazel +++ b/pkg/controller/provider/container/ovirt/BUILD.bazel @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "ovirt", @@ -29,3 +29,18 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta", ], ) + +go_test( + name = "ovirt_test", + srcs = [ + "collector_suite_test.go", + "collector_test.go", + ], + embed = [":ovirt"], + deps = [ + "//vendor/github.com/onsi/ginkgo", + "//vendor/github.com/onsi/ginkgo/extensions/table", + "//vendor/github.com/onsi/gomega", + "//vendor/github.com/onsi/gomega/types", + ], +) diff --git a/pkg/controller/provider/container/ovirt/collector.go b/pkg/controller/provider/container/ovirt/collector.go index 420efae3c..c8440b96f 100644 --- a/pkg/controller/provider/container/ovirt/collector.go +++ b/pkg/controller/provider/container/ovirt/collector.go @@ -143,7 +143,12 @@ func (r *Collector) Version() (major, minor, build, revision string, err error) if err != nil { return } - version := strings.Split(system.Product.Version.FullVersion, ".") + major, minor, build, revision = parseVersion(system.Product.Version.FullVersion) + return +} + +func parseVersion(fullVersion string) (major, minor, build, revision string) { + version := strings.Split(fullVersion, ".") major = version[0] minor = version[1] @@ -157,7 +162,6 @@ func (r *Collector) Version() (major, minor, build, revision string, err error) default: revision = "0" } - return } diff --git a/pkg/controller/provider/container/ovirt/collector_suite_test.go b/pkg/controller/provider/container/ovirt/collector_suite_test.go new file mode 100644 index 000000000..dc13296bb --- /dev/null +++ b/pkg/controller/provider/container/ovirt/collector_suite_test.go @@ -0,0 +1,22 @@ +package ovirt + +import ( + "testing" + + "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +// forkliftFailHandler call ginkgo.Fail with printing the additional information +func forkliftFailHandler(message string, callerSkip ...int) { + if len(callerSkip) > 0 { + callerSkip[0]++ + } + ginkgo.Fail(message, callerSkip...) +} + +func TestTests(t *testing.T) { + defer ginkgo.GinkgoRecover() + RegisterFailHandler(forkliftFailHandler) + ginkgo.RunSpecs(t, "ovirt collector") +} diff --git a/pkg/controller/provider/container/ovirt/collector_test.go b/pkg/controller/provider/container/ovirt/collector_test.go new file mode 100644 index 000000000..f9bb5aab2 --- /dev/null +++ b/pkg/controller/provider/container/ovirt/collector_test.go @@ -0,0 +1,20 @@ +package ovirt + +import ( + "strings" + + "github.com/onsi/ginkgo" + "github.com/onsi/ginkgo/extensions/table" + . "github.com/onsi/gomega" + gtypes "github.com/onsi/gomega/types" +) + +var _ = ginkgo.Describe("ovirt collector", func() { + table.DescribeTable("should", func(version string, matchVersion gtypes.GomegaMatcher) { + major, minor, build, revision := parseVersion(version) + Expect(strings.Join([]string{major, minor, build, revision}, ".")).Should(matchVersion) + }, + table.Entry("get version when revision is in the 3rd element", "4.5.5-1.el8", Equal("4.5.5.1")), + table.Entry("get version when revision is in the 4th element", "4.5.3.4-1.el8ev", Equal("4.5.3.4")), + ) +})