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

Add test for plugin scaffold command #613

Merged
merged 11 commits into from
Oct 3, 2024
Merged

Add test for plugin scaffold command #613

merged 11 commits into from
Oct 3, 2024

Conversation

mostafa
Copy link
Member

@mostafa mostafa commented Oct 2, 2024

Ticket(s)

Closes #475.

Description

Add test for plugin scaffold command. Thanks to @zeina1i for implementing the feature and writing the original test.

Related PRs

N/A

Development Checklist

  • I have added a descriptive title to this PR.
  • I have squashed related commits together.
  • I have rebased my branch on top of the latest main branch.
  • I have performed a self-review of my own code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added docstring(s) to my code.
  • I have made corresponding changes to the documentation (docs).
  • I have updated docs using make gen-docs command.
  • I have added tests for my changes.
  • I have signed all the commits.

Legal Checklist

Copy link

github-actions bot commented Oct 2, 2024

Overview

Image reference ghcr.io/gatewayd-io/gatewayd:efc8668 gatewaydio/gatewayd:latest
- digest b49fbaec2dff c0d87a848696
- tag efc8668 latest
- provenance 9ec6b54
- vulnerabilities critical: 0 high: 0 medium: 0 low: 0 critical: 0 high: 0 medium: 0 low: 0
- platform linux/amd64 linux/amd64
- size 19 MB 17 MB (-2.2 MB)
- packages 136 132 (-4)
Base Image alpine:3
also known as:
3.20
3.20.3
latest
alpine:3.20
also known as:
3
3.20.3
latest
- vulnerabilities critical: 0 high: 0 medium: 0 low: 0 critical: 0 high: 0 medium: 0 low: 0
Packages and Vulnerabilities (5 package changes and 0 vulnerability changes)
  • ➖ 3 packages removed
  • ♾️ 2 packages changed
  • 127 packages unchanged
Changes for packages of type apk (3 changes)
Package Version
ghcr.io/gatewayd-io/gatewayd:efc8668
Version
gatewaydio/gatewayd:latest
ca-certificates 20240705-r0
openssl 3.3.2-r0
pax-utils 1.3.7-r2
Changes for packages of type golang (2 changes)
Package Version
ghcr.io/gatewayd-io/gatewayd:efc8668
Version
gatewaydio/gatewayd:latest
♾️ github.com/gatewayd-io/gatewayd (devel) 0.9.8
♾️ stdlib go1.23.2 1.23.1

@mostafa mostafa force-pushed the zeina1i/scaffold-tests branch from 6dd9adb to 07f1338 Compare October 2, 2024 21:32
require.NoError(t, err, "plugin scaffold should not return an error")
assert.Contains(t, output, "scaffold done")
assert.Contains(t, output, "created files:")
assert.Contains(t, output, "test-gatewayd-plugin/.github/issue_template.md")
Copy link
Collaborator

Choose a reason for hiding this comment

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

we use test-gateway-plugin multiple times, we can have a valuable for it like
pluginDir := filepath.Join(t.TempDir(), "test-gatewayd-plugin")

Copy link
Collaborator

Choose a reason for hiding this comment

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

we can use t.TempDir() to avoid polluting the working directory - https://pkg.go.dev/testing#B.TempDir

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in d7cf221.

err = yamlv3.Unmarshal(pluginsConfig, &localPluginsConfig)
require.NoError(t, err, "unmarshalling yaml file should not return error")

tidy := exec.Command("go", "mod", "tidy")
Copy link
Collaborator

Choose a reason for hiding this comment

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

now, we use the same structure twice; we can create a separate function for it

func runCommand(command string, args ...string) error {
	cmd := exec.Command(command, args...)
	cmd.Dir = args[len(args)-1]
	return cmd.Run()
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in d7cf221.

err = build.Run()
assert.NoError(t, err)

_, err = os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

if we want to verify that file exist we can use os.stat without reading the binary

Suggested change
_, err = os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin"))
_, err = os.Stat(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin"))

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in d7cf221.

assert.NoError(t, err)

_, err = os.ReadFile(filepath.Join("plugins", "test-gatewayd-plugin", "test-gatewayd-plugin"))
require.NoError(t, err, "reading plugin binary file should not return an error")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
require.NoError(t, err, "reading plugin binary file should not return an error")
require.NoError(t, err, "plugin binary file should exist")

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in d7cf221.


pluginsList := cast.ToSlice(localPluginsConfig["plugins"])
plugin := cast.ToStringMap(pluginsList[0])
pluginsList[0] = plugin
Copy link
Collaborator

Choose a reason for hiding this comment

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

unnecessary, we do the same later

Suggested change
pluginsList[0] = plugin

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in d7cf221.


waitGroup.Wait()

assert.NoError(t, os.RemoveAll("plugins"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

if we use t.TempDir() we dont need this line

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in d7cf221.

@mostafa mostafa requested a review from sinadarbouy October 3, 2024 15:38
Copy link
Collaborator

@sinadarbouy sinadarbouy left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@mostafa mostafa merged commit 041dc62 into main Oct 3, 2024
5 checks passed
@mostafa mostafa deleted the zeina1i/scaffold-tests branch October 3, 2024 15:54
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.

plugin scaffold: Create a plugin skeleton/scaffold (init)
3 participants