Skip to content

Commit

Permalink
Merge pull request #352 from kubeshop/jacek/feature/unit-tests-1
Browse files Browse the repository at this point in the history
unit tests for worker
  • Loading branch information
exu committed Oct 4, 2021
2 parents 94c0934 + e27b799 commit 7f56305
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/api/v1/kubtest/model_execution_result_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (e *ExecutionResult) Duration() time.Duration {

return end.Sub(e.StartTime)
}
func (r ExecutionResult) Err(err error) ExecutionResult {
func (r *ExecutionResult) Err(err error) *ExecutionResult {
r.ErrorMessage = err.Error()
return r
}
101 changes: 92 additions & 9 deletions pkg/worker/worker_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
package worker

import (
"io"
"io/ioutil"
"context"
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/kubeshop/kubtest/pkg/api/v1/kubtest"
"github.com/stretchr/testify/assert"
)

func TestWorker_Run(t *testing.T) {
// TODO implement me
t.Skip("not implemented")
t.Run("worker channel pipeline", func(t *testing.T) {
runner := &RunnerMock{T: t}
repo := &RepositoryMock{}
worker := NewWorker(repo, runner)

execution := kubtest.NewExecutionWithID("1", "test", "execution-1")
execution.ExecutionResult = &kubtest.ExecutionResult{}

executionChan := make(chan kubtest.Execution, 2)
executionChan <- execution

worker.Run(executionChan)
time.Sleep(10 * time.Millisecond)
})
}

func TestWorker_RunExecution(t *testing.T) {
t.Run("success", func(t *testing.T) {
runner := &RunnerMock{T: t}
repo := &RepositoryMock{}
worker := NewWorker(repo, runner)

execution := kubtest.NewExecutionWithID("1", "test", "execution-1")
execution.ExecutionResult = &kubtest.ExecutionResult{}

result, err := worker.RunExecution(context.Background(), execution)
assert.NoError(t, err)
assert.Equal(t, *result.ExecutionResult.Status, kubtest.SUCCESS_ExecutionStatus)
})

t.Run("runner error", func(t *testing.T) {
rerr := fmt.Errorf("runner error")
runner := &RunnerMock{T: t, Error: rerr}
repo := &RepositoryMock{}
worker := NewWorker(repo, runner)

execution := kubtest.NewExecutionWithID("1", "test", "execution-1")
execution.ExecutionResult = &kubtest.ExecutionResult{}

result, err := worker.RunExecution(context.Background(), execution)
assert.Error(t, err)
assert.Equal(t, *result.ExecutionResult.Status, kubtest.ERROR__ExecutionStatus)
})
}

type RunnerMock struct {
Expand All @@ -19,9 +62,49 @@ type RunnerMock struct {
T *testing.T
}

func (r RunnerMock) Run(input io.Reader) (string, error) {
body, err := ioutil.ReadAll(input)
require.NoError(r.T, err)
require.Contains(r.T, string(body), "kubtestExampleCollection")
func (r RunnerMock) Run(execution kubtest.Execution) kubtest.ExecutionResult {
result := *execution.ExecutionResult
status := kubtest.SUCCESS_ExecutionStatus
result.Status = &status
if r.Error != nil {
result.Err(r.Error)
}
return result
}

type RepositoryMock struct {
Result kubtest.Execution
CallCount int
Error error
}

// Get gets execution result by id
func (r *RepositoryMock) Get(ctx context.Context, id string) (kubtest.Execution, error) {
r.CallCount++
return r.Result, r.Error
}

// Insert inserts new execution result
func (r *RepositoryMock) Insert(ctx context.Context, result kubtest.Execution) error {
r.CallCount++
return r.Error
}

// Update updates execution result
func (r *RepositoryMock) Update(ctx context.Context, result kubtest.Execution) error {
r.CallCount++
return r.Error
}

//UpdateResult updates only result part of execution
func (r *RepositoryMock) UpdateResult(ctx context.Context, id string, result kubtest.ExecutionResult) (err error) {
r.CallCount++
return r.Error

}

// QueuePull pulls from queue and locks other clients to read (changes state from queued->pending)
func (r *RepositoryMock) QueuePull(ctx context.Context) (kubtest.Execution, error) {
r.CallCount++
return r.Result, r.Error
}

0 comments on commit 7f56305

Please sign in to comment.