mirror of https://github.com/helm/helm
parent
36606cf152
commit
383a9c186a
@ -0,0 +1,50 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// labels is a map of key value pairs to be included as metadata in a configmap object.
|
||||
type labels map[string]string
|
||||
|
||||
func (lbs *labels) init() { *lbs = labels(make(map[string]string)) }
|
||||
func (lbs labels) get(key string) string { return lbs[key] }
|
||||
func (lbs labels) set(key, val string) { lbs[key] = val }
|
||||
|
||||
func (lbs labels) keys() (ls []string) {
|
||||
for key := range lbs {
|
||||
ls = append(ls, key)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (lbs labels) match(set labels) bool {
|
||||
for _, key := range set.keys() {
|
||||
if lbs.get(key) != set.get(key) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (lbs labels) toMap() map[string]string { return lbs }
|
||||
|
||||
func (lbs *labels) fromMap(kvs map[string]string) {
|
||||
for k, v := range kvs {
|
||||
lbs.set(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (lbs labels) dump(w io.Writer) error {
|
||||
var b bytes.Buffer
|
||||
|
||||
fmt.Fprintln(&b, "labels:")
|
||||
for k, v := range lbs {
|
||||
fmt.Fprintf(&b, "\t- %q -> %q\n", k, v)
|
||||
}
|
||||
|
||||
_, err := w.Write(b.Bytes())
|
||||
return err
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLabelsMatch(t *testing.T) {
|
||||
var tests = []struct {
|
||||
desc string
|
||||
set1 labels
|
||||
set2 labels
|
||||
expect bool
|
||||
}{
|
||||
{
|
||||
"equal labels sets",
|
||||
labels(map[string]string{"KEY_A": "VAL_A", "KEY_B": "VAL_B"}),
|
||||
labels(map[string]string{"KEY_A": "VAL_A", "KEY_B": "VAL_B"}),
|
||||
true,
|
||||
},
|
||||
{
|
||||
"disjoint label sets",
|
||||
labels(map[string]string{"KEY_C": "VAL_C", "KEY_D": "VAL_D"}),
|
||||
labels(map[string]string{"KEY_A": "VAL_A", "KEY_B": "VAL_B"}),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if !tt.set1.match(tt.set2) && tt.expect {
|
||||
t.Fatalf("Expected match '%s'\n", tt.desc)
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
rspb "k8s.io/helm/pkg/proto/hapi/release"
|
||||
)
|
||||
|
||||
// records holds a list of in-memory release records
|
||||
type records []*record
|
||||
|
||||
func (rs records) Len() int { return len(rs) }
|
||||
func (rs records) Swap(i, j int) { rs[i], rs[j] = rs[j], rs[i] }
|
||||
func (rs records) Less(i, j int) bool { return rs[i].rls.Version < rs[j].rls.Version }
|
||||
|
||||
func (rs *records) Add(r *record) error {
|
||||
if r == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if rs.Exists(r.key) {
|
||||
return ErrReleaseExists
|
||||
}
|
||||
|
||||
*rs = append(*rs, r)
|
||||
sort.Sort(*rs)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs records) Get(key string) *record {
|
||||
if i, ok := rs.Index(key); ok {
|
||||
return rs[i]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs *records) Iter(fn func(int, *record) bool) {
|
||||
cp := make([]*record, len(*rs))
|
||||
copy(cp, *rs)
|
||||
|
||||
for i, r := range cp {
|
||||
if !fn(i, r) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (rs *records) Index(key string) (int, bool) {
|
||||
for i, r := range *rs {
|
||||
if r.key == key {
|
||||
return i, true
|
||||
}
|
||||
}
|
||||
return -1, false
|
||||
}
|
||||
|
||||
func (rs records) Exists(key string) bool {
|
||||
_, ok := rs.Index(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (rs *records) Remove(key string) (r *record) {
|
||||
if i, ok := rs.Index(key); ok {
|
||||
return rs.removeAt(i)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs *records) Replace(key string, rec *record) *record {
|
||||
if i, ok := rs.Index(key); ok {
|
||||
old := (*rs)[i]
|
||||
(*rs)[i] = rec
|
||||
return old
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rs records) FindByVersion(vers int32) (int, bool) {
|
||||
i := sort.Search(len(rs), func(i int) bool {
|
||||
return rs[i].rls.Version == vers
|
||||
})
|
||||
if i < len(rs) && rs[i].rls.Version == vers {
|
||||
return i, true
|
||||
}
|
||||
return i, false
|
||||
}
|
||||
|
||||
func (rs *records) removeAt(index int) *record {
|
||||
r := (*rs)[index]
|
||||
(*rs)[index] = nil
|
||||
copy((*rs)[index:], (*rs)[index+1:])
|
||||
*rs = (*rs)[:len(*rs)-1]
|
||||
return r
|
||||
}
|
||||
|
||||
// record is the data structure used to cache releases
|
||||
// for the in-memory storage driver
|
||||
type record struct {
|
||||
key string
|
||||
lbs labels
|
||||
rls *rspb.Release
|
||||
}
|
||||
|
||||
// newRecord creates a new in-memory release record
|
||||
func newRecord(key string, rls *rspb.Release) *record {
|
||||
var lbs labels
|
||||
|
||||
lbs.init()
|
||||
lbs.set("NAME", rls.Name)
|
||||
lbs.set("STATUS", rspb.Status_Code_name[int32(rls.Info.Status.Code)])
|
||||
lbs.set("VERSION", strconv.Itoa(int(rls.Version)))
|
||||
|
||||
return &record{key: key, lbs: lbs, rls: rls}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
rspb "k8s.io/helm/pkg/proto/hapi/release"
|
||||
)
|
||||
|
||||
func TestRecordsAdd(t *testing.T) {
|
||||
rs := records([]*record{
|
||||
newRecord("rls-a.v1", releaseStub("rls-a", 1, rspb.Status_SUPERSEDED)),
|
||||
newRecord("rls-a.v2", releaseStub("rls-a", 2, rspb.Status_DEPLOYED)),
|
||||
})
|
||||
|
||||
var tests = []struct {
|
||||
desc string
|
||||
key string
|
||||
ok bool
|
||||
rec *record
|
||||
}{
|
||||
{
|
||||
"add valid key",
|
||||
"rls-a.v3",
|
||||
false,
|
||||
newRecord("rls-a.v3", releaseStub("rls-a", 3, rspb.Status_SUPERSEDED)),
|
||||
},
|
||||
{
|
||||
"add already existing key",
|
||||
"rls-a.v1",
|
||||
true,
|
||||
newRecord("rls-a.v1", releaseStub("rls-a", 1, rspb.Status_DEPLOYED)),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
if err := rs.Add(tt.rec); err != nil {
|
||||
if !tt.ok {
|
||||
t.Fatalf("failed: %q: %s\n", tt.desc, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecordsRemove(t *testing.T) {
|
||||
var tests = []struct {
|
||||
desc string
|
||||
key string
|
||||
ok bool
|
||||
}{
|
||||
{"remove valid key", "rls-a.v1", false},
|
||||
{"remove invalid key", "rls-a.v", true},
|
||||
{"remove non-existant key", "rls-z.v1", true},
|
||||
}
|
||||
|
||||
rs := records([]*record{
|
||||
newRecord("rls-a.v1", releaseStub("rls-a", 1, rspb.Status_SUPERSEDED)),
|
||||
newRecord("rls-a.v2", releaseStub("rls-a", 2, rspb.Status_DEPLOYED)),
|
||||
})
|
||||
|
||||
for _, tt := range tests {
|
||||
if r := rs.Remove(tt.key); r == nil {
|
||||
if !tt.ok {
|
||||
t.Fatalf("Failed to %q (key = %s). Expected nil, got %s",
|
||||
tt.desc,
|
||||
tt.key,
|
||||
r,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
package driver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
rspb "k8s.io/helm/pkg/proto/hapi/release"
|
||||
"k8s.io/kubernetes/pkg/client/unversioned"
|
||||
kberrs "k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
func releaseStub(name string, vers int32, code rspb.Status_Code) *rspb.Release {
|
||||
return &rspb.Release{
|
||||
Name: name,
|
||||
Version: vers,
|
||||
Info: &rspb.Info{Status: &rspb.Status{Code: code}},
|
||||
}
|
||||
}
|
||||
|
||||
func testKey(name string, vers int32) string {
|
||||
return fmt.Sprintf("%s.v%d", name, vers)
|
||||
}
|
||||
|
||||
func tsFixtureMemory(t *testing.T) *Memory {
|
||||
hs := []*rspb.Release{
|
||||
// rls-a
|
||||
releaseStub("rls-a", 4, rspb.Status_DEPLOYED),
|
||||
releaseStub("rls-a", 1, rspb.Status_SUPERSEDED),
|
||||
releaseStub("rls-a", 3, rspb.Status_SUPERSEDED),
|
||||
releaseStub("rls-a", 2, rspb.Status_SUPERSEDED),
|
||||
// rls-b
|
||||
releaseStub("rls-b", 4, rspb.Status_DEPLOYED),
|
||||
releaseStub("rls-b", 1, rspb.Status_SUPERSEDED),
|
||||
releaseStub("rls-b", 3, rspb.Status_SUPERSEDED),
|
||||
releaseStub("rls-b", 2, rspb.Status_SUPERSEDED),
|
||||
}
|
||||
|
||||
mem := NewMemory()
|
||||
for _, tt := range hs {
|
||||
err := mem.Create(testKey(tt.Name, tt.Version), tt)
|
||||
if err != nil {
|
||||
t.Fatalf("Test setup failed to create: %s\n", err)
|
||||
}
|
||||
}
|
||||
return mem
|
||||
}
|
||||
|
||||
// newTestFixture initializes a MockConfigMapsInterface.
|
||||
// ConfigMaps are created for each release provided.
|
||||
func newTestFixtureCfgMaps(t *testing.T, releases ...*rspb.Release) *ConfigMaps {
|
||||
var mock MockConfigMapsInterface
|
||||
mock.Init(t, releases...)
|
||||
|
||||
return NewConfigMaps(&mock)
|
||||
}
|
||||
|
||||
// MockConfigMapsInterface mocks a kubernetes ConfigMapsInterface
|
||||
type MockConfigMapsInterface struct {
|
||||
unversioned.ConfigMapsInterface
|
||||
|
||||
objects map[string]*api.ConfigMap
|
||||
}
|
||||
|
||||
func (mock *MockConfigMapsInterface) Init(t *testing.T, releases ...*rspb.Release) {
|
||||
mock.objects = map[string]*api.ConfigMap{}
|
||||
|
||||
for _, rls := range releases {
|
||||
objkey := testKey(rls.Name, rls.Version)
|
||||
|
||||
cfgmap, err := newConfigMapsObject(objkey, rls, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create configmap: %s", err)
|
||||
}
|
||||
mock.objects[objkey] = cfgmap
|
||||
}
|
||||
}
|
||||
|
||||
func (mock *MockConfigMapsInterface) Get(name string) (*api.ConfigMap, error) {
|
||||
object, ok := mock.objects[name]
|
||||
if !ok {
|
||||
return nil, kberrs.NewNotFound(api.Resource("tests"), name)
|
||||
}
|
||||
return object, nil
|
||||
}
|
||||
|
||||
func (mock *MockConfigMapsInterface) List(opts api.ListOptions) (*api.ConfigMapList, error) {
|
||||
var list api.ConfigMapList
|
||||
for _, cfgmap := range mock.objects {
|
||||
list.Items = append(list.Items, *cfgmap)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (mock *MockConfigMapsInterface) Update(cfgmap *api.ConfigMap) (*api.ConfigMap, error) {
|
||||
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
|
||||
}
|
||||
|
||||
func (mock *MockConfigMapsInterface) Delete(name string) error {
|
||||
if _, ok := mock.objects[name]; !ok {
|
||||
return kberrs.NewNotFound(api.Resource("tests"), name)
|
||||
}
|
||||
delete(mock.objects, name)
|
||||
return nil
|
||||
}
|
Loading…
Reference in new issue