Skip to content

Commit

Permalink
complete testing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed May 23, 2019
1 parent 05151a1 commit 9ebb8bb
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 79 deletions.
40 changes: 23 additions & 17 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"testing"
"time"

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

func TestOutput(t *testing.T) {
outCmd := RootCmd()
outCmd.Flags().Set("config", "../etc/job.yaml")
outCmd.Flags().Set("output", "true")
assert.Nil(t, Main(outCmd, []string{}))
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("config", "../etc/job.yaml")
cmd.Flags().Set("output", "true")
assert.Nil(t, Main(cmd, []string{}))
}

func TestMain(t *testing.T) {
Expand Down Expand Up @@ -53,26 +55,30 @@ func TestMain(t *testing.T) {
)
defer ts.Close()

mainCmd := RootCmd()
mainCmd.Flags().Set("config", "../etc/job.yaml")
mainCmd.Flags().Set("report", "true")
assert.Nil(t, Main(mainCmd, []string{}))
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("config", "../etc/job.yaml")
cmd.Flags().Set("report", "true")
assert.Nil(t, Main(cmd, []string{}))
}

func TestVersion(t *testing.T) {
verCmd := RootCmd()
verCmd.Flags().Set("version", "true")
assert.Nil(t, Main(verCmd, []string{}))
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("version", "true")
assert.Nil(t, Main(cmd, []string{}))
}

func TestReport(t *testing.T) {
rptCmd := RootCmd()
rptCmd.Flags().Set("report", "true")
assert.Nil(t, Main(rptCmd, []string{"echo", "hello"}))
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("report", "true")
assert.Nil(t, Main(cmd, []string{"echo", "hello"}))
}

func TestVerbose(t *testing.T) {
verbCmd := RootCmd()
verbCmd.Flags().Set("verbose", "true")
assert.Nil(t, Main(verbCmd, []string{"echox", "hello"}))
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
cmd.Flags().Set("verbose", "true")
assert.Nil(t, Main(cmd, []string{"echox", "hello"}))
}
48 changes: 23 additions & 25 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var rootCmd *cobra.Command

//RootCmd new root cmd
func RootCmd() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "job [flags] [command args ...]",
Short: "Job, make your short-term command as a long-term job",
Example: `
Expand All @@ -58,6 +58,28 @@ func RootCmd() *cobra.Command {
exitForErr(Main(cmd, args))
},
}
cmd.Flags().StringP("config", "f", "", "job config file path")
cmd.Flags().StringP("name", "N", "", "job name definition")
metadata = cmd.Flags().StringToStringP("metadata", "M", map[string]string{}, "job metadata definition")
envs = cmd.Flags().StringToStringP("cmd-env", "e", map[string]string{}, "job command environmental variables")
cmd.Flags().IntP("cmd-retry", "r", 0, "job command retry times when failed")
cmd.Flags().DurationP("cmd-timeout", "t", 0, "job command timeout duration")
cmd.Flags().BoolP("cmd-stdout-discard", "d", false, "job command stdout discard ?")

cmd.Flags().IntP("concurrent", "c", 0, "job concurrent numbers ")
cmd.Flags().IntP("repeat-times", "n", 1, "job repeat times, 0 means forever")
cmd.Flags().DurationP("repeat-interval", "i", 0*time.Second, "job repeat interval duration")
cmd.Flags().StringP("schedule", "s", "", "job schedule in crontab format")
cmd.Flags().DurationP("timeout", "T", 0, "job timeout duration")
cmd.Flags().BoolP("guarantee", "G", false, "job guarantee mode enable ?")
cmd.Flags().BoolP("report", "R", false, "job report enable ?")
cmd.Flags().StringP("report-push-gateway", "P", "", "job report to prometheus push gateway address")
cmd.Flags().DurationP("report-push-interval", "I", 0*time.Second, "job report to prometheus push gateway interval")
cmd.Flags().BoolP("output", "o", false, "job yaml config output enable ?")
// cmd.Flags().StringP("output-command-format", "F", "shell", "job yaml config output command format ?")
cmd.Flags().BoolP("verbose", "V", false, "job verbose log enable ?")
cmd.Flags().BoolP("version", "v", false, "job version")
return cmd
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -70,30 +92,6 @@ func Execute() {

func init() {
rootCmd = RootCmd()
rootCmd.Flags().StringP("config", "f", "", "job config file path")
rootCmd.Flags().StringP("name", "N", "", "job name definition")
metadata = rootCmd.Flags().StringToStringP("metadata", "M", map[string]string{}, "job metadata definition")
envs = rootCmd.Flags().StringToStringP("cmd-env", "e", map[string]string{}, "job command environmental variables")
rootCmd.Flags().IntP("cmd-retry", "r", 0, "job command retry times when failed")
rootCmd.Flags().DurationP("cmd-timeout", "t", 0, "job command timeout duration")
rootCmd.Flags().BoolP("cmd-stdout-discard", "d", false, "job command stdout discard ?")

rootCmd.Flags().IntP("concurrent", "c", 0, "job concurrent numbers ")
rootCmd.Flags().IntP("repeat-times", "n", 1, "job repeat times, 0 means forever")
rootCmd.Flags().DurationP("repeat-interval", "i", 0*time.Second, "job repeat interval duration")
rootCmd.Flags().StringP("schedule", "s", "", "job schedule in crontab format")
rootCmd.Flags().DurationP("timeout", "T", 0, "job timeout duration")
rootCmd.Flags().BoolP("guarantee", "G", false, "job guarantee mode enable ?")
rootCmd.Flags().BoolP("report", "R", false, "job report enable ?")
rootCmd.Flags().StringP("report-push-gateway", "P", "", "job report to prometheus push gateway address")
rootCmd.Flags().DurationP("report-push-interval", "I", 0*time.Second, "job report to prometheus push gateway interval")
rootCmd.Flags().BoolP("output", "o", false, "job yaml config output enable ?")
// rootCmd.Flags().StringP("output-command-format", "F", "shell", "job yaml config output command format ?")
rootCmd.Flags().BoolP("verbose", "V", false, "job verbose log enable ?")
rootCmd.Flags().BoolP("version", "v", false, "job version")

// TODO support Distributed-Job
// rootCmd.Flags().StringP("host", "H", "", "dispatch JOB to the Host")
viper.BindPFlags(rootCmd.Flags())
rootCmd.HelpFunc()
}
29 changes: 29 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"testing"

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

func TestRootCmd(t *testing.T) {
cmd := RootCmd()
viper.BindPFlags(cmd.Flags())
assert.Nil(t, cmd.Flags().Set("output", "true"))
assert.Equal(t, true, viper.GetBool("output"))
}
6 changes: 6 additions & 0 deletions config/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ func TestParseJDs(t *testing.T) {
jds, err := ParseJDs("../etc/job.yaml")
assert.Nil(t, err)
assert.Equal(t, 7, len(jds))

_, err = ParseJDs("../etc/noexist.yaml")
assert.NotNil(t, err)

_, err = ParseJDs("../etc/err-yaml")
assert.NotNil(t, err)
}
21 changes: 21 additions & 0 deletions etc/err-yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Job:
name: "demo"
command:
shell:
name: "echo"
args:
- hello
- world
envs:
- name: "key"
value: "val"
retry: 3
timeout: 3s
guarantee: false
crontab: ""
wait: false
--
Job:
name: "get"
command:
retry: 3
149 changes: 149 additions & 0 deletions etc/http.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
Job:
name: "get"
command:
retry: 3
timeout: 3s
stdout: true
http:
request:
url: "http://localhost:8080/get"
method: GET
crontab: ""
concurrent: 1
repeat:
times: 1
interval: "0s"
timeout: 3s
report: false
order:
weight: 1
precondition:
wait: false
---
Job:
name: "json"
command:
retry: 3
timeout: 3s
stdout: true
http:
request:
url: "http://localhost:8080/json"
method: POST
headers:
Content-Type: application/json
body:
json:
hello: "demo"
person:
name: jay
age: 39
crontab: ""
concurrent: 1
repeat:
times: 1
interval: "0s"
timeout: 3s
report: false
order:
weight: 2
precondition:
wait: false
---
Job:
name: "text"
command:
retry: 3
timeout: 3s
stdout: true
http:
request:
url: "http://localhost:8080/text"
method: GET
body:
text: "text"
crontab: ""
concurrent: 1
repeat:
times: 1
interval: "0s"
timeout: 3s
report: false
order:
weight: 3
precondition:
wait: false
---
Job:
name: "xml"
command:
retry: 3
timeout: 3s
stdout: true
http:
request:
url: "http://localhost:8080/xml"
method: POST
headers:
Content-Type: application/xml
body:
json:
hello: "demo"
person:
name: jay
age: 39
crontab: ""
concurrent: 1
repeat:
times: 1
interval: "0s"
timeout: 3s
report: false
order:
weight: 4
precondition:
wait: false
---
Job:
name: "nofound"
command:
retry: 3
timeout: 3s
stdout: true
http:
request:
url: "http://localhost:8080/nofound"
method: GET
crontab: ""
concurrent: 1
repeat:
times: 1
interval: "0s"
timeout: 3s
report: false
order:
weight: 5
precondition:
wait: false
---
Job:
name: "timeout"
command:
retry: 3
timeout: 3s
stdout: true
http:
request:
url: "http://localhost:8080/timeout"
method: GET
crontab: ""
concurrent: 1
repeat:
times: 1
interval: "0s"
timeout: 1s
report: false
order:
weight: 6
precondition:
wait: false
Loading

0 comments on commit 9ebb8bb

Please sign in to comment.