-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: write new tests for worker code
- Loading branch information
1 parent
1686394
commit 92f8b5a
Showing
2 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |