-
Notifications
You must be signed in to change notification settings - Fork 486
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
Module id fail #5261
Merged
Merged
Module id fail #5261
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6db453b
push changes
mattdurham a2c2755
Add remove whenever using the component module.
mattdurham 13c99ca
Add additional context and remove dead file.
mattdurham dfa0b77
Add long changelog comment
mattdurham 01552ce
merge main
mattdurham f8f9930
fix linter
mattdurham 8ceafb2
Remove mutex check
mattdurham 54b238c
Add changes to support module id removal.
mattdurham 91c0abe
Remove unneeded line
mattdurham f7caffe
main merge
mattdurham cb8d986
Fix merge errors.
mattdurham 92cb1c1
Ensure module is checked on first run.
mattdurham f9e89e0
rename and add comments
mattdurham 6d28509
Add manual removal back in and make test closer to actual usage.
mattdurham a96a32e
Merge branch 'main' into module_id_fail
mattdurham 6c44b21
move changelog comment to correct location
mattdurham db23876
A different approach by keying off run instead of build.
mattdurham a9d509d
Add test for duplicate registration.
mattdurham e81a2ef
Minor changes
mattdurham e5588be
Merge branch 'main' into module_id_fail
mattdurham e69db8c
PR feedback
mattdurham acaae50
Merge remote-tracking branch 'origin/module_id_fail' into module_id_fail
mattdurham 77777b0
add locks around the reads for tests, its a bit hacky.
mattdurham 9b9f7ad
Merge branch 'main' into module_id_fail
mattdurham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package flow | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/agent/component" | ||
mod "github.com/grafana/agent/component/module" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIDRemovalIfFailedToLoad(t *testing.T) { | ||
f := New(testOptions(t)) | ||
|
||
fullContent := "test.fail.module \"t1\" { content = \"\" }" | ||
fl, err := ParseSource("test", []byte(fullContent)) | ||
require.NoError(t, err) | ||
err = f.LoadSource(fl, nil) | ||
require.NoError(t, err) | ||
ctx := context.Background() | ||
ctx, cnc := context.WithTimeout(ctx, 600*time.Second) | ||
|
||
go f.Run(ctx) | ||
require.Eventually(t, func() bool { | ||
t1 := f.loader.Components()[0].Component().(*testFailModule) | ||
return t1 != nil | ||
}, 10*time.Second, 100*time.Millisecond) | ||
t1 := f.loader.Components()[0].Component().(*testFailModule) | ||
badContent := | ||
`test.fail.module "int" { | ||
content="" | ||
fail=true | ||
}` | ||
err = t1.updateContent(badContent) | ||
// Because we have bad content this should fail, but the ids should be removed. | ||
require.Error(t, err) | ||
goodContent := | ||
`test.fail.module "int" { | ||
content="" | ||
fail=false | ||
}` | ||
err = t1.updateContent(goodContent) | ||
require.NoError(t, err) | ||
cnc() | ||
} | ||
|
||
func init() { | ||
mattdurham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
component.Register(component.Registration{ | ||
Name: "test.fail.module", | ||
Args: TestFailArguments{}, | ||
Exports: mod.Exports{}, | ||
|
||
Build: func(opts component.Options, args component.Arguments) (component.Component, error) { | ||
m, err := mod.NewModuleComponent(opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if args.(TestFailArguments).Fail { | ||
return nil, fmt.Errorf("module told to fail") | ||
} | ||
err = m.LoadFlowSource(nil, args.(TestFailArguments).Content) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &testFailModule{ | ||
mc: m, | ||
content: args.(TestFailArguments).Content, | ||
opts: opts, | ||
fail: args.(TestFailArguments).Fail, | ||
ch: make(chan error), | ||
}, nil | ||
}, | ||
}) | ||
} | ||
|
||
type TestFailArguments struct { | ||
Content string `river:"content,attr"` | ||
Fail bool `river:"fail,attr,optional"` | ||
} | ||
|
||
type testFailModule struct { | ||
content string | ||
opts component.Options | ||
ch chan error | ||
mc *mod.ModuleComponent | ||
fail bool | ||
} | ||
|
||
func (t *testFailModule) Run(ctx context.Context) error { | ||
go t.mc.RunFlowController(ctx) | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
func (t *testFailModule) updateContent(content string) error { | ||
t.content = content | ||
err := t.mc.LoadFlowSource(nil, t.content) | ||
return err | ||
} | ||
|
||
func (t *testFailModule) Update(_ component.Arguments) error { | ||
return nil | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's still a problem here: what if a component fails to be constructed more than once?
Rather than using a sync.Once, this check could be moved down to line 289 (i.e., if
cn.reg.Build
fails) so you only clear module IDs for components which can't be built, no matter how many times the construction fails.