8015: Fix compile, need to fix tests.
[arvados.git] / services / crunch-run / upload_test.go
1 package main
2
3 import (
4         . "gopkg.in/check.v1"
5         "io/ioutil"
6         "log"
7         "os"
8         "sync"
9 )
10
11 type UploadTestSuite struct{}
12
13 // Gocheck boilerplate
14 var _ = Suite(&UploadTestSuite{})
15
16 func (s *TestSuite) TestSimpleUpload(c *C) {
17         tmpdir, _ := ioutil.TempDir("", "")
18         defer func() {
19                 os.RemoveAll(tmpdir)
20         }()
21
22         ioutil.WriteFile(tmpdir+"/"+"file1.txt", []byte("foo"), 0600)
23
24         cw := CollectionWriter{&KeepTestClient{}, nil, sync.Mutex{}}
25         str, err := cw.WriteTree(tmpdir, log.New(os.Stdout, "", 0))
26         c.Check(err, IsNil)
27         c.Check(str, Equals, ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:file1.txt\n")
28 }
29
30 func (s *TestSuite) TestSimpleUploadTwofiles(c *C) {
31         tmpdir, _ := ioutil.TempDir("", "")
32         defer func() {
33                 os.RemoveAll(tmpdir)
34         }()
35
36         ioutil.WriteFile(tmpdir+"/"+"file1.txt", []byte("foo"), 0600)
37         ioutil.WriteFile(tmpdir+"/"+"file2.txt", []byte("bar"), 0600)
38
39         cw := CollectionWriter{&KeepTestClient{}, nil, sync.Mutex{}}
40         str, err := cw.WriteTree(tmpdir, log.New(os.Stdout, "", 0))
41
42         c.Check(err, IsNil)
43         c.Check(str, Equals, ". 3858f62230ac3c915f300c664312c63f+6 0:3:file1.txt 3:3:file2.txt\n")
44 }
45
46 func (s *TestSuite) TestSimpleUploadSubdir(c *C) {
47         tmpdir, _ := ioutil.TempDir("", "")
48         defer func() {
49                 os.RemoveAll(tmpdir)
50         }()
51
52         os.Mkdir(tmpdir+"/subdir", 0700)
53
54         ioutil.WriteFile(tmpdir+"/"+"file1.txt", []byte("foo"), 0600)
55         ioutil.WriteFile(tmpdir+"/subdir/file2.txt", []byte("bar"), 0600)
56
57         cw := CollectionWriter{&KeepTestClient{}, nil, sync.Mutex{}}
58         str, err := cw.WriteTree(tmpdir, log.New(os.Stdout, "", 0))
59
60         c.Check(err, IsNil)
61         c.Check(str, Equals, `. acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:file1.txt
62 ./subdir 37b51d194a7513e45b56f6524f2d51f2+3 0:3:file2.txt
63 `)
64 }
65
66 func (s *TestSuite) TestSimpleUploadLarge(c *C) {
67         tmpdir, _ := ioutil.TempDir("", "")
68         defer func() {
69                 os.RemoveAll(tmpdir)
70         }()
71
72         file, _ := os.Create(tmpdir + "/" + "file1.txt")
73         data := make([]byte, 1024*1024-1)
74         for i := range data {
75                 data[i] = byte(i % 10)
76         }
77         for i := 0; i < 65; i++ {
78                 file.Write(data)
79         }
80         file.Close()
81
82         ioutil.WriteFile(tmpdir+"/"+"file2.txt", []byte("bar"), 0600)
83
84         cw := CollectionWriter{&KeepTestClient{}, nil, sync.Mutex{}}
85         str, err := cw.WriteTree(tmpdir, log.New(os.Stdout, "", 0))
86
87         c.Check(err, IsNil)
88         c.Check(str, Equals, ". 00ecf01e0d93385115c9f8bed757425d+67108864 485cd630387b6b1846fe429f261ea05f+1048514 0:68157375:file1.txt 68157375:3:file2.txt\n")
89 }
90
91 func (s *TestSuite) TestUploadEmptySubdir(c *C) {
92         tmpdir, _ := ioutil.TempDir("", "")
93         defer func() {
94                 os.RemoveAll(tmpdir)
95         }()
96
97         os.Mkdir(tmpdir+"/subdir", 0700)
98
99         ioutil.WriteFile(tmpdir+"/"+"file1.txt", []byte("foo"), 0600)
100
101         cw := CollectionWriter{&KeepTestClient{}, nil, sync.Mutex{}}
102         str, err := cw.WriteTree(tmpdir, log.New(os.Stdout, "", 0))
103
104         c.Check(err, IsNil)
105         c.Check(str, Equals, `. acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:file1.txt
106 `)
107 }
108
109 func (s *TestSuite) TestUploadEmptyFile(c *C) {
110         tmpdir, _ := ioutil.TempDir("", "")
111         defer func() {
112                 os.RemoveAll(tmpdir)
113         }()
114
115         ioutil.WriteFile(tmpdir+"/"+"file1.txt", []byte(""), 0600)
116
117         cw := CollectionWriter{&KeepTestClient{}, nil, sync.Mutex{}}
118         str, err := cw.WriteTree(tmpdir, log.New(os.Stdout, "", 0))
119
120         c.Check(err, IsNil)
121         c.Check(str, Equals, `. d41d8cd98f00b204e9800998ecf8427e+0 0:0:file1.txt
122 `)
123 }
124
125 func (s *TestSuite) TestUploadError(c *C) {
126         tmpdir, _ := ioutil.TempDir("", "")
127         defer func() {
128                 os.RemoveAll(tmpdir)
129         }()
130
131         ioutil.WriteFile(tmpdir+"/"+"file1.txt", []byte("foo"), 0600)
132
133         cw := CollectionWriter{&KeepErrorTestClient{}, nil, sync.Mutex{}}
134         str, err := cw.WriteTree(tmpdir, log.New(os.Stdout, "", 0))
135
136         c.Check(err, NotNil)
137         c.Check(str, Equals, "")
138 }