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

fix: upgrade the test framework form monkey to mockey #561

Merged
merged 9 commits into from
Oct 20, 2023
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module kusionstack.io/kusion
go 1.19

require (
bou.ke/monkey v1.0.2
github.com/AlecAivazis/survey/v2 v2.3.4
github.com/Azure/go-autorest/autorest/mocks v0.4.1
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ atomicgo.dev/keyboard v0.2.9 h1:tOsIid3nlPLZ3lwgG8KZMp/SFmr7P0ssEN5JUsm78K8=
atomicgo.dev/keyboard v0.2.9/go.mod h1:BC4w9g00XkxH/f1HXhW2sXmJFOCWbKn9xrOunSFtExQ=
atomicgo.dev/schedule v0.0.2 h1:2e/4KY6t3wokja01Cyty6qgkQM8MotJzjtqCH70oX2Q=
atomicgo.dev/schedule v0.0.2/go.mod h1:xeUa3oAkiuHYh8bKiQBRojqAMq3PXXbJujjb0hw8pEU=
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down
127 changes: 55 additions & 72 deletions pkg/cmd/apply/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"reflect"
"testing"

"bou.ke/monkey"
"github.com/AlecAivazis/survey/v2"
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"kusionstack.io/kusion/pkg/cmd/spec"
Expand All @@ -26,12 +26,11 @@ import (
)

func TestApplyOptions_Run(t *testing.T) {
t.Run("Detail is true", func(t *testing.T) {
defer monkey.UnpatchAll()
mockDetectProjectAndStack()
mockGenerateSpec()
mockNewKubernetesRuntime()
mockOperationPreview()
mockey.PatchConvey("Detail is true", t, func() {
mockeyPatchDetectProjectAndStack()
mockeyPatchGenerateSpec()
mockeyPatchNewKubernetesRuntime()
mockeyPatchOperationPreview()

o := NewApplyOptions()
o.Detail = true
Expand All @@ -41,12 +40,11 @@ func TestApplyOptions_Run(t *testing.T) {
assert.Nil(t, err)
})

t.Run("DryRun is true", func(t *testing.T) {
defer monkey.UnpatchAll()
mockDetectProjectAndStack()
mockGenerateSpec()
mockNewKubernetesRuntime()
mockOperationPreview()
mockey.PatchConvey("DryRun is true", t, func() {
mockeyPatchDetectProjectAndStack()
mockeyPatchGenerateSpec()
mockeyPatchNewKubernetesRuntime()
mockeyPatchOperationPreview()
mockOperationApply(opsmodels.Success)

o := NewApplyOptions()
Expand All @@ -71,28 +69,28 @@ var (
}
)

func mockDetectProjectAndStack() {
monkey.Patch(projectstack.DetectProjectAndStack, func(stackDir string) (*projectstack.Project, *projectstack.Stack, error) {
func mockeyPatchDetectProjectAndStack() *mockey.Mocker {
return mockey.Mock(projectstack.DetectProjectAndStack).To(func(stackDir string) (*projectstack.Project, *projectstack.Stack, error) {
project.Path = stackDir
stack.Path = stackDir
return project, stack, nil
})
}).Build()
}

func mockGenerateSpec() {
monkey.Patch(spec.GenerateSpecWithSpinner, func(
func mockeyPatchGenerateSpec() *mockey.Mocker {
return mockey.Mock(spec.GenerateSpec).To(func(
o *generator.Options,
project *projectstack.Project,
stack *projectstack.Stack,
) (*models.Spec, error) {
return &models.Spec{Resources: []models.Resource{sa1, sa2, sa3}}, nil
})
}).Build()
}

func mockNewKubernetesRuntime() {
monkey.Patch(kubernetes.NewKubernetesRuntime, func() (runtime.Runtime, error) {
func mockeyPatchNewKubernetesRuntime() *mockey.Mocker {
return mockey.Mock(kubernetes.NewKubernetesRuntime).To(func() (runtime.Runtime, error) {
return &fakerRuntime{}, nil
})
}).Build()
}

var _ runtime.Runtime = (*fakerRuntime)(nil)
Expand Down Expand Up @@ -131,33 +129,31 @@ func (f *fakerRuntime) Watch(ctx context.Context, request *runtime.WatchRequest)
return nil
}

func mockOperationPreview() {
monkey.Patch((*operation.PreviewOperation).Preview,
func(*operation.PreviewOperation, *operation.PreviewRequest) (rsp *operation.PreviewResponse, s status.Status) {
return &operation.PreviewResponse{
Order: &opsmodels.ChangeOrder{
StepKeys: []string{sa1.ID, sa2.ID, sa3.ID},
ChangeSteps: map[string]*opsmodels.ChangeStep{
sa1.ID: {
ID: sa1.ID,
Action: opsmodels.Create,
From: &sa1,
},
sa2.ID: {
ID: sa2.ID,
Action: opsmodels.UnChanged,
From: &sa2,
},
sa3.ID: {
ID: sa3.ID,
Action: opsmodels.Undefined,
From: &sa1,
},
func mockeyPatchOperationPreview() *mockey.Mocker {
return mockey.Mock((*operation.PreviewOperation).Preview).To(func(*operation.PreviewOperation, *operation.PreviewRequest) (rsp *operation.PreviewResponse, s status.Status) {
return &operation.PreviewResponse{
Order: &opsmodels.ChangeOrder{
StepKeys: []string{sa1.ID, sa2.ID, sa3.ID},
ChangeSteps: map[string]*opsmodels.ChangeStep{
sa1.ID: {
ID: sa1.ID,
Action: opsmodels.Create,
From: &sa1,
},
sa2.ID: {
ID: sa2.ID,
Action: opsmodels.UnChanged,
From: &sa2,
},
sa3.ID: {
ID: sa3.ID,
Action: opsmodels.Undefined,
From: &sa1,
},
},
}, nil
},
)
},
}, nil
}).Build()
}

const (
Expand Down Expand Up @@ -189,9 +185,7 @@ func newSA(name string) models.Resource {

func Test_apply(t *testing.T) {
stateStorage := &local.FileSystemState{Path: filepath.Join("", local.KusionState)}
t.Run("dry run", func(t *testing.T) {
defer monkey.UnpatchAll()

mockey.PatchConvey("dry run", t, func() {
planResources := &models.Spec{Resources: []models.Resource{sa1}}
order := &opsmodels.ChangeOrder{
StepKeys: []string{sa1.ID},
Expand All @@ -209,10 +203,8 @@ func Test_apply(t *testing.T) {
err := Apply(o, stateStorage, planResources, changes, os.Stdout)
assert.Nil(t, err)
})
t.Run("apply success", func(t *testing.T) {
defer monkey.UnpatchAll()
mockey.PatchConvey("apply success", t, func() {
mockOperationApply(opsmodels.Success)

o := NewApplyOptions()
planResources := &models.Spec{Resources: []models.Resource{sa1, sa2}}
order := &opsmodels.ChangeOrder{
Expand All @@ -235,8 +227,7 @@ func Test_apply(t *testing.T) {
err := Apply(o, stateStorage, planResources, changes, os.Stdout)
assert.Nil(t, err)
})
t.Run("apply failed", func(t *testing.T) {
defer monkey.UnpatchAll()
mockey.PatchConvey("apply failed", t, func() {
mockOperationApply(opsmodels.Failed)

o := NewApplyOptions()
Expand All @@ -259,7 +250,7 @@ func Test_apply(t *testing.T) {
}

func mockOperationApply(res opsmodels.OpResult) {
monkey.Patch((*operation.ApplyOperation).Apply,
mockey.Mock((*operation.ApplyOperation).Apply).To(
func(o *operation.ApplyOperation, request *operation.ApplyRequest) (*operation.ApplyResponse, status.Status) {
var err error
if res == opsmodels.Failed {
Expand All @@ -283,34 +274,26 @@ func mockOperationApply(res opsmodels.OpResult) {
return nil, status.NewErrorStatus(err)
}
return &operation.ApplyResponse{}, nil
})
}).Build()
}

func Test_prompt(t *testing.T) {
t.Run("prompt error", func(t *testing.T) {
monkey.Patch(
survey.AskOne,
func(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error {
return errors.New("mock error")
},
)
mockey.PatchConvey("prompt error", t, func() {
mockey.Mock(survey.AskOne).Return(errors.New("mock error")).Build()
_, err := prompt()
assert.NotNil(t, err)
})

t.Run("prompt yes", func(t *testing.T) {
mockey.PatchConvey("prompt yes", t, func() {
mockPromptOutput("yes")
_, err := prompt()
assert.Nil(t, err)
})
}

func mockPromptOutput(res string) {
monkey.Patch(
survey.AskOne,
func(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error {
reflect.ValueOf(response).Elem().Set(reflect.ValueOf(res))
return nil
},
)
mockey.Mock(survey.AskOne).To(func(p survey.Prompt, response interface{}, opts ...survey.AskOpt) error {
reflect.ValueOf(response).Elem().Set(reflect.ValueOf(res))
return nil
}).Build()
}
15 changes: 6 additions & 9 deletions pkg/cmd/check/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ package check
import (
"testing"

"bou.ke/monkey"
"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"kusionstack.io/kusion/pkg/cmd/compile"
)

func TestNewCmdCheck(t *testing.T) {
t.Run("", func(t *testing.T) {
defer monkey.UnpatchAll()

monkey.Patch((*compile.Options).Complete, func(o *compile.Options, args []string) error {
mockey.PatchConvey("", t, func() {
mockey.Mock((*compile.Options).Complete).To(func(o *compile.Options, args []string) error {
o.Output = "stdout"
return nil
})
monkey.Patch((*compile.Options).Run, func(*compile.Options) error {
}).Build()
mockey.Mock((*compile.Options).Run).To(func(*compile.Options) error {
return nil
})

}).Build()
cmd := NewCmdCheck()
err := cmd.Execute()
assert.Nil(t, err)
Expand Down
40 changes: 39 additions & 1 deletion pkg/cmd/compile/options_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package compile

import (
"errors"
"io/fs"
"os"
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/bytedance/mockey"
"kusionstack.io/kusion/pkg/cmd/spec"
"kusionstack.io/kusion/pkg/engine"
"kusionstack.io/kusion/pkg/generator"
Expand Down Expand Up @@ -35,6 +36,8 @@ var (
sa1 = newSA("sa1")
sa2 = newSA("sa2")
sa3 = newSA("sa3")

errTest = errors.New("test error")
)

func TestCompileOptions_preSet(t *testing.T) {
Expand Down Expand Up @@ -113,6 +116,27 @@ func TestCompileOptions_Run(t *testing.T) {
err := o.Run()
assert.Nil(t, err)
})

mockey.PatchConvey("detect project and spec failed", t, func() {
m1 := mockDetectProjectAndStackFail()
defer m1.UnPatch()

o := NewCompileOptions()
o.NoStyle = true
err := o.Run()
assert.Equal(t, errTest, err)
})

mockey.PatchConvey("generate spec failed", t, func() {
m1 := mockDetectProjectAndStack()
m2 := mockGenerateSpecFail()
defer m1.UnPatch()
defer m2.UnPatch()
o := NewCompileOptions()
o.NoStyle = true
err := o.Run()
assert.Equal(t, errTest, err)
})
}

func newSA(name string) models.Resource {
Expand All @@ -138,6 +162,14 @@ func mockDetectProjectAndStack() *mockey.Mocker {
}).Build()
}

func mockDetectProjectAndStackFail() *mockey.Mocker {
return mockey.Mock(projectstack.DetectProjectAndStack).To(func(stackDir string) (*projectstack.Project, *projectstack.Stack, error) {
project.Path = stackDir
stack.Path = stackDir
return project, stack, errTest
}).Build()
}

func mockGenerateSpec() *mockey.Mocker {
return mockey.Mock(spec.GenerateSpecWithSpinner).To(func(
o *generator.Options,
Expand All @@ -148,6 +180,12 @@ func mockGenerateSpec() *mockey.Mocker {
}).Build()
}

func mockGenerateSpecFail() *mockey.Mocker {
return mockey.Mock(spec.GenerateSpecWithSpinner).To(func(o *generator.Options, project *projectstack.Project, stack *projectstack.Stack) (*models.Spec, error) {
return &models.Spec{Resources: []models.Resource{sa1, sa2, sa3}}, errTest
}).Build()
}

func mockWriteFile() *mockey.Mocker {
return mockey.Mock(os.WriteFile).To(func(name string, data []byte, perm fs.FileMode) error {
return nil
Expand Down
Loading
Loading