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

test: add integration plugin tests #7299

Merged
merged 5 commits into from
Sep 2, 2024

Conversation

DmitriyLewen
Copy link
Contributor

@DmitriyLewen DmitriyLewen commented Aug 2, 2024

Description

See #7276

CI/CD test - https://github.com/aquasecurity/trivy/actions/runs/10214386342/job/28261673506?pr=7299#step:5:1044

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@DmitriyLewen DmitriyLewen self-assigned this Aug 2, 2024
@DmitriyLewen DmitriyLewen marked this pull request as ready for review August 2, 2024 09:51
@@ -1,4 +1,4 @@
//go:build integration || vm_integration || module_integration || k8s_integration
//go:build integration || vm_integration || module_integration || k8s_integration || plugin_integration
Copy link
Collaborator

Choose a reason for hiding this comment

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

The VM, module, and K8s tests take longer to run and require a special setup. That's why they are separated. I think the plugin test can be part of the normal integration tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated in 33cf9d5


// Overwrite Stdout to get output of plugin
defaultStdout := os.Stdout
os.Stdout = f
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if passing io.Writer here?

app.SetOut(io.Discard)

Copy link
Collaborator

Choose a reason for hiding this comment

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

I mean

func execute(osArgs []string, out io.Writer) error {
    if out == nil {
        out = io.Discard
    }
    ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to use your idea.
But we use os.Stdout in plugin package.

I used NewManager instead of defaultManager in app.go to use Stdout from cobra.
It works, but there is problem with case when plugin is output.
We need to add output for plugin in Options:

trivy/pkg/flag/options.go

Lines 511 to 533 in 0c6687d

func (o *Options) outputPluginWriter(ctx context.Context) (io.Writer, func() error, error) {
pluginName := strings.TrimPrefix(o.Output, "plugin=")
pr, pw := io.Pipe()
wait, err := plugin.Start(ctx, pluginName, plugin.Options{
Args: o.OutputPluginArgs,
Stdin: pr,
})
if err != nil {
return nil, nil, xerrors.Errorf("plugin start: %w", err)
}
cleanup := func() error {
if err = pw.Close(); err != nil {
return xerrors.Errorf("failed to close pipe: %w", err)
}
if err = wait(); err != nil {
return xerrors.Errorf("plugin error: %w", err)
}
return nil
}
return pw, cleanup, nil
}

i am not sure that we need to do that now.
I would leave this workaround until the tests become more numerous and complex, which would require these changes.

But if you want to do it - tell me and I will continue the research

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, we can improve it later.


// Overwrite Stdout to get output of plugin
defaultStdout := os.Stdout
os.Stdout = f
Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, we can improve it later.

@knqyf263 knqyf263 added this pull request to the merge queue Sep 2, 2024
Merged via the queue into aquasecurity:main with commit 1a6295c Sep 2, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

test(plugin): add integration tests
2 participants