VC-35630: Add unit tests to the code that loads config/flags#563
VC-35630: Add unit tests to the code that loads config/flags#563inteon merged 3 commits intodisable-dump-config-startupfrom
Conversation
804abfd to
23d22aa
Compare
|
I've moved |
23d22aa to
0f8b639
Compare
0f8b639 to
bd8355e
Compare
At first, I had moved config.go and config_test.go to a separate folder to make things a bit cleaner, but it made reviewing super hard, so I ended up keeping them in the `agent` package.
bd8355e to
7f6334f
Compare
| false, | ||
| "Enables Prometheus metrics server on the agent (port: 8081).", | ||
| ) | ||
| agent.InitAgentCmdFlags(agentCmd, &agent.Flags) |
There was a problem hiding this comment.
Self-review: I've moved the definition of the flags for the agent command to the config package so that the struct and the flags they relate to are defined in the same file. I find it easier to keep things consistent this way.
| UploadPath string `yaml:"upload_path,omitempty"` | ||
| } | ||
|
|
||
| type AgentCmdFlags struct { |
There was a problem hiding this comment.
Self-review: these were defined as package-level variables, so I moved them to be a structs so that I can test getConfiguration.
|
|
||
| // Prometheus flag enabled Prometheus metrics endpoint to run on the agent | ||
| var Prometheus bool | ||
| var Flags AgentCmdFlags |
There was a problem hiding this comment.
Self-review: This variable is public so that the cmd/agent.go file can import the global var Flags to register the command line flags. These variables were defined as package-level variables and I moved them to be a struct so that I can test getConfiguration.
| // getConfiguration combines the input configuration with the flags passed to | ||
| // the agent and returns the final configuration as well as the Venafi client to | ||
| // be used to upload data. | ||
| func getConfiguration(log *log.Logger, cfg Config, flags AgentCmdFlags) (Config, client.Client, error) { |
There was a problem hiding this comment.
Self-review: I moved getConfiguration over from run.go as-is without any modification. This is because I think this belongs to config.go, not run.go. But let me know if it makes things too difficult to review.
pkg/agent/config_test.go
Outdated
| func TestGetConfiguration(t *testing.T) { | ||
| t.Run("minimal successful configuration", func(t *testing.T) { | ||
| got, cl, err := getConfiguration(discardLogs(t), | ||
| Config{Server: "http://localhost:8080", Period: 1 * time.Hour}, |
There was a problem hiding this comment.
Self-review: This URL isn't actually used, I guess I should have picked a clearer URL like https://fake.
There was a problem hiding this comment.
I've renamed it to http://api.venafi.eu.
The URL http://localhost:8080 suggested that this test was using a fake server, and didn't indicate clearly which API (the old jetstack-secure API or the newer Venafi Cloud Plaform API).
c8a94a5 to
0e8875d
Compare
pkg/agent/config_test.go
Outdated
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/d4l3k/messagediff" |
There was a problem hiding this comment.
Self-review: I'd be in favor of removing this dependency. Even if it's just a testing dep, it isn't needed: we can use testify's Equal which already gives us a good diff.
Testify, which we already import and that I trust, has the same functionality. github.com/d4l3k/messagediff is a relatively small Go project (250 stars) that hasn't been updated since Nov 11, 2020.
|
|
|
Something I forgot to mention: this PR is stacked on top of #564, meaning that:
|
| logs.Log.Fatalf("Failed to read config file: %s", err) | ||
| } | ||
|
|
||
| cfg, err := ParseConfig(b, Flags.StrictMode) |
There was a problem hiding this comment.
Post-merge comment: Oops, it looks like I made a mistake here... It should be Flags.VenafiCloudMode, not Flags.StrictMode!
| cfg, err := ParseConfig(b, Flags.StrictMode) | |
| cfg, err := ParseConfig(b, Flags.VenafiCloudMode) |
Stacked on top of #564.
Ref: VC-35630
I found that almost none of the "config/flags" code is tested, which made me wary of changing the code without adding more tests. And since I'll have to change this code again for VC-35630, I'd prefer writing some unit tests first.
This PR aims to change no actual logic. The changes you see are the refactoring I had to do to be able to unit test how configuration is loaded.