-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* push changes * Add remove whenever using the component module. * Add additional context and remove dead file. * Add long changelog comment * fix linter * Remove mutex check * Add changes to support module id removal. * Remove unneeded line * Fix merge errors. * Ensure module is checked on first run. * rename and add comments * Add manual removal back in and make test closer to actual usage. * move changelog comment to correct location * A different approach by keying off run instead of build. * Add test for duplicate registration. * Minor changes * PR feedback * add locks around the reads for tests, its a bit hacky.
- Loading branch information
1 parent
4d07963
commit 473938a
Showing
11 changed files
with
243 additions
and
51 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
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,67 @@ | ||
package componenttest | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/grafana/agent/component" | ||
mod "github.com/grafana/agent/component/module" | ||
) | ||
|
||
func init() { | ||
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
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,76 @@ | ||
package flow | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/agent/pkg/flow/componenttest" | ||
"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) | ||
var t1 *componenttest.TestFailModule | ||
require.Eventually(t, func() bool { | ||
t1 = f.loader.Components()[0].Component().(*componenttest.TestFailModule) | ||
return t1 != nil | ||
}, 10*time.Second, 100*time.Millisecond) | ||
require.Eventually(t, func() bool { | ||
f.loadMut.RLock() | ||
defer f.loadMut.RUnlock() | ||
// This should be one due to t1. | ||
return len(f.modules.List()) == 1 | ||
}, 10*time.Second, 100*time.Millisecond) | ||
badContent := | ||
`test.fail.module "bad" { | ||
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) | ||
require.Eventually(t, func() bool { | ||
f.loadMut.RLock() | ||
defer f.loadMut.RUnlock() | ||
// Only one since the bad one never should have been added. | ||
rightLength := len(f.modules.List()) == 1 | ||
_, foundT1 := f.modules.Get("test.fail.module.t1") | ||
return rightLength && foundT1 | ||
}, 10*time.Second, 100*time.Millisecond) | ||
// fail a second time to ensure the once is done again. | ||
err = t1.UpdateContent(badContent) | ||
require.Error(t, err) | ||
|
||
goodContent := | ||
`test.fail.module "good" { | ||
content="" | ||
fail=false | ||
}` | ||
err = t1.UpdateContent(goodContent) | ||
require.NoError(t, err) | ||
require.Eventually(t, func() bool { | ||
f.loadMut.RLock() | ||
defer f.loadMut.RUnlock() | ||
modT1, foundT1 := f.modules.Get("test.fail.module.t1") | ||
modGood, foundGood := f.modules.Get("test.fail.module.t1/test.fail.module.good") | ||
return modT1 != nil && modGood != nil && foundT1 && foundGood | ||
}, 10*time.Second, 100*time.Millisecond) | ||
cnc() | ||
require.Eventually(t, func() bool { | ||
f.loadMut.RLock() | ||
defer f.loadMut.RUnlock() | ||
// All should be cleaned up. | ||
return len(f.modules.List()) == 0 | ||
}, 10*time.Second, 100*time.Millisecond) | ||
} |
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
Oops, something went wrong.