3826: Add a couple of easy unit tests.
[arvados.git] / services / crunchstat / crunchstat_test.go
1 package main
2
3 import (
4         "regexp"
5         "testing"
6 )
7
8 func TestOpenAndReadAllFail(t *testing.T) {
9         log_chan := make(chan string)
10         go func() {
11                 defer close(log_chan)
12                 if x, err := OpenAndReadAll("/nonexistent/file", log_chan); err == nil {
13                         t.Fatalf("Expected error, got %v", x)
14                 }
15         }()
16         if _, ok := <-log_chan; !ok {
17                 t.Fatalf("Expected error message about nonexistent file")
18         }
19         if msg, ok := <-log_chan; ok {
20                 t.Fatalf("Expected channel to close, got %s", msg)
21         }
22 }
23
24 func TestOpenAndReadAllSuccess(t *testing.T) {
25         log_chan := make(chan string)
26         go func() {
27                 defer close(log_chan)
28                 data, err := OpenAndReadAll("./crunchstat_test.go", log_chan)
29                 if err != nil {
30                         t.Fatalf("got error %s", err)
31                 }
32                 if matched, err := regexp.MatchString("^package main\n", string(data)); err != nil || !matched {
33                         t.Fatalf("data failed regexp: %s", err)
34                 }
35         }()
36         if msg, ok := <-log_chan; ok {
37                 t.Fatalf("Expected channel to close, got %s", msg)
38         }
39 }