Skip to content

Commit

Permalink
tests: Add runner package tests
Browse files Browse the repository at this point in the history
Signed-off-by: konradasb <konradasb0@gmail.com>
  • Loading branch information
konradasb committed Nov 27, 2024
1 parent 9ba5a10 commit 3a7b209
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 10 deletions.
53 changes: 43 additions & 10 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ const (
// Runner represents a virtual machine agent that's responsible for running
// the actual GitHub runner.
type Runner struct {
config string
stdout io.Writer
stderr io.Writer
logger *zerolog.Logger
config string
directory string
owner string
group string
stdout io.Writer
stderr io.Writer
logger *zerolog.Logger
}

// Opt is a functional option for Runner.
Expand Down Expand Up @@ -57,14 +60,44 @@ func WithLogger(logger *zerolog.Logger) Opt {
return f
}

// WithDirectory sets the directory where the GitHub runner is located.
func WithDirectory(dir string) Opt {
f := func(r *Runner) {
r.directory = dir
}

return f
}

// WithOwner sets the owner of the GitHub runner.
func WithOwner(owner string) Opt {
f := func(r *Runner) {
r.owner = owner
}

return f
}

// WithGroup sets the group of the GitHub runner.
func WithGroup(group string) Opt {
f := func(r *Runner) {
r.group = group
}

return f
}

// New creates a new Runner.
func New(config string, opts ...Opt) *Runner {
logger := zerolog.Nop()
runner := &Runner{
config: config,
stdout: os.Stdout,
stderr: os.Stderr,
logger: &logger,
config: config,
directory: defaultDir,
owner: "runner",
group: "docker",
stdout: os.Stdout,
stderr: os.Stderr,
logger: &logger,
}

for _, opt := range opts {
Expand All @@ -85,7 +118,7 @@ func (r *Runner) Run(ctx context.Context) error {
runCmd.Stderr = r.stderr
runCmd.Dir = defaultDir

owner, err := user.Lookup("runner")
owner, err := user.Lookup(r.owner)
if err != nil {
return fmt.Errorf("lookup: %w", err)
}
Expand All @@ -95,7 +128,7 @@ func (r *Runner) Run(ctx context.Context) error {
return fmt.Errorf("owner: uid: atoi: %w", err)
}

group, err := user.LookupGroup("docker")
group, err := user.LookupGroup(r.group)
if err != nil {
return fmt.Errorf("group: lookup: %w", err)
}
Expand Down
52 changes: 52 additions & 0 deletions runner/runner_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
package runner

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
r := New("test")

assert.Equal(t, "test", r.config)
assert.Equal(t, defaultDir, r.directory)
assert.NotNil(t, r.stdout)
assert.NotNil(t, r.stderr)
assert.NotNil(t, r.logger)
}

func TestWithStdout(t *testing.T) {
r := New("test", WithStdout(nil))

assert.Nil(t, r.stdout)
}

func TestWithStderr(t *testing.T) {
r := New("test", WithStderr(nil))

assert.Nil(t, r.stderr)
}

func TestWithLogger(t *testing.T) {
r := New("test", WithLogger(nil))

assert.Nil(t, r.logger)
}

func TestWithDirectory(t *testing.T) {
r := New("test", WithDirectory("test"))

assert.Equal(t, "test", r.directory)
}

func TestWithOwner(t *testing.T) {
r := New("test", WithOwner("test"))

assert.Equal(t, "test", r.owner)
}

func TestWithGroup(t *testing.T) {
r := New("test", WithGroup("test"))

assert.Equal(t, "test", r.group)
}

0 comments on commit 3a7b209

Please sign in to comment.