Skip to content

Commit a11f921

Browse files
chore: cover supervisor package with unit tests (#12)
* chore: cover supervisor package with unit tests this commit covers 84.1% of the supervisor package with unit tests. * chore: fix lint warnings. - keep lines shorter. - added a package comment to the supervisor package. * chore: fixed wrong unit tests these tests were wrong.
1 parent f14eea0 commit a11f921

File tree

8 files changed

+430
-463
lines changed

8 files changed

+430
-463
lines changed

pkg/controller/k0scontroller.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"encoding/csv"
77
"fmt"
8-
"io"
98
"os"
109
"os/user"
1110
"path/filepath"
@@ -28,8 +27,7 @@ import (
2827
type K0sController struct {
2928
Options config.K0sControllerOptions
3029

31-
supervisor supervisor.Supervisor
32-
Output io.Writer
30+
supervisor *supervisor.Supervisor
3331
uid int
3432
gid int
3533
}
@@ -68,16 +66,9 @@ func (k *K0sController) Init(_ context.Context) error {
6866
if k.Options.CmdLogLevels != nil {
6967
args = append(args, fmt.Sprintf("--logging=%s", createS2SFlag(k.Options.CmdLogLevels)))
7068
}
71-
k.supervisor = supervisor.Supervisor{
72-
Name: "k0s",
73-
UID: k.uid,
74-
GID: k.gid,
75-
BinPath: assets.BinPath("k0s", k.Options.BinDir()),
76-
Output: k.Output,
77-
RunDir: k.Options.RunDir(),
78-
DataDir: k.Options.DataDir,
79-
KeepEnvPrefix: true,
80-
Args: args,
69+
k0spath := assets.BinPath("k0s", k.Options.BinDir())
70+
if k.supervisor, err = supervisor.New(k0spath, args); err != nil {
71+
return fmt.Errorf("failed to create supervisor: %w", err)
8172
}
8273
return nil
8374
}

pkg/supervisor/detachattr.go

Lines changed: 0 additions & 27 deletions
This file was deleted.

pkg/supervisor/logwriter.go

Lines changed: 0 additions & 82 deletions
This file was deleted.

pkg/supervisor/options.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package supervisor
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// Option sets an option on a Supervisor reference.
9+
type Option func(*Supervisor)
10+
11+
// WithName sets the name of the Supervisor.
12+
func WithName(name string) Option {
13+
return func(s *Supervisor) {
14+
s.name = name
15+
s.log = s.log.WithField("component", name)
16+
s.pidFile = fmt.Sprintf("/run/replicated/%s.pid", name)
17+
}
18+
}
19+
20+
// WithTimeoutRespawn sets the timeout for respawning a process.
21+
func WithTimeoutRespawn(timeoutRespawn time.Duration) Option {
22+
return func(s *Supervisor) {
23+
s.timeoutRespawn = timeoutRespawn
24+
}
25+
}
26+
27+
// WithTimeoutStop sets the timeout for stopping a process.
28+
func WithTimeoutStop(timeoutStop time.Duration) Option {
29+
return func(s *Supervisor) {
30+
s.timeoutStop = timeoutStop
31+
}
32+
}
33+
34+
// WithUID sets the UID of the Supervisor.
35+
func WithUID(uid int) Option {
36+
return func(s *Supervisor) {
37+
s.uid = uid
38+
}
39+
}
40+
41+
// WithGID sets the GID of the Supervisor.
42+
func WithGID(gid int) Option {
43+
return func(s *Supervisor) {
44+
s.gid = gid
45+
}
46+
}
47+
48+
// WithPIDFile sets the PID file of the Supervisor.
49+
func WithPIDFile(pidFile string) Option {
50+
return func(s *Supervisor) {
51+
s.pidFile = pidFile
52+
}
53+
}

pkg/supervisor/options_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package supervisor
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestWithName(t *testing.T) {
11+
s, err := New("/bin/cat", []string{"-"}, WithName("CAT"))
12+
assert.NoError(t, err)
13+
assert.Equal(t, "CAT", s.name)
14+
assert.Equal(t, "/run/replicated/CAT.pid", s.pidFile)
15+
}
16+
17+
func TestWithTimeoutRespawn(t *testing.T) {
18+
s, err := New("/bin/cat", []string{"-"}, WithTimeoutRespawn(time.Second))
19+
assert.NoError(t, err)
20+
assert.Equal(t, time.Second, s.timeoutRespawn)
21+
}
22+
23+
func TestWithTimeoutStop(t *testing.T) {
24+
s, err := New("/bin/cat", []string{"-"}, WithTimeoutStop(time.Second))
25+
assert.NoError(t, err)
26+
assert.Equal(t, time.Second, s.timeoutStop)
27+
}
28+
29+
func TestWithGID(t *testing.T) {
30+
s, err := New("/bin/cat", []string{"-"}, WithGID(1000))
31+
assert.NoError(t, err)
32+
assert.Equal(t, 1000, s.gid)
33+
}
34+
35+
func TestWithUID(t *testing.T) {
36+
s, err := New("/bin/cat", []string{"-"}, WithUID(1000))
37+
assert.NoError(t, err)
38+
assert.Equal(t, 1000, s.uid)
39+
}
40+
41+
func TestWithPIDFile(t *testing.T) {
42+
s, err := New("/bin/cat", []string{"-"}, WithPIDFile("/run/abc.pid"))
43+
assert.NoError(t, err)
44+
assert.Equal(t, "/run/abc.pid", s.pidFile)
45+
}

0 commit comments

Comments
 (0)