Skip to content

Commit

Permalink
Adds unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiubelu committed Dec 13, 2024
1 parent ee1d914 commit 5ce8ec3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/k8s/pkg/snap/mock/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ type Runner struct {
CalledWithCommand []string
Err error
Log bool
RunOutput string
}

// Run is a mock implementation of CommandRunner.
func (m *Runner) Run(ctx context.Context, command []string, opts ...func(*exec.Cmd)) error {
m.CalledWithCommand = append(m.CalledWithCommand, strings.Join(command, " "))
m.CalledWithCtx = ctx

// In some cases, it is expected to get the command's stdout.
cmd := &exec.Cmd{}
for _, o := range opts {
o(cmd)
}
if m.RunOutput != "" {
cmd.Stdout.Write([]byte(m.RunOutput))
}

return m.Err
}
42 changes: 42 additions & 0 deletions src/k8s/pkg/snap/pebble_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,48 @@ func TestPebble(t *testing.T) {
})
})

t.Run("GetServiceState", func(t *testing.T) {
g := NewWithT(t)
mockRunner := &mock.Runner{
RunOutput: "Service Startup Current Since\ntest-service enabled active -",
}
snap := snap.NewPebble(snap.PebbleOpts{
SnapDir: "testdir",
RunCommand: mockRunner.Run,
})

state, err := snap.GetServiceState(context.Background(), "test-service")

g.Expect(err).To(Not(HaveOccurred()))
g.Expect(state).To(Equal("active"))
g.Expect(mockRunner.CalledWithCommand).To(ConsistOf("testdir/bin/pebble services test-service"))

t.Run("run error", func(t *testing.T) {
g := NewWithT(t)
mockRunner.Err = fmt.Errorf("some error")

_, err := snap.GetServiceState(context.Background(), "test-service")

g.Expect(err).To(HaveOccurred())
})

t.Run("invalid output", func(t *testing.T) {
g := NewWithT(t)

mockRunner.RunOutput = "Service Startup Current Since"
_, err := snap.GetServiceState(context.Background(), "test-service")
g.Expect(err).To(HaveOccurred())

mockRunner.RunOutput = "Service Startup Current Since\nFoo"
_, err = snap.GetServiceState(context.Background(), "test-service")
g.Expect(err).To(HaveOccurred())

mockRunner.RunOutput = "Service Startup Current Since\ntest-service enabled foo -"
_, err = snap.GetServiceState(context.Background(), "test-service")
g.Expect(err).To(HaveOccurred())
})
})

t.Run("Restart", func(t *testing.T) {
g := NewWithT(t)
mockRunner := &mock.Runner{}
Expand Down
51 changes: 51 additions & 0 deletions src/k8s/pkg/snap/snap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"reflect"
"syscall"
"testing"
"unsafe"

"github.com/canonical/k8s/pkg/k8sd/types"
"github.com/canonical/k8s/pkg/snap"
Expand Down Expand Up @@ -105,6 +109,53 @@ func TestSnap(t *testing.T) {
})
})

t.Run("GetServiceState", func(t *testing.T) {
g := NewWithT(t)
mockRunner := &mock.Runner{
RunOutput: "active\n",
}
snap := snap.NewSnap(snap.SnapOpts{
RunCommand: mockRunner.Run,
})

state, err := snap.GetServiceState(context.Background(), "test-service")

g.Expect(err).To(Not(HaveOccurred()))
g.Expect(state).To(Equal("active"))
g.Expect(mockRunner.CalledWithCommand).To(ConsistOf("systemctl is-active snap.k8s.test-service"))

t.Run("failed unit", func(t *testing.T) {
g := NewWithT(t)
exitErr := &exec.ExitError{
ProcessState: &os.ProcessState{},
}

// exit code 3.
status := syscall.WaitStatus(0x0300)

// We can't set the state directly, so we're using reflection instead.
psField := reflect.ValueOf(exitErr.ProcessState).Elem().FieldByName("status")
psField = reflect.NewAt(psField.Type(), unsafe.Pointer(psField.UnsafeAddr())).Elem()
psField.Set(reflect.ValueOf(status))

mockRunner.Err = exitErr

state, err = snap.GetServiceState(context.Background(), "test-service")

g.Expect(err).To(Not(HaveOccurred()))
g.Expect(state).To(Equal("failed"))
})

t.Run("run error", func(t *testing.T) {
g := NewWithT(t)
mockRunner.Err = fmt.Errorf("some error")

_, err := snap.GetServiceState(context.Background(), "test-service")

g.Expect(err).To(HaveOccurred())
})
})

t.Run("Restart", func(t *testing.T) {
g := NewWithT(t)
mockRunner := &mock.Runner{}
Expand Down

0 comments on commit 5ce8ec3

Please sign in to comment.