Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add a service commnd The following command will install atest as a Linux service. `atest service --action install`. Set the default service port to be 7070 to avoid confliction * add unit tests * add more unit tests
- Loading branch information
1 parent
9ee38ff
commit e08c204
Showing
16 changed files
with
248 additions
and
36 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
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
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
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
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
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
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
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
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,76 @@ | ||
// Package cmd provides a service command | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
fakeruntime "github.com/linuxsuren/go-fake-runtime" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func createServiceCommand(execer fakeruntime.Execer) (c *cobra.Command) { | ||
opt := &serviceOption{ | ||
Execer: execer, | ||
} | ||
c = &cobra.Command{ | ||
Use: "service", | ||
Aliases: []string{"s"}, | ||
Short: "Install atest as a Linux service", | ||
PreRunE: opt.preRunE, | ||
RunE: opt.runE, | ||
} | ||
flags := c.Flags() | ||
flags.StringVarP(&opt.action, "action", "a", "", "The action of service, support actions: install, start, stop, restart, status") | ||
flags.StringVarP(&opt.scriptPath, "script-path", "", "/lib/systemd/system/atest.service", "The service script file path") | ||
return | ||
} | ||
|
||
type serviceOption struct { | ||
action string | ||
scriptPath string | ||
fakeruntime.Execer | ||
} | ||
|
||
func (o *serviceOption) preRunE(c *cobra.Command, args []string) (err error) { | ||
if o.Execer.OS() != "linux" { | ||
err = fmt.Errorf("only support on Linux") | ||
} | ||
if o.action == "" && len(args) > 0 { | ||
o.action = args[0] | ||
} | ||
return | ||
} | ||
|
||
func (o *serviceOption) runE(c *cobra.Command, args []string) (err error) { | ||
var output string | ||
switch o.action { | ||
case "install", "i": | ||
err = os.WriteFile(o.scriptPath, []byte(script), os.ModeAppend) | ||
case "start": | ||
output, err = o.Execer.RunCommandAndReturn("systemctl", "", "start", "atest") | ||
case "stop": | ||
output, err = o.Execer.RunCommandAndReturn("systemctl", "", "stop", "atest") | ||
case "restart": | ||
output, err = o.Execer.RunCommandAndReturn("systemctl", "", "restart", "atest") | ||
case "status": | ||
output, err = o.Execer.RunCommandAndReturn("systemctl", "", "status", "atest") | ||
default: | ||
err = fmt.Errorf("not support action: '%s'", o.action) | ||
} | ||
|
||
if output != "" { | ||
c.Println(output) | ||
} | ||
return | ||
} | ||
|
||
var script = `[Unit] | ||
Description=API Testing | ||
[Service] | ||
ExecStart=atest server | ||
[Install] | ||
WantedBy=multi-user.target | ||
` |
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 cmd | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"testing" | ||
|
||
fakeruntime "github.com/linuxsuren/go-fake-runtime" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestService(t *testing.T) { | ||
root := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}) | ||
root.SetArgs([]string{"service", "fake"}) | ||
err := root.Execute() | ||
assert.NotNil(t, err) | ||
|
||
notLinux := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "fake"}) | ||
notLinux.SetArgs([]string{"service", "--action", "install"}) | ||
err = notLinux.Execute() | ||
assert.NotNil(t, err) | ||
|
||
tmpFile, err := os.CreateTemp(os.TempDir(), "service") | ||
assert.Nil(t, err) | ||
defer func() { | ||
os.RemoveAll(tmpFile.Name()) | ||
}() | ||
|
||
targetScript := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"}) | ||
targetScript.SetArgs([]string{"service", "--action", "install", "--script-path", tmpFile.Name()}) | ||
err = targetScript.Execute() | ||
assert.Nil(t, err) | ||
data, err := os.ReadFile(tmpFile.Name()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, script, string(data)) | ||
|
||
tests := []struct { | ||
name string | ||
action string | ||
expectOutput string | ||
}{{ | ||
name: "action: start", | ||
action: "start", | ||
expectOutput: "output1", | ||
}, { | ||
name: "action: stop", | ||
action: "stop", | ||
expectOutput: "output2", | ||
}, { | ||
name: "action: restart", | ||
action: "restart", | ||
expectOutput: "output3", | ||
}, { | ||
name: "action: status", | ||
action: "status", | ||
expectOutput: "output4", | ||
}} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
normalRoot := NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux", ExpectOutput: tt.expectOutput}) | ||
normalRoot.SetOut(buf) | ||
normalRoot.SetArgs([]string{"service", "--action", tt.action}) | ||
err = normalRoot.Execute() | ||
assert.Nil(t, err) | ||
assert.Equal(t, tt.expectOutput+"\n", buf.String()) | ||
}) | ||
} | ||
} |
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
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
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
Oops, something went wrong.