blob: 71b7d8e269dbb1960f4b22f8dcda697441d16822 [file] [log] [blame]
Scott Bakerf53bf152019-05-29 17:50:37 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package commands
18
19import (
20 "bytes"
21 "github.com/opencord/cordctl/testutils"
22 "io/ioutil"
23 "testing"
24)
25
26func TestDownload(t *testing.T) {
27 // use `python -m json.tool` to pretty-print json
28 expected := `[
29 {
30 "bytes": 6,
31 "checksum": "sha256:e9c0f8b575cbfcb42ab3b78ecc87efa3b011d9a5d10b09fa4e96f240bf6a82f5",
32 "chunks": 2,
33 "status": "SUCCESS"
34 }
35 ]`
36
37 got := new(bytes.Buffer)
38 OutputStream = got
39
40 var options TransferOpts
41 options.Download.OutputAs = "json"
42 options.Download.Args.LocalFileName = "/tmp/transfer.down"
43 options.Download.Args.URI = "file:///tmp/transfer.down"
44 err := options.Download.Execute([]string{})
45
46 if err != nil {
47 t.Errorf("%s: Received error %v", t.Name(), err)
48 return
49 }
50
51 testutils.AssertJSONEqual(t, got.String(), expected)
52}
53
54func TestUpload(t *testing.T) {
55 // use `python -m json.tool` to pretty-print json
56 expected := `[
57 {
58 "bytes": 6,
59 "checksum": "sha256:e9c0f8b575cbfcb42ab3b78ecc87efa3b011d9a5d10b09fa4e96f240bf6a82f5",
60 "chunks": 2,
61 "status": "SUCCESS"
62 }
63 ]`
64
65 err := ioutil.WriteFile("/tmp/transfer.up", []byte("ABCDEF"), 0644)
66
67 got := new(bytes.Buffer)
68 OutputStream = got
69
70 var options TransferOpts
71 options.Upload.OutputAs = "json"
72 options.Upload.Args.LocalFileName = "/tmp/transfer.up"
73 options.Upload.Args.URI = "file:///tmp/transfer.up"
74 options.Upload.ChunkSize = 3
75 err = options.Upload.Execute([]string{})
76
77 if err != nil {
78 t.Errorf("%s: Received error %v", t.Name(), err)
79 return
80 }
81
82 testutils.AssertJSONEqual(t, got.String(), expected)
83}