|
| 1 | +// Copyright 2019 The OpenSDS Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
1 | 15 | package client |
| 16 | + |
| 17 | +import ( |
| 18 | + "reflect" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/opensds/opensds/pkg/model" |
| 22 | +) |
| 23 | + |
| 24 | +var fakeHostMgr = &HostMgr{ |
| 25 | + Receiver: NewFakeHostReceiver(), |
| 26 | +} |
| 27 | + |
| 28 | +func TestCreateHost(t *testing.T) { |
| 29 | + expected := &model.HostSpec{ |
| 30 | + BaseModel: &model.BaseModel{ |
| 31 | + Id: "202964b5-8e73-46fd-b41b-a8e403f3c30b", |
| 32 | + CreatedAt: "2019-11-11T11:01:33", |
| 33 | + }, |
| 34 | + TenantId: "x", |
| 35 | + AccessMode: "agentless", |
| 36 | + HostName: "sap1", |
| 37 | + IP: "192.168.56.12", |
| 38 | + AvailabilityZones: []string{"az1", "az2"}, |
| 39 | + Initiators: []*model.Initiator{ |
| 40 | + &model.Initiator{ |
| 41 | + PortName: "20000024ff5bb888", |
| 42 | + Protocol: "iscsi", |
| 43 | + }, |
| 44 | + &model.Initiator{ |
| 45 | + PortName: "20000024ff5bc999", |
| 46 | + Protocol: "iscsi", |
| 47 | + }, |
| 48 | + }, |
| 49 | + } |
| 50 | + |
| 51 | + host, err := fakeHostMgr.CreateHost(&model.HostSpec{}) |
| 52 | + if err != nil { |
| 53 | + t.Error(err) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + if !reflect.DeepEqual(host, expected) { |
| 58 | + t.Errorf("expected %+v, got %+v", expected, host) |
| 59 | + return |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestGetHost(t *testing.T) { |
| 64 | + hostID := "d2975ebe-d82c-430f-b28e-f373746a71ca" |
| 65 | + expected := &model.HostSpec{ |
| 66 | + BaseModel: &model.BaseModel{ |
| 67 | + Id: "202964b5-8e73-46fd-b41b-a8e403f3c30b", |
| 68 | + CreatedAt: "2019-11-11T11:01:33", |
| 69 | + }, |
| 70 | + TenantId: "x", |
| 71 | + AccessMode: "agentless", |
| 72 | + HostName: "sap1", |
| 73 | + IP: "192.168.56.12", |
| 74 | + AvailabilityZones: []string{"az1", "az2"}, |
| 75 | + Initiators: []*model.Initiator{ |
| 76 | + &model.Initiator{ |
| 77 | + PortName: "20000024ff5bb888", |
| 78 | + Protocol: "iscsi", |
| 79 | + }, |
| 80 | + &model.Initiator{ |
| 81 | + PortName: "20000024ff5bc999", |
| 82 | + Protocol: "iscsi", |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + host, err := fakeHostMgr.GetHost(hostID) |
| 88 | + if err != nil { |
| 89 | + t.Error(err) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if !reflect.DeepEqual(host, expected) { |
| 94 | + t.Errorf("expected %v, got %v", expected, host) |
| 95 | + return |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestListHosts(t *testing.T) { |
| 100 | + sampleHosts := []model.HostSpec{ |
| 101 | + { |
| 102 | + BaseModel: &model.BaseModel{ |
| 103 | + Id: "202964b5-8e73-46fd-b41b-a8e403f3c30b", |
| 104 | + CreatedAt: "2019-11-11T11:01:33", |
| 105 | + }, |
| 106 | + TenantId: "x", |
| 107 | + AccessMode: "agentless", |
| 108 | + HostName: "sap1", |
| 109 | + IP: "192.168.56.12", |
| 110 | + AvailabilityZones: []string{"az1", "az2"}, |
| 111 | + Initiators: []*model.Initiator{ |
| 112 | + &model.Initiator{ |
| 113 | + PortName: "20000024ff5bb888", |
| 114 | + Protocol: "iscsi", |
| 115 | + }, |
| 116 | + &model.Initiator{ |
| 117 | + PortName: "20000024ff5bc999", |
| 118 | + Protocol: "iscsi", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + { |
| 123 | + BaseModel: &model.BaseModel{ |
| 124 | + Id: "eb73e59a-8b0f-4517-8b95-023ec134aec9", |
| 125 | + CreatedAt: "2019-11-11T11:13:57", |
| 126 | + }, |
| 127 | + TenantId: "x", |
| 128 | + AccessMode: "agentless", |
| 129 | + HostName: "sap2", |
| 130 | + IP: "192.168.56.13", |
| 131 | + AvailabilityZones: []string{"az1", "az2"}, |
| 132 | + Initiators: []*model.Initiator{ |
| 133 | + &model.Initiator{ |
| 134 | + PortName: "20012324ff5ac132", |
| 135 | + Protocol: "iscsi", |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + var expected []*model.HostSpec |
| 142 | + expected = append(expected, &sampleHosts[0]) |
| 143 | + expected = append(expected, &sampleHosts[1]) |
| 144 | + hosts, err := fakeHostMgr.ListHosts(map[string]string{"limit": "0", "offset": "10"}) |
| 145 | + |
| 146 | + if err != nil { |
| 147 | + t.Error(err) |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + if !reflect.DeepEqual(hosts, expected) { |
| 152 | + t.Errorf("expected %v, got %v", expected, hosts) |
| 153 | + return |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func TestUpdateHost(t *testing.T) { |
| 158 | + hostID := "202964b5-8e73-46fd-b41b-a8e403f3c30b" |
| 159 | + host := &model.HostSpec{ |
| 160 | + HostName: "sap1-updated", |
| 161 | + } |
| 162 | + |
| 163 | + result, err := fakeHostMgr.UpdateHost(hostID, host) |
| 164 | + if err != nil { |
| 165 | + t.Error(err) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + expected := &model.HostSpec{ |
| 170 | + BaseModel: &model.BaseModel{ |
| 171 | + Id: "202964b5-8e73-46fd-b41b-a8e403f3c30b", |
| 172 | + CreatedAt: "2019-11-11T11:01:33", |
| 173 | + }, |
| 174 | + TenantId: "x", |
| 175 | + AccessMode: "agentless", |
| 176 | + HostName: "sap1", |
| 177 | + IP: "192.168.56.12", |
| 178 | + AvailabilityZones: []string{"az1", "az2"}, |
| 179 | + Initiators: []*model.Initiator{ |
| 180 | + &model.Initiator{ |
| 181 | + PortName: "20000024ff5bb888", |
| 182 | + Protocol: "iscsi", |
| 183 | + }, |
| 184 | + &model.Initiator{ |
| 185 | + PortName: "20000024ff5bc999", |
| 186 | + Protocol: "iscsi", |
| 187 | + }, |
| 188 | + }, |
| 189 | + } |
| 190 | + |
| 191 | + if !reflect.DeepEqual(result, expected) { |
| 192 | + t.Errorf("expected %v, got %v", expected, result) |
| 193 | + return |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func TestDeleteHost(t *testing.T) { |
| 198 | + var hostID = "d202964b5-8e73-46fd-b41b-a8e403f3c30b" |
| 199 | + |
| 200 | + if err := fakeHostMgr.DeleteHost(hostID); err != nil { |
| 201 | + t.Error(err) |
| 202 | + return |
| 203 | + } |
| 204 | +} |
0 commit comments