Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
add a test case for readline
Browse files Browse the repository at this point in the history
Fixes #16
  • Loading branch information
chrisvaughn committed Mar 18, 2018
1 parent 831a9f0 commit e93bb05
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
37 changes: 18 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"google.golang.org/api/option"
)

var version = "0.3.0"
var version = "0.3.1"

// DNAQuery holds config and derived data
type DNAQuery struct {
Expand Down Expand Up @@ -56,24 +56,22 @@ func cleanupFiles(path ...string) {
}
}

func (d *DNAQuery) readLine(path string) chan [2]string {
func (d *DNAQuery) readLine(path string) (chan [2]string, error) {
log.Println("Opening Logfile", path)
inFile, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "Unable to open input: %s", path)
}
gzReader, err := gzip.NewReader(inFile)
if err != nil {
return nil, errors.Wrap(err, "Unable to create gzip reader")
}
ch := make(chan [2]string, 50)
go func(path string, ch chan [2]string) {
go func(inFile io.ReadCloser, ch chan [2]string) {
defer close(ch)
log.Println("Opening Logfile", path)
inFile, err := os.Open(path)
if err != nil {
log.Fatalf("Unable to open input: %s", path)
}
gz, err := gzip.NewReader(inFile)

if err != nil {
log.Fatal(err)
}
defer inFile.Close()
defer gz.Close()

scanner := bufio.NewScanner(gz)
scanner := bufio.NewScanner(inFile)
log.Println("Scanning log file")
lineCount := 0
for scanner.Scan() {
Expand All @@ -86,11 +84,11 @@ func (d *DNAQuery) readLine(path string) chan [2]string {
}
}
if err := scanner.Err(); err != nil {
log.Fatalln("Error reading log file:", err.Error())
log.Fatalf("Error reading log file: %v", err)
}
log.Printf("Scanning complete. %d lines scanned\n", lineCount)
}(path, ch)
return ch
}(gzReader, ch)
return ch, nil
}

func (d *DNAQuery) processLine(path string, ch chan [2]string) error {
Expand Down Expand Up @@ -312,7 +310,8 @@ func run(c *cli.Context) error {
defer cleanupFiles(logName)
}

ch := dna.readLine(logName)
ch, err := dna.readLine(logName)
CheckErr("Error: ", err)

outFile := "results_" + dateToProcess + ".csv"
outPath := filepath.Join(cfg.Storage.LogDirectory, outFile)
Expand Down
35 changes: 35 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,38 @@ func TestProcessLine(t *testing.T) {
}
cleanupFiles(outfile)
}

func TestReadLine(t *testing.T) {
c1 := App{
Name: "ingress-lb",
Regex: `^.*$`,
}
cfg := &Configuration{
Apps: []App{c1},
Storage: Storage{LogDirectory: "/tmp/"},
}
dna, err := NewDNAQuery(cfg)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
ch, err := dna.readLine("test_data/testlog")
if err == nil {
t.Error("expected error for invalid file")
}
ch, err = dna.readLine("main.go")
if err == nil {
t.Error("expected error for invalid gzip file")
}

ch, err = dna.readLine("test_data/testlog.json.gz")
if err != nil {
t.Fatalf("unexpected error in readLine, %v", err)
}
count := 0
for _ = range ch {
count += 1
}
if count != 6 {
t.Errorf("Unexpected # of logs found based on config, expected 6 found %v", count)
}
}
Binary file added test_data/testlog.json.gz
Binary file not shown.

0 comments on commit e93bb05

Please sign in to comment.