Skip to content

Commit

Permalink
agent: Improve with changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Woojciech committed Dec 5, 2024
1 parent e4c6d0d commit b87cb44
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion agent/app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FROM build AS tests
# Used in node agent tests
RUN mkdir -p /files && touch /files/file && chmod 777 /files/file

RUN echo "go env -w GOCACHE=/go/pkg/mod/ && go test -v -cover ./..." > test.sh
RUN echo "go env -w GOCACHE=/go/pkg/mod/ && go test -cover ./..." > test.sh
RUN chmod +x test.sh

ENTRYPOINT ["bash", "./test.sh"]
Expand Down
18 changes: 8 additions & 10 deletions agent/app/internal/agent/node/agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"time"

"github.com/Magpie-Monitor/magpie-monitor/agent/internal/agent/node/data"
"github.com/Magpie-Monitor/magpie-monitor/agent/pkg/envs"
"github.com/Magpie-Monitor/magpie-monitor/agent/pkg/tests"
"github.com/stretchr/testify/assert"
)

var INTEGRATION_TEST_WAIT_MODIFIER = envs.ConvertToInt("INTEGRATION_TEST_WAIT_MODIFIER")

func TestLogsSplit(t *testing.T) {

testCases := []struct {
Expand Down Expand Up @@ -134,29 +137,24 @@ func TestWatchFile(t *testing.T) {
}

go agent.watchFiles()
time.Sleep(3 * time.Second)
time.Sleep(3 * time.Second * time.Duration(INTEGRATION_TEST_WAIT_MODIFIER))

file, err := os.OpenFile(test.fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
t.Error("Error opening a file")
}
assert.NoError(t, err, "Error opening a file")

_, err = file.WriteString(test.logContent)
if err != nil {
t.Error("Error writing to file")
}
assert.NoError(t, err, "Error writing to file")

msg := <-results

// Add string escape character to the end of the content
assert.EqualValues(t, test.logContent+"\x00", msg.Content)
assert.Equal(t, test.clusterId, msg.ClusterId)
assert.Equal(t, test.nodeName, msg.Name)
assert.Equal(t, test.fileName, msg.Filename)

err = os.Truncate(test.fileName, 0)
if err != nil {
t.Error("Error wiping content of the file")
}
assert.NoError(t, err, "Error wiping content of the file")
}

t.Run(test.name, testFunc)
Expand Down
4 changes: 0 additions & 4 deletions agent/app/internal/agent/pods/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ func (a *Agent) fetchDaemonSetLogsSinceTime(namespace string, daemonSets []v2.Da
}

func (a *Agent) fetchPodLogsSinceTime(selector *metav1.LabelSelector, namespace string) ([][]data.Pod, error) {
fmt.Println("namespace:", namespace)

pods, err := a.kubernetesClient.GetPods(selector, namespace)
if err != nil {
return nil, err
Expand Down Expand Up @@ -266,8 +264,6 @@ func (a *Agent) fetchContainerLogsSinceTime(container *v1.Container, podName, na
logs, err := a.kubernetesClient.GetContainerLogsSinceTime(podName, container.Name, namespace, sinceTime, true)
afterTs := time.Now().UnixNano()

fmt.Println("logs:", logs)

if err != nil {
log.Println("Error fetching logs for Pod: ", podName, " container: ", container.Name)
return nil
Expand Down
30 changes: 30 additions & 0 deletions agent/app/pkg/envs/envs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package envs

import (
"fmt"
"os"
"strconv"
)

func ValidateEnvs(message string, envKeys []string) {

for _, env := range envKeys {
_, isSet := os.LookupEnv(env)
if !isSet {
panic(fmt.Sprintf(message, env))
}
}

}

// Validates if env exists and converts it to int type, panics on error
func ConvertToInt(env string) int {
ValidateEnvs("%s env variable not set", []string{env})

envInt, err := strconv.Atoi(os.Getenv(env))
if err != nil {
panic(fmt.Sprintf("%s has to be numeric", env))
}

return envInt
}
2 changes: 2 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ services:
dockerfile: Dockerfile
target: tests
restart: always
environment:
- INTEGRATION_TEST_WAIT_MODIFIER=${INTEGRATION_TEST_WAIT_MODIFIER}
command:
- "--runningMode"
- "local"
Expand Down

0 comments on commit b87cb44

Please sign in to comment.