Skip to content

Commit

Permalink
Add one extra test
Browse files Browse the repository at this point in the history
  • Loading branch information
mgianluc committed Nov 20, 2023
1 parent 531500f commit 6d88898
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ create-cluster: $(KIND) $(KUBECTL) ## Create a new kind cluster designed for dev
@echo "apply PersistentVolumeClaim"
$(KUBECTL) apply -f test/pvc.yaml

$(KUBECTL) apply -f test/configmap.yaml
$(KUBECTL) create configmap k8s-collector --from-file test/configmap.yaml

@echo 'Load k8s-collector image into cluster'
$(MAKE) load-image
Expand Down
51 changes: 47 additions & 4 deletions pkg/utils/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ import (
"github.com/gianlucam76/k8s_collector/pkg/utils"
)

var (
data = `resources:
- group: ""
version: v1
kind: Pod
namespace: default
- group: apps
version: v1
kind: Deployment
logs:
- namespace: kube-system
sinceSeconds:600`
)

var _ = Describe("Collect", func() {
It("loadConfiguration loads configuration from ConfigMap (YAML)", func() {
sinceSecond := int64(600)
Expand All @@ -29,7 +43,7 @@ var _ = Describe("Collect", func() {
},
}

jsonBytes, err := yaml.Marshal(collectorConfig)
dataBytes, err := yaml.Marshal(collectorConfig)
Expect(err).To(BeNil())

configMap := &corev1.ConfigMap{
Expand All @@ -38,7 +52,7 @@ var _ = Describe("Collect", func() {
Name: "foo",
},
Data: map[string]string{
"config": string(jsonBytes),
"config": string(dataBytes),
},
}

Expand Down Expand Up @@ -69,7 +83,7 @@ var _ = Describe("Collect", func() {
},
}

jsonBytes, err := json.Marshal(collectorConfig)
dataBytes, err := json.Marshal(collectorConfig)
Expect(err).To(BeNil())

configMap := &corev1.ConfigMap{
Expand All @@ -78,7 +92,7 @@ var _ = Describe("Collect", func() {
Name: "bar",
},
Data: map[string]string{
"config": string(jsonBytes),
"config": string(dataBytes),
},
}

Expand All @@ -95,4 +109,33 @@ var _ = Describe("Collect", func() {
Expect(err).To(BeNil())
Expect(collectorConfig).ToNot(BeNil())
})

It("loadConfiguration loads configuration from ConfigMap ", func() {
dataBytes, err := yaml.Marshal(data)
Expect(err).To(BeNil())

configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
Name: "test",
},
Data: map[string]string{
"config": string(dataBytes),
},
}

collector, err := utils.GetCollectorInstance(scheme, env.Config, "", configMap.Name)
Expect(err).To(BeNil())
Expect(k8sClient.Create(context.TODO(), configMap)).To(Succeed())

waitForObject(context.TODO(), k8sClient, configMap)

os.Setenv("COLLECTOR_NAMESPACE", configMap.Namespace)
config := textlogger.NewConfig(textlogger.Verbosity(1))
logger := textlogger.NewLogger(config)
collectorConfig, err := utils.LoadConfiguration(collector, context.TODO(), logger)
Expect(err).To(BeNil())
Expect(collectorConfig).ToNot(BeNil())
Expect(len(collectorConfig.Resources)).To(Equal(2))
})
})
18 changes: 11 additions & 7 deletions test/configmap.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
apiVersion: v1
data:
config.yaml: '{"resources":[{"group":"","version":"v1","kind":"Secret"},{"group":"apps","version":"v1","kind":"Deployment"}],"logs":[{"namespace":"kube-system","sinceSeconds":600}]}'
kind: ConfigMap
metadata:
name: k8s-collector
namespace: default
resources:
- group: ""
version: v1
kind: Pod
namespace: default
- group: apps
version: v1
kind: Deployment
logs:
- namespace: kube-system
sinceSeconds: 600

0 comments on commit 6d88898

Please sign in to comment.