Skip to content

Commit

Permalink
test: write new tests for worker code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKujawa committed Nov 24, 2022
1 parent 1686394 commit 92f8b5a
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
112 changes: 112 additions & 0 deletions go-chaos/worker/chaos_worker_test.go
Original file line number Diff line number Diff line change
@@ -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

}
69 changes: 69 additions & 0 deletions go-chaos/worker/fake.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 92f8b5a

Please sign in to comment.