Skip to content

Commit

Permalink
Add unit tests for static ips builder
Browse files Browse the repository at this point in the history
Signed-off-by: Liran Rotenberg <[email protected]>
  • Loading branch information
liranr23 authored and ahadas committed May 16, 2024
1 parent ee69f9d commit a9772de
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/controller/plan/adapter/vsphere/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ go_library(
go_test(
name = "vsphere_test",
srcs = [
"builder_test.go",
"validator_test.go",
"vsphere_suite_test.go",
],
Expand All @@ -55,11 +56,17 @@ go_test(
"//pkg/apis/forklift/v1beta1",
"//pkg/apis/forklift/v1beta1/plan",
"//pkg/apis/forklift/v1beta1/ref",
"//pkg/controller/plan/context",
"//pkg/controller/provider/model/vsphere",
"//pkg/controller/provider/web",
"//pkg/controller/provider/web/vsphere",
"//pkg/lib/logging",
"//vendor/github.com/onsi/ginkgo/v2:ginkgo",
"//vendor/github.com/onsi/gomega",
"//vendor/github.com/vmware/govmomi/vim25/types",
"//vendor/k8s.io/api/apps/v1:apps",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta",
"//vendor/k8s.io/apimachinery/pkg/runtime",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client/fake",
],
)
65 changes: 65 additions & 0 deletions pkg/controller/plan/adapter/vsphere/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package vsphere

import (
v1beta1 "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context"
"github.com/konveyor/forklift-controller/pkg/controller/provider/model/vsphere"
model "github.com/konveyor/forklift-controller/pkg/controller/provider/web/vsphere"
"github.com/konveyor/forklift-controller/pkg/lib/logging"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/vmware/govmomi/vim25/types"
v1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

var builderLog = logging.WithName("vsphere-builder-test")

const ManualOrigin = string(types.NetIpConfigInfoIpAddressOriginManual)

var _ = Describe("vSphere builder", func() {
builder := createBuilder()
DescribeTable("should", func(vm *model.VM, outputMap string) {
Expect(builder.mapMacStaticIps(vm)).Should(Equal(outputMap))
},
Entry("no static ips", &model.VM{}, ""),
Entry("single static ip", &model.VM{GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: ManualOrigin}}}, "00:50:56:83:25:47:ip:172.29.3.193"),
Entry("multiple static ips", &model.VM{GuestNetworks: []vsphere.GuestNetwork{
{
MAC: "00:50:56:83:25:47",
IP: "172.29.3.193",
Origin: ManualOrigin,
},
{
MAC: "00:50:56:83:25:47",
IP: "fe80::5da:b7a5:e0a2:a097",
Origin: ManualOrigin,
},
},
}, "00:50:56:83:25:47:ip:172.29.3.193_00:50:56:83:25:47:ip:fe80::5da:b7a5:e0a2:a097"),
Entry("non-static ip", &model.VM{GuestNetworks: []vsphere.GuestNetwork{{MAC: "00:50:56:83:25:47", IP: "172.29.3.193", Origin: string(types.NetIpConfigInfoIpAddressOriginDhcp)}}}, ""),
)
})

func createBuilder(objs ...runtime.Object) *Builder {
scheme := runtime.NewScheme()
_ = v1.AddToScheme(scheme)
v1beta1.SchemeBuilder.AddToScheme(scheme)
client := fake.NewClientBuilder().
WithScheme(scheme).
WithRuntimeObjects(objs...).
Build()
return &Builder{
Context: &plancontext.Context{
Destination: plancontext.Destination{
Client: client,
},
Plan: createPlan(),
Log: builderLog,

// To make sure r.Scheme is not nil
Client: client,
},
}
}
2 changes: 1 addition & 1 deletion pkg/controller/plan/adapter/vsphere/vsphere_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package vsphere_test
package vsphere

import (
"testing"
Expand Down

0 comments on commit a9772de

Please sign in to comment.