Skip to content

Commit 7f6334f

Browse files
committed
tests: add unit tests to the code that loads config/flags
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.
1 parent f980359 commit 7f6334f

File tree

6 files changed

+642
-384
lines changed

6 files changed

+642
-384
lines changed

cmd/agent.go

Lines changed: 4 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package cmd
33
import (
44
"fmt"
55
"io/ioutil"
6-
"os"
7-
"time"
86

97
"github.com/jetstack/preflight/pkg/agent"
108
"github.com/jetstack/preflight/pkg/logs"
@@ -37,16 +35,16 @@ var agentRBACCmd = &cobra.Command{
3735
Long: `Print RBAC string by reading GVRs`,
3836
Run: func(cmd *cobra.Command, args []string) {
3937

40-
b, err := ioutil.ReadFile(agent.ConfigFilePath)
38+
b, err := ioutil.ReadFile(agent.Flags.ConfigFilePath)
4139
if err != nil {
4240
logs.Log.Fatalf("Failed to read config file: %s", err)
4341
}
44-
config, err := agent.ParseConfig(b, false)
42+
cfg, err := agent.ParseConfig(b, false)
4543
if err != nil {
4644
logs.Log.Fatalf("Failed to parse config file: %s", err)
4745
}
4846

49-
out := permissions.GenerateFullManifest(config.DataGatherers)
47+
out := permissions.GenerateFullManifest(cfg.DataGatherers)
5048
fmt.Print(out)
5149
},
5250
}
@@ -55,119 +53,5 @@ func init() {
5553
rootCmd.AddCommand(agentCmd)
5654
agentCmd.AddCommand(agentInfoCmd)
5755
agentCmd.AddCommand(agentRBACCmd)
58-
agentCmd.PersistentFlags().StringVarP(
59-
&agent.ConfigFilePath,
60-
"agent-config-file",
61-
"c",
62-
"./agent.yaml",
63-
"Config file location, default is `agent.yaml` in the current working directory.",
64-
)
65-
agentCmd.PersistentFlags().DurationVarP(
66-
&agent.Period,
67-
"period",
68-
"p",
69-
0,
70-
"Override time between scans in the configuration file (given as XhYmZs).",
71-
)
72-
agentCmd.PersistentFlags().StringVarP(
73-
&agent.CredentialsPath,
74-
"credentials-file",
75-
"k",
76-
"",
77-
"Location of the credentials file. For OAuth2 based authentication.",
78-
)
79-
agentCmd.PersistentFlags().BoolVarP(
80-
&agent.VenafiCloudMode,
81-
"venafi-cloud",
82-
"",
83-
false,
84-
"Runs agent with parsing config (and credentials file if provided) in Venafi Cloud format if true.",
85-
)
86-
agentCmd.PersistentFlags().StringVarP(
87-
&agent.ClientID,
88-
"client-id",
89-
"",
90-
"",
91-
"Venafi Cloud Service Account client ID. If you use this flag you don't need to use --venafi-cloud as it will assume you are authenticating against Venafi Cloud. Using this removes the need to use a credentials file with Venafi Cloud mode.",
92-
)
93-
agentCmd.PersistentFlags().StringVarP(
94-
&agent.PrivateKeyPath,
95-
"private-key-path",
96-
"",
97-
"/etc/venafi/agent/key/privatekey.pem",
98-
"Venafi Cloud Service Account private key path.",
99-
)
100-
agentCmd.PersistentFlags().BoolVarP(
101-
&agent.OneShot,
102-
"one-shot",
103-
"",
104-
false,
105-
"Runs agent a single time if true, or continously if false",
106-
)
107-
agentCmd.PersistentFlags().StringVarP(
108-
&agent.OutputPath,
109-
"output-path",
110-
"",
111-
"",
112-
"Output file path, if used, it will write data to a local file instead of uploading to the preflight server",
113-
)
114-
agentCmd.PersistentFlags().StringVarP(
115-
&agent.InputPath,
116-
"input-path",
117-
"",
118-
"",
119-
"Input file path, if used, it will read data from a local file instead of gathering data from clusters",
120-
)
121-
agentCmd.PersistentFlags().DurationVarP(
122-
&agent.BackoffMaxTime,
123-
"backoff-max-time",
124-
"",
125-
10*time.Minute,
126-
"Max time for retrying failed data gatherers (given as XhYmZs).",
127-
)
128-
agentCmd.PersistentFlags().BoolVarP(
129-
&agent.StrictMode,
130-
"strict",
131-
"",
132-
false,
133-
"Runs agent in strict mode. No retry attempts will be made for a missing data gatherer's data.",
134-
)
135-
agentCmd.PersistentFlags().StringVar(
136-
&agent.APIToken,
137-
"api-token",
138-
os.Getenv("API_TOKEN"),
139-
"Token used for authentication when API tokens are in use on the backend",
140-
)
141-
agentCmd.PersistentFlags().StringVar(
142-
&agent.VenConnName,
143-
"venafi-connection",
144-
"",
145-
"Name of the VenafiConnection to be used. Using this flag will enable the VenafiConnection mode.",
146-
)
147-
agentCmd.PersistentFlags().StringVar(
148-
&agent.VenConnNS,
149-
"venafi-connection-namespace",
150-
"",
151-
"Namespace of the VenafiConnection to be used. It is only useful when the VenafiConnection isn't in the same namespace as the agent. The field `allowReferencesFrom` must be present on the cross-namespace VenafiConnection for the agent to use it.",
152-
)
153-
agentCmd.PersistentFlags().StringVar(
154-
&agent.InstallNS,
155-
"install-namespace",
156-
"",
157-
"Namespace in which the agent is running. Only needed when running the agent outside of Kubernetes. Used for testing purposes.",
158-
)
159-
agentCmd.PersistentFlags().BoolVarP(
160-
&agent.Profiling,
161-
"enable-pprof",
162-
"",
163-
false,
164-
"Enables the pprof profiling server on the agent (port: 6060).",
165-
)
166-
agentCmd.PersistentFlags().BoolVarP(
167-
&agent.Prometheus,
168-
"enable-metrics",
169-
"",
170-
false,
171-
"Enables Prometheus metrics server on the agent (port: 8081).",
172-
)
56+
agent.InitAgentCmdFlags(agentCmd, &agent.Flags)
17357
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ require (
2121
github.com/spf13/cobra v1.8.0
2222
github.com/spf13/pflag v1.0.5
2323
github.com/stretchr/testify v1.9.0
24-
gopkg.in/d4l3k/messagediff.v1 v1.2.1
2524
gopkg.in/yaml.v2 v2.4.0
2625
k8s.io/api v0.30.3
2726
k8s.io/apimachinery v0.30.3

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWn
280280
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
281281
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
282282
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
283-
gopkg.in/d4l3k/messagediff.v1 v1.2.1 h1:70AthpjunwzUiarMHyED52mj9UwtAnE89l1Gmrt3EU0=
284-
gopkg.in/d4l3k/messagediff.v1 v1.2.1/go.mod h1:EUzikiKadqXWcD1AzJLagx0j/BeeWGtn++04Xniyg44=
285283
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
286284
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
287285
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=

0 commit comments

Comments
 (0)