Skip to content

Commit

Permalink
Improve testing (#15)
Browse files Browse the repository at this point in the history
* Added some more table tests, refactored some other tests. Fixed a bug
  • Loading branch information
kyallanum authored Jan 14, 2024
1 parent 553ea49 commit 93b09ee
Show file tree
Hide file tree
Showing 12 changed files with 893 additions and 522 deletions.
157 changes: 100 additions & 57 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,132 @@
package cmd

import (
"fmt"
"io"
"os"
"reflect"
"strings"
"testing"

config "github.com/kyallanum/athena/models/config"
models "github.com/kyallanum/athena/models/library"
logs "github.com/kyallanum/athena/models/logs"
"github.com/sirupsen/logrus"
)

func TestResolveFile(t *testing.T) {
logger := logrus.New()
logger.SetOutput(io.Discard)

os.Stdout, _ = os.Open(os.DevNull)
defer os.Stdout.Close()
logFile, _ := logs.LoadLogFile("../examples/apt-term.log")
configuration, _ := config.CreateConfiguration("../examples/apt-term-config.json")

_, err := resolveLogFile(logFile, configuration, logger)

if err != nil {
t.Errorf("An error occurred while resolving log file: %s", err)
func checkExpectedError(actualError, expectedError any) bool {
if actualError == nil && expectedError == nil {
return true
}
}

func TestResolveLogFileBadLog(t *testing.T) {
os.Stdout, _ = os.Open(os.DevNull)
defer os.Stdout.Close()

logFile, _ := logs.LoadLogFile("../examples/apt-term-bad.log")
configuration, _ := config.CreateConfiguration("../examples/apt-term-config.json")

_, err := resolveLogFile(logFile, configuration, nil)

if err.Error() != "log file contains no contents" {
t.Errorf("Error was not properly returned when checking log file contents")
if actualError != nil {
if expectedError != nil {
if strings.Contains(actualError.(error).Error(), expectedError.(error).Error()) {
return true
}
}
}
return false
}

func TestResolveLogFileBadConfig(t *testing.T) {
func TestResolveFile(t *testing.T) {
logger := logrus.New()
logger.SetOutput(io.Discard)

os.Stdout, _ = os.Open(os.DevNull)
defer os.Stdout.Close()

logFile, _ := logs.LoadLogFile("../examples/apt-term.log")
configuration, _ := config.CreateConfiguration("../examples/apt-term-config-bad.json")

_, err := resolveLogFile(logFile, configuration, logger)
testTable := []struct {
name string
fileName string
configFile string
expectedReturn *models.Library
expectedLogFileError error
expectedConfigFileError error
expectedResolveError error
}{
{
name: "Test_Good_Log_And_Configuration_File",
fileName: "../examples/apt-term.log",
configFile: "../examples/apt-term-config.json",
expectedReturn: &models.Library{},
expectedLogFileError: nil,
expectedConfigFileError: nil,
expectedResolveError: nil,
},
{
name: "Test_Bad_Log_File",
fileName: "../examples/apt-term-bad.log",
configFile: "../examples/apt-term-config.json",
expectedReturn: nil,
expectedLogFileError: fmt.Errorf("../examples/apt-term-bad.log: no such file or directory"),
expectedConfigFileError: nil,
expectedResolveError: fmt.Errorf("log file contains no contents"),
},
{
name: "Test_Bad_Config_File",
fileName: "../examples/apt-term.log",
configFile: "../examples/apt-term-config-bad.json",
expectedReturn: nil,
expectedLogFileError: nil,
expectedConfigFileError: fmt.Errorf("../examples/apt-term-config-bad.json: no such file or directory"),
expectedResolveError: fmt.Errorf("configuration file has no contents"),
},
}

if err.Error() != "configuration file has no contents" {
t.Errorf("Error was not properly returned when checking configuration contents")
for _, test := range testTable {
t.Run(test.name, func(t *testing.T) {
logFile, err := logs.LoadLogFile(test.fileName)
if !checkExpectedError(err, test.expectedLogFileError) {
t.Errorf("Error was incorrectly thrown while loading log file when it shouldn't have: \n\tExpected: %v\n\tReceived: %v", test.expectedLogFileError, err)
}
configFile, err := config.CreateConfiguration(test.configFile)
if !checkExpectedError(err, test.expectedConfigFileError) {
t.Errorf("Error was incorrectly thrown while loading configuration file when it shouldn't have: \n\tExpected: %v\n\tReceived: %v", test.expectedConfigFileError, err)
}

library, err := resolveLogFile(logFile, configFile, logger)
if !checkExpectedError(err, test.expectedResolveError) {
t.Errorf("Error was incorrectly thrown while resolving log file: \n\tExpected: %v\n\tReceived: %v", test.expectedResolveError, err)
}

if reflect.TypeOf(library).String() != reflect.TypeOf(test.expectedReturn).String() {
t.Errorf("Log file resolution did not return the proper type: Expected: %v\n\tReceived: %v", reflect.TypeOf(test.expectedReturn).String(), reflect.TypeOf(library).String())
}
})
}
}

func TestResolveLogFileNoConfigName(t *testing.T) {
func TestConfigErrors(t *testing.T) {
logger := logrus.New()
logger.SetOutput(io.Discard)

logFile, _ := logs.LoadLogFile("../examples/apt-term.log")
configuration := &config.Configuration{
Rules: make([]config.Rule, 1),
}

_, err := resolveLogFile(logFile, configuration, logger)

if err.Error() != "configuration file contains no log name" {
t.Errorf("Error was not properly returned when checking configuration name")
testTable := []struct {
name string
configuration *config.Configuration
expectedError error
}{
{
name: "Test_No_Config_Name",
configuration: &config.Configuration{
Rules: make([]config.Rule, 1),
},
expectedError: fmt.Errorf("configuration file contains no log name"),
},
{
name: "Test_No_Config_Rules",
configuration: &config.Configuration{
Name: "test",
},
expectedError: fmt.Errorf("configuration does not have any rules"),
},
}
}

func TestResolveLogFileNoConfigRules(t *testing.T) {
logger := logrus.New()
logger.SetOutput(io.Discard)
for _, test := range testTable {
t.Run(test.name, func(t *testing.T) {
logFile, _ := logs.LoadLogFile("../examples/apt-term.log")

logFile, _ := logs.LoadLogFile("../examples/apt-term.log")
configuration := &config.Configuration{
Name: "test",
}
_, err := resolveLogFile(logFile, test.configuration, logger)

_, err := resolveLogFile(logFile, configuration, logger)
if err.Error() != "configuration does not have any rules" {
t.Errorf("Error was not properly returned when checking configuration rules")
if !checkExpectedError(err, test.expectedError) {
t.Errorf("Error was not properly returned when checking configuration: \n\tExpected: %v\n\tReceived: %v", test.expectedError, err)
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func printSummary(library *library.Library, logger *logrus.Logger) error {
logger.Infof("\n--------------- %s Log File Summary ---------------\n", libraryName)
libraryKeys := library.LibraryKeys()
for _, rule := range libraryKeys {
logger.Infof("Rule: %s\n", rule)
logger.Infof("Rule: %s", rule)
ruleData, _ := library.RuleData(rule)
summaryDataLen := ruleData.SummaryDataLen()
if summaryDataLen == 0 {
Expand Down
Loading

0 comments on commit 93b09ee

Please sign in to comment.