-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some improvements to error filtering
- Loading branch information
1 parent
6b4f5b6
commit 7169425
Showing
5 changed files
with
146 additions
and
7 deletions.
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
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
126 changes: 126 additions & 0 deletions
126
internal/pkg/agent/application/upgrade/artifact/download/verifier_test.go
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package download | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/elastic/elastic-agent/internal/pkg/agent/errors" | ||
"github.com/elastic/elastic-agent/pkg/core/logger" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPgpBytesFromSource(t *testing.T) { | ||
testCases := []struct { | ||
Name string | ||
Source string | ||
ClientDoErr error | ||
ClientBody []byte | ||
ClientStatus int | ||
|
||
ExpectedPGP []byte | ||
ExpectedErr error | ||
ExpectedLogMessage string | ||
}{ | ||
{ | ||
"successfull call", | ||
PgpSourceURIPrefix + "https://location/path", | ||
nil, | ||
[]byte("pgp-body"), | ||
200, | ||
[]byte("pgp-body"), | ||
nil, | ||
"", | ||
}, | ||
{ | ||
"unknown source call", | ||
"https://location/path", | ||
nil, | ||
[]byte("pgp-body"), | ||
200, | ||
nil, | ||
ErrUnknownPGPSource, | ||
"", | ||
}, | ||
{ | ||
"invalid location is filtered call", | ||
PgpSourceURIPrefix + "http://location/path", | ||
nil, | ||
[]byte("pgp-body"), | ||
200, | ||
nil, | ||
nil, | ||
"Skipped remote PGP located ", | ||
}, | ||
{ | ||
"do error is filtered", | ||
PgpSourceURIPrefix + "https://location/path", | ||
errors.New("error"), | ||
[]byte("pgp-body"), | ||
200, | ||
nil, | ||
nil, | ||
"Skipped remote PGP located", | ||
}, | ||
{ | ||
"invalid status code is filtered out", | ||
PgpSourceURIPrefix + "https://location/path", | ||
nil, | ||
[]byte("pgp-body"), | ||
500, | ||
nil, | ||
nil, | ||
"Failed to fetch remote PGP", | ||
}, | ||
{ | ||
"invalid status code is filtered out", | ||
PgpSourceURIPrefix + "https://location/path", | ||
nil, | ||
[]byte("pgp-body"), | ||
404, | ||
nil, | ||
nil, | ||
"Failed to fetch remote PGP", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
log, obs := logger.NewTesting(tc.Name) | ||
mockClient := &MockClient{ | ||
DoFunc: func(req *http.Request) (*http.Response, error) { | ||
if tc.ClientDoErr != nil { | ||
return nil, tc.ClientDoErr | ||
} | ||
|
||
return &http.Response{ | ||
StatusCode: tc.ClientStatus, | ||
Body: io.NopCloser(bytes.NewReader(tc.ClientBody)), | ||
}, nil | ||
}, | ||
} | ||
|
||
resPgp, resErr := PgpBytesFromSource(log, tc.Source, mockClient) | ||
require.Equal(t, tc.ExpectedErr, resErr) | ||
require.Equal(t, tc.ExpectedPGP, resPgp) | ||
if tc.ExpectedLogMessage != "" { | ||
logs := obs.FilterMessageSnippet(tc.ExpectedLogMessage) | ||
require.NotEqual(t, 0, logs.Len()) | ||
} | ||
|
||
}) | ||
} | ||
} | ||
|
||
type MockClient struct { | ||
DoFunc func(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
func (m *MockClient) Do(req *http.Request) (*http.Response, error) { | ||
return m.DoFunc(req) | ||
} |
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