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 k8s plugin's toolregistry #5246

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
24 changes: 10 additions & 14 deletions pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,31 @@ package toolregistry

import (
"context"

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

// Registry provides functions to get path to the needed tools.
type Registry interface {
Kubectl(ctx context.Context, version string) (string, error)
Kustomize(ctx context.Context, version string) (string, error)
Helm(ctx context.Context, version string) (string, error)
type client interface {
InstallTool(ctx context.Context, name, version, script string) (string, error)
}

func NewRegistry(client toolregistry.ToolRegistry) Registry {
return &registry{
func NewRegistry(client client) *Registry {
return &Registry{
client: client,
}
}

type registry struct {
client toolregistry.ToolRegistry
// Registry provides functions to get path to the needed tools.
type Registry struct {
client client
}

func (r *registry) Kubectl(ctx context.Context, version string) (string, error) {
func (r *Registry) Kubectl(ctx context.Context, version string) (string, error) {
return r.client.InstallTool(ctx, "kubectl", version, kubectlInstallScript)
}

func (r *registry) Kustomize(ctx context.Context, version string) (string, error) {
func (r *Registry) Kustomize(ctx context.Context, version string) (string, error) {
return r.client.InstallTool(ctx, "kustomize", version, kustomizeInstallScript)
}

func (r *registry) Helm(ctx context.Context, version string) (string, error) {
func (r *Registry) Helm(ctx context.Context, version string) (string, error) {
return r.client.InstallTool(ctx, "helm", version, helmInstallScript)
}
93 changes: 93 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2024 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package toolregistry_test
Copy link
Member

Choose a reason for hiding this comment

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

we usually not create test package, just keep it package toolregistry

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed on a6079c7


import (
"context"
"os/exec"
"strings"
"testing"

"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/toolregistry"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/toolregistry/toolregistrytest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRegistry_Kubectl(t *testing.T) {
t.Parallel()

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

r := toolregistry.NewRegistry(c)

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

p, err := r.Kubectl(context.Background(), "1.30.2")
require.NoError(t, err)
require.NotEmpty(t, p)

out, err := exec.CommandContext(context.Background(), p, "version", "--client=true").CombinedOutput()
require.NoError(t, err)

expected := "Client Version: v1.30.2\nKustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3"

assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
}

func TestRegistry_Kustomize(t *testing.T) {
t.Parallel()

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

r := toolregistry.NewRegistry(c)

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

p, err := r.Kustomize(context.Background(), "5.4.3")
require.NoError(t, err)
require.NotEmpty(t, p)

out, err := exec.CommandContext(context.Background(), p, "version").CombinedOutput()
require.NoError(t, err)

expected := "v5.4.3"

assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
}

func TestRegistry_Helm(t *testing.T) {
t.Parallel()

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

r := toolregistry.NewRegistry(c)

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

p, err := r.Helm(context.Background(), "3.16.1")
require.NoError(t, err)
require.NotEmpty(t, p)

out, err := exec.CommandContext(context.Background(), p, "version").CombinedOutput()
require.NoError(t, err)

expected := `version.BuildInfo{Version:"v3.16.1", GitCommit:"5a5449dc42be07001fd5771d56429132984ab3ab", GitTreeState:"clean", GoVersion:"go1.22.7"}`

assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2024 The PipeCD Authors.
khanhtc1202 marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package toolregistrytest

import (
"bytes"
"context"
"os"
"os/exec"
"runtime"
"testing"
"text/template"
)

type ToolRegistry struct {
testingT *testing.T
tmpDir string
}

type templateValues struct {
Name string
Version string
OutPath string
TmpDir string
Arch string
Os string
}

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

Check warning on line 45 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L44-L45

Added lines #L44 - L45 were not covered by tests
return &ToolRegistry{
testingT: t,
tmpDir: tmpDir,
}, nil
}

func (r *ToolRegistry) newTmpDir() (string, error) {
return os.MkdirTemp(r.tmpDir, "")
}

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

Check warning on line 60 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L59-L60

Added lines #L59 - L60 were not covered by tests
return target, nil
}

func (r *ToolRegistry) outPath() (string, error) {
target, err := r.newTmpDir()
if err != nil {
return "", err
}

Check warning on line 68 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L67-L68

Added lines #L67 - L68 were not covered by tests
return target + "/out", nil
}

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

Check warning on line 76 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L75-L76

Added lines #L75 - L76 were not covered by tests

tmpDir, err := r.newTmpDir()
if err != nil {
return "", err
}

Check warning on line 81 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L80-L81

Added lines #L80 - L81 were not covered by tests

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

Check warning on line 86 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L85-L86

Added lines #L85 - L86 were not covered by tests

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

Check warning on line 91 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L90-L91

Added lines #L90 - L91 were not covered by tests

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

Check warning on line 104 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L103-L104

Added lines #L103 - L104 were 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
}

Check warning on line 110 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L108-L110

Added lines #L108 - L110 were not covered by tests

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

Check warning on line 114 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L113-L114

Added lines #L113 - L114 were not covered by tests

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

Check warning on line 120 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L118-L120

Added lines #L118 - L120 were not covered by tests

if err := os.RemoveAll(tmpDir); err != nil {
return "", err
}

Check warning on line 124 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L123-L124

Added lines #L123 - L124 were not covered by tests

return target, nil
}

func (r *ToolRegistry) Close() error {
if err := os.RemoveAll(r.tmpDir); err != nil {
return err
}

Check warning on line 132 in pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go#L131-L132

Added lines #L131 - L132 were not covered by tests
return nil
}
Loading