Added unit tests TestGetBlockOneKeepOK and TestGetBlockFail (refs #2291)
[arvados.git] / services / keep / keep_test.go
1 package main
2
3 import (
4         "crypto/md5"
5         "fmt"
6         "io/ioutil"
7         "os"
8         "path"
9         "testing"
10 )
11
12 var TEST_BLOCK = []byte("The quick brown fox jumps over the lazy dog.")
13 var TEST_HASH = "e4d909c290d0fb1ca068ffaddf22cbd0"
14
15 // Test simple block reads.
16 func TestGetBlockOK(t *testing.T) {
17         var err error
18
19         defer teardown()
20
21         // Create two test Keep volumes and store a block in each of them.
22         if err := setup(2); err != nil {
23                 t.Fatal(err)
24         }
25         for _, vol := range KeepVolumes {
26                 if err := storeTestBlock(vol, TEST_BLOCK); err != nil {
27                         t.Fatal(err)
28                 }
29         }
30
31         // Check that GetBlock returns success.
32         result, err := GetBlock(TEST_HASH)
33         if err != nil {
34                 t.Errorf("GetBlock error: %s", err)
35         }
36         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
37                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
38         }
39 }
40
41 // Test block reads when one Keep volume is missing.
42 func TestGetBlockOneKeepOK(t *testing.T) {
43         var err error
44
45         defer teardown()
46
47         // Two test Keep volumes, only the second has a block.
48         if err := setup(2); err != nil {
49                 t.Fatal(err)
50         }
51         if err := storeTestBlock(KeepVolumes[1], TEST_BLOCK); err != nil {
52                 t.Fatal(err)
53         }
54
55         // Check that GetBlock returns success.
56         result, err := GetBlock(TEST_HASH)
57         if err != nil {
58                 t.Errorf("GetBlock error: %s", err)
59         }
60         if fmt.Sprint(result) != fmt.Sprint(TEST_BLOCK) {
61                 t.Errorf("expected %s, got %s", TEST_BLOCK, result)
62         }
63 }
64
65 // Test block read failure.
66 func TestGetBlockFail(t *testing.T) {
67         var err error
68
69         defer teardown()
70
71         // Create two empty test Keep volumes.
72         if err := setup(2); err != nil {
73                 t.Fatal(err)
74         }
75
76         // Check that GetBlock returns failure.
77         result, err := GetBlock(TEST_HASH)
78         if err == nil {
79                 t.Errorf("GetBlock incorrectly returned success: ", result)
80         }
81 }
82
83 // setup
84 //     Create KeepVolumes for testing.
85 func setup(nkeeps int) error {
86         KeepVolumes = make([]string, 2)
87         for i := range KeepVolumes {
88                 if dir, err := ioutil.TempDir(os.TempDir(), "keeptest"); err == nil {
89                         KeepVolumes[i] = dir + "/keep"
90                 } else {
91                         return err
92                 }
93         }
94         return nil
95 }
96
97 func teardown() {
98         for _, vol := range KeepVolumes {
99                 os.RemoveAll(path.Dir(vol))
100         }
101 }
102
103 func storeTestBlock(keepdir string, block []byte) error {
104         testhash := fmt.Sprintf("%x", md5.Sum(block))
105
106         blockdir := fmt.Sprintf("%s/%s", keepdir, testhash[:3])
107         if err := os.MkdirAll(blockdir, 0755); err != nil {
108                 return err
109         }
110
111         blockpath := fmt.Sprintf("%s/%s", blockdir, testhash)
112         if f, err := os.Create(blockpath); err == nil {
113                 f.Write(block)
114                 f.Close()
115         } else {
116                 return err
117         }
118
119         return nil
120 }