Skip to content

Commit

Permalink
ocp: extract to function and add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Benny Zlotnik <[email protected]>
  • Loading branch information
bennyz committed Sep 18, 2023
1 parent e40039c commit 1171069
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
9 changes: 8 additions & 1 deletion pkg/controller/plan/adapter/ocp/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 = "ocp",
Expand Down Expand Up @@ -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"],
)
22 changes: 13 additions & 9 deletions pkg/controller/plan/adapter/ocp/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
41 changes: 41 additions & 0 deletions pkg/controller/plan/adapter/ocp/validator_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 1171069

Please sign in to comment.