From 05274b1b149139d91445ca10a73defe41f14824a Mon Sep 17 00:00:00 2001 From: Victor Morales Date: Tue, 18 Sep 2018 17:19:00 -0700 Subject: Add UTs to plugins Deployment, service and namespace are plugins which offers CRUD operations to manage their resources. They haven't implemented Unit Tests which makes fragile to change/refactor the source code. This change adds their corresponding Unit Tests and defines a standard interface. Change-Id: I1e1eb40f1a18ba33c74069a117462c8df17767ac Signed-off-by: Victor Morales Issue-ID: MULTICLOUD-301 --- src/k8splugin/krd/plugins.go | 49 +++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'src/k8splugin/krd') diff --git a/src/k8splugin/krd/plugins.go b/src/k8splugin/krd/plugins.go index 612e3f6b..41b83226 100644 --- a/src/k8splugin/krd/plugins.go +++ b/src/k8splugin/krd/plugins.go @@ -14,31 +14,46 @@ limitations under the License. package krd import ( + "io/ioutil" + "log" + "os" "plugin" - appsV1 "k8s.io/api/apps/v1" - coreV1 "k8s.io/api/core/v1" - "k8s.io/client-go/kubernetes" + pkgerrors "github.com/pkg/errors" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/kubernetes/scheme" ) // LoadedPlugins stores references to the stored plugins var LoadedPlugins = map[string]*plugin.Plugin{} -// KubeResourceClient has the signature methods to create Kubernetes reources -type KubeResourceClient interface { - CreateResource(GenericKubeResourceData, *kubernetes.Clientset) (string, error) - ListResources(string, string) (*[]string, error) - DeleteResource(string, string, *kubernetes.Clientset) error - GetResource(string, string, *kubernetes.Clientset) (string, error) +const ResourcesListLimit = 10 + +// ResourceData stores all supported Kubernetes plugin types +type ResourceData struct { + YamlFilePath string + Namespace string + VnfId string } -// GenericKubeResourceData stores all supported Kubernetes plugin types -type GenericKubeResourceData struct { - YamlFilePath string - Namespace string - InternalVNFID string +// DecodeYAML reads a YAMl file to extract the Kubernetes object definition +var DecodeYAML = func(path string) (runtime.Object, error) { + if _, err := os.Stat(path); os.IsNotExist(err) { + return nil, pkgerrors.New("File " + path + " not found") + } + + log.Println("Reading deployment YAML") + rawBytes, err := ioutil.ReadFile(path) + if err != nil { + return nil, pkgerrors.Wrap(err, "Deployment YAML file read error") + } + + log.Println("Decoding deployment YAML") + decode := scheme.Codecs.UniversalDeserializer().Decode + obj, _, err := decode(rawBytes, nil, nil) + if err != nil { + return nil, pkgerrors.Wrap(err, "Deserialize deployment error") + } - // Add additional Kubernetes plugins below kinds - DeploymentData *appsV1.Deployment - ServiceData *coreV1.Service + return obj, nil } -- cgit 1.2.3-korg