From b5431ed7c0f4659269143daedb1651ef9a303a89 Mon Sep 17 00:00:00 2001 From: Konrad Bańka Date: Wed, 4 Sep 2019 17:22:42 +0200 Subject: Remove ovn4nfvk8s network plugin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As ovn4nfv networks are now handled on k8s side by created CRD, this plugin is no longer necessary. Removed all references to ovn Issue-ID: MULTICLOUD-733 Signed-off-by: Konrad Bańka Change-Id: I7ad6c6d9b3b4fd8f249796f437e69c7df4e701cc --- src/k8splugin/Makefile | 1 - src/k8splugin/internal/config/config.go | 2 - .../mock_files/mock_plugins/mocknetworkplugin.go | 41 ----- .../mock_files/mock_yamls/ovn4nfvk8s.yaml | 9 -- src/k8splugin/plugins/network/plugin.go | 111 ------------- src/k8splugin/plugins/network/plugin_test.go | 173 --------------------- src/k8splugin/plugins/network/v1/types.go | 54 ------- src/k8splugin/plugins/ovn4nfvk8s-network/plugin.go | 153 ------------------ .../plugins/ovn4nfvk8s-network/plugin_test.go | 145 ----------------- 9 files changed, 689 deletions(-) delete mode 100644 src/k8splugin/mock_files/mock_plugins/mocknetworkplugin.go delete mode 100644 src/k8splugin/mock_files/mock_yamls/ovn4nfvk8s.yaml delete mode 100644 src/k8splugin/plugins/network/plugin.go delete mode 100644 src/k8splugin/plugins/network/plugin_test.go delete mode 100644 src/k8splugin/plugins/network/v1/types.go delete mode 100644 src/k8splugin/plugins/ovn4nfvk8s-network/plugin.go delete mode 100644 src/k8splugin/plugins/ovn4nfvk8s-network/plugin_test.go (limited to 'src/k8splugin') diff --git a/src/k8splugin/Makefile b/src/k8splugin/Makefile index 942c3032..7d41158c 100644 --- a/src/k8splugin/Makefile +++ b/src/k8splugin/Makefile @@ -26,7 +26,6 @@ deploy: build .PHONY: test test: clean @go build -buildmode=plugin -o ./mock_files/mock_plugins/mockplugin.so ./mock_files/mock_plugins/mockplugin.go - @go build -buildmode=plugin -o ./mock_files/mock_plugins/mocknetworkplugin.so ./mock_files/mock_plugins/mocknetworkplugin.go @go test -v ./... format: diff --git a/src/k8splugin/internal/config/config.go b/src/k8splugin/internal/config/config.go index 23ec401e..0e45308c 100644 --- a/src/k8splugin/internal/config/config.go +++ b/src/k8splugin/internal/config/config.go @@ -37,7 +37,6 @@ type Configuration struct { EtcdCert string `json:"etcd-cert"` EtcdKey string `json:"etcd-key"` EtcdCAFile string `json:"etcd-ca-file"` - OVNCentralAddress string `json:"ovn-central-address"` ServicePort string `json:"service-port"` KubernetesLabelName string `json:"kubernetes-label-name"` } @@ -86,7 +85,6 @@ func defaultConfiguration() *Configuration { EtcdCert: "etcd.cert", EtcdKey: "etcd.key", EtcdCAFile: "etcd-ca.cert", - OVNCentralAddress: "127.0.0.1:6641", ServicePort: "9015", KubernetesLabelName: "k8splugin.io/rb-instance-id", } diff --git a/src/k8splugin/mock_files/mock_plugins/mocknetworkplugin.go b/src/k8splugin/mock_files/mock_plugins/mocknetworkplugin.go deleted file mode 100644 index f71311b1..00000000 --- a/src/k8splugin/mock_files/mock_plugins/mocknetworkplugin.go +++ /dev/null @@ -1,41 +0,0 @@ -/* -Copyright 2018 Intel Corporation. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - pkgerrors "github.com/pkg/errors" - "github.com/onap/multicloud-k8s/src/k8splugin/plugins/network/v1" -) - -// Err is the error message to be sent during functional testing -var Err string - -// NetworkName is the output used for functional tests -var NetworkName string - -// CreateNetwork resource -func CreateNetwork(network *v1.OnapNetwork) (string, error) { - if Err != "" { - return "", pkgerrors.New(Err) - } - return NetworkName, nil -} - -// DeleteNetwork resource -func DeleteNetwork(name string) error { - if Err != "" { - return pkgerrors.New(Err) - } - return nil -} diff --git a/src/k8splugin/mock_files/mock_yamls/ovn4nfvk8s.yaml b/src/k8splugin/mock_files/mock_yamls/ovn4nfvk8s.yaml deleted file mode 100644 index 31351dee..00000000 --- a/src/k8splugin/mock_files/mock_yamls/ovn4nfvk8s.yaml +++ /dev/null @@ -1,9 +0,0 @@ -apiVersion: v1 -kind: Network -metadata: - name: ovn-priv-net -spec: - cnitype: "ovn4nfvk8s" - name: "ovn-priv-net" - subnet: "172.16.33.0/24" - gateway: "172.16.33.1/24" diff --git a/src/k8splugin/plugins/network/plugin.go b/src/k8splugin/plugins/network/plugin.go deleted file mode 100644 index aa0d584b..00000000 --- a/src/k8splugin/plugins/network/plugin.go +++ /dev/null @@ -1,111 +0,0 @@ -/* -Copyright 2018 Intel Corporation. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "regexp" - - v1 "github.com/onap/multicloud-k8s/src/k8splugin/plugins/network/v1" - - utils "github.com/onap/multicloud-k8s/src/k8splugin/internal" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/plugin" - - pkgerrors "github.com/pkg/errors" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -// Compile time check to see if networkPlugin implements the correct interface -var _ plugin.Reference = networkPlugin{} - -// ExportedVariable is what we will look for when calling the plugin -var ExportedVariable networkPlugin - -type networkPlugin struct { -} - -func extractData(data string) (cniType, networkName string, err error) { - re := regexp.MustCompile("_") - split := re.Split(data, -1) - if len(split) != 2 { - err = pkgerrors.New("Couldn't split resource '" + data + - "' into CNI type and Network name") - return - } - cniType = split[0] - networkName = split[1] - return -} - -// Create an ONAP Network object -func (p networkPlugin) Create(yamlFilePath string, namespace string, client plugin.KubernetesConnector) (string, error) { - network := &v1.OnapNetwork{} - if _, err := utils.DecodeYAML(yamlFilePath, network); err != nil { - return "", pkgerrors.Wrap(err, "Decode network object error") - } - - cniType := network.Spec.CniType - typePlugin, ok := utils.LoadedPlugins[cniType+"-network"] - if !ok { - return "", pkgerrors.New("No plugin for resource " + cniType + " found") - } - - symCreateNetworkFunc, err := typePlugin.Lookup("CreateNetwork") - if err != nil { - return "", pkgerrors.Wrap(err, "Error fetching "+cniType+" plugin") - } - - name, err := symCreateNetworkFunc.(func(*v1.OnapNetwork) (string, error))(network) - if err != nil { - return "", pkgerrors.Wrap(err, "Error during the creation for "+cniType+" plugin") - } - - return cniType + "_" + name, nil -} - -// Get a Network -func (p networkPlugin) Get(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) (string, error) { - return "", nil -} - -// List of Networks -func (p networkPlugin) List(gvk schema.GroupVersionKind, namespace string, - client plugin.KubernetesConnector) ([]helm.KubernetesResource, error) { - - return nil, nil -} - -// Delete an existing Network -func (p networkPlugin) Delete(resource helm.KubernetesResource, namespace string, client plugin.KubernetesConnector) error { - cniType, networkName, err := extractData(resource.Name) - if err != nil { - return pkgerrors.Wrap(err, "Error extracting CNI type from resource") - } - - typePlugin, ok := utils.LoadedPlugins[cniType+"-network"] - if !ok { - return pkgerrors.New("No plugin for resource " + cniType + " found") - } - - symDeleteNetworkFunc, err := typePlugin.Lookup("DeleteNetwork") - if err != nil { - return pkgerrors.Wrap(err, "Error fetching "+cniType+" plugin") - } - - if err := symDeleteNetworkFunc.(func(string) error)(networkName); err != nil { - return pkgerrors.Wrap(err, "Error during the deletion for "+cniType+" plugin") - } - - return nil -} diff --git a/src/k8splugin/plugins/network/plugin_test.go b/src/k8splugin/plugins/network/plugin_test.go deleted file mode 100644 index 33cae1c7..00000000 --- a/src/k8splugin/plugins/network/plugin_test.go +++ /dev/null @@ -1,173 +0,0 @@ -/* -Copyright 2018 Intel Corporation. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - utils "github.com/onap/multicloud-k8s/src/k8splugin/internal" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/helm" - "os" - "plugin" - "reflect" - "strings" - "testing" - - pkgerrors "github.com/pkg/errors" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -func LoadMockNetworkPlugins(krdLoadedPlugins *map[string]*plugin.Plugin, networkName, errMsg string) error { - if _, err := os.Stat("../../mock_files/mock_plugins/mocknetworkplugin.so"); os.IsNotExist(err) { - return pkgerrors.New("mocknetworkplugin.so does not exist. Please compile mocknetworkplugin.go to generate") - } - - mockNetworkPlugin, err := plugin.Open("../../mock_files/mock_plugins/mocknetworkplugin.so") - if err != nil { - return pkgerrors.Cause(err) - } - - symErrVar, err := mockNetworkPlugin.Lookup("Err") - if err != nil { - return err - } - symNetworkNameVar, err := mockNetworkPlugin.Lookup("NetworkName") - if err != nil { - return err - } - - *symErrVar.(*string) = errMsg - *symNetworkNameVar.(*string) = networkName - (*krdLoadedPlugins)["ovn4nfvk8s-network"] = mockNetworkPlugin - - return nil -} - -func TestCreateNetwork(t *testing.T) { - oldkrdPluginData := utils.LoadedPlugins - - defer func() { - utils.LoadedPlugins = oldkrdPluginData - }() - - testCases := []struct { - label string - input string - mockError string - mockOutput string - expectedResult string - expectedError string - }{ - { - label: "Fail to decode a network object", - input: "../../mock_files/mock_yamls/service.yaml", - expectedError: "No plugin for resource", - }, - { - label: "Fail to create a network", - input: "../../mock_files/mock_yamls/ovn4nfvk8s.yaml", - mockError: "Internal error", - expectedError: "Error during the creation for ovn4nfvk8s plugin: Internal error", - }, - { - label: "Successfully create a ovn4nfv network", - input: "../../mock_files/mock_yamls/ovn4nfvk8s.yaml", - expectedResult: "ovn4nfvk8s_myNetwork", - mockOutput: "myNetwork", - }, - } - - for _, testCase := range testCases { - t.Run(testCase.label, func(t *testing.T) { - err := LoadMockNetworkPlugins(&utils.LoadedPlugins, testCase.mockOutput, testCase.mockError) - if err != nil { - t.Fatalf("TestCreateNetwork returned an error (%s)", err) - } - result, err := networkPlugin{}.Create(testCase.input, "", nil) - if err != nil { - if testCase.expectedError == "" { - t.Fatalf("Create method return an un-expected (%s)", err) - } - if !strings.Contains(string(err.Error()), testCase.expectedError) { - t.Fatalf("Create method returned an error (%s)", err) - } - } else { - if testCase.expectedError != "" && testCase.expectedResult == "" { - t.Fatalf("Create method was expecting \"%s\" error message", testCase.expectedError) - } - if !reflect.DeepEqual(testCase.expectedResult, result) { - - t.Fatalf("Create method returned: \n%v\n and it was expected: \n%v", result, testCase.expectedResult) - } - } - }) - } -} - -func TestDeleteNetwork(t *testing.T) { - oldkrdPluginData := utils.LoadedPlugins - - defer func() { - utils.LoadedPlugins = oldkrdPluginData - }() - - testCases := []struct { - label string - input string - mockError string - mockOutput string - expectedResult string - expectedError string - }{ - { - label: "Fail to load non-existing plugin", - input: "non-existing-cni_test", - expectedError: "No plugin for resource non-existing-cni", - }, - { - label: "Fail to extract cni from network name", - input: "1_ovn4nfvk8s_test", - expectedError: "Error extracting CNI type from resource: Couldn't split resource '1_ovn4nfvk8s_test' into CNI type and Network name", - }, - { - label: "Fail to delete a network", - input: "ovn4nfvk8s_test", - mockError: "Internal error", - expectedError: "Error during the deletion for ovn4nfvk8s plugin: Internal error", - }, - { - label: "Successfully delete a ovn4nfv network", - input: "ovn4nfvk8s_test", - }, - } - - for _, testCase := range testCases { - t.Run(testCase.label, func(t *testing.T) { - err := LoadMockNetworkPlugins(&utils.LoadedPlugins, testCase.mockOutput, testCase.mockError) - if err != nil { - t.Fatalf("TestDeleteNetwork returned an error (%s)", err) - } - err = networkPlugin{}.Delete(helm.KubernetesResource{ - GVK: schema.GroupVersionKind{Group: "", Version: "", Kind: "Network"}, - Name: testCase.input, - }, "", nil) - if err != nil { - if testCase.expectedError == "" { - t.Fatalf("Create method return an un-expected (%s)", err) - } - if !strings.Contains(string(err.Error()), testCase.expectedError) { - t.Fatalf("Create method returned an error (%s)", err) - } - } - }) - } -} diff --git a/src/k8splugin/plugins/network/v1/types.go b/src/k8splugin/plugins/network/v1/types.go deleted file mode 100644 index 96484efa..00000000 --- a/src/k8splugin/plugins/network/v1/types.go +++ /dev/null @@ -1,54 +0,0 @@ -/* -Copyright 2018 Intel Corporation. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1 - -import ( - - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object - -// OnapNetwork describes an ONAP network resouce -type OnapNetwork struct { - metaV1.TypeMeta `json:",inline"` - metaV1.ObjectMeta `json:"metadata,omitempty"` - Spec OnapNetworkSpec `json:"spec"` -} - -// OnapNetworkSpec is the spec for OnapNetwork resource -type OnapNetworkSpec struct { - CniType string `json:"cnitype"` - Name string `json:"name"` - Subnet string `json:"subnet"` - Gateway string `json:"gateway"` - -} - -// DeepCopyObject returns a generically typed copy of an object -func (in OnapNetwork) DeepCopyObject() runtime.Object { - out := OnapNetwork{} - out.TypeMeta = in.TypeMeta - out.ObjectMeta = in.ObjectMeta - out.Spec = in.Spec - - return &out -} - -// GetObjectKind -func (in OnapNetwork) GetObjectKind() schema.ObjectKind { - return &in.TypeMeta -} diff --git a/src/k8splugin/plugins/ovn4nfvk8s-network/plugin.go b/src/k8splugin/plugins/ovn4nfvk8s-network/plugin.go deleted file mode 100644 index bc2b2916..00000000 --- a/src/k8splugin/plugins/ovn4nfvk8s-network/plugin.go +++ /dev/null @@ -1,153 +0,0 @@ -/* -Copyright 2018 Intel Corporation. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - "bytes" - "fmt" - "log" - "math/rand" - "strings" - "time" - "unicode" - - "github.com/onap/multicloud-k8s/src/k8splugin/internal/config" - v1 "github.com/onap/multicloud-k8s/src/k8splugin/plugins/network/v1" - - pkgerrors "github.com/pkg/errors" - kexec "k8s.io/utils/exec" -) - -const ( - ovn4nfvRouter = "ovn4nfv-master" - ovnNbctlCommand = "ovn-nbctl" -) - -type OVNNbctler interface { - Run(args ...string) (string, string, error) -} - -type OVNNbctl struct { - run func(args ...string) (string, string, error) - exec kexec.Interface - path string -} - -// Run a command via ovn-nbctl -func (ctl *OVNNbctl) Run(args ...string) (string, string, error) { - if ctl.exec == nil { - ctl.exec = kexec.New() - } - if ctl.path == "" { - nbctlPath, err := ctl.exec.LookPath(ovnNbctlCommand) - if err != nil { - return "", "", pkgerrors.Wrap(err, "Look nbctl path error") - } - ctl.path = nbctlPath - } - - stdout := &bytes.Buffer{} - stderr := &bytes.Buffer{} - cmd := ctl.exec.Command(ctl.path, args...) - cmd.SetStdout(stdout) - cmd.SetStderr(stderr) - err := cmd.Run() - - return strings.Trim(strings.TrimFunc(stdout.String(), unicode.IsSpace), "\""), - stderr.String(), err -} - -var ovnCmd OVNNbctler - -func init() { - ovnCmd = &OVNNbctl{} -} - -// CreateNetwork in OVN controller -func CreateNetwork(network *v1.OnapNetwork) (string, error) { - - name := network.Spec.Name - if name == "" { - return "", pkgerrors.New("Invalid Network Name") - } - - subnet := network.Spec.Subnet - if subnet == "" { - return "", pkgerrors.New("Invalid Subnet Address") - } - - gatewayIPMask := network.Spec.Gateway - if gatewayIPMask == "" { - return "", pkgerrors.New("Invalid Gateway Address") - } - - routerMac, stderr, err := ovnCmd.Run(getAuthStr(), "--if-exist", "-v", "get", "logical_router_port", "rtos-"+name, "mac") - if err != nil { - return "", pkgerrors.Wrapf(err, "Failed to get logical router port,stderr: %q, error: %v", stderr, err) - } - - if routerMac == "" { - log.Print("Generate MAC address") - prefix := "00:00:00" - newRand := rand.New(rand.NewSource(time.Now().UnixNano())) - routerMac = fmt.Sprintf("%s:%02x:%02x:%02x", prefix, newRand.Intn(255), newRand.Intn(255), newRand.Intn(255)) - } - - _, stderr, err = ovnCmd.Run(getAuthStr(), "--may-exist", "lrp-add", ovn4nfvRouter, "rtos-"+name, routerMac, gatewayIPMask) - if err != nil { - return "", pkgerrors.Wrapf(err, "Failed to add logical port to router, stderr: %q, error: %v", stderr, err) - } - - // Create a logical switch and set its subnet. - stdout, stderr, err := ovnCmd.Run(getAuthStr(), "--", "--may-exist", "ls-add", name, "--", "set", "logical_switch", name, "other-config:subnet="+subnet, "external-ids:gateway_ip="+gatewayIPMask) - if err != nil { - return "", pkgerrors.Wrapf(err, "Failed to create a logical switch %v, stdout: %q, stderr: %q, error: %v", name, stdout, stderr, err) - } - - // Connect the switch to the router. - stdout, stderr, err = ovnCmd.Run(getAuthStr(), "--", "--may-exist", "lsp-add", name, "stor-"+name, "--", "set", "logical_switch_port", "stor-"+name, "type=router", "options:router-port=rtos-"+name, "addresses="+"\""+routerMac+"\"") - if err != nil { - return "", pkgerrors.Wrapf(err, "Failed to add logical port to switch, stdout: %q, stderr: %q, error: %v", stdout, stderr, err) - } - - return name, nil -} - -// DeleteNetwork in OVN controller -func DeleteNetwork(name string) error { - log.Printf("Deleting Network: Ovn4nfvk8s %s", name) - - stdout, stderr, err := ovnCmd.Run(getAuthStr(), "--if-exist", "ls-del", name) - if err != nil { - return pkgerrors.Wrapf(err, "Failed to delete switch %v, stdout: %q, stderr: %q, error: %v", name, stdout, stderr, err) - } - - stdout, stderr, err = ovnCmd.Run(getAuthStr(), "--if-exist", "lrp-del", "rtos-"+name) - if err != nil { - return pkgerrors.Wrapf(err, "Failed to delete router port %v, stdout: %q, stderr: %q, error: %v", name, stdout, stderr, err) - } - - stdout, stderr, err = ovnCmd.Run(getAuthStr(), "--if-exist", "lsp-del", "stor-"+name) - if err != nil { - return pkgerrors.Wrapf(err, "Failed to delete switch port %v, stdout: %q, stderr: %q, error: %v", name, stdout, stderr, err) - } - - return nil -} - -func getAuthStr() string { - //TODO: Remove hardcoding: Use ESR data passed to Initialize - ovnCentralAddress := config.GetConfiguration().OVNCentralAddress - return "--db=tcp:" + ovnCentralAddress -} diff --git a/src/k8splugin/plugins/ovn4nfvk8s-network/plugin_test.go b/src/k8splugin/plugins/ovn4nfvk8s-network/plugin_test.go deleted file mode 100644 index d1a94859..00000000 --- a/src/k8splugin/plugins/ovn4nfvk8s-network/plugin_test.go +++ /dev/null @@ -1,145 +0,0 @@ -/* -Copyright 2018 Intel Corporation. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package main - -import ( - pkgerrors "github.com/pkg/errors" - metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "github.com/onap/multicloud-k8s/src/k8splugin/plugins/network/v1" - "reflect" - "strings" - "testing" -) - -type mockOVNCmd struct { - StdOut string - StdErr string - Err error -} - -func (cmd *mockOVNCmd) Run(args ...string) (string, string, error) { - return cmd.StdOut, cmd.StdErr, cmd.Err -} - -func TestCreateOVN4NFVK8SNetwork(t *testing.T) { - testCases := []struct { - label string - input *v1.OnapNetwork - mock *mockOVNCmd - expectedResult string - expectedError string - }{ - { - label: "Fail to decode a network", - input: &v1.OnapNetwork{}, - expectedError: "Invalid Network Name", - }, - { - label: "Fail to create a network", - input: &v1.OnapNetwork{ - ObjectMeta: metaV1.ObjectMeta{ - Name: "test", - }, - Spec: v1.OnapNetworkSpec{ - CniType: "ovn4nfvk8s", Name: "mynet", Subnet: "172.16.33.0/24", Gateway: "172.16.33.1/24", - }, - }, - expectedError: "Failed to get logical router", - mock: &mockOVNCmd{ - Err: pkgerrors.New("Internal error"), - }, - }, - { - label: "Successfully create a ovn4nfv network", - input: &v1.OnapNetwork{ - ObjectMeta: metaV1.ObjectMeta{ - Name: "test", - }, - Spec: v1.OnapNetworkSpec{ - CniType: "ovn4nfvk8s", Name: "mynet", Subnet: "172.16.33.0/24", Gateway: "172.16.33.1/24", - }, - }, - expectedResult: "mynet", - mock: &mockOVNCmd{}, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.label, func(t *testing.T) { - if testCase.mock != nil { - ovnCmd = testCase.mock - } - result, err := CreateNetwork(testCase.input) - if err != nil { - if testCase.expectedError == "" { - t.Fatalf("CreateNetwork method return an un-expected (%s)", err) - } - if !strings.Contains(string(err.Error()), testCase.expectedError) { - t.Fatalf("CreateNetwork method returned an error (%s)", err) - } - } else { - if testCase.expectedError != "" && testCase.expectedResult == "" { - t.Fatalf("CreateNetwork method was expecting \"%s\" error message", testCase.expectedError) - } - if result == "" { - t.Fatal("CreateNetwork method returned nil result") - } - if !reflect.DeepEqual(testCase.expectedResult, result) { - - t.Fatalf("CreateNetwork method returned: \n%v\n and it was expected: \n%v", result, testCase.expectedResult) - } - } - }) - } -} - -func TestDeleteOVN4NFVK8SNetwork(t *testing.T) { - testCases := []struct { - label string - input string - mock *mockOVNCmd - expectedError string - }{ - { - label: "Fail to delete a network", - input: "test", - expectedError: "Failed to delete switch test", - mock: &mockOVNCmd{ - Err: pkgerrors.New("Internal error"), - }, - }, - { - label: "Successfully delete a ovn4nfv network", - input: "test", - mock: &mockOVNCmd{}, - }, - } - - for _, testCase := range testCases { - t.Run(testCase.label, func(t *testing.T) { - if testCase.mock != nil { - ovnCmd = testCase.mock - } - err := DeleteNetwork(testCase.input) - if err != nil { - if testCase.expectedError == "" { - t.Fatalf("DeleteNetwork method return an un-expected (%s)", err) - } - if !strings.Contains(string(err.Error()), testCase.expectedError) { - t.Fatalf("DeleteNetwork method returned an error (%s)", err) - } - } - }) - } -} -- cgit 1.2.3-korg