From 92f8b5aeef38394b392cc4d8b44b11f25f8debcf Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Thu, 24 Nov 2022 16:34:26 +0100 Subject: [PATCH] test: write new tests for worker code --- go-chaos/worker/chaos_worker_test.go | 112 +++++++++++++++++++++++++++ go-chaos/worker/fake.go | 69 +++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 go-chaos/worker/chaos_worker_test.go create mode 100644 go-chaos/worker/fake.go diff --git a/go-chaos/worker/chaos_worker_test.go b/go-chaos/worker/chaos_worker_test.go new file mode 100644 index 000000000..251b621b3 --- /dev/null +++ b/go-chaos/worker/chaos_worker_test.go @@ -0,0 +1,112 @@ +package worker + +import ( + "context" + "encoding/json" + "errors" + "testing" + + "github.com/camunda/zeebe/clients/go/v8/pkg/entities" + "github.com/camunda/zeebe/clients/go/v8/pkg/pb" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func Test_ShouldFailToHandleJobWithoutPayload(t *testing.T) { + // given + fakeJobClient := &FakeJobClient{} + job := entities.Job{ + &pb.ActivatedJob{ + Key: 123, + }, + } + + // when + HandleZbChaosJob(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{} + jsonString, err := createVariablesAsJson() + var appliedArgs []string + CommandRunner = func(args []string, ctx context.Context) error { + appliedArgs = args + return nil // success + } + + require.NoError(t, err) + job := entities.Job{ + &pb.ActivatedJob{ + Key: 123, + Variables: jsonString, + }, + } + + // when + HandleZbChaosJob(fakeJobClient, job) + + // then + assert.True(t, fakeJobClient.Succeeded) + assert.Equal(t, 123, fakeJobClient.Key) + var expectedArgs = []string{ + "--namespace", "clusterId-zeebe", "--clientId", "clientId", "--clientSecret", "clientSecret", "--audience", "audience", "disconnect", "gateway", "--all"} + assert.Equal(t, expectedArgs, appliedArgs) +} + +func Test_ShouldFailJobWhenHandleFails(t *testing.T) { + // given + fakeJobClient := &FakeJobClient{} + jsonString, err := createVariablesAsJson() + var appliedArgs []string + CommandRunner = func(args []string, ctx context.Context) error { + appliedArgs = args + return errors.New("failed") + } + + require.NoError(t, err) + job := entities.Job{ + &pb.ActivatedJob{ + Retries: 3, + Key: 123, + Variables: jsonString, + }, + } + + // when + HandleZbChaosJob(fakeJobClient, job) + + // then + assert.True(t, fakeJobClient.Failed) + assert.Equal(t, 123, fakeJobClient.Key) + assert.Equal(t, 2, fakeJobClient.RetriesVal) + var expectedArgs = []string{ + "--namespace", "clusterId-zeebe", "--clientId", "clientId", "--clientSecret", "clientSecret", "--audience", "audience", "disconnect", "gateway", "--all"} + assert.Equal(t, expectedArgs, appliedArgs) +} + +func createVariablesAsJson() (string, error) { + clusterId := "clusterId" + variables := ZbChaosVariables{ + ClusterId: &clusterId, + Provider: ChaosProvider{ + Path: "zbchaos", + Arguments: []string{"disconnect", "gateway", "--all"}, + }, + AuthenticationDetails: AuthenticationProvider{ + Audience: "audience", + ClientId: "clientId", + ClientSecret: "clientSecret", + ContactPoint: "contactPoint", + }, + } + + marshal, err := json.Marshal(variables) + return string(marshal), err + +} diff --git a/go-chaos/worker/fake.go b/go-chaos/worker/fake.go new file mode 100644 index 000000000..93188bf74 --- /dev/null +++ b/go-chaos/worker/fake.go @@ -0,0 +1,69 @@ +package worker + +import ( + "context" + + "github.com/camunda/zeebe/clients/go/v8/pkg/commands" + "github.com/camunda/zeebe/clients/go/v8/pkg/pb" + "github.com/camunda/zeebe/clients/go/v8/pkg/worker" +) + +type FakeJobClient struct { + worker.JobClient + + Key int + RetriesVal int + ErrorMsg string + Failed bool + Succeeded bool +} + +type FakeCompleteClient struct { + commands.CompleteJobCommandStep1 + commands.CompleteJobCommandStep2 + + JobClient *FakeJobClient +} + +func (f *FakeJobClient) NewCompleteJobCommand() commands.CompleteJobCommandStep1 { + f.Succeeded = true + return &FakeCompleteClient{JobClient: f} +} + +func (f *FakeCompleteClient) JobKey(key int64) commands.CompleteJobCommandStep2 { + f.JobClient.Key = int(key) + return f +} + +func (f *FakeCompleteClient) Send(ctx context.Context) (*pb.CompleteJobResponse, error) { + return &pb.CompleteJobResponse{}, nil +} + +// Fake FAIL Client + +func (f *FakeJobClient) NewFailJobCommand() commands.FailJobCommandStep1 { + f.Failed = true + return &FakeFailClient{JobClient: f} +} + +type FakeFailClient struct { + commands.FailJobCommandStep1 + commands.FailJobCommandStep2 + commands.FailJobCommandStep3 + + JobClient *FakeJobClient +} + +func (f *FakeFailClient) JobKey(key int64) commands.FailJobCommandStep2 { + f.JobClient.Key = int(key) + return f +} + +func (f *FakeFailClient) Retries(retries int32) commands.FailJobCommandStep3 { + f.JobClient.RetriesVal = int(retries) + return f +} + +func (f *FakeFailClient) Send(ctx context.Context) (*pb.FailJobResponse, error) { + return &pb.FailJobResponse{}, nil +}