Skip to content

Commit

Permalink
add tests for plugin package
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Jul 15, 2019
1 parent ce4b741 commit 53fb5f1
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/app/backend/plugin/detail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2017 The Kubernetes Authors.
//
// 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 plugin

import (
"bytes"
"github.com/kubernetes/dashboard/src/app/backend/plugin/apis/v1alpha1"
fakePluginClientset "github.com/kubernetes/dashboard/src/app/backend/plugin/client/clientset/versioned/fake"
coreV1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fakeK8sClient "k8s.io/client-go/kubernetes/fake"
"testing"
)

var srcData = "randomPluginSourceCode"

func TestGetPluginSource(t *testing.T) {
ns := "default"
pluginName := "test-plugin"
filename := "plugin-test.js"
cfgMapName := "plugin-test-cfgMap"

pcs := fakePluginClientset.NewSimpleClientset()
cs := fakeK8sClient.NewSimpleClientset()

_, err := GetPluginSource(pcs, cs, ns, pluginName)
if err == nil {
t.Errorf("error 'plugins.dashboard.k8s.io \"%s\" not found' did not occur", pluginName)
}

_, err = pcs.DashboardV1alpha1().Plugins(ns).Create(&v1alpha1.Plugin{
ObjectMeta: v1.ObjectMeta{Name: pluginName, Namespace: ns},
Spec: v1alpha1.PluginSpec{
Source: v1alpha1.Source{
ConfigMapRef: &coreV1.ConfigMapEnvSource{
LocalObjectReference: coreV1.LocalObjectReference{Name: cfgMapName},
},
Filename: filename}},
})

_, err = GetPluginSource(pcs, cs, ns, pluginName)
if err == nil {
t.Errorf("error 'configmaps \"%s\" not found' did not occur", cfgMapName)
}

_, _ = cs.CoreV1().ConfigMaps(ns).Create(&coreV1.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: cfgMapName, Namespace: ns},
Data: map[string]string{filename: srcData},
})

data, err := GetPluginSource(pcs, cs, ns, pluginName)
if err != nil {
t.Errorf("error while fetching plugin source: %s", err)
}

if !bytes.Equal(data, []byte(srcData)) {
t.Error("bytes in configMap and bytes from GetPluginSource are different")
}
}
30 changes: 30 additions & 0 deletions src/app/backend/plugin/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2017 The Kubernetes Authors.
//
// 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 plugin

import (
"github.com/emicklei/go-restful"
"testing"
)

func TestIntegrationHandler_Install(t *testing.T) {
pHandler := NewPluginHandler(nil)
ws := new(restful.WebService)
pHandler.Install(ws)

if len(ws.Routes()) == 0 {
t.Error("Failed to install routes.")
}
}
51 changes: 51 additions & 0 deletions src/app/backend/plugin/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2017 The Kubernetes Authors.
//
// 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 plugin

import (
"github.com/kubernetes/dashboard/src/app/backend/plugin/apis/v1alpha1"
fakePluginClientset "github.com/kubernetes/dashboard/src/app/backend/plugin/client/clientset/versioned/fake"
coreV1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func TestGetPluginList(t *testing.T) {
ns := "default"
pluginName := "test-plugin"
filename := "plugin-test.js"
cfgMapName := "plugin-test-cfgMap"

pcs := fakePluginClientset.NewSimpleClientset()

_, _ = pcs.DashboardV1alpha1().Plugins(ns).Create(&v1alpha1.Plugin{
ObjectMeta: v1.ObjectMeta{Name: pluginName, Namespace: ns},
Spec: v1alpha1.PluginSpec{
Source: v1alpha1.Source{
ConfigMapRef: &coreV1.ConfigMapEnvSource{
LocalObjectReference: coreV1.LocalObjectReference{Name: cfgMapName},
},
Filename: filename}},
})

data, err := GetPluginList(pcs, ns)
if err != nil {
t.Errorf("error while fetching plugins: %s", err)
}

if data.ListMeta.TotalItems != 1 {
t.Errorf("there should be one plugin registered, got %d", data.ListMeta.TotalItems)
}
}

0 comments on commit 53fb5f1

Please sign in to comment.