Add unit test for Secrets/ConfigMaps (#7765)

* test(pkg/storage/secrets): make MockSecretsInterface.List follow ListOptions

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>

* test(pkg/storage/secrets): add unit test for Secrets.Query

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>

* test(pkg/storage/cfgmaps): make MockConfigMapsInterface.List follow ListOptions

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>

* test(pkg/storage/cfgmaps): add unit test for ConfigMaps.Query

Signed-off-by: Lu Fengqi <lufq.fnst@cn.fujitsu.com>
pull/7928/head
Lu Fengqi 5 years ago committed by GitHub
parent ad3ba46de1
commit 9e1f381bf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -130,6 +130,30 @@ func TestConfigMapList(t *testing.T) {
}
}
func TestConfigMapQuery(t *testing.T) {
cfgmaps := newTestFixtureCfgMaps(t, []*rspb.Release{
releaseStub("key-1", 1, "default", rspb.StatusUninstalled),
releaseStub("key-2", 1, "default", rspb.StatusUninstalled),
releaseStub("key-3", 1, "default", rspb.StatusDeployed),
releaseStub("key-4", 1, "default", rspb.StatusDeployed),
releaseStub("key-5", 1, "default", rspb.StatusSuperseded),
releaseStub("key-6", 1, "default", rspb.StatusSuperseded),
}...)
rls, err := cfgmaps.Query(map[string]string{"status": "deployed"})
if err != nil {
t.Errorf("Failed to query: %s", err)
}
if len(rls) != 2 {
t.Errorf("Expected 2 results, got %d", len(rls))
}
_, err = cfgmaps.Query(map[string]string{"name": "notExist"})
if err != ErrReleaseNotFound {
t.Errorf("Expected {%v}, got {%v}", ErrReleaseNotFound, err)
}
}
func TestConfigMapCreate(t *testing.T) {
cfgmaps := newTestFixtureCfgMaps(t)

@ -24,6 +24,7 @@ import (
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kblabels "k8s.io/apimachinery/pkg/labels"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
rspb "helm.sh/helm/v3/pkg/release"
@ -114,8 +115,16 @@ func (mock *MockConfigMapsInterface) Get(_ context.Context, name string, _ metav
// List returns the a of ConfigMaps.
func (mock *MockConfigMapsInterface) List(_ context.Context, _ metav1.ListOptions) (*v1.ConfigMapList, error) {
var list v1.ConfigMapList
labelSelector, err := kblabels.Parse(opts.LabelSelector)
if err != nil {
return nil, err
}
for _, cfgmap := range mock.objects {
list.Items = append(list.Items, *cfgmap)
if labelSelector.Matches(kblabels.Set(cfgmap.ObjectMeta.Labels)) {
list.Items = append(list.Items, *cfgmap)
}
}
return &list, nil
}
@ -192,8 +201,16 @@ func (mock *MockSecretsInterface) Get(_ context.Context, name string, _ metav1.G
// List returns the a of Secret.
func (mock *MockSecretsInterface) List(_ context.Context, _ metav1.ListOptions) (*v1.SecretList, error) {
var list v1.SecretList
labelSelector, err := kblabels.Parse(opts.LabelSelector)
if err != nil {
return nil, err
}
for _, secret := range mock.objects {
list.Items = append(list.Items, *secret)
if labelSelector.Matches(kblabels.Set(secret.ObjectMeta.Labels)) {
list.Items = append(list.Items, *secret)
}
}
return &list, nil
}

@ -130,6 +130,30 @@ func TestSecretList(t *testing.T) {
}
}
func TestSecretQuery(t *testing.T) {
secrets := newTestFixtureSecrets(t, []*rspb.Release{
releaseStub("key-1", 1, "default", rspb.StatusUninstalled),
releaseStub("key-2", 1, "default", rspb.StatusUninstalled),
releaseStub("key-3", 1, "default", rspb.StatusDeployed),
releaseStub("key-4", 1, "default", rspb.StatusDeployed),
releaseStub("key-5", 1, "default", rspb.StatusSuperseded),
releaseStub("key-6", 1, "default", rspb.StatusSuperseded),
}...)
rls, err := secrets.Query(map[string]string{"status": "deployed"})
if err != nil {
t.Fatalf("Failed to query: %s", err)
}
if len(rls) != 2 {
t.Fatalf("Expected 2 results, actual %d", len(rls))
}
_, err = secrets.Query(map[string]string{"name": "notExist"})
if err != ErrReleaseNotFound {
t.Errorf("Expected {%v}, got {%v}", ErrReleaseNotFound, err)
}
}
func TestSecretCreate(t *testing.T) {
secrets := newTestFixtureSecrets(t)

Loading…
Cancel
Save