Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scheme required for webhook url in amtool #3509

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions config/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,11 +503,6 @@ func (c *WebhookConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if c.URL != nil && c.URLFile != "" {
return fmt.Errorf("at most one of url & url_file must be configured")
}
if c.URL != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We think this is duplicate code as SecretURL is just a URL, whose UnmarshalYaml function also calls parseURL which contains the same check.

if c.URL.Scheme != "https" && c.URL.Scheme != "http" {
return fmt.Errorf("scheme required for webhook url")
}
}
return nil
}

Expand Down
22 changes: 22 additions & 0 deletions test/cli/acceptance.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,28 @@ func (am *Alertmanager) UpdateConfig(conf string) {
}
}

func (am *Alertmanager) ShowRoute() ([]byte, error) {
return am.showRouteCommand()
}

func (am *Alertmanager) showRouteCommand() ([]byte, error) {
amURLFlag := "--alertmanager.url=" + am.getURL("/")
args := []string{amURLFlag, "config", "routes", "show"}
cmd := exec.Command(amtool, args...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add one for config routes test?

return cmd.CombinedOutput()
}

func (am *Alertmanager) TestRoute() ([]byte, error) {
return am.testRouteCommand()
}

func (am *Alertmanager) testRouteCommand() ([]byte, error) {
amURLFlag := "--alertmanager.url=" + am.getURL("/")
args := []string{amURLFlag, "config", "routes", "test"}
cmd := exec.Command(amtool, args...)
return cmd.CombinedOutput()
}

func (am *Alertmanager) getURL(path string) string {
return fmt.Sprintf("http://%s%s%s", am.apiAddr, am.opts.RoutePrefix, path)
}
66 changes: 66 additions & 0 deletions test/cli/acceptance/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,69 @@ receivers:
t.Errorf("Incorrect number of silences queried, expected: %v, actual: %v", expectedSils, len(sils))
}
}

func TestRoutesShow(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert the fix and this test fails:

--- FAIL: TestRoutesShow (1.09s)
    cli_test.go:202: amtool: error: scheme required for webhook url

    cli_test.go:203:
        	Error Trace:	/Users/grobinson/go/src/github.com/prometheus/alertmanager/test/cli/acceptance/cli_test.go:203
        	Error:      	Received unexpected error:
        	            	exit status 1
        	Test:       	TestRoutesShow
FAIL

t.Parallel()

conf := `
route:
receiver: "default"
group_by: [alertname]
group_wait: 1s
group_interval: 1s
repeat_interval: 1ms

receivers:
- name: "default"
webhook_configs:
- url: 'http://%s'
send_resolved: true
`

at := NewAcceptanceTest(t, &AcceptanceOpts{
Tolerance: 1 * time.Second,
})
co := at.Collector("webhook")
wh := NewWebhook(co)

amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
require.NoError(t, amc.Start())
defer amc.Terminate()

am := amc.Members()[0]
_, err := am.ShowRoute()
require.NoError(t, err)
}

func TestRoutesTest(t *testing.T) {
t.Parallel()

conf := `
route:
receiver: "default"
group_by: [alertname]
group_wait: 1s
group_interval: 1s
repeat_interval: 1ms

receivers:
- name: "default"
webhook_configs:
- url: 'http://%s'
send_resolved: true
`

at := NewAcceptanceTest(t, &AcceptanceOpts{
Tolerance: 1 * time.Second,
})
co := at.Collector("webhook")
wh := NewWebhook(co)

amc := at.AlertmanagerCluster(fmt.Sprintf(conf, wh.Address()), 1)
require.NoError(t, amc.Start())
defer amc.Terminate()

am := amc.Members()[0]
_, err := am.TestRoute()
require.NoError(t, err)
}