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/>.
|
|
|
|
|
2018-08-16 01:07:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/core"
|
|
|
|
"github.com/hunterlong/statping/utils"
|
2018-08-16 01:07:02 +00:00
|
|
|
"github.com/rendon/testcli"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-11-21 08:45:55 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2018-08-16 01:07:02 +00:00
|
|
|
"testing"
|
2018-11-21 08:45:55 +00:00
|
|
|
"time"
|
2018-08-16 01:07:02 +00:00
|
|
|
)
|
|
|
|
|
2018-11-21 08:45:55 +00:00
|
|
|
var (
|
|
|
|
dir string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
dir = utils.Directory
|
2019-03-05 20:13:25 +00:00
|
|
|
core.SampleHits = 480
|
2018-08-30 04:49:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 08:45:55 +00:00
|
|
|
func TestStartServerCommand(t *testing.T) {
|
2019-03-05 20:28:37 +00:00
|
|
|
t.SkipNow()
|
2018-11-21 08:45:55 +00:00
|
|
|
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)
|
2018-11-21 08:45:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-16 01:07:02 +00:00
|
|
|
func TestVersionCommand(t *testing.T) {
|
2018-12-04 04:17:29 +00:00
|
|
|
c := testcli.Command("statping", "version")
|
2018-08-16 01:07:02 +00:00
|
|
|
c.Run()
|
2019-12-28 09:01:07 +00:00
|
|
|
assert.True(t, c.StdoutContains(VERSION))
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHelpCommand(t *testing.T) {
|
2018-12-04 04:17:29 +00:00
|
|
|
c := testcli.Command("statping", "help")
|
2018-08-16 01:07:02 +00:00
|
|
|
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"))
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
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")
|
2018-11-21 08:45:55 +00:00
|
|
|
var got = make(chan string)
|
2018-11-25 03:56:09 +00:00
|
|
|
commandAndSleep(cmd, time.Duration(10*time.Second), got)
|
2018-11-21 08:45:55 +00:00
|
|
|
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")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 08:45:55 +00:00
|
|
|
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)
|
2019-12-28 09:01:07 +00:00
|
|
|
assert.Contains(t, gg, VERSION)
|
2018-11-21 08:45:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 01:07:02 +00:00
|
|
|
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")
|
2018-08-16 01:07:02 +00:00
|
|
|
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)
|
2018-08-16 01:07:02 +00:00
|
|
|
assert.FileExists(t, dir+"/assets/robots.txt")
|
|
|
|
assert.FileExists(t, dir+"/assets/scss/base.scss")
|
|
|
|
}
|
|
|
|
|
2018-11-21 08:45:55 +00:00
|
|
|
func TestRunCommand(t *testing.T) {
|
2019-03-05 20:28:37 +00:00
|
|
|
t.SkipNow()
|
2018-11-21 08:45:55 +00:00
|
|
|
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)
|
2018-11-21 08:45:55 +00:00
|
|
|
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")
|
2018-11-21 08:45:55 +00:00
|
|
|
c.Run()
|
2018-12-04 04:17:29 +00:00
|
|
|
assert.True(t, c.StdoutContains("Statping Environment Variable"))
|
2018-11-21 08:45:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 01:07:02 +00:00
|
|
|
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")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAssetsCLI(t *testing.T) {
|
2019-02-20 19:31:31 +00:00
|
|
|
catchCLI([]string{"assets"})
|
|
|
|
//assert.EqualError(t, run, "end")
|
2018-08-16 01:15:14 +00:00
|
|
|
assert.FileExists(t, dir+"/assets/css/base.css")
|
|
|
|
assert.FileExists(t, dir+"/assets/scss/base.scss")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSassCLI(t *testing.T) {
|
2018-11-28 21:36:03 +00:00
|
|
|
catchCLI([]string{"sass"})
|
2018-08-16 01:15:14 +00:00
|
|
|
assert.FileExists(t, dir+"/assets/css/base.css")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"})
|
2018-11-21 08:45:55 +00:00
|
|
|
assert.EqualError(t, run, "end")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnvCLI(t *testing.T) {
|
2018-10-11 16:53:13 +00:00
|
|
|
run := catchCLI([]string{"env"})
|
2018-08-16 01:07:02 +00:00
|
|
|
assert.Error(t, run)
|
2018-11-21 08:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
2018-11-21 08:45:55 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommand(c *exec.Cmd, out chan<- string) {
|
|
|
|
bout, _ := c.CombinedOutput()
|
|
|
|
out <- string(bout)
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|