Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#861] test: add: integration test for logDir configuration #864

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/cmd/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,15 @@ func (ctx *Context) OpenLogFile(
if err != nil {
return nil, fmt.Errorf("failed to expand log directory: %w", err)
}
dagLogDir, err := cmdutil.EvalString(ctx, dag.LogDir)
if err != nil {
return nil, fmt.Errorf("failed to expand DAG log directory: %w", err)
}

config := LogFileSettings{
Prefix: prefix,
LogDir: logDir,
DAGLogDir: dag.LogDir,
DAGLogDir: dagLogDir,
DAGName: dag.Name,
RequestID: requestID,
}
Expand Down
4 changes: 4 additions & 0 deletions internal/cmdutil/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func BuildCommandEscapedString(command string, args []string) string {

// EvalString substitutes environment variables and commands in the input string
func EvalString(ctx context.Context, input string, opts ...EvalOption) (string, error) {
if input == "" {
return "", nil // nothing to do
}

options := newEvalOptions()
for _, opt := range opts {
opt(options)
Expand Down
4 changes: 2 additions & 2 deletions internal/integration/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

"github.com/dagu-org/dagu/internal/cmd"
"github.com/dagu-org/dagu/internal/test" // provides testSetup helper
"github.com/dagu-org/dagu/internal/test"
"github.com/stretchr/testify/require"
)

Expand All @@ -35,7 +35,7 @@ basePath: "/dagu"
require.NoError(t, os.WriteFile(configFile, []byte(configContent), 0644))

// Use the provided test helper to set up context and cancellation.
th := test.SetupCommand(t) // from github.com/dagu-org/dagu/internal/test
th := test.SetupCommand(t)

// Cancel the test context after a delay so the server doesn’t run forever.
go func() {
Expand Down
105 changes: 105 additions & 0 deletions internal/integration/start_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package integration_test

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

"github.com/dagu-org/dagu/internal/cmd"
"github.com/dagu-org/dagu/internal/test"
"github.com/stretchr/testify/require"
)

func TestServer_StartWithConfig(t *testing.T) {
testCases := []struct {
name string
setupFunc func(t *testing.T) (string, string) // returns configFile and tempDir
dagPath func(t *testing.T, tempDir string) string
envVarName string
}{
{
name: "GlobalLogDir",
setupFunc: func(t *testing.T) (string, string) {
tempDir := t.TempDir()
configFile := filepath.Join(tempDir, "config.yaml")
configContent := `logDir: ${TMP_LOGS_DIR}/logs`
require.NoError(t, os.WriteFile(configFile, []byte(configContent), 0644))
return configFile, tempDir
},
dagPath: func(t *testing.T, _ string) string {
return test.TestdataPath(t, path.Join("integration", "basic"))
},
envVarName: "TMP_LOGS_DIR",
},
{
name: "DAGLocalLogDir",
setupFunc: func(t *testing.T) (string, string) {
tempDir := t.TempDir()
dagFile := filepath.Join(tempDir, "basic.yaml")
dagContent := `
logDir: ${DAG_TMP_LOGS_DIR}/logs
steps:
- name: step1
command: echo "Hello, world!"
`
require.NoError(t, os.WriteFile(dagFile, []byte(dagContent), 0644))
return dagFile, tempDir
},
dagPath: func(_ *testing.T, tempDir string) string {
return filepath.Join(tempDir, "basic.yaml")
},
envVarName: "DAG_TMP_LOGS_DIR",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup test case
configFile, tempDir := tc.setupFunc(t)
os.Setenv(tc.envVarName, tempDir)
defer os.Unsetenv(tc.envVarName)

// Get DAG path
dagPath := tc.dagPath(t, tempDir)

// Run command
th := test.SetupCommand(t)
args := []string{"start"}
if tc.name == "GlobalLogDir" {
args = append(args, "--config", configFile)
}
args = append(args, dagPath)

th.RunCommand(t, cmd.CmdStart(), test.CmdTest{
Args: args,
ExpectedOut: []string{"DAG execution finished"},
})

// Verify log directory and files
verifyLogs(t, tempDir)
})
}
}

// verifyLogs checks if the expected log directory and files exist
func verifyLogs(t *testing.T, tempDir string) {
// Check if the logs directory was created
_, err := os.Stat(tempDir + "/logs/basic")
require.NoError(t, err)

// Check if the log file was created with the expected pattern
files, err := os.ReadDir(tempDir + "/logs/basic")
require.NoError(t, err)

// Look for a log file that matches the expected pattern
logFileFound := false
for _, file := range files {
if strings.HasPrefix(file.Name(), "start_basic.") && strings.HasSuffix(file.Name(), ".log") {
logFileFound = true
break
}
}
require.True(t, logFileFound, "No log file found with expected naming pattern")
}
3 changes: 3 additions & 0 deletions internal/testdata/integration/basic.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
steps:
- name: step1
command: echo "Hello, world!"
Loading