Skip to content

Commit

Permalink
test: test readExperiments handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKujawa committed Nov 25, 2022
1 parent aa9382a commit 31ae47c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
59 changes: 59 additions & 0 deletions go-chaos/worker/chaos_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/camunda/zeebe/clients/go/v8/pkg/pb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
chaos_experiments "github.com/zeebe-io/zeebe-chaos/go-chaos/internal/chaos-experiments"
)

func Test_ShouldFailToHandleJobWithoutPayload(t *testing.T) {
Expand All @@ -47,6 +48,24 @@ func Test_ShouldFailToHandleJobWithoutPayload(t *testing.T) {
assert.Equal(t, 0, fakeJobClient.RetriesVal)
}

func Test_ShouldFailToHandleReadExperimentsJobWithoutPayload(t *testing.T) {
// given
fakeJobClient := &FakeJobClient{}
job := entities.Job{
&pb.ActivatedJob{
Key: 123,
},
}

// when
HandleReadExperiments(fakeJobClient, job)

// then
assert.True(t, fakeJobClient.Failed)
assert.Equal(t, 123, fakeJobClient.Key)
assert.Equal(t, 0, fakeJobClient.RetriesVal)
}

func Test_ShouldHandleCommand(t *testing.T) {
// given
fakeJobClient := &FakeJobClient{}
Expand Down Expand Up @@ -76,6 +95,46 @@ func Test_ShouldHandleCommand(t *testing.T) {
assert.Equal(t, expectedArgs, appliedArgs)
}

func Test_ShouldSendExperimentsForClusterPlan(t *testing.T) {
// given
fakeJobClient := &FakeJobClient{}
job := entities.Job{
&pb.ActivatedJob{
Key: 123,
Variables: "{\"clusterPlan\":\"Production - S\"}",
},
}

// when
HandleReadExperiments(fakeJobClient, job)

// then
assert.True(t, fakeJobClient.Succeeded)
assert.Equal(t, 123, fakeJobClient.Key)
experiments, err := chaos_experiments.ReadExperimentsForClusterPlan("Production - S")
require.NoError(t, err)
assert.Equal(t, experiments, fakeJobClient.Variables)
}

func Test_ShouldFailWhenNoClusterPlanForReadExperimentsJob(t *testing.T) {
// given
fakeJobClient := &FakeJobClient{}
job := entities.Job{
&pb.ActivatedJob{
Key: 123,
Variables: "{\"clusterPlan\":\"noop\"}",
},
}

// when
HandleReadExperiments(fakeJobClient, job)

// then
assert.True(t, fakeJobClient.Failed)
assert.Equal(t, 123, fakeJobClient.Key)
assert.Equal(t, "open camunda-cloud/noop: file does not exist", fakeJobClient.ErrorMsg)
}

func Test_ShouldFailJobWhenHandleFails(t *testing.T) {
// given
fakeJobClient := &FakeJobClient{}
Expand Down
11 changes: 11 additions & 0 deletions go-chaos/worker/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type FakeJobClient struct {
ErrorMsg string
Failed bool
Succeeded bool
Variables interface{}
}

type FakeCompleteClient struct {
Expand All @@ -53,6 +54,11 @@ func (f *FakeCompleteClient) Send(ctx context.Context) (*pb.CompleteJobResponse,
return &pb.CompleteJobResponse{}, nil
}

func (f *FakeCompleteClient) VariablesFromObject(v interface{}) (commands.DispatchCompleteJobCommand, error) {
f.JobClient.Variables = v
return f, nil
}

// Fake FAIL Client

func (f *FakeJobClient) NewFailJobCommand() commands.FailJobCommandStep1 {
Expand All @@ -78,6 +84,11 @@ func (f *FakeFailClient) Retries(retries int32) commands.FailJobCommandStep3 {
return f
}

func (f *FakeFailClient) ErrorMessage(errorMsg string) commands.FailJobCommandStep3 {
f.JobClient.ErrorMsg = errorMsg
return f
}

func (f *FakeFailClient) Send(ctx context.Context) (*pb.FailJobResponse, error) {
return &pb.FailJobResponse{}, nil
}

0 comments on commit 31ae47c

Please sign in to comment.