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 | "testing" |
| 23 | ) |
| 24 | |
| 25 | func TestServicesList(t *testing.T) { |
| 26 | // use `python -m json.tool` to pretty-print json |
| 27 | expected := `[ |
| 28 | { |
| 29 | "name": "onos", |
| 30 | "state": "present", |
| 31 | "version": "2.1.1-dev" |
| 32 | }, |
| 33 | { |
| 34 | "name": "kubernetes", |
| 35 | "state": "present", |
| 36 | "version": "1.2.1" |
| 37 | } |
| 38 | ]` |
| 39 | |
| 40 | got := new(bytes.Buffer) |
| 41 | OutputStream = got |
| 42 | |
| 43 | var options ServiceOpts |
| 44 | options.List.OutputAs = "json" |
| 45 | err := options.List.Execute([]string{}) |
| 46 | |
| 47 | if err != nil { |
| 48 | t.Errorf("%s: Received error %v", t.Name(), err) |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | testutils.AssertJSONEqual(t, got.String(), expected) |
| 53 | } |
| 54 | |
| 55 | func TestServicesListTable(t *testing.T) { |
| 56 | // We'll use the ServicesList command to be our end-to-end test for |
| 57 | // table formatted commands, as the output is relatively simple. |
| 58 | expected := `NAME VERSION STATE |
| 59 | onos 2.1.1-dev present |
| 60 | kubernetes 1.2.1 present |
| 61 | ` |
| 62 | |
| 63 | got := new(bytes.Buffer) |
| 64 | OutputStream = got |
| 65 | |
| 66 | var options ServiceOpts |
| 67 | err := options.List.Execute([]string{}) |
| 68 | |
| 69 | if err != nil { |
| 70 | t.Errorf("%s: Received error %v", t.Name(), err) |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | testutils.AssertStringEqual(t, got.String(), expected) |
| 75 | } |
| 76 | |
| 77 | func TestServicesListYaml(t *testing.T) { |
| 78 | // We'll use the ServicesList command to be our end-to-end test for |
| 79 | // yaml formatted commands, as the output is relatively simple. |
| 80 | expected := `- name: onos |
| 81 | version: 2.1.1-dev |
| 82 | state: present |
| 83 | - name: kubernetes |
| 84 | version: 1.2.1 |
| 85 | state: present |
| 86 | ` |
| 87 | |
| 88 | got := new(bytes.Buffer) |
| 89 | OutputStream = got |
| 90 | |
| 91 | var options ServiceOpts |
| 92 | options.List.OutputAs = "yaml" |
| 93 | err := options.List.Execute([]string{}) |
| 94 | |
| 95 | if err != nil { |
| 96 | t.Errorf("%s: Received error %v", t.Name(), err) |
| 97 | return |
| 98 | } |
| 99 | |
| 100 | testutils.AssertStringEqual(t, got.String(), expected) |
| 101 | } |