Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): 'test notify' button on create/edit #379

Merged
merged 24 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b7dd6c7
feat(web): test notifier option on create/edit
JosephKav Feb 25, 2024
a40129f
feat(web): frontend of test notifier
JosephKav Mar 12, 2024
af50f38
feat(web): switch `/api/v1/notify/test` to post
JosephKav Mar 14, 2024
4557297
feat(web): conversions/default handling in more places
JosephKav Mar 19, 2024
faa1125
refactor(web): convertedDefaults in service-edit
JosephKav Mar 20, 2024
772e3dc
refactor(web): improved default handling for lists
JosephKav Mar 21, 2024
b274bf3
refactor(web): comment blocks for functions, error hooks, padding func
JosephKav Mar 25, 2024
3dca862
Merge branch 'master' into 368-feat-add-a-test-connection-for-notifiers
JosephKav Mar 26, 2024
c65995e
fix(web): api-ui-conversion fixes and refactoring
JosephKav Mar 28, 2024
735d0dd
Merge branch 'master' into 368-feat-add-a-test-connection-for-notifiers
JosephKav Apr 4, 2024
3c34f19
test(test): add missing tests
JosephKav Apr 5, 2024
dbeec2d
test(db): fix races with logfrom
JosephKav Apr 6, 2024
62a2d5e
fix(web): react-query, don't use stale with disabled queries
JosephKav Apr 7, 2024
21d30ef
fix(web): set arrays on notify type change
JosephKav Apr 7, 2024
3d415e6
fix(web): service-title colouring
JosephKav Apr 7, 2024
06e0927
refactor(web): form-validate.tsx for reused form validations
JosephKav Apr 8, 2024
a554b51
fix(web): refresh service/order on ws reconnect
JosephKav Apr 8, 2024
d1edb5b
fix(web): extract errors, use ref to identify errors
JosephKav Apr 8, 2024
ab2fb9f
fix(web): various fixes
JosephKav Apr 10, 2024
dd3d02a
refactor(web): use empty array/object functions
JosephKav Apr 11, 2024
f4d4c6b
fix(web): commands, update removeLast with removeMe changes
JosephKav Apr 11, 2024
cd69833
perf(web): wait for a gap in typing before rendering preview images
JosephKav Apr 13, 2024
595ef98
refactor(notify): pass the specific notify being tested
JosephKav Apr 14, 2024
f427ade
fix(shoutrrr): don't use delay on test messages
JosephKav Apr 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ go-test-coverage:

.PHONY: web-install
web-install:
cd $(UI_PATH) && { npx update-browserslist-db@latest -y || true; } && npm install
cd $(UI_PATH) && { npx --yes update-browserslist-db@latest || true; } && npm install

.PHONY: web-build
web-build:
Expand Down
3 changes: 2 additions & 1 deletion cmd/argus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func main() {
}
}

go db.Run(&config, &jLog)
db.LogInit(&jLog, config.Settings.DataDatabaseFile())
go db.Run(&config)

// Track all targets for changes in version and act on any found changes.
go (&config).Service.Track(&config.Order, &config.OrderMutex)
Expand Down
3 changes: 2 additions & 1 deletion config/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func testLoad(file string, t *testing.T) (config *Config) {
loadMutex.Lock()
defer loadMutex.Unlock()
config.Load(file, &flags, log)
t.Cleanup(func() { os.Remove(*config.Settings.DataDatabaseFile()) })
t.Cleanup(func() { os.Remove(config.Settings.DataDatabaseFile()) })

return
}
Expand Down Expand Up @@ -143,6 +143,7 @@ func testLoadBasic(file string, t *testing.T) (config *Config) {
service.ID = name
}
config.CheckValues()
t.Log("Loaded", file)

return
}
Expand Down
2 changes: 1 addition & 1 deletion config/ordering_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestConfig_LoadOrdering(t *testing.T) {
lock.Lock()
config.Load(file, &flags, log)
lock.Unlock()
defer os.Remove(*config.Settings.DataDatabaseFile())
defer os.Remove(config.Settings.DataDatabaseFile())

// THEN it gets the ordering correctly
gotOrder := config.Order
Expand Down
6 changes: 3 additions & 3 deletions config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ func (s *Settings) LogLevel() string {
}

// DataDatabaseFile.
func (s *Settings) DataDatabaseFile() *string {
return util.FirstNonNilPtr(
func (s *Settings) DataDatabaseFile() string {
return util.DefaultIfNil(util.FirstNonNilPtr(
s.FromFlags.Data.DatabaseFile,
s.Data.DatabaseFile,
s.HardDefaults.Data.DatabaseFile)
s.HardDefaults.Data.DatabaseFile))
}

// WebListenHost.
Expand Down
12 changes: 6 additions & 6 deletions config/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,17 @@ func TestSettings_GetString(t *testing.T) {
want: "ERROR",
},
"data.database-file hard default": {
getFuncPtr: settings.DataDatabaseFile,
flag: &DataDatabaseFile, want: "data/argus.db",
getFunc: settings.DataDatabaseFile,
flag: &DataDatabaseFile, want: "data/argus.db",
nilConfig: true, configPtr: &settings.Data.DatabaseFile,
},
"data.database-file config": {
getFuncPtr: settings.DataDatabaseFile,
flag: &DataDatabaseFile, want: "somewhere.db",
getFunc: settings.DataDatabaseFile,
flag: &DataDatabaseFile, want: "somewhere.db",
},
"data.database-file flag": {
getFuncPtr: settings.DataDatabaseFile,
flag: &DataDatabaseFile, flagVal: stringPtr("ERROR"),
getFunc: settings.DataDatabaseFile,
flag: &DataDatabaseFile, flagVal: stringPtr("ERROR"),
want: "ERROR",
},
"web.listen-host hard default": {
Expand Down
18 changes: 10 additions & 8 deletions db/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@
if jLog.IsLevel("DEBUG") {
jLog.Debug(
fmt.Sprintf("%s, %v", sqlStmt, params),
*logFrom, true)
logFrom, true)
}
res, err := api.db.Exec(sqlStmt, params...)
// Query failed
if err != nil {
jLog.Error(
fmt.Sprintf("updateRow UPDATE: %q %v, %s", sqlStmt, params, util.ErrorToString(err)),
*logFrom, true)
logFrom, true)

Check warning on line 76 in db/handlers.go

View check run for this annotation

Codecov / codecov/patch

db/handlers.go#L76

Added line #L76 was not covered by tests
return
}

Expand Down Expand Up @@ -105,12 +105,13 @@
if jLog.IsLevel("DEBUG") {
jLog.Debug(
fmt.Sprintf("%s, %v", sqlStmt, params),
*logFrom, true)
logFrom, true)
}
_, err = api.db.Exec(sqlStmt, params...)
jLog.Error(
fmt.Sprintf("updateRow INSERT: %q %v, %s", sqlStmt, params, util.ErrorToString(err)),
*logFrom,
fmt.Sprintf("updateRow INSERT: %q %v, %s",
sqlStmt, params, util.ErrorToString(err)),
logFrom,
err != nil)
}
}
Expand All @@ -123,11 +124,12 @@
if jLog.IsLevel("DEBUG") {
jLog.Debug(
fmt.Sprintf("%s, %v", sqlStmt, serviceID),
*logFrom, true)
logFrom, true)
}
_, err := api.db.Exec(sqlStmt, serviceID)
jLog.Error(
fmt.Sprintf("deleteRow: %q with %q, %s", sqlStmt, serviceID, util.ErrorToString(err)),
*logFrom,
fmt.Sprintf("deleteRow: %q with %q, %s",
sqlStmt, serviceID, util.ErrorToString(err)),
logFrom,
err != nil)
}
67 changes: 35 additions & 32 deletions db/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ func TestAPI_UpdateRow(t *testing.T) {
t.Parallel()

cfg := testConfig()
api := api{config: cfg}
*api.config.Settings.Data.DatabaseFile = fmt.Sprintf("%s.db", strings.ReplaceAll(name, " ", "_"))
defer os.Remove(*api.config.Settings.Data.DatabaseFile)
api.initialise()
testAPI := api{config: cfg}
*testAPI.config.Settings.Data.DatabaseFile = fmt.Sprintf("%s.db", strings.ReplaceAll(name, " ", "_"))
defer os.Remove(*cfg.Settings.Data.DatabaseFile)
testAPI.initialise()

// WHEN updateRow is called targeting single/multiple cells
api.updateRow(tc.target, tc.cells)
testAPI.updateRow(tc.target, tc.cells)
time.Sleep(100 * time.Millisecond)

// THEN those cell(s) are changed in the DB
row := queryRow(t, api.db, tc.target)
row := queryRow(t, testAPI.db, tc.target)
for _, cell := range tc.cells {
var got string
switch cell.Column {
Expand All @@ -103,7 +103,7 @@ func TestAPI_UpdateRow(t *testing.T) {
cell.Column, cell.Value, got)
}
}
api.db.Close()
testAPI.db.Close()
time.Sleep(100 * time.Millisecond)
})
}
Expand All @@ -128,33 +128,36 @@ func TestAPI_DeleteRow(t *testing.T) {
t.Parallel()

cfg := testConfig()
api := api{config: cfg}
*api.config.Settings.Data.DatabaseFile = fmt.Sprintf("%s.db", strings.ReplaceAll(name, " ", "_"))
api.initialise()
testAPI := api{config: cfg}
*testAPI.config.Settings.Data.DatabaseFile = fmt.Sprintf("%s.db", strings.ReplaceAll(name, " ", "_"))
testAPI.initialise()

// Ensure the row exists if tc.exists
if tc.exists {
api.updateRow(tc.serviceID, []dbtype.Cell{
{Column: "latest_version", Value: "9.9.9"}, {Column: "deployed_version", Value: "8.8.8"}})
testAPI.updateRow(
tc.serviceID,
[]dbtype.Cell{
{Column: "latest_version", Value: "9.9.9"}, {Column: "deployed_version", Value: "8.8.8"}},
)
time.Sleep(100 * time.Millisecond)
}
// Check the row existance before the test
row := queryRow(t, api.db, tc.serviceID)
row := queryRow(t, testAPI.db, tc.serviceID)
if tc.exists && (row.LatestVersion() == "" || row.DeployedVersion() == "") {
t.Errorf("expecting row to exist. got %#v", row)
}

// WHEN deleteRow is called targeting a row
api.deleteRow(tc.serviceID)
testAPI.deleteRow(tc.serviceID)
time.Sleep(100 * time.Millisecond)

// THEN the row is deleted from the DB
row = queryRow(t, api.db, tc.serviceID)
row = queryRow(t, testAPI.db, tc.serviceID)
if row.LatestVersion() != "" || row.DeployedVersion() != "" {
t.Errorf("expecting row to be deleted. got %#v", row)
}
api.db.Close()
os.Remove(*api.config.Settings.Data.DatabaseFile)
testAPI.db.Close()
os.Remove(*testAPI.config.Settings.Data.DatabaseFile)
time.Sleep(100 * time.Millisecond)
})
}
Expand All @@ -163,21 +166,21 @@ func TestAPI_DeleteRow(t *testing.T) {
func TestAPI_Handler(t *testing.T) {
// GIVEN a DB with a few service status'
cfg := testConfig()
api := api{config: cfg}
*api.config.Settings.Data.DatabaseFile = "TestHandler.db"
defer os.Remove(*api.config.Settings.Data.DatabaseFile)
defer os.Remove(*api.config.Settings.Data.DatabaseFile + "-journal")
api.initialise()
go api.handler()
defer api.db.Close()
testAPI := api{config: cfg}
*testAPI.config.Settings.Data.DatabaseFile = "TestHandler.db"
defer os.Remove(*testAPI.config.Settings.Data.DatabaseFile)
defer os.Remove(*testAPI.config.Settings.Data.DatabaseFile + "-journal")
testAPI.initialise()
go testAPI.handler()
defer testAPI.db.Close()

// WHEN a message is sent to the DatabaseChannel targeting latest_version
target := "keep0"
cell1 := dbtype.Cell{
Column: "latest_version", Value: "9.9.9"}
cell2 := dbtype.Cell{
Column: cell1.Column, Value: cell1.Value + "-dev"}
want := queryRow(t, api.db, target)
want := queryRow(t, testAPI.db, target)
want.SetLatestVersion(cell1.Value, false)
msg1 := dbtype.Message{
ServiceID: target,
Expand All @@ -187,11 +190,11 @@ func TestAPI_Handler(t *testing.T) {
ServiceID: target,
Cells: []dbtype.Cell{cell2},
}
*api.config.DatabaseChannel <- msg1
*testAPI.config.DatabaseChannel <- msg1
time.Sleep(250 * time.Millisecond)

// THEN the cell was changed in the DB
got := queryRow(t, api.db, target)
got := queryRow(t, testAPI.db, target)
if got.LatestVersion() != want.LatestVersion() {
t.Errorf("Expected %q to be updated to %q\ngot %#v\nwant %#v",
cell1.Column, cell1.Value, got, want)
Expand All @@ -200,28 +203,28 @@ func TestAPI_Handler(t *testing.T) {
// ------------------------------

// WHEN a message is sent to the DatabaseChannel deleting a row
*api.config.DatabaseChannel <- dbtype.Message{
*testAPI.config.DatabaseChannel <- dbtype.Message{
ServiceID: target,
Delete: true,
}
time.Sleep(250 * time.Millisecond)

// THEN the row is deleted from the DB
got = queryRow(t, api.db, target)
got = queryRow(t, testAPI.db, target)
if got.LatestVersion() != "" || got.DeployedVersion() != "" {
t.Errorf("Expected row to be deleted\ngot %#v\nwant %#v", got, want)
}

// ------------------------------

// WHEN multiple messages are targeting the same row in quick succession
*api.config.DatabaseChannel <- msg1
*testAPI.config.DatabaseChannel <- msg1
wantLatestVersion := msg2.Cells[0].Value
*api.config.DatabaseChannel <- msg2
*testAPI.config.DatabaseChannel <- msg2
time.Sleep(250 * time.Millisecond)

// THEN the last message is the one that is applied
got = queryRow(t, api.db, target)
got = queryRow(t, testAPI.db, target)
if got.LatestVersion() != wantLatestVersion {
t.Errorf("Expected %q to be updated to %q\ngot %#v\nwant %#v",
cell2.Column, cell2.Value, got, want)
Expand Down
8 changes: 5 additions & 3 deletions db/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ var cfg *config.Config

func TestMain(m *testing.M) {
log := util.NewJLog("DEBUG", false)
logFrom = &util.LogFrom{}
log.Testing = true
databaseFile := "TestRun.db"
LogInit(log, databaseFile)

cfg = testConfig()
*cfg.Settings.Data.DatabaseFile = "TestRun.db"
*cfg.Settings.Data.DatabaseFile = databaseFile
defer os.Remove(*cfg.Settings.Data.DatabaseFile)
go Run(cfg, log)
go Run(cfg)
time.Sleep(250 * time.Millisecond) // Time for db to start

os.Exit(m.Run())
}
Expand Down
Loading
Loading