Skip to content

Fix toolregistrytest by using fake #5627

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

Merged
merged 5 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions pkg/app/pipedv1/plugin/kubernetes/deployment/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ func TestDeploymentService_executeK8sSyncStage(t *testing.T) {
}

// initialize tool registry
testRegistry, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
testRegistry := toolregistrytest.NewTestToolRegistry(t)

// initialize plugin config and dynamic client for assertions with envtest
pluginCfg, dynamicClient := setupTestPluginConfigAndDynamicClient(t)
Expand Down Expand Up @@ -151,8 +150,7 @@ func TestDeploymentService_executeK8sSyncStage_withInputNamespace(t *testing.T)
}

// initialize tool registry
testRegistry, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
testRegistry := toolregistrytest.NewTestToolRegistry(t)

// initialize plugin config and dynamic client for assertions with envtest
pluginCfg, dynamicClient := setupTestPluginConfigAndDynamicClient(t)
Expand Down Expand Up @@ -187,8 +185,7 @@ func TestDeploymentService_executeK8sSyncStage_withPrune(t *testing.T) {
ctx := context.Background()

// initialize tool registry
testRegistry, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
testRegistry := toolregistrytest.NewTestToolRegistry(t)

// initialize plugin config and dynamic client for assertions with envtest
pluginCfg, dynamicClient := setupTestPluginConfigAndDynamicClient(t)
Expand Down Expand Up @@ -309,8 +306,7 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_changesNamespace(t *tes
ctx := context.Background()

// initialize tool registry
testRegistry, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
testRegistry := toolregistrytest.NewTestToolRegistry(t)

// initialize plugin config and dynamic client for assertions with envtest
pluginCfg, dynamicClient := setupTestPluginConfigAndDynamicClient(t)
Expand Down Expand Up @@ -447,8 +443,7 @@ func TestDeploymentService_executeK8sSyncStage_withPrune_clusterScoped(t *testin
ctx := context.Background()

// initialize tool registry
testRegistry, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
testRegistry := toolregistrytest.NewTestToolRegistry(t)

// initialize plugin config and dynamic client for assertions with envtest
pluginCfg, dynamicClient := setupTestPluginConfigAndDynamicClient(t)
Expand Down
8 changes: 2 additions & 6 deletions pkg/app/pipedv1/plugin/kubernetes/provider/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ func TestTemplateLocalChart(t *testing.T) {
chartPath = "testchart"
)

c, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
t.Cleanup(func() { c.Close() })
c := toolregistrytest.NewTestToolRegistry(t)

r := toolregistry.NewRegistry(c)
helmPath, err := r.Helm(ctx, "3.16.1")
Expand All @@ -65,9 +63,7 @@ func TestTemplateLocalChart_WithNamespace(t *testing.T) {
namespace = "testnamespace"
)

c, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
t.Cleanup(func() { c.Close() })
c := toolregistrytest.NewTestToolRegistry(t)

r := toolregistry.NewRegistry(c)
helmPath, err := r.Helm(ctx, "3.16.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ func TestKustomizeTemplate(t *testing.T) {
appDir = "testdata/testkustomize"
)

c, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)

c := toolregistrytest.NewTestToolRegistry(t)
r := toolregistry.NewRegistry(c)

t.Cleanup(func() { c.Close() })

kustomizePath, err := r.Kustomize(context.Background(), "5.4.3")
require.NoError(t, err)
require.NotEmpty(t, kustomizePath)
Expand Down
4 changes: 1 addition & 3 deletions pkg/app/pipedv1/plugin/kubernetes/provider/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,7 @@ invalid yaml content
}

func TestLoader_templateHelmChart(t *testing.T) {
c, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
t.Cleanup(func() { c.Close() })
c := toolregistrytest.NewTestToolRegistry(t)

loader := &Loader{
toolRegistry: toolregistry.NewRegistry(c),
Expand Down
15 changes: 3 additions & 12 deletions pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ import (
func TestRegistry_Kubectl(t *testing.T) {
t.Parallel()

c, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
c := toolregistrytest.NewTestToolRegistry(t)

r := NewRegistry(c)

t.Cleanup(func() { c.Close() })

p, err := r.Kubectl(context.Background(), "1.30.2")
require.NoError(t, err)
require.NotEmpty(t, p)
Expand All @@ -51,13 +48,10 @@ func TestRegistry_Kubectl(t *testing.T) {
func TestRegistry_Kustomize(t *testing.T) {
t.Parallel()

c, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
c := toolregistrytest.NewTestToolRegistry(t)

r := NewRegistry(c)

t.Cleanup(func() { c.Close() })

p, err := r.Kustomize(context.Background(), "5.4.3")
require.NoError(t, err)
require.NotEmpty(t, p)
Expand All @@ -73,13 +67,10 @@ func TestRegistry_Kustomize(t *testing.T) {
func TestRegistry_Helm(t *testing.T) {
t.Parallel()

c, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)
c := toolregistrytest.NewTestToolRegistry(t)

r := NewRegistry(c)

t.Cleanup(func() { c.Close() })

p, err := r.Helm(context.Background(), "3.16.1")
require.NoError(t, err)
require.NotEmpty(t, p)
Expand Down
93 changes: 38 additions & 55 deletions pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"runtime"
"testing"
"text/template"

"google.golang.org/grpc"

"github.com/pipe-cd/pipecd/pkg/plugin/pipedservice"
"github.com/pipe-cd/pipecd/pkg/plugin/toolregistry"
)

type ToolRegistry struct {
Expand All @@ -38,97 +43,75 @@
Os string
}

func NewToolRegistry(t *testing.T) (*ToolRegistry, error) {
tmpDir, err := os.MkdirTemp("", "tool-registry-test")
if err != nil {
return nil, err
}
return &ToolRegistry{
testingT: t,
tmpDir: tmpDir,
}, nil
}

func (r *ToolRegistry) newTmpDir() (string, error) {
return os.MkdirTemp(r.tmpDir, "")
type fakeClient struct {
pipedservice.PluginServiceClient
testingT *testing.T
tmpDir string
}

func (r *ToolRegistry) binDir() (string, error) {
target := r.tmpDir + "/bin"
func (c *fakeClient) binDir() (string, error) {
target := c.tmpDir + "/bin"
if err := os.MkdirAll(target, 0o755); err != nil {
return "", err
}
return target, nil
}

func (r *ToolRegistry) outPath() (string, error) {
target, err := r.newTmpDir()
if err != nil {
return "", err
}
return target + "/out", nil
func (c *fakeClient) outPath() string {
return c.tmpDir + "/out"
}

func (r *ToolRegistry) InstallTool(ctx context.Context, name, version, script string) (path string, err error) {
outPath, err := r.outPath()
if err != nil {
return "", err
}

tmpDir, err := r.newTmpDir()
if err != nil {
return "", err
}
func (c *fakeClient) InstallTool(ctx context.Context, in *pipedservice.InstallToolRequest, opts ...grpc.CallOption) (*pipedservice.InstallToolResponse, error) {
outPath := c.outPath()

binDir, err := r.binDir()
binDir, err := c.binDir()
if err != nil {
return "", err
return nil, err

Check warning on line 69 in pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L69

Added line #L69 was not covered by tests
}

t, err := template.New("install script").Parse(script)
t, err := template.New("install script").Parse(in.GetInstallScript())
if err != nil {
return "", err
return nil, err

Check warning on line 74 in pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L74

Added line #L74 was not covered by tests
}

vars := templateValues{
Name: name,
Version: version,
Name: in.GetName(),
Version: in.GetVersion(),
OutPath: outPath,
TmpDir: tmpDir,
TmpDir: c.testingT.TempDir(),
Arch: runtime.GOARCH,
Os: runtime.GOOS,
}
var buf bytes.Buffer
if err := t.Execute(&buf, vars); err != nil {
return "", err
return nil, err

Check warning on line 87 in pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L87

Added line #L87 was not covered by tests
}

cmd := exec.CommandContext(ctx, "/bin/sh", "-c", buf.String())
if out, err := cmd.CombinedOutput(); err != nil {
r.testingT.Log(string(out))
return "", err
c.testingT.Log(string(out))
return nil, err

Check warning on line 93 in pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L92-L93

Added lines #L92 - L93 were not covered by tests
}

if err := os.Chmod(outPath, 0o755); err != nil {
return "", err
return nil, err

Check warning on line 97 in pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L97

Added line #L97 was not covered by tests
}

target := binDir + "/" + name + "-" + version
target := binDir + "/" + in.GetName() + "-" + in.GetVersion()
if out, err := exec.CommandContext(ctx, "/bin/sh", "-c", "mv "+outPath+" "+target).CombinedOutput(); err != nil {
r.testingT.Log(string(out))
return "", err
}

if err := os.RemoveAll(tmpDir); err != nil {
return "", err
c.testingT.Log(string(out))
return nil, err

Check warning on line 103 in pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L102-L103

Added lines #L102 - L103 were not covered by tests
}

return target, nil
return &pipedservice.InstallToolResponse{
InstalledPath: target,
}, nil
}

func (r *ToolRegistry) Close() error {
if err := os.RemoveAll(r.tmpDir); err != nil {
return err
}
return nil
// NewTestToolRegistry returns a new instance of ToolRegistry for testing purpose.
func NewTestToolRegistry(t *testing.T) *toolregistry.ToolRegistry {
return toolregistry.NewToolRegistry(&fakeClient{
testingT: t,
tmpDir: t.TempDir(),
})
}