statping/cmd/cli_test.go

125 lines
2.9 KiB
Go
Raw Normal View History

package main
import (
2020-04-16 09:57:00 +00:00
"bytes"
2020-08-03 10:35:48 +00:00
"github.com/statping/statping/source"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/utils"
"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"
2020-08-03 10:35:48 +00:00
"os"
"testing"
)
var (
dir string
)
func init() {
2020-06-04 14:08:53 +00:00
utils.InitLogs()
}
2020-04-17 03:21:17 +00:00
func TestStatpingDirectory(t *testing.T) {
dir = utils.Params.GetString("STATPING_DIR")
require.NotContains(t, dir, "/cmd")
require.NotEmpty(t, dir)
}
2020-04-17 03:21:17 +00:00
func TestEnvCLI(t *testing.T) {
2020-08-03 10:35:48 +00:00
os.Setenv("API_SECRET", "demoapisecret123")
os.Setenv("SASS", "/usr/local/bin/sass")
2020-04-17 03:21:17 +00:00
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"env"})
err := cmd.Execute()
2020-03-19 01:50:53 +00:00
require.Nil(t, err)
2020-04-17 03:21:17 +00:00
out, err := ioutil.ReadAll(b)
2020-03-19 01:50:53 +00:00
require.Nil(t, err)
2020-04-17 03:21:17 +00:00
assert.Contains(t, string(out), VERSION)
assert.Contains(t, utils.Directory, string(out))
assert.Contains(t, "SAMPLE_DATA=true", string(out))
2020-08-03 10:35:48 +00:00
assert.Contains(t, "API_SECRET=demoapisecret123", string(out))
assert.Contains(t, "STATPING_DIR="+dir, string(out))
assert.Contains(t, "SASS=/usr/local/bin/sass", string(out))
os.Unsetenv("API_SECRET")
os.Unsetenv("SASS")
}
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"})
2020-04-17 03:21:17 +00:00
err := cmd.Execute()
require.Nil(t, err)
2020-04-16 09:57:00 +00:00
out, err := ioutil.ReadAll(b)
2020-04-17 03:21:17 +00:00
require.Nil(t, err)
assert.Contains(t, VERSION, string(out))
}
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"})
2020-08-03 10:35:48 +00:00
err := cmd.Execute()
require.Nil(t, err)
2020-04-16 09:57:00 +00:00
out, err := ioutil.ReadAll(b)
assert.Nil(t, err)
assert.Contains(t, string(out), VERSION)
2020-08-03 10:35:48 +00:00
for _, f := range source.RequiredFiles {
assert.FileExists(t, utils.Directory+"/assets/"+f)
}
}
func TestUpdateCLI(t *testing.T) {
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"update"})
err := cmd.Execute()
require.Nil(t, err)
out, err := ioutil.ReadAll(b)
require.Nil(t, err)
assert.Contains(t, string(out), VERSION)
}
2020-04-17 03:21:17 +00:00
func TestHelpCLI(t *testing.T) {
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"help"})
err := cmd.Execute()
require.Nil(t, err)
out, err := ioutil.ReadAll(b)
require.Nil(t, err)
assert.Contains(t, string(out), VERSION)
}
2020-04-17 03:21:17 +00:00
func TestResetCLI(t *testing.T) {
err := utils.SaveFile(utils.Directory+"/statping.db", []byte("test data"))
require.Nil(t, err)
2020-04-17 03:21:17 +00:00
cmd := rootCmd
b := bytes.NewBufferString("")
cmd.SetOut(b)
cmd.SetArgs([]string{"reset"})
err = cmd.Execute()
require.Nil(t, err)
out, err := ioutil.ReadAll(b)
require.Nil(t, err)
assert.Contains(t, string(out), VERSION)
2020-04-17 03:21:17 +00:00
assert.NoDirExists(t, utils.Directory+"/assets")
assert.NoDirExists(t, utils.Directory+"/logs")
assert.NoFileExists(t, utils.Directory+"/config.yml")
assert.NoFileExists(t, utils.Directory+"/statping.db")
assert.FileExists(t, utils.Directory+"/statping.db.backup")
2020-04-17 03:21:17 +00:00
err = utils.DeleteFile(utils.Directory + "/statping.db.backup")
require.Nil(t, err)
}