Skip to content

Commit 6d88898

Browse files
author
mgianluc
committed
Add one extra test
1 parent 531500f commit 6d88898

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ create-cluster: $(KIND) $(KUBECTL) ## Create a new kind cluster designed for dev
147147
@echo "apply PersistentVolumeClaim"
148148
$(KUBECTL) apply -f test/pvc.yaml
149149

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

152152
@echo 'Load k8s-collector image into cluster'
153153
$(MAKE) load-image

pkg/utils/collect_test.go

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ import (
1616
"github.com/gianlucam76/k8s_collector/pkg/utils"
1717
)
1818

19+
var (
20+
data = `resources:
21+
- group: ""
22+
version: v1
23+
kind: Pod
24+
namespace: default
25+
- group: apps
26+
version: v1
27+
kind: Deployment
28+
logs:
29+
- namespace: kube-system
30+
sinceSeconds:600`
31+
)
32+
1933
var _ = Describe("Collect", func() {
2034
It("loadConfiguration loads configuration from ConfigMap (YAML)", func() {
2135
sinceSecond := int64(600)
@@ -29,7 +43,7 @@ var _ = Describe("Collect", func() {
2943
},
3044
}
3145

32-
jsonBytes, err := yaml.Marshal(collectorConfig)
46+
dataBytes, err := yaml.Marshal(collectorConfig)
3347
Expect(err).To(BeNil())
3448

3549
configMap := &corev1.ConfigMap{
@@ -38,7 +52,7 @@ var _ = Describe("Collect", func() {
3852
Name: "foo",
3953
},
4054
Data: map[string]string{
41-
"config": string(jsonBytes),
55+
"config": string(dataBytes),
4256
},
4357
}
4458

@@ -69,7 +83,7 @@ var _ = Describe("Collect", func() {
6983
},
7084
}
7185

72-
jsonBytes, err := json.Marshal(collectorConfig)
86+
dataBytes, err := json.Marshal(collectorConfig)
7387
Expect(err).To(BeNil())
7488

7589
configMap := &corev1.ConfigMap{
@@ -78,7 +92,7 @@ var _ = Describe("Collect", func() {
7892
Name: "bar",
7993
},
8094
Data: map[string]string{
81-
"config": string(jsonBytes),
95+
"config": string(dataBytes),
8296
},
8397
}
8498

@@ -95,4 +109,33 @@ var _ = Describe("Collect", func() {
95109
Expect(err).To(BeNil())
96110
Expect(collectorConfig).ToNot(BeNil())
97111
})
112+
113+
It("loadConfiguration loads configuration from ConfigMap ", func() {
114+
dataBytes, err := yaml.Marshal(data)
115+
Expect(err).To(BeNil())
116+
117+
configMap := &corev1.ConfigMap{
118+
ObjectMeta: metav1.ObjectMeta{
119+
Namespace: "default",
120+
Name: "test",
121+
},
122+
Data: map[string]string{
123+
"config": string(dataBytes),
124+
},
125+
}
126+
127+
collector, err := utils.GetCollectorInstance(scheme, env.Config, "", configMap.Name)
128+
Expect(err).To(BeNil())
129+
Expect(k8sClient.Create(context.TODO(), configMap)).To(Succeed())
130+
131+
waitForObject(context.TODO(), k8sClient, configMap)
132+
133+
os.Setenv("COLLECTOR_NAMESPACE", configMap.Namespace)
134+
config := textlogger.NewConfig(textlogger.Verbosity(1))
135+
logger := textlogger.NewLogger(config)
136+
collectorConfig, err := utils.LoadConfiguration(collector, context.TODO(), logger)
137+
Expect(err).To(BeNil())
138+
Expect(collectorConfig).ToNot(BeNil())
139+
Expect(len(collectorConfig.Resources)).To(Equal(2))
140+
})
98141
})

test/configmap.yaml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
apiVersion: v1
2-
data:
3-
config.yaml: '{"resources":[{"group":"","version":"v1","kind":"Secret"},{"group":"apps","version":"v1","kind":"Deployment"}],"logs":[{"namespace":"kube-system","sinceSeconds":600}]}'
4-
kind: ConfigMap
5-
metadata:
6-
name: k8s-collector
7-
namespace: default
1+
resources:
2+
- group: ""
3+
version: v1
4+
kind: Pod
5+
namespace: default
6+
- group: apps
7+
version: v1
8+
kind: Deployment
9+
logs:
10+
- namespace: kube-system
11+
sinceSeconds: 600

0 commit comments

Comments
 (0)