2018-08-16 01:07:02 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-04-16 09:57:00 +00:00
|
|
|
"bytes"
|
2018-08-16 01:07:02 +00:00
|
|
|
"github.com/rendon/testcli"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/utils"
|
2018-08-16 01:07:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-03-19 01:50:53 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-16 09:57:00 +00:00
|
|
|
"io/ioutil"
|
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
|
2020-03-04 14:20:47 +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
|
|
|
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)
|
2020-03-19 01:50:53 +00:00
|
|
|
err := utils.DeleteDirectory(dir + "/assets")
|
|
|
|
require.Nil(t, err)
|
2018-08-16 01:07:02 +00:00
|
|
|
assert.FileExists(t, dir+"/assets/robots.txt")
|
|
|
|
assert.FileExists(t, dir+"/assets/scss/base.scss")
|
2020-03-19 01:50:53 +00:00
|
|
|
assert.FileExists(t, dir+"/assets/scss/main.scss")
|
|
|
|
assert.FileExists(t, dir+"/assets/scss/variables.scss")
|
|
|
|
assert.FileExists(t, dir+"/assets/css/main.css")
|
|
|
|
assert.FileExists(t, dir+"/assets/css/vendor.css")
|
|
|
|
assert.FileExists(t, dir+"/assets/css/style.css")
|
|
|
|
err = utils.DeleteDirectory(dir + "/assets")
|
|
|
|
require.Nil(t, err)
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-16 09:57:00 +00:00
|
|
|
cmd := rootCmd
|
|
|
|
b := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(b)
|
|
|
|
cmd.SetArgs([]string{"version"})
|
|
|
|
cmd.Execute()
|
|
|
|
out, err := ioutil.ReadAll(b)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Contains(t, string(out), VERSION)
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAssetsCLI(t *testing.T) {
|
2020-04-16 09:57:00 +00:00
|
|
|
cmd := rootCmd
|
|
|
|
b := bytes.NewBufferString("")
|
|
|
|
cmd.SetOut(b)
|
|
|
|
cmd.SetArgs([]string{"assets"})
|
|
|
|
cmd.Execute()
|
|
|
|
out, err := ioutil.ReadAll(b)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Contains(t, string(out), VERSION)
|
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")
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSassCLI(t *testing.T) {
|
2020-04-16 09:57:00 +00:00
|
|
|
c := testcli.Command("statping", "assets")
|
|
|
|
c.Run()
|
|
|
|
t.Log(c.Stdout())
|
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: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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|