Skip to content

Commit 2465c2a

Browse files
author
k-stryapkov
committed
proxy & binproc config validation tests
commit_hash:f27611e310b77857f3951dcd22320ed619702a4c
1 parent dd97f30 commit 2465c2a

File tree

9 files changed

+107
-77
lines changed

9 files changed

+107
-77
lines changed

perforator/cmd/binaryprocessor/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/yandex/perforator/perforator/pkg/maxprocs"
1616
"github.com/yandex/perforator/perforator/pkg/mlock"
1717
"github.com/yandex/perforator/perforator/pkg/must"
18+
"github.com/yandex/perforator/perforator/pkg/validateconfig"
1819
"github.com/yandex/perforator/perforator/pkg/xlog"
1920
)
2021

@@ -118,6 +119,15 @@ func init() {
118119
"Port to start metrics server on",
119120
)
120121

122+
binprocCmd.AddCommand(validateconfig.NewValidateConfigCmd(
123+
"binproc",
124+
validateconfig.ValidateConfigFunc(
125+
func(configPath string) error {
126+
_, err := binaryprocessor.ParseConfig(configPath)
127+
return err
128+
}),
129+
))
130+
121131
cobrabuildinfo.Init(binprocCmd)
122132
}
123133

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
package main
22

33
import (
4-
"fmt"
5-
"os"
6-
7-
"github.com/spf13/cobra"
8-
9-
"github.com/yandex/perforator/perforator/pkg/must"
104
"github.com/yandex/perforator/perforator/pkg/storage/bundle"
11-
)
12-
13-
var (
14-
gcConfigForValidationPath string
15-
16-
gcValidateConfigCmd = &cobra.Command{
17-
Use: "validate-config",
18-
Short: "Validate GC config",
19-
Args: cobra.NoArgs,
20-
Run: func(cmd *cobra.Command, args []string) {
21-
config, err := bundle.ParseConfig(gcConfigForValidationPath, true /* strict */)
22-
if err != nil {
23-
fmt.Fprintf(os.Stderr, "Invalid config: %v\n", err)
24-
os.Exit(1)
25-
}
26-
fmt.Printf("%#v\n", config)
27-
},
28-
}
5+
"github.com/yandex/perforator/perforator/pkg/validateconfig"
296
)
307

318
func init() {
32-
gcValidateConfigCmd.Flags().StringVar(&gcConfigForValidationPath, "config", "", "Path to the config file")
33-
must.Must(gcValidateConfigCmd.MarkFlagRequired("config"))
34-
gcCmd.AddCommand(gcValidateConfigCmd)
9+
gcCmd.AddCommand(validateconfig.NewValidateConfigCmd(
10+
"GC",
11+
validateconfig.ValidateConfigFunc(
12+
func(configPath string) error {
13+
_, err := bundle.ParseConfig(configPath, true /* strict */)
14+
return err
15+
}),
16+
))
3517
}

perforator/cmd/proxy/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/yandex/perforator/perforator/pkg/mlock"
1717
"github.com/yandex/perforator/perforator/pkg/must"
1818
"github.com/yandex/perforator/perforator/pkg/polyheapprof"
19+
"github.com/yandex/perforator/perforator/pkg/validateconfig"
1920
"github.com/yandex/perforator/perforator/pkg/xlog"
2021
)
2122

@@ -147,6 +148,24 @@ func init() {
147148
"Whether to profile allocations",
148149
)
149150

151+
proxyCmd.AddCommand(validateconfig.NewValidateConfigCmd(
152+
"proxy",
153+
validateconfig.ValidateConfigFunc(
154+
func(configPath string) error {
155+
config, err := server.ParseConfig(configPath)
156+
if err != nil {
157+
return err
158+
}
159+
160+
err = config.Validate()
161+
if err != nil {
162+
return err
163+
}
164+
165+
return nil
166+
}),
167+
))
168+
150169
cobrabuildinfo.Init(proxyCmd)
151170
}
152171

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
package main
22

33
import (
4-
"fmt"
5-
"os"
6-
7-
"github.com/spf13/cobra"
8-
94
"github.com/yandex/perforator/perforator/internal/agent_gateway/server"
10-
"github.com/yandex/perforator/perforator/pkg/must"
11-
)
12-
13-
var (
14-
storageConfigForValidationPath string
15-
16-
storageValidateConfigCmd = &cobra.Command{
17-
Use: "validate-config",
18-
Short: "Validate storage config",
19-
Args: cobra.NoArgs,
20-
Run: func(cmd *cobra.Command, args []string) {
21-
config, err := server.ParseConfig(storageConfigForValidationPath, true /* strict */)
22-
if err != nil {
23-
fmt.Fprintf(os.Stderr, "Invalid config: %v\n", err)
24-
os.Exit(1)
25-
}
26-
fmt.Printf("%#v\n", config)
27-
},
28-
}
5+
"github.com/yandex/perforator/perforator/pkg/validateconfig"
296
)
307

318
func init() {
32-
storageValidateConfigCmd.Flags().StringVar(&storageConfigForValidationPath, "config", "", "Path to the config file")
33-
must.Must(storageValidateConfigCmd.MarkFlagRequired("config"))
34-
storageCmd.AddCommand(storageValidateConfigCmd)
9+
storageCmd.AddCommand(validateconfig.NewValidateConfigCmd(
10+
"storage",
11+
validateconfig.ValidateConfigFunc(
12+
func(configPath string) error {
13+
_, err := server.ParseConfig(configPath, true /* strict */)
14+
return err
15+
},
16+
),
17+
),
18+
)
3519
}

perforator/cmd/web/main.go

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/yandex/perforator/perforator/internal/buildinfo/cobrabuildinfo"
2020
service "github.com/yandex/perforator/perforator/internal/web"
2121
"github.com/yandex/perforator/perforator/internal/xmetrics"
22+
"github.com/yandex/perforator/perforator/pkg/validateconfig"
2223
"github.com/yandex/perforator/perforator/pkg/xlog"
2324
)
2425

@@ -38,35 +39,23 @@ var (
3839
logLevel string
3940
)
4041

41-
var (
42-
storageConfigForValidationPath string
43-
44-
storageValidateConfigCmd = &cobra.Command{
45-
Use: "validate-config",
46-
Short: "Validate storage config",
47-
Args: cobra.NoArgs,
48-
Run: func(cmd *cobra.Command, args []string) {
49-
config, err := service.ParseConfig(storageConfigForValidationPath)
50-
if err != nil {
51-
fmt.Fprintf(os.Stderr, "Invalid config: %v\n", err)
52-
os.Exit(1)
53-
}
54-
fmt.Printf("%#v\n", config)
55-
},
56-
}
57-
)
58-
5942
func init() {
6043
rootCmd.Flags().StringVarP(&configPath, "config", "c", "", "path to web service config")
6144
rootCmd.Flags().StringVarP(&logLevel, "log-level", "l", "info", "log level (must be one of `debug`, `info`, `warn`, `error`)")
6245

6346
cobrabuildinfo.Init(rootCmd)
6447

6548
rootCmd.MarkFlagsOneRequired("config")
66-
rootCmd.AddCommand(storageValidateConfigCmd)
6749

68-
storageValidateConfigCmd.Flags().StringVarP(&storageConfigForValidationPath, "config", "c", "", "path to web service config")
69-
storageValidateConfigCmd.MarkFlagsOneRequired("config")
50+
rootCmd.AddCommand(validateconfig.NewValidateConfigCmd(
51+
"web service",
52+
validateconfig.ValidateConfigFunc(
53+
func(configPath string) error {
54+
_, err := service.ParseConfig(configPath)
55+
return err
56+
},
57+
),
58+
))
7059
}
7160

7261
func main() {

perforator/internal/symbolizer/proxy/server/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ func newInvalidFieldError(fieldName string, err error) error {
140140

141141
func (c *Config) Validate() error {
142142
var errs []error
143-
if err := c.BinaryProcessorClientConfig.Validate(); err != nil {
144-
errs = append(errs, newInvalidFieldError("binary_processor_client", err))
143+
if c.BinaryProcessorClientConfig != nil {
144+
err := c.BinaryProcessorClientConfig.Validate()
145+
if err != nil {
146+
errs = append(errs, newInvalidFieldError("binary_processor_client", err))
147+
}
145148
}
146149

147150
return errors.Join(errs...)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package validateconfig
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/yandex/perforator/perforator/pkg/must"
10+
)
11+
12+
type ValidateConfigFunc func(configPath string) error
13+
14+
func NewValidateConfigCmd(
15+
componentName string,
16+
validateFunc ValidateConfigFunc,
17+
) *cobra.Command {
18+
var configPath string
19+
validateConfigCmd := &cobra.Command{
20+
Use: "validate-config",
21+
Short: fmt.Sprintf("Validate %s config", componentName),
22+
Args: cobra.NoArgs,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
err := validateFunc(configPath)
25+
if err != nil {
26+
fmt.Fprintf(os.Stderr, "Invalid config: %v\n", err)
27+
os.Exit(1)
28+
}
29+
},
30+
}
31+
32+
validateConfigCmd.Flags().StringVarP(&configPath, "config", "c", "", "Path to the config file")
33+
must.Must(validateConfigCmd.MarkFlagRequired("config"))
34+
return validateConfigCmd
35+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
GO_LIBRARY()
2+
3+
SRCS(
4+
validateconfig.go
5+
)
6+
7+
END()

perforator/pkg/ya.make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ RECURSE(
3030
storage
3131
tls
3232
tracing
33+
validateconfig
3334
weightedlru
3435
xelf
3536
xlog

0 commit comments

Comments
 (0)