Commit 9d3a1ed2 authored by fibonacci1729's avatar fibonacci1729

basic cfgmaps driver tests

parent d5aca166
...@@ -21,127 +21,199 @@ import ( ...@@ -21,127 +21,199 @@ import (
"testing" "testing"
rspb "k8s.io/helm/pkg/proto/hapi/release" rspb "k8s.io/helm/pkg/proto/hapi/release"
"k8s.io/kubernetes/pkg/client/unversioned/testclient"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/api"
kberrs "k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/client/unversioned"
) )
func TestConfigMapGet(t *testing.T) { func TestConfigMapGet(t *testing.T) {
// test release
key := "key-1" key := "key-1"
rls := newTestRelease(key, 1, rspb.Status_DEPLOYED) rel := newTestRelease(key, 1, rspb.Status_DEPLOYED)
// create test fixture cfgmaps := newTestFixture(t, []*rspb.Release{rel}...)
cfgmaps := newTestFixture(t, rls)
// get the release from configmaps // get release with key
got, err := cfgmaps.Get(key) got, err := cfgmaps.Get(key)
if err != nil { if err != nil {
t.Fatalf("failed to get release with key %q. %s", key, err) t.Fatalf("Failed to get release: %s", err)
} }
// compare fetched release with original // compare fetched release with original
if !reflect.DeepEqual(rls, got) { if !reflect.DeepEqual(rel, got) {
t.Errorf("expected {%q}, got {%q}", rls, got) t.Errorf("Expected {%q}, got {%q}", rel, got)
} }
} }
func TestConfigMapList(t *testing.T) { func TestConfigMapList(t *testing.T) {
t.Skip("ConfigMapList") cfgmaps := newTestFixture(t, []*rspb.Release{
newTestRelease("key-1", 1, rspb.Status_DELETED),
newTestRelease("key-2", 1, rspb.Status_DELETED),
newTestRelease("key-3", 1, rspb.Status_DEPLOYED),
newTestRelease("key-4", 1, rspb.Status_DEPLOYED),
newTestRelease("key-5", 1, rspb.Status_SUPERSEDED),
newTestRelease("key-6", 1, rspb.Status_SUPERSEDED),
}...)
// list all deleted releases
del, err := cfgmaps.List(func(rel *rspb.Release) bool {
return rel.Info.Status.Code == rspb.Status_DELETED
})
// check
if err != nil {
t.Errorf("Failed to list deleted: %s", err)
}
if len(del) != 2 {
t.Errorf("Expected 2 deleted, got %d:\n%v\n", len(del), del)
}
// list all deployed releases
dpl, err := cfgmaps.List(func(rel *rspb.Release) bool {
return rel.Info.Status.Code == rspb.Status_DEPLOYED
})
// check
if err != nil {
t.Errorf("Failed to list deployed: %s", err)
}
if len(dpl) != 2 {
t.Errorf("Expected 2 deployed, got %d", len(dpl))
}
// list all superseded releases
ssd, err := cfgmaps.List(func(rel *rspb.Release) bool {
return rel.Info.Status.Code == rspb.Status_SUPERSEDED
})
// check
if err != nil {
t.Errorf("Failed to list superseded: %s", err)
}
if len(ssd) != 2 {
t.Errorf("Expected 2 superseded, got %d", len(ssd))
}
} }
func TestConfigMapCreate(t *testing.T) { func TestConfigMapCreate(t *testing.T) {
// setup cfgmaps := newTestFixture(t)
key := "key-1"
rls := newTestRelease(key, 1, rspb.Status_DEPLOYED)
// create test fixture key := "key-1"
cfgmaps := newTestFixture(t, rls) rel := newTestRelease(key, 1, rspb.Status_DEPLOYED)
// store the release in a configmap // store the release in a configmap
if err := cfgmaps.Create(rls); err != nil { if err := cfgmaps.Create(rel); err != nil {
t.Fatalf("failed to create release with key %q: %s", key, err) t.Fatalf("Failed to create release with key %q: %s", key, err)
} }
// get the release back // get the release back
got, err := cfgmaps.Get(key) got, err := cfgmaps.Get(key)
if err != nil { if err != nil {
t.Fatalf("failed to get release with key %q: %s", key, err) t.Fatalf("Failed to get release with key %q: %s", key, err)
} }
// compare created release with original // compare created release with original
if !reflect.DeepEqual(rls, got) { if !reflect.DeepEqual(rel, got) {
t.Errorf("expected {%q}, got {%q}", rls, got) t.Errorf("Expected {%q}, got {%q}", rel, got)
} }
} }
func TestConfigMapDelete(t *testing.T) { func TestConfigMapUpdate(t *testing.T) {
// setup
key := "key-1" key := "key-1"
rls := newTestRelease(key, 1, rspb.Status_DELETED) rel := newTestRelease(key, 1, rspb.Status_DEPLOYED)
// create test fixture cfgmaps := newTestFixture(t, []*rspb.Release{rel}...)
cfgmaps := newTestFixture(t, rls)
// delete the release // modify release status code & version
got, err := cfgmaps.Delete(key) rel = newTestRelease(key, 2, rspb.Status_SUPERSEDED)
// perform the update
if err := cfgmaps.Update(rel); err != nil {
t.Fatalf("Failed to update release: %s", err)
}
// fetch the updated release
got, err := cfgmaps.Get(key)
if err != nil { if err != nil {
t.Fatalf("failed to delete release with key %q: %s", key, err) t.Fatalf("Failed to get release with key %q: %s", key, err)
} }
// compare deleted release with original // check release has actually been updated by comparing modified fields
if !reflect.DeepEqual(rls, got) { switch {
t.Errorf("expected {%q}, got {%q}", rls, got) case rel.Info.Status.Code != got.Info.Status.Code:
t.Errorf("Expected status %s, got status %s", rel.Info.Status.Code, got.Info.Status.Code)
case rel.Version != got.Version:
t.Errorf("Expected version %d, got version %d", rel.Version, got.Version)
} }
} }
func TestConfigMapUpdate(t *testing.T) { // newTestFixture initializes a MockConfigMapsInterface.
// setup // ConfigMaps are created for each release provided.
key := "key-1" func newTestFixture(t *testing.T, releases ...*rspb.Release) *ConfigMaps {
rls := newTestRelease(key, 1, rspb.Status_SUPERSEDED) var mock MockConfigMapsInterface
mock.Init(t, releases...)
// create test fixture return NewConfigMaps(&mock)
cfgmaps := newTestFixture(t, rls) }
// update release version // newTestRelease creates a release object for testing.
rls.Version = 2 func newTestRelease(key string, version int32, status rspb.Status_Code) *rspb.Release {
return &rspb.Release{Name: key, Info: &rspb.Info{Status: &rspb.Status{Code: status}}, Version: version}
}
// update the release // MockConfigMapsInterface mocks a kubernetes ConfigMapsInterface
if err := cfgmaps.Update(rls); err != nil { type MockConfigMapsInterface struct {
t.Fatalf("failed to update release with key %q: %s", key, err) unversioned.ConfigMapsInterface
}
// fetch the updated release objects map[string]*api.ConfigMap
got, err := cfgmaps.Get(key) }
func (mock *MockConfigMapsInterface) Init(t *testing.T, releases ...*rspb.Release) {
mock.objects = map[string]*api.ConfigMap{}
for _, rls := range releases {
cfgmap, err := newConfigMapsObject(rls, nil)
if err != nil { if err != nil {
t.Fatalf("failed to get release with key %q: %s", key, err) t.Fatalf("Failed to create configmap: %s", err)
}
mock.objects[rls.Name] = cfgmap
} }
_ = got
//TODO: validate the version was update correctly
//if rls.Version != got.Version {
// t.Fatalf("expected version %d, got version %d", rls.Version, got.Version)
//}
} }
// newTestFixture prepopulates a mock implementation of a kubernetes func (mock *MockConfigMapsInterface) Get(name string) (*api.ConfigMap, error) {
// ConfigMapsInterface returning an initialized driver.ConfigMaps. object, ok := mock.objects[name]
func newTestFixture(t *testing.T, list ...*rspb.Release) *ConfigMaps { if !ok {
var objs []runtime.Object return nil, kberrs.NewNotFound(api.Resource("tests"), name)
}
return object, nil
}
for i := range list { func (mock *MockConfigMapsInterface) List(opts api.ListOptions) (*api.ConfigMapList, error) {
obj, err := newConfigMapsObject(list[i], nil) var list api.ConfigMapList
if err != nil { for _, cfgmap := range mock.objects {
t.Fatalf("failed to create object: %s", err) list.Items = append(list.Items, *cfgmap)
} }
objs = append(objs, obj) return &list, nil
}
func (mock *MockConfigMapsInterface) Create(cfgmap *api.ConfigMap) (*api.ConfigMap, error) {
name := cfgmap.ObjectMeta.Name
if object, ok := mock.objects[name]; ok {
return object, kberrs.NewAlreadyExists(api.Resource("tests"), name)
} }
mock.objects[name] = cfgmap
return cfgmap, nil
}
return NewConfigMaps(&testclient.FakeConfigMaps{ func (mock *MockConfigMapsInterface) Update(cfgmap *api.ConfigMap) (*api.ConfigMap, error) {
Fake: testclient.NewSimpleFake(objs...), name := cfgmap.ObjectMeta.Name
}) if _, ok := mock.objects[name]; !ok {
return nil, kberrs.NewNotFound(api.Resource("tests"), name)
}
mock.objects[name] = cfgmap
return cfgmap, nil
} }
// newTestRelease creates a release object for testing func (mock *MockConfigMapsInterface) Delete(name string) error {
func newTestRelease(key string, version int32, status rspb.Status_Code) *rspb.Release { if _, ok := mock.objects[name]; !ok {
return &rspb.Release{Name: key, Info: &rspb.Info{Status: &rspb.Status{Code: status}}, Version: version} return kberrs.NewNotFound(api.Resource("tests"), name)
}
delete(mock.objects, name)
return nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment