-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Elastic Agent] Fix composed.Verifier so files are removed when inval…
…id (#30281) * Fix composed.Verifier. * Add tests and changelog.
- Loading branch information
1 parent
c997c7d
commit e6c1950
Showing
3 changed files
with
98 additions
and
3 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
95 changes: 95 additions & 0 deletions
95
x-pack/elastic-agent/pkg/artifact/download/composed/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,95 @@ | ||
// 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 composed | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/program" | ||
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/artifact/download" | ||
) | ||
|
||
type ErrorVerifier struct { | ||
called bool | ||
} | ||
|
||
func (d *ErrorVerifier) Verify(spec program.Spec, version string, removeOnFailure bool) (bool, error) { | ||
d.called = true | ||
return false, errors.New("failing") | ||
} | ||
|
||
func (d *ErrorVerifier) Called() bool { return d.called } | ||
|
||
type FailVerifier struct { | ||
called bool | ||
} | ||
|
||
func (d *FailVerifier) Verify(spec program.Spec, version string, removeOnFailure bool) (bool, error) { | ||
d.called = true | ||
return false, nil | ||
} | ||
|
||
func (d *FailVerifier) Called() bool { return d.called } | ||
|
||
type SuccVerifier struct { | ||
called bool | ||
removeOnFailure bool | ||
} | ||
|
||
func (d *SuccVerifier) Verify(spec program.Spec, version string, removeOnFailure bool) (bool, error) { | ||
d.called = true | ||
return true, nil | ||
} | ||
|
||
func (d *SuccVerifier) Called() bool { return d.called } | ||
|
||
func TestVerifier(t *testing.T) { | ||
testCases := []verifyTestCase{ | ||
{ | ||
verifiers: []CheckableVerifier{&ErrorVerifier{}, &SuccVerifier{}, &FailVerifier{}}, | ||
checkFunc: func(d []CheckableVerifier) bool { return d[0].Called() && d[1].Called() && !d[2].Called() }, | ||
expectedResult: true, | ||
}, { | ||
verifiers: []CheckableVerifier{&SuccVerifier{}, &ErrorVerifier{}, &FailVerifier{}}, | ||
checkFunc: func(d []CheckableVerifier) bool { return d[0].Called() && !d[1].Called() && !d[2].Called() }, | ||
expectedResult: true, | ||
}, { | ||
verifiers: []CheckableVerifier{&FailVerifier{}, &ErrorVerifier{}, &SuccVerifier{}}, | ||
checkFunc: func(d []CheckableVerifier) bool { return d[0].Called() && !d[1].Called() && !d[2].Called() }, | ||
expectedResult: false, | ||
}, { | ||
verifiers: []CheckableVerifier{&ErrorVerifier{}, &FailVerifier{}, &SuccVerifier{}}, | ||
checkFunc: func(d []CheckableVerifier) bool { return d[0].Called() && d[1].Called() && !d[2].Called() }, | ||
expectedResult: false, | ||
}, { | ||
verifiers: []CheckableVerifier{&ErrorVerifier{}, &ErrorVerifier{}, &SuccVerifier{}}, | ||
checkFunc: func(d []CheckableVerifier) bool { return d[0].Called() && d[1].Called() && d[2].Called() }, | ||
expectedResult: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
d := NewVerifier(tc.verifiers[0], tc.verifiers[1], tc.verifiers[2]) | ||
r, _ := d.Verify(program.Spec{Name: "a", Cmd: "a", Artifact: "a/a"}, "b", true) | ||
|
||
assert.Equal(t, tc.expectedResult, r) | ||
|
||
assert.True(t, tc.checkFunc(tc.verifiers)) | ||
} | ||
} | ||
|
||
type CheckableVerifier interface { | ||
download.Verifier | ||
Called() bool | ||
} | ||
|
||
type verifyTestCase struct { | ||
verifiers []CheckableVerifier | ||
checkFunc func(verifiers []CheckableVerifier) bool | ||
expectedResult bool | ||
} |