Skip to content

Commit 9a26ca2

Browse files
committed
update log collector test
1 parent 4349afb commit 9a26ca2

File tree

2 files changed

+27
-69
lines changed

2 files changed

+27
-69
lines changed

.github/workflows/logzio-logs-collector-test.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ jobs:
1010
test-helm-chart:
1111
name: Test Helm Chart on Kind
1212
runs-on: ubuntu-latest
13-
env:
14-
ENV_ID: logs-test-run-${{ github.run_id }}-number-${{ github.run_number }}
1513
steps:
14+
- name: Generate random id
15+
id: random_id
16+
run: echo "::set-output name=rand::$(echo $RANDOM)"
17+
18+
- name: Set ENV_ID
19+
run: echo "ENV_ID=logs-test-run-${{ steps.random_id.outputs.rand }}" >> $GITHUB_ENV
20+
1621
- name: Checkout repository
1722
uses: actions/checkout@v4
1823

@@ -52,7 +57,7 @@ jobs:
5257
kubectl apply -f tests/resources/logsgen.yaml
5358
kubectl rollout status deployment/log-generator --timeout=300s
5459
55-
- name: sleep for 2 minutes
60+
- name: sleep
5661
run: sleep 120
5762

5863
- name: Run Go Tests

tests/logs_e2e_test.go

Lines changed: 19 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"go.uber.org/zap"
78
"io"
89
"net/http"
910
"os"
1011
"testing"
1112
)
1213

13-
// LogResponse represents the structure of the logs API response
1414
type LogResponse struct {
1515
Hits struct {
1616
Total int `json:"total"`
1717
Hits []struct {
1818
Source struct {
19-
Kubernetes struct {
20-
ContainerImageTag string `json:"container_image_tag"`
21-
ContainerName string `json:"container_name"`
22-
ContainerImage string `json:"container_image"`
23-
NamespaceName string `json:"namespace_name"`
24-
PodName string `json:"pod_name"`
25-
PodID string `json:"pod_id"`
26-
Host string `json:"host"`
27-
} `json:"kubernetes"`
19+
ContainerImageTag string `json:"container_image_tag"`
20+
ContainerImageName string `json:"container_image_name"`
21+
ContainerName string `json:"k8s_container_name"`
22+
NamespaceName string `json:"k8s_namespace_name"`
23+
PodName string `json:"k8s_pod_name"`
24+
PodUID string `json:"k8s_pod_uid"`
25+
NodeName string `json:"k8s_node_name"`
26+
LogLevel string `json:"log_level"`
2827
} `json:"_source"`
2928
} `json:"hits"`
3029
} `json:"hits"`
@@ -44,21 +43,24 @@ func TestLogzioMonitoringLogs(t *testing.T) {
4443
if logResponse.Hits.Total == 0 {
4544
t.Errorf("No logs found")
4645
}
47-
// Verify required fields
48-
requiredFields := []string{"container_image_tag", "container_name", "container_image", "namespace_name", "pod_name", "pod_id", "host"}
49-
missingFields := verifyLogs(logResponse, requiredFields)
50-
if len(missingFields) > 0 {
51-
t.Errorf("Missing log fields: %v", missingFields)
46+
47+
for _, hit := range logResponse.Hits.Hits {
48+
kubernetes := hit.Source
49+
if kubernetes.ContainerImageTag == "" || kubernetes.ContainerName == "" || kubernetes.NamespaceName == "" || kubernetes.PodName == "" || kubernetes.PodUID == "" || kubernetes.NodeName == "" || kubernetes.ContainerImageName == "" || kubernetes.LogLevel == "" {
50+
logger.Error("Missing log fields", zap.Any("log", hit))
51+
t.Errorf("Missing log fields")
52+
break
53+
}
5254
}
5355
}
5456

55-
// fetchLogs fetches the logs from the logz.io API
5657
func fetchLogs(logsApiKey string) (*LogResponse, error) {
5758
url := fmt.Sprintf("%s/search", BaseLogzioApiUrl)
5859
client := &http.Client{}
5960
envID := os.Getenv("ENV_ID")
60-
query := fmt.Sprintf("env_id:%s AND type:agent-k8s", envID)
61+
query := fmt.Sprintf("env_id:%s AND type:agent-k8s AND k8s_deployment_name:log-generator", envID)
6162
formattedQuery := formatQuery(query)
63+
logger.Info("sending api request", zap.String("url", url), zap.String("query", query))
6264
req, err := http.NewRequest("POST", url, bytes.NewBufferString(formattedQuery))
6365
if err != nil {
6466
return nil, err
@@ -90,52 +92,3 @@ func fetchLogs(logsApiKey string) (*LogResponse, error) {
9092

9193
return &logResponse, nil
9294
}
93-
94-
// verifyLogs checks if the logs contain the required Kubernetes fields
95-
func verifyLogs(logResponse *LogResponse, requiredFields []string) []string {
96-
missingFieldsMap := make(map[string]bool, len(requiredFields))
97-
for _, field := range requiredFields {
98-
missingFieldsMap[field] = false
99-
}
100-
101-
for _, hit := range logResponse.Hits.Hits {
102-
kubernetes := hit.Source.Kubernetes
103-
if kubernetes.ContainerImageTag == "" {
104-
missingFieldsMap["container_image_tag"] = true
105-
break
106-
}
107-
if kubernetes.ContainerName == "" {
108-
missingFieldsMap["container_name"] = true
109-
break
110-
}
111-
if kubernetes.ContainerImage == "" {
112-
missingFieldsMap["container_image"] = true
113-
break
114-
}
115-
if kubernetes.NamespaceName == "" {
116-
missingFieldsMap["namespace_name"] = true
117-
break
118-
}
119-
if kubernetes.PodName == "" {
120-
missingFieldsMap["pod_name"] = true
121-
break
122-
}
123-
if kubernetes.PodID == "" {
124-
missingFieldsMap["pod_id"] = true
125-
break
126-
}
127-
if kubernetes.Host == "" {
128-
missingFieldsMap["host"] = true
129-
break
130-
}
131-
}
132-
133-
var missingFields []string
134-
for field, value := range missingFieldsMap {
135-
if value == true {
136-
missingFields = append(missingFields, field)
137-
}
138-
}
139-
140-
return missingFields
141-
}

0 commit comments

Comments
 (0)