This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
config: added tests for various hostname acquisition strategies #472
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,11 +165,8 @@ func (c *AgentConfig) LoadYaml(path string) error { | |
return nil | ||
} | ||
|
||
// LoadEnv reads environment variable values into the config. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love the fact this does not need to be public any more as |
||
func (c *AgentConfig) LoadEnv() { c.loadEnv() } | ||
|
||
// Validate validates if the current configuration is good for the agent to start with. | ||
func (c *AgentConfig) Validate() error { | ||
func (c *AgentConfig) validate() error { | ||
if c.APIKey == "" { | ||
return ErrMissingAPIKey | ||
} | ||
|
@@ -181,6 +178,10 @@ func (c *AgentConfig) Validate() error { | |
return nil | ||
} | ||
|
||
// fallbackHostnameFunc specifies the function to use for obtaining the hostname | ||
// when it can not be obtained by any other means. It is replaced in tests. | ||
var fallbackHostnameFunc = os.Hostname | ||
|
||
// acquireHostname attempts to acquire a hostname for this configuration. It | ||
// tries to shell out to the infrastructure agent for this, if DD_AGENT_BIN is | ||
// set, otherwise falling back to os.Hostname. | ||
|
@@ -205,17 +206,31 @@ func (c *AgentConfig) acquireHostname() error { | |
err := cmd.Run() | ||
c.Hostname = strings.TrimSpace(out.String()) | ||
if err != nil || c.Hostname == "" { | ||
c.Hostname, err = os.Hostname() | ||
c.Hostname, err = fallbackHostnameFunc() | ||
} | ||
if c.Hostname == "" { | ||
err = ErrMissingHostname | ||
} | ||
return err | ||
} | ||
|
||
// Load attempts to load the configuration from the given path. If it's not found | ||
// it returns an error and a default configuration. | ||
// Load returns a new configuration based on the given path. The path must not necessarily exist | ||
// and a valid configuration can be returned based on defaults and environment variables. If a | ||
// valid configuration can not be obtained, an error is returned. | ||
func Load(path string) (*AgentConfig, error) { | ||
cfg, err := loadFile(path) | ||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
} else { | ||
log.Infof("Loaded configuration: %s", cfg.ConfigPath) | ||
} | ||
cfg.loadEnv() | ||
return cfg, cfg.validate() | ||
} | ||
|
||
func loadFile(path string) (*AgentConfig, error) { | ||
cfgPath := path | ||
if cfgPath == flags.DefaultConfigPath && !osutil.Exists(cfgPath) && osutil.Exists(agent5Config) { | ||
// attempting to load inexistent default path, but found existing Agent 5 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic was moved into
config.Load
because config should be validated when loaded, not separately using another call. This way, the config is loaded, validated, and overridden from environment in one call.