Skip to content

Commit 3c0a57d

Browse files
committed
Move log persister for testing to separate package
Signed-off-by: Shinnosuke Sawada-Dazai <[email protected]>
1 parent 6460f32 commit 3c0a57d

File tree

2 files changed

+66
-47
lines changed

2 files changed

+66
-47
lines changed

pkg/app/pipedv1/plugin/kubernetes/deployment/server_test.go

+2-47
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"path"
2222
"path/filepath"
2323
"testing"
24-
"time"
2524

2625
"github.com/stretchr/testify/assert"
2726
"github.com/stretchr/testify/require"
@@ -38,54 +37,10 @@ import (
3837
config "github.com/pipe-cd/pipecd/pkg/configv1"
3938
"github.com/pipe-cd/pipecd/pkg/model"
4039
"github.com/pipe-cd/pipecd/pkg/plugin/api/v1alpha1/deployment"
41-
"github.com/pipe-cd/pipecd/pkg/plugin/logpersister"
40+
"github.com/pipe-cd/pipecd/pkg/plugin/logpersister/logpersistertest"
4241
"github.com/pipe-cd/pipecd/pkg/plugin/toolregistry/toolregistrytest"
4342
)
4443

45-
// newTestLogPersister creates a new testLogPersister for testing.
46-
//
47-
// TODO: move to a common package
48-
func newTestLogPersister(t *testing.T) testLogPersister {
49-
return testLogPersister{t}
50-
}
51-
52-
// testLogPersister implements logpersister for testing.
53-
type testLogPersister struct {
54-
t *testing.T
55-
}
56-
57-
func (lp testLogPersister) StageLogPersister(deploymentID, stageID string) logpersister.StageLogPersister {
58-
return lp
59-
}
60-
61-
func (lp testLogPersister) Write(log []byte) (int, error) {
62-
// Write the log to the test logger
63-
lp.t.Log(string(log))
64-
return 0, nil
65-
}
66-
func (lp testLogPersister) Info(log string) {
67-
lp.t.Log("INFO", log)
68-
}
69-
func (lp testLogPersister) Infof(format string, a ...interface{}) {
70-
lp.t.Logf("INFO "+format, a...)
71-
}
72-
func (lp testLogPersister) Success(log string) {
73-
lp.t.Log("SUCCESS", log)
74-
}
75-
func (lp testLogPersister) Successf(format string, a ...interface{}) {
76-
lp.t.Logf("SUCCESS "+format, a...)
77-
}
78-
func (lp testLogPersister) Error(log string) {
79-
lp.t.Log("ERROR", log)
80-
}
81-
func (lp testLogPersister) Errorf(format string, a ...interface{}) {
82-
lp.t.Logf("ERROR "+format, a...)
83-
}
84-
func (lp testLogPersister) Complete(timeout time.Duration) error {
85-
lp.t.Logf("Complete stage log persister with timeout: %v", timeout)
86-
return nil
87-
}
88-
8944
// TODO: move to a common package
9045
func examplesDir() string {
9146
d, _ := os.Getwd()
@@ -191,7 +146,7 @@ func TestDeploymentService_executeK8sSyncStage(t *testing.T) {
191146
},
192147
}
193148

194-
svc := NewDeploymentService(pluginCfg, zaptest.NewLogger(t), testRegistry, newTestLogPersister(t))
149+
svc := NewDeploymentService(pluginCfg, zaptest.NewLogger(t), testRegistry, logpersistertest.NewTestLogPersister(t))
195150
resp, err := svc.ExecuteStage(ctx, req)
196151

197152
require.NoError(t, err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2024 The PipeCD Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package logpersistertest
16+
17+
import (
18+
"testing"
19+
"time"
20+
21+
"github.com/pipe-cd/pipecd/pkg/plugin/logpersister"
22+
)
23+
24+
// NewTestLogPersister creates a new testLogPersister for testing.
25+
func NewTestLogPersister(t *testing.T) TestLogPersister {
26+
return TestLogPersister{t}
27+
}
28+
29+
// TestLogPersister implements logpersister for testing.
30+
type TestLogPersister struct {
31+
t *testing.T
32+
}
33+
34+
func (lp TestLogPersister) StageLogPersister(deploymentID, stageID string) logpersister.StageLogPersister {
35+
return lp
36+
}
37+
38+
func (lp TestLogPersister) Write(log []byte) (int, error) {
39+
// Write the log to the test logger
40+
lp.t.Log(string(log))
41+
return 0, nil
42+
}
43+
func (lp TestLogPersister) Info(log string) {
44+
lp.t.Log("INFO", log)
45+
}
46+
func (lp TestLogPersister) Infof(format string, a ...interface{}) {
47+
lp.t.Logf("INFO "+format, a...)
48+
}
49+
func (lp TestLogPersister) Success(log string) {
50+
lp.t.Log("SUCCESS", log)
51+
}
52+
func (lp TestLogPersister) Successf(format string, a ...interface{}) {
53+
lp.t.Logf("SUCCESS "+format, a...)
54+
}
55+
func (lp TestLogPersister) Error(log string) {
56+
lp.t.Log("ERROR", log)
57+
}
58+
func (lp TestLogPersister) Errorf(format string, a ...interface{}) {
59+
lp.t.Logf("ERROR "+format, a...)
60+
}
61+
func (lp TestLogPersister) Complete(timeout time.Duration) error {
62+
lp.t.Logf("Complete stage log persister with timeout: %v", timeout)
63+
return nil
64+
}

0 commit comments

Comments
 (0)