diff --git a/checks/folder.go b/checks/folder.go index b19759f1c..f1f0ae7af 100644 --- a/checks/folder.go +++ b/checks/folder.go @@ -89,10 +89,11 @@ 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) @@ -100,23 +101,31 @@ func checkLocalFolder(ctx *context.Context, check v1.FolderCheck) pkg.Results { 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, @@ -126,7 +135,7 @@ 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 @@ -134,7 +143,7 @@ func getLocalFolderCheck(path string, filter v1.FolderFilter) (*FolderCheck, err result.Append(info) } - return &result, err + return result, err } func getGenericFolderCheck(fs Filesystem, dir string, filter v1.FolderFilter) (*FolderCheck, error) { diff --git a/checks/folder_check.go b/checks/folder_check.go index 4b3834ec8..3090ac5c1 100644 --- a/checks/folder_check.go +++ b/checks/folder_check.go @@ -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 { diff --git a/checks/sql.go b/checks/sql.go index dfa4c648a..fbfbb8c11 100644 --- a/checks/sql.go +++ b/checks/sql.go @@ -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"` } @@ -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() { @@ -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{}) @@ -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 @@ -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) } diff --git a/fixtures/datasources/folder_pass.yaml b/fixtures/datasources/folder_pass.yaml index 5e7ef73b8..ef824518d 100644 --- a/fixtures/datasources/folder_pass.yaml +++ b/fixtures/datasources/folder_pass.yaml @@ -3,7 +3,7 @@ kind: Canary metadata: name: folder-pass spec: - interval: 10 + interval: 300 folder: - path: /etc/ name: Check for updated /etc files @@ -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