Skip to content

Commit

Permalink
Add unit tests to the versioning
Browse files Browse the repository at this point in the history
Signed-off-by: Liran Rotenberg <lrotenbe@redhat.com>
  • Loading branch information
liranr23 authored and ahadas committed May 19, 2024
1 parent 4cdd6e1 commit ce2032b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
17 changes: 16 additions & 1 deletion pkg/controller/provider/container/ovirt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
],
)
8 changes: 6 additions & 2 deletions pkg/controller/provider/container/ovirt/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -157,7 +162,6 @@ func (r *Collector) Version() (major, minor, build, revision string, err error)
default:
revision = "0"
}

return
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/provider/container/ovirt/collector_suite_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
20 changes: 20 additions & 0 deletions pkg/controller/provider/container/ovirt/collector_test.go
Original file line number Diff line number Diff line change
@@ -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")),
)
})

0 comments on commit ce2032b

Please sign in to comment.