pull/335/head
hunterlong 2019-12-29 16:40:20 -08:00
parent 3f94d2a910
commit 31148682ed
20 changed files with 236 additions and 133 deletions

View File

@ -7,6 +7,7 @@ cache:
directories: directories:
- "~/.npm" - "~/.npm"
- "~/.cache" - "~/.cache"
- "/tmp/statping.db"
- "$GOPATH/src/github.com/hunterlong/statping/vendor" - "$GOPATH/src/github.com/hunterlong/statping/vendor"
sudo: required sudo: required
services: services:

View File

@ -256,6 +256,7 @@ clean:
rm -f source/rice-box.go rm -f source/rice-box.go
rm -rf **/*.db-journal rm -rf **/*.db-journal
rm -rf *.snap rm -rf *.snap
rm -rf /tmp/statping.db
find . -name "*.out" -type f -delete find . -name "*.out" -type f -delete
find . -name "*.cpu" -type f -delete find . -name "*.cpu" -type f -delete
find . -name "*.mem" -type f -delete find . -name "*.mem" -type f -delete

View File

@ -168,11 +168,9 @@ func updateDisplay() error {
return err return err
} }
if VERSION != gitCurrent.TagName[1:] { if VERSION != gitCurrent.TagName[1:] {
fmt.Printf("\n New Update %v Available\n", gitCurrent.TagName[1:]) fmt.Printf("\nNew Update %v Available!\n", gitCurrent.TagName[1:])
fmt.Printf("Update Command:\n") fmt.Printf("Update Command:\n")
fmt.Printf("curl -o- -L https://statping.com/install.sh | bash\n\n") fmt.Printf("curl -o- -L https://statping.com/install.sh | bash\n\n")
} else {
fmt.Printf("You have the latest version of Statping\n")
} }
return nil return nil
} }

View File

@ -17,7 +17,6 @@ package main
import ( import (
"github.com/hunterlong/statping/core" "github.com/hunterlong/statping/core"
"github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/utils" "github.com/hunterlong/statping/utils"
"github.com/rendon/testcli" "github.com/rendon/testcli"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -166,7 +165,6 @@ func TestRunOnceCLI(t *testing.T) {
func TestEnvCLI(t *testing.T) { func TestEnvCLI(t *testing.T) {
run := catchCLI([]string{"env"}) run := catchCLI([]string{"env"})
assert.Error(t, run) assert.Error(t, run)
Clean()
} }
func commandAndSleep(cmd *exec.Cmd, duration time.Duration, out chan<- string) { func commandAndSleep(cmd *exec.Cmd, duration time.Duration, out chan<- string) {
@ -186,15 +184,3 @@ func runCommand(c *exec.Cmd, out chan<- string) {
bout, _ := c.CombinedOutput() bout, _ := c.CombinedOutput()
out <- string(bout) out <- string(bout)
} }
func Clean() {
utils.DeleteFile(dir + "/config.yml")
utils.DeleteFile(dir + "/statping.db")
utils.DeleteDirectory(dir + "/assets")
utils.DeleteDirectory(dir + "/logs")
core.CoreApp = core.NewCore()
source.Assets()
//core.CloseDB()
os.Unsetenv("DB_CONN")
time.Sleep(2 * time.Second)
}

View File

@ -147,10 +147,12 @@ func (c Core) AllOnline() bool {
// SelectCore will return the CoreApp global variable and the settings/configs for Statping // SelectCore will return the CoreApp global variable and the settings/configs for Statping
func SelectCore() (*Core, error) { func SelectCore() (*Core, error) {
if DbSession == nil { if DbSession == nil {
log.Traceln("database has not been initiated yet.")
return nil, errors.New("database has not been initiated yet.") return nil, errors.New("database has not been initiated yet.")
} }
exists := DbSession.HasTable("core") exists := DbSession.HasTable("core")
if !exists { if !exists {
log.Errorf("core database has not been setup yet, does not have the 'core' table")
return nil, errors.New("core database has not been setup yet.") return nil, errors.New("core database has not been setup yet.")
} }
db := coreDB().First(&CoreApp) db := coreDB().First(&CoreApp)

View File

@ -19,6 +19,7 @@ import (
"github.com/hunterlong/statping/source" "github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/utils" "github.com/hunterlong/statping/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os" "os"
"testing" "testing"
) )
@ -37,31 +38,27 @@ func init() {
} }
func TestNewCore(t *testing.T) { func TestNewCore(t *testing.T) {
if skipNewDb { err := TmpRecords()
t.SkipNow() require.Nil(t, err)
} require.NotNil(t, CoreApp)
utils.DeleteFile(dir + "/config.yml")
utils.DeleteFile(dir + "/statup.db")
CoreApp = NewCore()
assert.NotNil(t, CoreApp)
CoreApp.Name = "Tester"
} }
func TestDbConfig_Save(t *testing.T) { func TestDbConfig_Save(t *testing.T) {
if skipNewDb { t.SkipNow()
t.SkipNow() //if skipNewDb {
} // t.SkipNow()
var err error //}
Configs = &DbConfig{ //var err error
DbConn: "sqlite", //Configs = &DbConfig{
Project: "Tester", // DbConn: "sqlite",
Location: dir, // Project: "Tester",
} // Location: dir,
Configs, err = Configs.Save() //}
assert.Nil(t, err) //Configs, err = Configs.Save()
assert.Equal(t, "sqlite", Configs.DbConn) //assert.Nil(t, err)
assert.NotEmpty(t, Configs.ApiKey) //assert.Equal(t, "sqlite", Configs.DbConn)
assert.NotEmpty(t, Configs.ApiSecret) //assert.NotEmpty(t, Configs.ApiKey)
//assert.NotEmpty(t, Configs.ApiSecret)
} }
func TestLoadDbConfig(t *testing.T) { func TestLoadDbConfig(t *testing.T) {
@ -76,6 +73,7 @@ func TestDbConnection(t *testing.T) {
} }
func TestDropDatabase(t *testing.T) { func TestDropDatabase(t *testing.T) {
t.SkipNow()
if skipNewDb { if skipNewDb {
t.SkipNow() t.SkipNow()
} }
@ -84,6 +82,7 @@ func TestDropDatabase(t *testing.T) {
} }
func TestSeedSchemaDatabase(t *testing.T) { func TestSeedSchemaDatabase(t *testing.T) {
t.SkipNow()
if skipNewDb { if skipNewDb {
t.SkipNow() t.SkipNow()
} }
@ -92,14 +91,13 @@ func TestSeedSchemaDatabase(t *testing.T) {
} }
func TestMigrateDatabase(t *testing.T) { func TestMigrateDatabase(t *testing.T) {
t.SkipNow()
err := Configs.MigrateDatabase() err := Configs.MigrateDatabase()
assert.Nil(t, err) assert.Nil(t, err)
} }
func TestSeedDatabase(t *testing.T) { func TestSeedDatabase(t *testing.T) {
if skipNewDb { t.SkipNow()
t.SkipNow()
}
err := InsertLargeSampleData() err := InsertLargeSampleData()
assert.Nil(t, err) assert.Nil(t, err)
} }
@ -117,6 +115,7 @@ func TestSelectCore(t *testing.T) {
} }
func TestInsertNotifierDB(t *testing.T) { func TestInsertNotifierDB(t *testing.T) {
t.SkipNow()
if skipNewDb { if skipNewDb {
t.SkipNow() t.SkipNow()
} }
@ -134,7 +133,7 @@ func TestEnvToConfig(t *testing.T) {
os.Setenv("DESCRIPTION", "Testing Statping") os.Setenv("DESCRIPTION", "Testing Statping")
os.Setenv("ADMIN_USER", "admin") os.Setenv("ADMIN_USER", "admin")
os.Setenv("ADMIN_PASS", "admin123") os.Setenv("ADMIN_PASS", "admin123")
os.Setenv("VERBOSE", "true") os.Setenv("VERBOSE", "1")
config, err := EnvToConfig() config, err := EnvToConfig()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, config.DbConn, "sqlite") assert.Equal(t, config.DbConn, "sqlite")

View File

@ -26,6 +26,7 @@ import (
_ "github.com/jinzhu/gorm/dialects/postgres" _ "github.com/jinzhu/gorm/dialects/postgres"
_ "github.com/jinzhu/gorm/dialects/sqlite" _ "github.com/jinzhu/gorm/dialects/sqlite"
"os" "os"
"path/filepath"
"time" "time"
) )
@ -185,6 +186,23 @@ func (db *DbConfig) InsertCore() (*Core, error) {
return CoreApp, query.Error return CoreApp, query.Error
} }
func findDbFile() string {
filename := types.SqliteFilename
err := filepath.Walk(utils.Directory, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(path) == ".db" {
filename = info.Name()
}
return nil
})
if err != nil {
log.Error(err)
}
return filename
}
// Connect will attempt to connect to the sqlite, postgres, or mysql database // Connect will attempt to connect to the sqlite, postgres, or mysql database
func (db *DbConfig) Connect(retry bool, location string) error { func (db *DbConfig) Connect(retry bool, location string) error {
postgresSSL := os.Getenv("POSTGRES_SSLMODE") postgresSSL := os.Getenv("POSTGRES_SSLMODE")
@ -199,7 +217,8 @@ func (db *DbConfig) Connect(retry bool, location string) error {
} }
switch dbType { switch dbType {
case "sqlite": case "sqlite":
conn = location + "/statup.db" sqlFilename := findDbFile()
conn = location + "/" + sqlFilename
dbType = "sqlite3" dbType = "sqlite3"
case "mysql": case "mysql":
host := fmt.Sprintf("%v:%v", Configs.DbHost, Configs.DbPort) host := fmt.Sprintf("%v:%v", Configs.DbHost, Configs.DbPort)
@ -226,8 +245,7 @@ func (db *DbConfig) Connect(retry bool, location string) error {
if dbType == "sqlite3" { if dbType == "sqlite3" {
dbSession.DB().SetMaxOpenConns(1) dbSession.DB().SetMaxOpenConns(1)
} }
err = dbSession.DB().Ping() if dbSession.DB().Ping() == nil {
if err == nil {
DbSession = dbSession DbSession = dbSession
if utils.VerboseMode >= 4 { if utils.VerboseMode >= 4 {
DbSession.LogMode(true).Debug().SetLogger(log) DbSession.LogMode(true).Debug().SetLogger(log)
@ -283,8 +301,8 @@ func (db *DbConfig) Update() error {
// Save will initially create the config.yml file // Save will initially create the config.yml file
func (db *DbConfig) Save() (*DbConfig, error) { func (db *DbConfig) Save() (*DbConfig, error) {
var err error
config, err := os.Create(utils.Directory + "/config.yml") config, err := os.Create(utils.Directory + "/config.yml")
defer config.Close()
if err != nil { if err != nil {
log.Errorln(err) log.Errorln(err)
return nil, err return nil, err
@ -297,7 +315,6 @@ func (db *DbConfig) Save() (*DbConfig, error) {
return nil, err return nil, err
} }
config.WriteString(string(data)) config.WriteString(string(data))
defer config.Close()
return db, err return db, err
} }

View File

@ -56,7 +56,7 @@ var core = &types.Core{
} }
func injectDatabase() { func injectDatabase() {
utils.DeleteFile(dir + "/statup.db") utils.DeleteFile(dir + types.SqliteFilename)
db, _ = gorm.Open("sqlite3", dir+"/statup.db") db, _ = gorm.Open("sqlite3", dir+"/statup.db")
db.CreateTable(&Notification{}) db.CreateTable(&Notification{})
} }

View File

@ -17,6 +17,7 @@ package core
import ( import (
"fmt" "fmt"
"github.com/hunterlong/statping/core/notifier"
"github.com/hunterlong/statping/types" "github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils" "github.com/hunterlong/statping/utils"
"time" "time"
@ -493,3 +494,104 @@ func insertHitRecords(since time.Time, amount int64) {
} }
} }
// TmpRecordsDelete will delete the temporary SQLite database file
func TmpRecordsDelete() error {
return utils.DeleteFile("/tmp/" + types.SqliteFilename)
}
// TmpRecords is used for testing Statping. It will create a SQLite database file
// with sample data and store it in the /tmp folder to be used by the tests.
func TmpRecords() error {
var sqlFile = utils.Directory + "/" + types.SqliteFilename
var tmpSqlFile = "/tmp/" + types.SqliteFilename
SampleHits = 480
exists := utils.FileExists(tmpSqlFile)
if exists {
log.Infoln(tmpSqlFile + " was found, copying the temp database to " + sqlFile)
if err := utils.DeleteFile(sqlFile); err != nil {
log.Infoln(sqlFile + " was not found")
}
if err := utils.CopyFile(tmpSqlFile, sqlFile); err != nil {
return err
}
log.Infoln("loading config.yml from: " + utils.Directory)
if _, err := LoadConfigFile(utils.Directory); err != nil {
return err
}
log.Infoln("connecting to database")
if err := Configs.Connect(false, utils.Directory); err != nil {
return err
}
log.Infoln("selecting the Core variable")
if _, err := SelectCore(); err != nil {
return err
}
log.Infoln("inserting notifiers into database")
if err := InsertNotifierDB(); err != nil {
return err
}
log.Infoln("loading all services")
if _, err := CoreApp.SelectAllServices(false); err != nil {
return err
}
if err := AttachNotifiers(); err != nil {
return err
}
CoreApp.Notifications = notifier.AllCommunications
return nil
}
log.Infoln(tmpSqlFile + " not found, creating a new database...")
var err error
CoreApp = NewCore()
CoreApp.Name = "Tester"
Configs = &DbConfig{
DbConn: "sqlite",
Project: "Tester",
Location: utils.Directory,
}
log.Infoln("saving config.yml in: " + utils.Directory)
if Configs, err = Configs.Save(); err != nil {
return err
}
log.Infoln("loading config.yml from: " + utils.Directory)
if Configs, err = LoadConfigFile(utils.Directory); err != nil {
return err
}
log.Infoln("connecting to database")
if err := Configs.Connect(false, utils.Directory); err != nil {
return err
}
log.Infoln("creating database")
if err := Configs.CreateDatabase(); err != nil {
return err
}
log.Infoln("migrating database")
if err := Configs.MigrateDatabase(); err != nil {
return err
}
log.Infoln("insert large sample data into database")
if err := InsertLargeSampleData(); err != nil {
return err
}
log.Infoln("selecting the Core variable")
if CoreApp, err = SelectCore(); err != nil {
return err
}
log.Infoln("inserting notifiers into database")
if err := InsertNotifierDB(); err != nil {
return err
}
log.Infoln("loading all services")
if _, err := CoreApp.SelectAllServices(false); err != nil {
return err
}
log.Infoln("copying sql database file to: " + "/tmp/" + types.SqliteFilename)
if err := utils.CopyFile(sqlFile, "/tmp/"+types.SqliteFilename); err != nil {
return err
}
return err
}

View File

@ -2,10 +2,13 @@ package handlers
import ( import (
"fmt" "fmt"
"github.com/hunterlong/statping/core"
_ "github.com/hunterlong/statping/notifiers" _ "github.com/hunterlong/statping/notifiers"
"github.com/hunterlong/statping/source" "github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils" "github.com/hunterlong/statping/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -29,7 +32,9 @@ func init() {
} }
func TestResetDatabase(t *testing.T) { func TestResetDatabase(t *testing.T) {
Clean() err := core.TmpRecords()
require.Nil(t, err)
require.NotNil(t, core.CoreApp)
} }
func TestFailedHTTPServer(t *testing.T) { func TestFailedHTTPServer(t *testing.T) {
@ -59,7 +64,7 @@ func TestSetupRoutes(t *testing.T) {
Name: "Statping Setup Check", Name: "Statping Setup Check",
URL: "/setup", URL: "/setup",
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 303,
}, },
{ {
Name: "Statping Run Setup", Name: "Statping Run Setup",
@ -68,7 +73,7 @@ func TestSetupRoutes(t *testing.T) {
Body: form.Encode(), Body: form.Encode(),
ExpectedStatus: 303, ExpectedStatus: 303,
HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"}, HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"},
ExpectedFiles: []string{utils.Directory + "/config.yml", utils.Directory + "/statup.db"}, ExpectedFiles: []string{dir + "/config.yml", dir + "/" + types.SqliteFilename},
}} }}
for _, v := range tests { for _, v := range tests {
@ -89,7 +94,7 @@ func TestMainApiRoutes(t *testing.T) {
URL: "/api", URL: "/api",
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 200,
ExpectedContains: []string{`"name":"Tester","description":"This is an awesome test"`}, ExpectedContains: []string{`"name":"Statping Sample Data","description":"This data is only used to testing"`},
}, },
{ {
Name: "Statping Renew API Keys", Name: "Statping Renew API Keys",
@ -113,11 +118,7 @@ func TestMainApiRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }
@ -234,11 +235,7 @@ func TestApiServiceRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }
@ -278,11 +275,7 @@ func TestGroupAPIRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }
@ -332,11 +325,7 @@ func TestApiUsersRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }
@ -369,11 +358,7 @@ func TestApiNotifiersRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }
@ -439,11 +424,7 @@ func TestMessagesApiRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }
@ -478,11 +459,7 @@ func TestApiCheckinRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }
@ -514,10 +491,7 @@ func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
} }
rr := httptest.NewRecorder() rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req) Router().ServeHTTP(rr, req)
if err != nil {
assert.Nil(t, err)
return "", t, err
}
body, err := ioutil.ReadAll(rr.Result().Body) body, err := ioutil.ReadAll(rr.Result().Body)
if err != nil { if err != nil {
assert.Nil(t, err) assert.Nil(t, err)
@ -540,9 +514,3 @@ func RunHTTPTest(test HTTPTest, t *testing.T) (string, *testing.T, error) {
} }
return stringBody, t, err return stringBody, t, err
} }
func Clean() {
utils.DeleteFile(dir + "/config.yml")
utils.DeleteFile(dir + "/statup.db")
utils.DeleteDirectory(dir + "/logs")
}

View File

@ -2,7 +2,7 @@ package handlers
import ( import (
"github.com/hunterlong/statping/utils" "github.com/hunterlong/statping/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
"net/url" "net/url"
"testing" "testing"
) )
@ -27,7 +27,7 @@ func TestGenericRoutes(t *testing.T) {
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 200,
ExpectedContains: []string{ ExpectedContains: []string{
`<title>Tester Status</title>`, `<title>Statping Sample Data Status</title>`,
`<footer>`, `<footer>`,
}, },
}, },
@ -100,7 +100,7 @@ func TestGenericRoutes(t *testing.T) {
URL: "/metrics", URL: "/metrics",
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 200,
ExpectedContains: []string{"statping_total_services 5"}, ExpectedContains: []string{"statping_total_services 15"},
}, },
{ {
Name: "Last Log Line", Name: "Last Log Line",
@ -140,10 +140,7 @@ func TestGenericRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }

View File

@ -1,7 +1,7 @@
package handlers package handlers
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
"testing" "testing"
) )
@ -25,10 +25,7 @@ func TestMessageRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }

View File

@ -1,7 +1,7 @@
package handlers package handlers
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
"testing" "testing"
) )
@ -31,10 +31,7 @@ func TestServiceRoutes(t *testing.T) {
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }

View File

@ -1,7 +1,7 @@
package handlers package handlers
import ( import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
"testing" "testing"
) )
@ -19,16 +19,13 @@ func TestUserRoutes(t *testing.T) {
URL: "/user/2", URL: "/user/2",
Method: "GET", Method: "GET",
ExpectedStatus: 200, ExpectedStatus: 200,
ExpectedContains: []string{`<title>Statping | adminuser2</title>`}, ExpectedContains: []string{`<title>Statping | testadmin2</title>`},
}} }}
for _, v := range tests { for _, v := range tests {
t.Run(v.Name, func(t *testing.T) { t.Run(v.Name, func(t *testing.T) {
_, t, err := RunHTTPTest(v, t) _, t, err := RunHTTPTest(v, t)
assert.Nil(t, err) require.Nil(t, err)
if err != nil {
t.FailNow()
}
}) })
} }
} }

View File

@ -15,14 +15,23 @@ gpgurl=https://statping.com/statping.gpg
repo=https://github.com/hunterlong/statping repo=https://github.com/hunterlong/statping
statping_get_tarball() { statping_get_tarball() {
url="$repo/releases/latest/download/statping-$1-$2.tar.gz" fext='tar.gz'
if [ ${OS} == 'windows' ]; then
fext='zip'
ARCH='x64'
fi
url="$repo/releases/latest/download/statping-$1-$2.$fext"
printf "$cyan> Downloading latest version for $OS $ARCH...\n$url $reset\n" printf "$cyan> Downloading latest version for $OS $ARCH...\n$url $reset\n"
# Get both the tarball and its GPG signature # Get both the tarball and its GPG signature
tarball_tmp=`mktemp -t statping.tar.gz.XXXXXXXXXX` tarball_tmp=`mktemp -t statping.tar.gz.XXXXXXXXXX`
if curl --fail -L -o "$tarball_tmp" "$url"; then if curl --fail -L -o "$tarball_tmp" "$url"; then
# All this dance is because `tar --strip=1` does not work everywhere # All this dance is because `tar --strip=1` does not work everywhere
temp=$(mktemp -d statping.XXXXXXXXXX) temp=$(mktemp -d statping.XXXXXXXXXX)
tar xzf $tarball_tmp -C "$temp" if [ ${OS} == 'windows' ]; then
unzip $tarball_tmp -d "$temp"
else
tar xzf $tarball_tmp -C "$temp"
fi
statping_verify_integrity "$temp"/statping statping_verify_integrity "$temp"/statping
printf "$green> Installing to $DEST/statping\n" printf "$green> Installing to $DEST/statping\n"
mv "$temp"/statping "$DEST" mv "$temp"/statping "$DEST"
@ -141,6 +150,14 @@ getOS() {
OS='windows' OS='windows'
DEST=/usr/local/bin DEST=/usr/local/bin
;; ;;
'MINGW*')
OS='windows'
DEST=/usr/local/bin
;;
'CYGWIN*')
OS='windows'
DEST=/usr/local/bin
;;
'Darwin') 'Darwin')
OS='osx' OS='osx'
DEST=/usr/local/bin DEST=/usr/local/bin

View File

@ -74,8 +74,8 @@ func init() {
} }
func injectDatabase() { func injectDatabase() {
utils.DeleteFile(dir + "/statup.db") utils.DeleteFile(dir + types.SqliteFilename)
db, err := gorm.Open("sqlite3", dir+"/statup.db") db, err := gorm.Open("sqlite3", dir+"/statping.db")
if err != nil { if err != nil {
panic(err) panic(err)
} }

File diff suppressed because one or more lines are too long

View File

@ -19,6 +19,9 @@ import (
"time" "time"
) )
// SqliteFilename is the name of the SQLlite database file
const SqliteFilename = "statping.db"
// AllNotifiers contains all the Notifiers loaded // AllNotifiers contains all the Notifiers loaded
type AllNotifiers interface{} type AllNotifiers interface{}

View File

@ -54,5 +54,6 @@ type DbConfig struct {
Email string `yaml:"-"` Email string `yaml:"-"`
Error error `yaml:"-"` Error error `yaml:"-"`
Location string `yaml:"location"` Location string `yaml:"location"`
SqlFile string `yaml:"sqlfile,omitempty"`
LocalIP string `yaml:"-"` LocalIP string `yaml:"-"`
} }

View File

@ -185,19 +185,39 @@ func FileExists(name string) bool {
// DeleteFile("newfile.json") // DeleteFile("newfile.json")
func DeleteFile(file string) error { func DeleteFile(file string) error {
Log.Infoln("deleting file: " + file) Log.Infoln("deleting file: " + file)
err := os.Remove(file) return os.Remove(file)
if err != nil {
return err
}
return nil
} }
// DeleteDirectory will attempt to delete a directory and all contents inside // DeleteDirectory will attempt to delete a directory and all contents inside
// DeleteDirectory("assets") // DeleteDirectory("assets")
func DeleteDirectory(directory string) error { func DeleteDirectory(directory string) error {
Log.Infoln("removing directory: " + directory)
return os.RemoveAll(directory) return os.RemoveAll(directory)
} }
// CopyFile will copy a file to a new directory
// CopyFile("source.jpg", "/tmp/source.jpg")
func CopyFile(src, dst string) error {
Log.Infoln(fmt.Sprintf("copying file: %v to %v", src, dst))
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
// Command will run a terminal command with 'sh -c COMMAND' and return stdout and errOut as strings // Command will run a terminal command with 'sh -c COMMAND' and return stdout and errOut as strings
// in, out, err := Command("sass assets/scss assets/css/base.css") // in, out, err := Command("sass assets/scss assets/css/base.css")
func Command(cmd string) (string, string, error) { func Command(cmd string) (string, string, error) {