-
Notifications
You must be signed in to change notification settings - Fork 157
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 executing K8S_SYNC stage at the kubernetes plugin #5431
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ccebc52
Fix commit hash in loadManifests function
Warashi 098d1da
Fix resource key to keep compatibility
Warashi 6460f32
Add tests for executing K8S_SYNC
Warashi 3c0a57d
Move log persister for testing to separate package
Warashi 63c8d77
Merge branch 'master' into k8s-plugin-sync-test
Warashi 057b9f5
Revert "Fix resource key to keep compatibility"
Warashi e030be1
Change the assertion and add comment
Warashi 8fe9dad
Remove TODO comment
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
170 changes: 170 additions & 0 deletions
170
pkg/app/pipedv1/plugin/kubernetes/deployment/server_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,170 @@ | ||
// 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 deployment | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zaptest" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
|
||
kubeConfigPkg "github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/config" | ||
config "github.com/pipe-cd/pipecd/pkg/configv1" | ||
"github.com/pipe-cd/pipecd/pkg/model" | ||
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment" | ||
"github.com/pipe-cd/pipecd/pkg/plugin/logpersister/logpersistertest" | ||
"github.com/pipe-cd/pipecd/pkg/plugin/toolregistry/toolregistrytest" | ||
) | ||
|
||
// TODO: move to a common package | ||
func examplesDir() string { | ||
d, _ := os.Getwd() | ||
for { | ||
if _, err := os.Stat(filepath.Join(d, "examples")); err == nil { | ||
return filepath.Join(d, "examples") | ||
} | ||
d = filepath.Dir(d) | ||
} | ||
} | ||
|
||
func kubeconfigFromRestConfig(restConfig *rest.Config) (string, error) { | ||
clusters := make(map[string]*clientcmdapi.Cluster) | ||
clusters["default-cluster"] = &clientcmdapi.Cluster{ | ||
Server: restConfig.Host, | ||
CertificateAuthorityData: restConfig.CAData, | ||
} | ||
contexts := make(map[string]*clientcmdapi.Context) | ||
contexts["default-context"] = &clientcmdapi.Context{ | ||
Cluster: "default-cluster", | ||
AuthInfo: "default-user", | ||
} | ||
authinfos := make(map[string]*clientcmdapi.AuthInfo) | ||
authinfos["default-user"] = &clientcmdapi.AuthInfo{ | ||
ClientCertificateData: restConfig.CertData, | ||
ClientKeyData: restConfig.KeyData, | ||
} | ||
clientConfig := clientcmdapi.Config{ | ||
Kind: "Config", | ||
APIVersion: "v1", | ||
Clusters: clusters, | ||
Contexts: contexts, | ||
CurrentContext: "default-context", | ||
AuthInfos: authinfos, | ||
} | ||
b, err := clientcmd.Write(clientConfig) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(b), nil | ||
} | ||
|
||
func TestDeploymentService_executeK8sSyncStage(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
// initialize tool registry | ||
testRegistry, err := toolregistrytest.NewToolRegistry(t) | ||
require.NoError(t, err) | ||
|
||
// initialize envtest | ||
tEnv := new(envtest.Environment) | ||
kubeCfg, err := tEnv.Start() | ||
require.NoError(t, err) | ||
t.Cleanup(func() { tEnv.Stop() }) | ||
|
||
kubeconfig, err := kubeconfigFromRestConfig(kubeCfg) | ||
require.NoError(t, err) | ||
|
||
workDir := t.TempDir() | ||
kubeconfigPath := path.Join(workDir, "kubeconfig") | ||
err = os.WriteFile(kubeconfigPath, []byte(kubeconfig), 0755) | ||
require.NoError(t, err) | ||
|
||
deployTarget, err := json.Marshal(kubeConfigPkg.KubernetesDeployTargetConfig{KubeConfigPath: kubeconfigPath}) | ||
require.NoError(t, err) | ||
|
||
// prepare the piped plugin config | ||
pluginCfg := &config.PipedPlugin{ | ||
Name: "kubernetes", | ||
URL: "file:///path/to/kubernetes/plugin", // dummy for testing | ||
Port: 0, // dummy for testing | ||
DeployTargets: []config.PipedDeployTarget{{ | ||
Name: "default", | ||
Labels: map[string]string{}, | ||
Config: json.RawMessage(deployTarget), | ||
}}, | ||
} | ||
|
||
cfg, err := os.ReadFile(filepath.Join(examplesDir(), "kubernetes", "simple", "app.pipecd.yaml")) | ||
require.NoError(t, err) | ||
|
||
req := &deployment.ExecuteStageRequest{ | ||
Input: &deployment.ExecutePluginInput{ | ||
Deployment: &model.Deployment{ | ||
PipedId: "piped-id", | ||
ApplicationId: "app-id", | ||
DeployTargets: []string{"default"}, | ||
}, | ||
Stage: &model.PipelineStage{ | ||
Id: "stage-id", | ||
Name: "K8S_SYNC", | ||
}, | ||
StageConfig: []byte(``), | ||
RunningDeploymentSource: nil, | ||
TargetDeploymentSource: &deployment.DeploymentSource{ | ||
ApplicationDirectory: filepath.Join(examplesDir(), "kubernetes", "simple"), | ||
Revision: "0123456789", | ||
ApplicationConfig: cfg, | ||
ApplicationConfigFilename: "app.pipecd.yaml", | ||
}, | ||
}, | ||
} | ||
|
||
svc := NewDeploymentService(pluginCfg, zaptest.NewLogger(t), testRegistry, logpersistertest.NewTestLogPersister(t)) | ||
resp, err := svc.ExecuteStage(ctx, req) | ||
|
||
require.NoError(t, err) | ||
assert.Equal(t, model.StageStatus_STAGE_SUCCESS.String(), resp.GetStatus().String()) | ||
|
||
// check the deployment is created with client-go | ||
dynamicClient, err := dynamic.NewForConfig(kubeCfg) | ||
require.NoError(t, err) | ||
|
||
deployment, err := dynamicClient.Resource(schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}).Namespace("default").Get(context.Background(), "simple", metav1.GetOptions{}) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "simple", deployment.GetName()) | ||
assert.Equal(t, "simple", deployment.GetLabels()["app"]) | ||
assert.Equal(t, "piped", deployment.GetAnnotations()["pipecd.dev/managed-by"]) | ||
assert.Equal(t, "piped-id", deployment.GetAnnotations()["pipecd.dev/piped"]) | ||
assert.Equal(t, "app-id", deployment.GetAnnotations()["pipecd.dev/application"]) | ||
assert.Equal(t, "apps/v1", deployment.GetAnnotations()["pipecd.dev/original-api-version"]) | ||
assert.Equal(t, "apps/v1:Deployment::simple", deployment.GetAnnotations()["pipecd.dev/resource-key"]) // This assertion differs from the non-plugin-arched piped's Kubernetes platform provider, but we decided to change this behavior. | ||
assert.Equal(t, "0123456789", deployment.GetAnnotations()["pipecd.dev/commit-hash"]) | ||
|
||
} |
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,64 @@ | ||
// 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 logpersistertest | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/pipe-cd/pipecd/pkg/plugin/logpersister" | ||
) | ||
|
||
// NewTestLogPersister creates a new testLogPersister for testing. | ||
func NewTestLogPersister(t *testing.T) TestLogPersister { | ||
return TestLogPersister{t} | ||
} | ||
|
||
// TestLogPersister implements logpersister for testing. | ||
type TestLogPersister struct { | ||
t *testing.T | ||
} | ||
|
||
func (lp TestLogPersister) StageLogPersister(deploymentID, stageID string) logpersister.StageLogPersister { | ||
return lp | ||
} | ||
|
||
func (lp TestLogPersister) Write(log []byte) (int, error) { | ||
// Write the log to the test logger | ||
lp.t.Log(string(log)) | ||
return 0, nil | ||
} | ||
func (lp TestLogPersister) Info(log string) { | ||
lp.t.Log("INFO", log) | ||
} | ||
func (lp TestLogPersister) Infof(format string, a ...interface{}) { | ||
lp.t.Logf("INFO "+format, a...) | ||
} | ||
func (lp TestLogPersister) Success(log string) { | ||
lp.t.Log("SUCCESS", log) | ||
} | ||
func (lp TestLogPersister) Successf(format string, a ...interface{}) { | ||
lp.t.Logf("SUCCESS "+format, a...) | ||
} | ||
func (lp TestLogPersister) Error(log string) { | ||
lp.t.Log("ERROR", log) | ||
} | ||
func (lp TestLogPersister) Errorf(format string, a ...interface{}) { | ||
lp.t.Logf("ERROR "+format, a...) | ||
} | ||
func (lp TestLogPersister) Complete(timeout time.Duration) error { | ||
lp.t.Logf("Complete stage log persister with timeout: %v", timeout) | ||
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.
This could be the same but I like the previous version more. How about to rename
GetRevision
toGetCommitHash
🤔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.
First, I also like
commit hash
more thanrevision
.However, this is not the same because the
deploymentSource
can be the running deploy source, not the target deploy source.The naming
GetRevision
comes from proto definition, so how about renaming this field ascommit_hash
?pipecd/pkg/plugin/api/v1alpha1/deployment/api.proto
Lines 142 to 143 in 17a5007
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.
I got your point. Also, it's a commit hash, so it should be named directly as a commit hash to reduce confusion. Let's address it by another PR (change proto and update this)