statping/cmd/cli_test.go

196 lines
5.2 KiB
Go
Raw Normal View History

2018-12-04 04:17:29 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core"
"github.com/hunterlong/statping/utils"
"github.com/rendon/testcli"
"github.com/stretchr/testify/assert"
"os"
"os/exec"
"testing"
"time"
)
var (
dir string
)
func init() {
dir = utils.Directory
2019-03-05 20:13:25 +00:00
core.SampleHits = 480
}
func TestStartServerCommand(t *testing.T) {
2019-03-05 20:28:37 +00:00
t.SkipNow()
os.Setenv("DB_CONN", "sqlite")
cmd := helperCommand(nil, "")
var got = make(chan string)
2019-03-05 20:13:25 +00:00
commandAndSleep(cmd, time.Duration(60*time.Second), got)
os.Unsetenv("DB_CONN")
gg, _ := <-got
assert.Contains(t, gg, "DB_CONN environment variable was found")
assert.Contains(t, gg, "Core database does not exist, creating now!")
assert.Contains(t, gg, "Starting monitoring process for 5 Services")
2018-08-16 06:22:20 +00:00
}
func TestVersionCommand(t *testing.T) {
2018-12-04 04:17:29 +00:00
c := testcli.Command("statping", "version")
c.Run()
assert.True(t, c.StdoutContains(VERSION))
}
func TestHelpCommand(t *testing.T) {
2018-12-04 04:17:29 +00:00
c := testcli.Command("statping", "help")
c.Run()
t.Log(c.Stdout())
2018-12-04 04:17:29 +00:00
assert.True(t, c.StdoutContains("statping help - Shows the user basic information about Statping"))
}
2019-02-06 21:08:01 +00:00
func TestStaticCommand(t *testing.T) {
2019-03-05 20:28:37 +00:00
t.SkipNow()
2018-11-25 03:56:09 +00:00
cmd := helperCommand(nil, "static")
var got = make(chan string)
2018-11-25 03:56:09 +00:00
commandAndSleep(cmd, time.Duration(10*time.Second), got)
gg, _ := <-got
t.Log(gg)
assert.Contains(t, gg, "Exporting Static 'index.html' page...")
2018-12-04 04:17:29 +00:00
assert.Contains(t, gg, "Exported Statping index page: 'index.html'")
2019-02-06 21:08:01 +00:00
assert.FileExists(t, dir+"/index.html")
}
func TestExportCommand(t *testing.T) {
2019-03-05 20:28:37 +00:00
t.SkipNow()
2019-02-06 21:08:01 +00:00
cmd := helperCommand(nil, "export")
var got = make(chan string)
commandAndSleep(cmd, time.Duration(10*time.Second), got)
gg, _ := <-got
t.Log(gg)
assert.FileExists(t, dir+"/statping-export.json")
}
func TestUpdateCommand(t *testing.T) {
2020-02-26 05:38:03 +00:00
t.SkipNow()
2018-11-25 03:56:09 +00:00
cmd := helperCommand(nil, "version")
var got = make(chan string)
2018-11-29 19:07:29 +00:00
commandAndSleep(cmd, time.Duration(15*time.Second), got)
2018-11-25 03:56:09 +00:00
gg, _ := <-got
t.Log(gg)
assert.Contains(t, gg, VERSION)
}
func TestAssetsCommand(t *testing.T) {
2020-02-26 05:38:03 +00:00
t.SkipNow()
2018-12-04 04:17:29 +00:00
c := testcli.Command("statping", "assets")
c.Run()
t.Log(c.Stdout())
t.Log("Directory for Assets: ", dir)
2019-12-30 01:40:21 +00:00
time.Sleep(1 * time.Second)
assert.FileExists(t, dir+"/assets/robots.txt")
assert.FileExists(t, dir+"/assets/scss/base.scss")
}
func TestRunCommand(t *testing.T) {
2019-03-05 20:28:37 +00:00
t.SkipNow()
cmd := helperCommand(nil, "run")
var got = make(chan string)
2018-11-29 19:07:29 +00:00
commandAndSleep(cmd, time.Duration(15*time.Second), got)
gg, _ := <-got
t.Log(gg)
assert.Contains(t, gg, "Running 1 time and saving to database...")
assert.Contains(t, gg, "Check is complete.")
}
func TestEnvironmentVarsCommand(t *testing.T) {
2018-12-04 04:17:29 +00:00
c := testcli.Command("statping", "env")
c.Run()
2018-12-04 04:17:29 +00:00
assert.True(t, c.StdoutContains("Statping Environment Variable"))
}
func TestVersionCLI(t *testing.T) {
2018-10-11 16:53:13 +00:00
run := catchCLI([]string{"version"})
2018-08-16 06:22:20 +00:00
assert.EqualError(t, run, "end")
}
func TestAssetsCLI(t *testing.T) {
2019-02-20 19:31:31 +00:00
catchCLI([]string{"assets"})
//assert.EqualError(t, run, "end")
2020-03-02 07:53:46 +00:00
assert.FileExists(t, dir+"/assets/css/main.css")
assert.FileExists(t, dir+"/assets/css/style.css")
assert.FileExists(t, dir+"/assets/css/vendor.css")
2018-08-16 01:15:14 +00:00
assert.FileExists(t, dir+"/assets/scss/base.scss")
2020-03-02 07:53:46 +00:00
assert.FileExists(t, dir+"/assets/scss/mobile.scss")
assert.FileExists(t, dir+"/assets/scss/variables.scss")
}
func TestSassCLI(t *testing.T) {
2018-11-28 21:36:03 +00:00
catchCLI([]string{"sass"})
2020-03-02 07:53:46 +00:00
assert.FileExists(t, dir+"/assets/css/main.css")
assert.FileExists(t, dir+"/assets/css/style.css")
assert.FileExists(t, dir+"/assets/css/vendor.css")
}
func TestUpdateCLI(t *testing.T) {
2019-02-12 18:45:52 +00:00
t.SkipNow()
2019-02-06 21:08:01 +00:00
cmd := helperCommand(nil, "update")
var got = make(chan string)
commandAndSleep(cmd, time.Duration(15*time.Second), got)
gg, _ := <-got
t.Log(gg)
assert.Contains(t, gg, "version")
}
func TestTestPackageCLI(t *testing.T) {
2018-11-25 10:18:21 +00:00
t.SkipNow()
2018-10-11 16:53:13 +00:00
run := catchCLI([]string{"test", "plugins"})
2018-08-16 06:22:20 +00:00
assert.EqualError(t, run, "end")
}
func TestHelpCLI(t *testing.T) {
2018-10-11 16:53:13 +00:00
run := catchCLI([]string{"help"})
2018-08-16 06:22:20 +00:00
assert.EqualError(t, run, "end")
}
func TestRunOnceCLI(t *testing.T) {
2019-03-05 20:28:37 +00:00
t.SkipNow()
2018-10-11 16:53:13 +00:00
run := catchCLI([]string{"run"})
assert.EqualError(t, run, "end")
}
func TestEnvCLI(t *testing.T) {
2018-10-11 16:53:13 +00:00
run := catchCLI([]string{"env"})
assert.Error(t, run)
}
func commandAndSleep(cmd *exec.Cmd, duration time.Duration, out chan<- string) {
go func(out chan<- string) {
runCommand(cmd, out)
}(out)
time.Sleep(duration)
cmd.Process.Kill()
}
func helperCommand(envs []string, s ...string) *exec.Cmd {
2018-12-04 04:17:29 +00:00
cmd := exec.Command("statping", s...)
return cmd
}
func runCommand(c *exec.Cmd, out chan<- string) {
bout, _ := c.CombinedOutput()
out <- string(bout)
}