Skip to content

Detect if defradb is being run in test mode #2801

@islamaliev

Description

@islamaliev

We currently employ a method for detecting test mode execution that may not be optimal or align with conventional practices:

func init() {
	arg := os.Args[0]
	// If the binary is a test binary, use a deterministic nonce.
	if strings.HasSuffix(arg, ".test") || strings.Contains(arg, "/defradb/tests/") {
		generateNonceFunc = generateTestNonce
		generateEncryptionKeyFunc = generateTestEncryptionKey
	}
}

The condition strings.Contains(arg, "/defradb/tests/") is specifically included to accommodate debug mode execution.

While we have explored using build tags (//go:build test) for conditional compilation, which would be the ideal solution, we encountered limitations. The build tag only takes effect when explicitly passed (-tags=test). We expected Go to automatically set a test-specific build tag during test runs, but it doesn't seem to be the case. Manually passing this flag is not a viable option as it would impede our ability to execute tests seamlessly from IDEs.

We need to implement a more robust and conventional method for test mode detection that:

  • Doesn't rely on string parsing of binary names or file paths
  • Works consistently across different execution environments (standard test runs, debug mode, IDE integrations)
  • Aligns with Go best practices for test configuration

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/testingRelated to any test or testing suitecode qualityRelated to improving code quality

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions