diff options
author | Victor Morales <victor.morales@intel.com> | 2018-09-18 17:19:00 -0700 |
---|---|---|
committer | Victor Morales <victor.morales@intel.com> | 2018-09-18 17:19:00 -0700 |
commit | 05274b1b149139d91445ca10a73defe41f14824a (patch) | |
tree | 2ad5e9169a521d29d0c909a271f1ff23b35d2dac /src/k8splugin/krd | |
parent | b368dfe25337494060eb8cd85a5becaf7a465643 (diff) |
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 <victor.morales@intel.com>
Issue-ID: MULTICLOUD-301
Diffstat (limited to 'src/k8splugin/krd')
-rw-r--r-- | src/k8splugin/krd/plugins.go | 49 |
1 files changed, 32 insertions, 17 deletions
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 } |