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: replace embedded Git repository with dynamically created repository #6824

Merged
merged 5 commits into from
Jun 3, 2024

Conversation

knqyf263
Copy link
Collaborator

Description

Currently, Trivy includes a separate Git bare repository committed within its codebase for testing purposes. For example: https://github.com/aquasecurity/trivy/tree/29615be85e8bfeaf5a0cd51829b1898c55fa4274/pkg/plugin/testdata/test_plugin.git

While this approach allows us to focus on the logic of the code we want to test, it also has some drawbacks. In order to understand the state of this Git repository, we need to clone it and perform operations using git commands. Additionally, if we want to make small modifications, we need to use git commands, and it's not possible to review the changes made on GitHub since those changes are binary.

CleanShot 2024-05-30 at 12 57 55

To address these issues, this PR proposes using go-git to dynamically create the test Git repository during the testing process. Although there is a potential downside that if the process of creating this test repository is incorrect, the tests will fail to run properly, the benefits outweigh this risk, IMO. Using Go to create the repository makes it clearer what branches are being created, what tags are being assigned, and so on. This approach also makes it easier to make changes and conduct reviews on GitHub.

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).

Signed-off-by: knqyf263 <knqyf263@gmail.com>
@knqyf263 knqyf263 self-assigned this May 30, 2024
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Signed-off-by: knqyf263 <knqyf263@gmail.com>
@knqyf263 knqyf263 marked this pull request as ready for review May 30, 2024 10:44
internal/gittest/server.go Outdated Show resolved Hide resolved
pkg/plugin/manager_unix_test.go Outdated Show resolved Hide resolved
}

func TestManager_Install(t *testing.T) {
gs := setupGitRepository(t, "test_plugin", "testdata/test_plugin")
t.Cleanup(gs.Close)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I detected one problem with this test server:
this zip file doesn't contain nested dir:

  • test_plugin.zip/plugin.yaml

But GitHub archive contains repo name dir:
check https://github.com/aquasecurity/trivy-plugin-kubectl/archive/refs/heads/main.zip

  • trivy-plugin-kubectl-main.zip/trivy-plugin-kubectl-main/plugin.yaml

I think we should handle this case, but perhaps in another PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. It's not introduced in this PR. I'll open another PR.

Signed-off-by: knqyf263 <knqyf263@gmail.com>
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

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

LGTM

@knqyf263 knqyf263 added this pull request to the merge queue Jun 3, 2024
Merged via the queue into aquasecurity:main with commit b7b8cdc Jun 3, 2024
12 checks passed
@knqyf263 knqyf263 deleted the test/create_git_repos branch June 3, 2024 07:55
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.

2 participants