-
Notifications
You must be signed in to change notification settings - Fork 156
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
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c33682e
Change struct member to interface type
Warashi b8a2a2d
Add toolregistry implementation for test
Warashi c7de833
Return struct not interface type
Warashi 867bdb7
Add test for k8s toolregistry
Warashi a6079c7
Rename test package
Warashi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
pkg/app/pipedv1/plugin/kubernetes/toolregistry/registry_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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))) | ||
} |
134 changes: 134 additions & 0 deletions
134
pkg/app/pipedv1/plugin/toolregistry/toolregistrytest/toolregistrytest.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
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 | ||
} | ||
return target, nil | ||
} | ||
|
||
func (r *ToolRegistry) outPath() (string, error) { | ||
target, err := r.newTmpDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
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 | ||
} | ||
|
||
tmpDir, err := r.newTmpDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
binDir, err := r.binDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
t, err := template.New("install script").Parse(script) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
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 | ||
} | ||
|
||
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", buf.String()) | ||
if out, err := cmd.CombinedOutput(); err != nil { | ||
r.testingT.Log(string(out)) | ||
return "", err | ||
} | ||
|
||
if err := os.Chmod(outPath, 0o755); err != nil { | ||
return "", err | ||
} | ||
|
||
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 | ||
} | ||
|
||
if err := os.RemoveAll(tmpDir); err != nil { | ||
return "", err | ||
} | ||
|
||
return target, nil | ||
} | ||
|
||
func (r *ToolRegistry) Close() error { | ||
if err := os.RemoveAll(r.tmpDir); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed on a6079c7