From b87cb44e9222435a8c4bc0f1a67fd3fd8d3d3b6b Mon Sep 17 00:00:00 2001 From: Wojciech Suszko Date: Thu, 5 Dec 2024 21:34:04 +0100 Subject: [PATCH] agent: Improve with changes from code review --- agent/app/Dockerfile | 2 +- .../internal/agent/node/agent/agent_test.go | 18 +++++------ agent/app/internal/agent/pods/agent/agent.go | 4 --- agent/app/pkg/envs/envs.go | 30 +++++++++++++++++++ docker-compose.test.yml | 2 ++ 5 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 agent/app/pkg/envs/envs.go diff --git a/agent/app/Dockerfile b/agent/app/Dockerfile index 9964333b..08465310 100644 --- a/agent/app/Dockerfile +++ b/agent/app/Dockerfile @@ -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"] diff --git a/agent/app/internal/agent/node/agent/agent_test.go b/agent/app/internal/agent/node/agent/agent_test.go index 586fe41e..f9181e79 100644 --- a/agent/app/internal/agent/node/agent/agent_test.go +++ b/agent/app/internal/agent/node/agent/agent_test.go @@ -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 { @@ -134,19 +137,16 @@ 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) @@ -154,9 +154,7 @@ func TestWatchFile(t *testing.T) { 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) diff --git a/agent/app/internal/agent/pods/agent/agent.go b/agent/app/internal/agent/pods/agent/agent.go index 0f464537..141efc37 100644 --- a/agent/app/internal/agent/pods/agent/agent.go +++ b/agent/app/internal/agent/pods/agent/agent.go @@ -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 @@ -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 diff --git a/agent/app/pkg/envs/envs.go b/agent/app/pkg/envs/envs.go new file mode 100644 index 00000000..585dd5fe --- /dev/null +++ b/agent/app/pkg/envs/envs.go @@ -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 +} diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 92ab6b68..597dd989 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -86,6 +86,8 @@ services: dockerfile: Dockerfile target: tests restart: always + environment: + - INTEGRATION_TEST_WAIT_MODIFIER=${INTEGRATION_TEST_WAIT_MODIFIER} command: - "--runningMode" - "local"