-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add k8s unittest for k8s (#404)
Add unittest for k8s logic Signed-off-by: jcriadomarco <[email protected]>
- Loading branch information
Showing
6 changed files
with
318 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
package datasource | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/vmware/kube-fluentd-operator/config-reloader/config" | ||
"github.com/vmware/kube-fluentd-operator/config-reloader/datasource/kubedatasource" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/informers" | ||
testclient "k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
type testConfig struct { | ||
config config.Config | ||
expectErr bool | ||
expectedResult int | ||
namespaces []string | ||
configmap map[string]string | ||
configmapData map[string]string | ||
} | ||
|
||
func importK8sObjects(factory informers.SharedInformerFactory, namespaces []string, configmaps map[string]string, configmapData map[string]string) { | ||
for _, ns := range namespaces { | ||
factory.Core().V1().Namespaces().Informer().GetIndexer().Add( | ||
&corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: ns, | ||
}, | ||
}, | ||
) | ||
} | ||
for cm, ns := range configmaps { | ||
factory.Core().V1().ConfigMaps().Informer().GetIndexer().Add( | ||
&corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: cm, | ||
Namespace: ns, | ||
}, | ||
Data: configmapData, | ||
}, | ||
) | ||
} | ||
} | ||
|
||
// UnitTest for GetNamespaces while using configmap mode | ||
func TestGetNamespaces(t *testing.T) { | ||
assert := assert.New(t) | ||
configs := []testConfig{ | ||
{ | ||
//TestCase: One Namespace without any fluentd config. Result empty | ||
config: config.Config{ | ||
Datasource: "default", | ||
AnnotConfigmapName: "logging.csp.vmware.com/fluentd-configmap", | ||
AnnotStatus: "logging.csp.vmware.com/fluentd-status", | ||
ID: "default", | ||
}, | ||
expectErr: false, | ||
expectedResult: 0, | ||
namespaces: []string{ | ||
"test1", | ||
}, | ||
}, | ||
{ | ||
//TestCase: Two Namespaces, one without cm and one with empty fluentd config. Result empty | ||
config: config.Config{ | ||
Datasource: "default", | ||
AnnotConfigmapName: "logging.csp.vmware.com/fluentd-configmap", | ||
AnnotStatus: "logging.csp.vmware.com/fluentd-status", | ||
ID: "default", | ||
}, | ||
expectErr: false, | ||
expectedResult: 0, | ||
namespaces: []string{ | ||
"test-empty", | ||
"test-empty-2", | ||
}, | ||
configmap: map[string]string{ | ||
"fluentd-config": "test-empty", | ||
"my-configmap1": "test-empty", | ||
"my-configmap2": "test-empty-2", | ||
}, | ||
}, | ||
{ | ||
//TestCase: Two Namespaces, one fluentd config. Result 1 | ||
config: config.Config{ | ||
Datasource: "default", | ||
DefaultConfigmapName: "fluentd-config", | ||
AnnotConfigmapName: "logging.csp.vmware.com/fluentd-configmap", | ||
AnnotStatus: "logging.csp.vmware.com/fluentd-status", | ||
ID: "default", | ||
}, | ||
expectErr: false, | ||
expectedResult: 1, | ||
namespaces: []string{ | ||
"test-not-empty", | ||
"test-not-empty-2", | ||
}, | ||
configmap: map[string]string{ | ||
"fluentd-config": "test-not-empty", | ||
}, | ||
configmapData: map[string]string{"fluent.conf": "<match **>\n@type stdout\n</match>"}, | ||
}, | ||
} | ||
for _, config := range configs { | ||
// Prepare TestCase | ||
ctx := context.Background() | ||
clientset := testclient.NewSimpleClientset() | ||
factory := informers.NewSharedInformerFactory(clientset, 0) | ||
kubeds, _ := kubedatasource.NewConfigMapDS(ctx, &config.config, factory, make(chan time.Time, 1)) | ||
var ds = &kubeInformerConnection{ | ||
client: clientset, | ||
cfg: &config.config, | ||
nslist: factory.Core().V1().Namespaces().Lister(), | ||
cmlist: factory.Core().V1().ConfigMaps().Lister(), | ||
podlist: factory.Core().V1().Pods().Lister(), | ||
kubeds: kubeds, | ||
mountedLabels: make(map[string][]map[string]string), | ||
} | ||
importK8sObjects(factory, config.namespaces, config.configmap, config.configmapData) | ||
// Run Test GetNamespace | ||
namespaces, err := ds.GetNamespaces(ctx) | ||
// Check Test Result | ||
if err != nil { | ||
logrus.Fatalf(err.Error()) | ||
} | ||
assert.Equal(config.expectedResult, len(namespaces)) | ||
for index, ns := range namespaces { | ||
assert.Equal(config.namespaces[index], ns.Name) | ||
} | ||
} | ||
} | ||
|
||
func TestDiscoverNamespaces(t *testing.T) { | ||
assert := assert.New(t) | ||
configs := []testConfig{ | ||
{ | ||
//TestCase: Two Namespaces, emptu DefaultConfigmapName. Result 2 | ||
config: config.Config{ | ||
Datasource: "default", | ||
AnnotConfigmapName: "logging.csp.vmware.com/fluentd-configmap", | ||
AnnotStatus: "logging.csp.vmware.com/fluentd-status", | ||
ID: "default", | ||
}, | ||
expectErr: false, | ||
expectedResult: 2, | ||
namespaces: []string{ | ||
"test1", | ||
"test2", | ||
}, | ||
}, | ||
{ | ||
//TestCase: Two Namespaces, use DefaultConfigmapName. Result 1 | ||
config: config.Config{ | ||
Datasource: "default", | ||
DefaultConfigmapName: "fluentd-config", | ||
AnnotConfigmapName: "logging.csp.vmware.com/fluentd-configmap", | ||
AnnotStatus: "logging.csp.vmware.com/fluentd-status", | ||
ID: "default", | ||
}, | ||
expectErr: false, | ||
expectedResult: 1, | ||
namespaces: []string{ | ||
"test1", | ||
"test2", | ||
}, | ||
configmap: map[string]string{ | ||
"configmap1": "test1", | ||
"configmap2": "test2", | ||
"fluentd-config": "test1", | ||
}, | ||
}, | ||
} | ||
for _, config := range configs { | ||
// Prepare TestCase | ||
ctx := context.Background() | ||
clientset := testclient.NewSimpleClientset() | ||
factory := informers.NewSharedInformerFactory(clientset, 0) | ||
var ds = &kubeInformerConnection{ | ||
client: clientset, | ||
cfg: &config.config, | ||
nslist: factory.Core().V1().Namespaces().Lister(), | ||
cmlist: factory.Core().V1().ConfigMaps().Lister(), | ||
} | ||
importK8sObjects(factory, config.namespaces, config.configmap, config.configmapData) | ||
// Run Test discoverNamespaces | ||
namespaces, err := ds.discoverNamespaces(ctx) | ||
// Check Test Result | ||
if err != nil { | ||
logrus.Fatalf(err.Error()) | ||
} | ||
assert.Equal(len(namespaces), config.expectedResult) | ||
} | ||
} | ||
|
||
// Test the updateStatus config creating a namespace and check label and value | ||
func TestUpdateStatus(t *testing.T) { | ||
assert := assert.New(t) | ||
annotationValue := "Example annotation" | ||
namespace := "test-namespace" | ||
testCfg := &config.Config{ | ||
Datasource: "default", | ||
AnnotConfigmapName: "logging.csp.vmware.com/fluentd-configmap", | ||
AnnotStatus: "logging.csp.vmware.com/fluentd-status", | ||
ID: "default", | ||
} | ||
ctx := context.Background() | ||
clientset := testclient.NewSimpleClientset( | ||
&corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespace, | ||
}, | ||
}, | ||
) | ||
factory := informers.NewSharedInformerFactory(clientset, 0) | ||
var ds = &kubeInformerConnection{ | ||
client: clientset, | ||
cfg: testCfg, | ||
nslist: factory.Core().V1().Namespaces().Lister(), | ||
cmlist: factory.Core().V1().ConfigMaps().Lister(), | ||
} | ||
|
||
ds.UpdateStatus(ctx, namespace, annotationValue) | ||
ns, err := clientset.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) | ||
if err != nil { | ||
logrus.Fatalf("Unable to read namespace in cluster: %+v", err) | ||
} | ||
assert.Equal(ns.Annotations[testCfg.AnnotStatus], annotationValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.