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