Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "github.com/opencord/cordctl/testutils" |
| 22 | "io/ioutil" |
| 23 | "testing" |
| 24 | ) |
| 25 | |
| 26 | func TestBackupCreate(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 BackupOpts |
| 41 | options.Create.OutputAs = "json" |
| 42 | options.Create.Args.LocalFileName = "/tmp/transfer.down" |
| 43 | options.Create.ChunkSize = 3 |
| 44 | err := options.Create.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 | |
| 54 | // Mock the CreateURI function in the Restore code to use file:///tmp/transfer.up |
| 55 | func CreateURI() (string, string) { |
| 56 | remote_name := "transfer.up" |
| 57 | uri := "file:///tmp/" + remote_name |
| 58 | return remote_name, uri |
| 59 | } |
| 60 | |
| 61 | func TestBackupRestore(t *testing.T) { |
| 62 | // use `python -m json.tool` to pretty-print json |
| 63 | expected := `[ |
| 64 | { |
| 65 | "bytes": 6, |
| 66 | "checksum": "sha256:e9c0f8b575cbfcb42ab3b78ecc87efa3b011d9a5d10b09fa4e96f240bf6a82f5", |
| 67 | "chunks": 2, |
| 68 | "status": "SUCCESS" |
| 69 | } |
| 70 | ]` |
| 71 | |
| 72 | err := ioutil.WriteFile("/tmp/transfer.up", []byte("ABCDEF"), 0644) |
| 73 | |
| 74 | got := new(bytes.Buffer) |
| 75 | OutputStream = got |
| 76 | |
| 77 | var options BackupRestore |
| 78 | options.OutputAs = "json" |
| 79 | options.Args.LocalFileName = "/tmp/transfer.up" |
| 80 | options.ChunkSize = 3 |
| 81 | options.CreateURIFunc = CreateURI |
| 82 | err = options.Execute([]string{}) |
| 83 | |
| 84 | if err != nil { |
| 85 | t.Errorf("%s: Received error %v", t.Name(), err) |
| 86 | return |
| 87 | } |
| 88 | |
| 89 | testutils.AssertJSONEqual(t, got.String(), expected) |
| 90 | } |