aa-log: add missing unit test.

This commit is contained in:
Alexandre Pujol 2021-12-12 12:35:08 +00:00
parent 73d59da67c
commit bcd1b0958d
No known key found for this signature in database
GPG key ID: C5469996F0DF68EC
2 changed files with 46 additions and 8 deletions

View file

@ -8,6 +8,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
) )
@ -156,24 +157,32 @@ func (aaLogs AppArmorLogs) String() string {
return res return res
} }
func main() { func aaLog(args []string, path string) error {
profile := "" profile := ""
if len(os.Args) >= 2 { if len(args) >= 2 {
profile = os.Args[1] profile = args[1]
} }
file, err := os.Open(LogFile) file, err := os.Open(filepath.Clean(path))
if err != nil { if err != nil {
fmt.Println(err) return err
os.Exit(1)
} }
/* #nosec G307 */
defer func() { defer func() {
if err := file.Close(); err != nil { if err := file.Close(); err != nil {
fmt.Println("Error closing file:", err) fmt.Println(err)
os.Exit(1)
} }
}() }()
aaLogs := NewApparmorLogs(file, profile) aaLogs := NewApparmorLogs(file, profile)
fmt.Print(aaLogs.String()) fmt.Print(aaLogs.String())
return err
}
func main() {
err := aaLog(os.Args, LogFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} }

View file

@ -127,3 +127,32 @@ func TestAppArmorLogs_String(t *testing.T) {
}) })
} }
} }
func Test_app(t *testing.T) {
tests := []struct {
name string
args []string
path string
wantErr bool
}{
{
name: "OK",
args: []string{"aa-log", ""},
path: "../../tests/audit.log",
wantErr: false,
},
{
name: "No logfile",
args: []string{"aa-log", ""},
path: "../../tests/log",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := aaLog(tt.args, tt.path); (err != nil) != tt.wantErr {
t.Errorf("aaLog() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}