diff --git a/go-chaos/worker/chaos_worker_test.go b/go-chaos/worker/chaos_worker_test.go index 6a0ce730e..f2073c30f 100644 --- a/go-chaos/worker/chaos_worker_test.go +++ b/go-chaos/worker/chaos_worker_test.go @@ -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) { @@ -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{} @@ -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{} diff --git a/go-chaos/worker/fake.go b/go-chaos/worker/fake.go index f2a9a4fb9..1d0fb7d84 100644 --- a/go-chaos/worker/fake.go +++ b/go-chaos/worker/fake.go @@ -30,6 +30,7 @@ type FakeJobClient struct { ErrorMsg string Failed bool Succeeded bool + Variables interface{} } type FakeCompleteClient struct { @@ -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 { @@ -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 }