Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelgavache committed Dec 10, 2024
1 parent 7726487 commit 3089d41
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pkg/fleet/installer/setup/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
datadogConfFile = filepath.Join(configDir, "datadog.yaml")
logsConfFile = filepath.Join(configDir, "conf.d/configured_at_install_logs.yaml")
sparkConfigFile = filepath.Join(configDir, "conf.d/spark.d/spark.yaml")
injectTracerConfigFile = filepath.Join(configDir, "/etc/datadog-agent/inject/tracer.yaml")
injectTracerConfigFile = filepath.Join(configDir, "inject/tracer.yaml")
)

// HostInstaller is a struct that represents the agent agentConfiguration
Expand Down Expand Up @@ -61,21 +61,21 @@ type logsConfig struct {
type LogConfig struct {
Type string `yaml:"type"`
Path string `yaml:"path"`
Service string `yaml:"service"`
Source string `yaml:"source"`
Service string `yaml:"service,omitempty"`
Source string `yaml:"source,omitempty"`
}

type sparkConfig struct {
InitConfig interface{} `yaml:"init_config"`
InitConfig interface{} `yaml:"init_config,omitempty"`
Instances []SparkInstance `yaml:"instances"`
}

// SparkInstance is a struct that represents a single spark instance
type SparkInstance struct {
SparkURL string `yaml:"spark_url"`
SparkClusterMode string `yaml:"spark_cluster_mode"`
SparkClusterMode string `yaml:"spark_cluster_mode,omitempty"`
ClusterName string `yaml:"cluster_name"`
StreamingMetrics bool `yaml:"streaming_metrics"`
StreamingMetrics bool `yaml:"streaming_metrics,omitempty"`
}

type injectorConfig struct {
Expand All @@ -100,7 +100,7 @@ func NewHostInstaller(env *env.Env) (*HostInstaller, error) {
}

func newHostInstaller(env *env.Env, ddUID, ddGID int) (*HostInstaller, error) {
i := &HostInstaller{}
i := &HostInstaller{agentConfig: make(map[string]interface{})}
if env.APIKey == "" {
return nil, fmt.Errorf("DD_API key is required")
}
Expand Down
61 changes: 61 additions & 0 deletions pkg/fleet/installer/setup/common/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

//go:build !windows

// Package common contains the HostInstaller struct which is used to write the agent agentConfiguration to disk
package common

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/DataDog/datadog-agent/pkg/fleet/installer/env"
)

func assertFileContent(t *testing.T, file, content string) {
b, err := os.ReadFile(file)
assert.NoError(t, err)
assert.Equal(t, content, string(b))
}

func TestAgentConfigs(t *testing.T) {
configDir = t.TempDir()
datadogConfFile = filepath.Join(configDir, "datadog.yaml")
logsConfFile = filepath.Join(configDir, "conf.d/configured_at_install_logs.yaml")
sparkConfigFile = filepath.Join(configDir, "conf.d/spark.d/spark.yaml")

i, err := newHostInstaller(&env.Env{APIKey: "a"}, 0, 0)
assert.NotNil(t, i)
assert.Nil(t, err)

i.AddAgentConfig("key", "value")
i.AddLogConfig(LogConfig{Type: "file", Path: "/var/log/app.log", Service: "app"})
i.AddHostTag("k1", "v1")
i.AddHostTag("k2", "v2")
i.AddSparkInstance(SparkInstance{ClusterName: "cluster", SparkURL: "http://localhost:8080"})

assert.NoError(t, i.writeConfigs())
assertFileContent(t, datadogConfFile, `api_key: a
key: value
logs_enabled: true
tags:
- k1:v1
- k2:v2
`)

assertFileContent(t, logsConfFile, `logs:
- type: file
path: /var/log/app.log
service: app
`)
assertFileContent(t, sparkConfigFile, `instances:
- spark_url: http://localhost:8080
cluster_name: cluster
`)
}

0 comments on commit 3089d41

Please sign in to comment.