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

fix: improve nil handling of sql/folder checks #1495

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 17 additions & 8 deletions checks/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,43 @@ func checkLocalFolder(ctx *context.Context, check v1.FolderCheck) pkg.Results {
var results pkg.Results
results = append(results, result)
folders, err := getLocalFolderCheck(check.Path, check.Filter)
result.AddDetails(folders)

if err != nil {
return results.ErrorMessage(err)
}
result.AddDetails(folders)

if test := folders.Test(check.FolderTest); test != "" {
return results.Failf(test)
}
return results
}

func getLocalFolderCheck(path string, filter v1.FolderFilter) (*FolderCheck, error) {
func getLocalFolderCheck(path string, filter v1.FolderFilter) (FolderCheck, error) {
result := FolderCheck{}
_filter, err := filter.New()
if err != nil {
return nil, err
return result, err
}
if dir, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
return result, nil
}
return result, err
} else if !dir.IsDir() {
return result, fmt.Errorf("%s is not a directory", path)
}
files, err := os.ReadDir(path)
if err != nil {
return nil, err
return result, err
}
if len(files) == 0 {
// directory is empty. returning duration of directory
info, err := os.Stat(path)
if err != nil {
return nil, err
return result, err
}
return &FolderCheck{
return FolderCheck{
Oldest: newFile(info),
Newest: newFile(info),
AvailableSize: SizeNotSupported,
Expand All @@ -126,15 +135,15 @@ func getLocalFolderCheck(path string, filter v1.FolderFilter) (*FolderCheck, err
for _, file := range files {
info, err := file.Info()
if err != nil {
return nil, err
return result, err
}
if file.IsDir() || !_filter.Filter(info) {
continue
}

result.Append(info)
}
return &result, err
return result, err
}

func getGenericFolderCheck(fs Filesystem, dir string, filter v1.FolderFilter) (*FolderCheck, error) {
Expand Down
2 changes: 1 addition & 1 deletion checks/folder_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type FolderCheck struct {
MaxSize *File `json:"largest,omitempty"`
TotalSize int64 `json:"size,omitempty"`
AvailableSize int64 `json:"availableSize,omitempty"`
Files []File `json:"files,omitempty"`
Files []File `json:"files"`
}

type File struct {
Expand Down
20 changes: 11 additions & 9 deletions checks/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type SQLChecker interface {
}

type SQLDetails struct {
Rows []map[string]interface{} `json:"rows,omitempty"`
Rows []map[string]interface{} `json:"rows"`
Count int `json:"count,omitempty"`
}

Expand All @@ -28,23 +28,24 @@ type SQLDetails struct {
// Connects to a db using the specified `driver` and `connectionstring`
// Performs the test query given in `query`.
// Gives the single row test query result as result.
func querySQL(driver string, connection string, query string) (*SQLDetails, error) {
func querySQL(driver string, connection string, query string) (SQLDetails, error) {
result := SQLDetails{}
db, err := sql.Open(driver, connection)
if err != nil {
return nil, fmt.Errorf("failed to connect to db: %w", err)
return result, fmt.Errorf("failed to connect to db: %w", err)
}
defer db.Close()

rows, err := db.Query(query)
result := SQLDetails{}

if err != nil || rows.Err() != nil {
return nil, fmt.Errorf("failed to query db: %w", err)
return result, fmt.Errorf("failed to query db: %w", err)
}
defer rows.Close()

columns, err := rows.Columns()
if err != nil {
return nil, fmt.Errorf("failed to get columns: %w", err)
return result, fmt.Errorf("failed to get columns: %w", err)
}

for rows.Next() {
Expand All @@ -54,7 +55,7 @@ func querySQL(driver string, connection string, query string) (*SQLDetails, erro
rowValues[i] = &s
}
if err := rows.Scan(rowValues...); err != nil {
return nil, err
return result, err
}

var row = make(map[string]interface{})
Expand All @@ -71,7 +72,7 @@ func querySQL(driver string, connection string, query string) (*SQLDetails, erro
}

result.Count = len(result.Rows)
return &result, nil
return result, nil
}

// CheckSQL : Attempts to connect to a DB using the specified
Expand Down Expand Up @@ -110,11 +111,12 @@ func CheckSQL(ctx *context.Context, checker SQLChecker) pkg.Results { // nolint:
}

details, err := querySQL(checker.GetDriver(), connection.URL, query)
result.AddDetails(details)

if err != nil {
return results.ErrorMessage(err)
}

result.AddDetails(details)
if details.Count < check.Result {
return results.Failf("Query returned %d rows, expected %d", details.Count, check.Result)
}
Expand Down
14 changes: 13 additions & 1 deletion fixtures/datasources/folder_pass.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ kind: Canary
metadata:
name: folder-pass
spec:
interval: 10
interval: 300
folder:
- path: /etc/
name: Check for updated /etc files
Expand Down Expand Up @@ -32,3 +32,15 @@ spec:
- name: new_files
value: results.?files.orValue([]).size()
type: counter
---
apiVersion: canaries.flanksource.com/v1
kind: Canary
metadata:
name: folder-pass-empty
spec:
interval: 300
folder:
- name: folder-nil-handling
path: /some/folder/that/does/not/exist
test:
expr: results.files.size() == 0
Loading