From 1171069edb911a8da0b34e83acf407205c27dce3 Mon Sep 17 00:00:00 2001 From: Benny Zlotnik Date: Mon, 18 Sep 2023 12:38:17 +0300 Subject: [PATCH] ocp: extract to function and add unit test Signed-off-by: Benny Zlotnik --- pkg/controller/plan/adapter/ocp/BUILD.bazel | 9 +++- pkg/controller/plan/adapter/ocp/validator.go | 22 ++++++---- .../plan/adapter/ocp/validator_test.go | 41 +++++++++++++++++++ 3 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 pkg/controller/plan/adapter/ocp/validator_test.go diff --git a/pkg/controller/plan/adapter/ocp/BUILD.bazel b/pkg/controller/plan/adapter/ocp/BUILD.bazel index 5a5d78c98..a9c3d5f6c 100644 --- a/pkg/controller/plan/adapter/ocp/BUILD.bazel +++ b/pkg/controller/plan/adapter/ocp/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 = "ocp", @@ -35,3 +35,10 @@ go_library( "//vendor/sigs.k8s.io/controller-runtime/pkg/client/config", ], ) + +go_test( + name = "ocp_test", + srcs = ["validator_test.go"], + embed = [":ocp"], + deps = ["//pkg/apis/forklift/v1beta1/ref"], +) diff --git a/pkg/controller/plan/adapter/ocp/validator.go b/pkg/controller/plan/adapter/ocp/validator.go index a597fc217..3c21368f0 100644 --- a/pkg/controller/plan/adapter/ocp/validator.go +++ b/pkg/controller/plan/adapter/ocp/validator.go @@ -161,15 +161,7 @@ func (r *Validator) NetworksMapped(vmRef ref.Ref) (ok bool, err error) { return false, err } } else if net.Multus != nil { - var namespace, name string - - if !strings.Contains(net.Multus.NetworkName, "/") { - namespace = vmRef.Namespace - name = net.Multus.NetworkName - } else { - splitName := strings.Split(net.Multus.NetworkName, "/") - namespace, name = splitName[0], splitName[1] - } + namespace, name := getNetworkNameAndNamespace(net.Multus.NetworkName, &vmRef) _, found := r.plan.Referenced.Map.Network.FindNetworkByNameAndNamespace(namespace, name) if !found { @@ -186,3 +178,15 @@ func (r *Validator) NetworksMapped(vmRef ref.Ref) (ok bool, err error) { return true, nil } + +func getNetworkNameAndNamespace(networkName string, vmRef *ref.Ref) (name, namespace string) { + if !strings.Contains(networkName, "/") { + namespace = vmRef.Namespace + name = networkName + } else { + splitName := strings.Split(networkName, "/") + namespace, name = splitName[0], splitName[1] + } + + return +} diff --git a/pkg/controller/plan/adapter/ocp/validator_test.go b/pkg/controller/plan/adapter/ocp/validator_test.go new file mode 100644 index 000000000..26c523f9b --- /dev/null +++ b/pkg/controller/plan/adapter/ocp/validator_test.go @@ -0,0 +1,41 @@ +package ocp + +import ( + "testing" + + "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/ref" +) + +func TestGetNetworkNameAndNamespace(t *testing.T) { + tests := []struct { + name string + networkName string + vmRef *ref.Ref + expectedName string + expectedNS string + }{ + { + name: "no slash in network name", + networkName: "network", + vmRef: &ref.Ref{Namespace: "vmNamespace"}, + expectedName: "network", + expectedNS: "vmNamespace", + }, + { + name: "slash in network name", + networkName: "namespace/network", + vmRef: &ref.Ref{Namespace: "vmNamespace"}, + expectedName: "network", + expectedNS: "namespace", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + actualName, actualNS := getNetworkNameAndNamespace(tt.networkName, &ref.Ref{Namespace: tt.vmRef.Namespace}) + if actualName != tt.expectedName || actualNS != tt.expectedNS { + t.Errorf("got (%s, %s), want (%s, %s)", actualName, actualNS, tt.expectedName, tt.expectedNS) + } + }) + } +}