khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 16 | package core |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "github.com/golang/protobuf/ptypes/empty" |
| 21 | "github.com/opencord/voltha-go/common/log" |
| 22 | "github.com/opencord/voltha-go/protos/common" |
| 23 | "github.com/opencord/voltha-go/protos/openflow_13" |
| 24 | "github.com/opencord/voltha-go/protos/voltha" |
| 25 | "github.com/stretchr/testify/assert" |
| 26 | "google.golang.org/grpc" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 28 | "google.golang.org/grpc/metadata" |
| 29 | "google.golang.org/grpc/status" |
| 30 | "os" |
| 31 | "testing" |
| 32 | ) |
| 33 | |
| 34 | var conn *grpc.ClientConn |
| 35 | var stub voltha.VolthaServiceClient |
| 36 | var testMode string |
| 37 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 38 | /* |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame^] | 39 | Prerequite: These tests require the rw_core to run prior to executing these test cases. |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | */ |
| 41 | |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame^] | 42 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 43 | func setup() { |
| 44 | var err error |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 45 | |
| 46 | if err = log.AddPackage(log.JSON, log.WarnLevel, log.Fields{"instanceId": "testing"}); err != nil { |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 47 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 48 | } |
| 49 | conn, err = grpc.Dial("localhost:50057", grpc.WithInsecure()) |
| 50 | if err != nil { |
| 51 | log.Fatalf("did not connect: %s", err) |
| 52 | } |
| 53 | |
| 54 | stub = voltha.NewVolthaServiceClient(conn) |
| 55 | testMode = common.TestModeKeys_api_test.String() |
| 56 | } |
| 57 | |
| 58 | func TestGetDevice(t *testing.T) { |
| 59 | var id common.ID |
| 60 | id.Id = "anyid" |
| 61 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 62 | response, err := stub.GetDevice(ctx, &id) |
| 63 | assert.Nil(t, response) |
| 64 | st, _ := status.FromError(err) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 65 | assert.Equal(t, id.Id, st.Message()) |
| 66 | assert.Equal(t, codes.NotFound, st.Code()) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func TestUpdateLogLevelError(t *testing.T) { |
| 70 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 71 | level := voltha.Logging{PackageName: "github.com/opencord/voltha-go/rw_core/core", Level: common.LogLevel_ERROR} |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 72 | response, err := stub.UpdateLogLevel(ctx, &level) |
| 73 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 74 | assert.Equal(t, &empty.Empty{}, response) |
| 75 | assert.Nil(t, err) |
| 76 | } |
| 77 | |
| 78 | func TestGetVoltha(t *testing.T) { |
| 79 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 80 | response, err := stub.GetVoltha(ctx, &empty.Empty{}) |
| 81 | assert.Nil(t, response) |
| 82 | st, _ := status.FromError(err) |
| 83 | assert.Equal(t, "UnImplemented", st.Message()) |
| 84 | } |
| 85 | |
| 86 | func TestUpdateLogLevelDebug(t *testing.T) { |
| 87 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 88 | level := voltha.Logging{PackageName: "github.com/opencord/voltha-go/rw_core/core", Level: common.LogLevel_DEBUG} |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 89 | response, err := stub.UpdateLogLevel(ctx, &level) |
| 90 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 91 | assert.Equal(t, &empty.Empty{}, response) |
| 92 | assert.Nil(t, err) |
| 93 | } |
| 94 | |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 95 | func TestGetCoreInstance(t *testing.T) { |
| 96 | id := &voltha.ID{Id: "getCoreInstance"} |
| 97 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 98 | response, err := stub.GetCoreInstance(ctx, id) |
| 99 | assert.Nil(t, response) |
| 100 | st, _ := status.FromError(err) |
| 101 | assert.Equal(t, "UnImplemented", st.Message()) |
| 102 | } |
| 103 | |
| 104 | func TestGetLogicalDevice(t *testing.T) { |
| 105 | id := &voltha.ID{Id: "getLogicalDevice"} |
| 106 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 107 | response, err := stub.GetLogicalDevice(ctx, id) |
| 108 | assert.Nil(t, response) |
| 109 | st, _ := status.FromError(err) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 110 | assert.Equal(t, id.Id, st.Message()) |
| 111 | assert.Equal(t, codes.NotFound, st.Code()) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | func TestGetLogicalDevicePort(t *testing.T) { |
| 115 | id := &voltha.LogicalPortId{Id: "GetLogicalDevicePort"} |
| 116 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 117 | response, err := stub.GetLogicalDevicePort(ctx, id) |
| 118 | assert.Nil(t, response) |
| 119 | st, _ := status.FromError(err) |
| 120 | assert.Equal(t, "UnImplemented", st.Message()) |
| 121 | } |
| 122 | |
| 123 | func TestListLogicalDevicePorts(t *testing.T) { |
| 124 | id := &voltha.ID{Id: "listLogicalDevicePorts"} |
| 125 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 126 | response, err := stub.ListLogicalDevicePorts(ctx, id) |
| 127 | assert.Nil(t, response) |
| 128 | st, _ := status.FromError(err) |
| 129 | assert.Equal(t, "UnImplemented", st.Message()) |
| 130 | } |
| 131 | |
| 132 | func TestListLogicalDeviceFlows(t *testing.T) { |
| 133 | id := &voltha.ID{Id: "ListLogicalDeviceFlows"} |
| 134 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 135 | response, err := stub.ListLogicalDeviceFlows(ctx, id) |
| 136 | assert.Nil(t, response) |
| 137 | st, _ := status.FromError(err) |
| 138 | assert.Equal(t, "UnImplemented", st.Message()) |
| 139 | } |
| 140 | |
| 141 | func TestListLogicalDeviceFlowGroups(t *testing.T) { |
| 142 | id := &voltha.ID{Id: "ListLogicalDeviceFlowGroups"} |
| 143 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 144 | response, err := stub.ListLogicalDeviceFlowGroups(ctx, id) |
| 145 | assert.Nil(t, response) |
| 146 | st, _ := status.FromError(err) |
| 147 | assert.Equal(t, "UnImplemented", st.Message()) |
| 148 | } |
| 149 | |
| 150 | func TestListDevices(t *testing.T) { |
| 151 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 152 | response, _ := stub.ListDevices(ctx, &empty.Empty{}) |
| 153 | assert.Equal(t, len(response.Items), 0) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | func TestListAdapters(t *testing.T) { |
| 157 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 158 | response, err := stub.ListAdapters(ctx, &empty.Empty{}) |
| 159 | assert.Nil(t, response) |
| 160 | st, _ := status.FromError(err) |
| 161 | assert.Equal(t, "UnImplemented", st.Message()) |
| 162 | } |
| 163 | |
| 164 | func TestListLogicalDevices(t *testing.T) { |
| 165 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 166 | response, _ := stub.ListLogicalDevices(ctx, &empty.Empty{}) |
| 167 | assert.Equal(t, len(response.Items), 0) |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | func TestListCoreInstances(t *testing.T) { |
| 171 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 172 | response, err := stub.ListCoreInstances(ctx, &empty.Empty{}) |
| 173 | assert.Nil(t, response) |
| 174 | st, _ := status.FromError(err) |
| 175 | assert.Equal(t, "UnImplemented", st.Message()) |
| 176 | } |
| 177 | |
| 178 | func TestCreateDevice(t *testing.T) { |
| 179 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 180 | device := &voltha.Device{Id: "newdevice"} |
| 181 | response, err := stub.CreateDevice(ctx, device) |
| 182 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 183 | assert.Equal(t, &voltha.Device{Id: "newdevice"}, response) |
| 184 | assert.Nil(t, err) |
| 185 | } |
| 186 | |
| 187 | func TestEnableDevice(t *testing.T) { |
| 188 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 189 | id := &voltha.ID{Id: "enabledevice"} |
| 190 | response, err := stub.EnableDevice(ctx, id) |
| 191 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 192 | assert.Equal(t, &empty.Empty{}, response) |
| 193 | assert.Nil(t, err) |
| 194 | } |
| 195 | |
| 196 | func TestDisableDevice(t *testing.T) { |
| 197 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 198 | id := &voltha.ID{Id: "DisableDevice"} |
| 199 | response, err := stub.DisableDevice(ctx, id) |
| 200 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 201 | assert.Equal(t, &empty.Empty{}, response) |
| 202 | assert.Nil(t, err) |
| 203 | } |
| 204 | |
| 205 | func TestRebootDevice(t *testing.T) { |
| 206 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 207 | id := &voltha.ID{Id: "RebootDevice"} |
| 208 | response, err := stub.RebootDevice(ctx, id) |
| 209 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 210 | assert.Equal(t, &empty.Empty{}, response) |
| 211 | assert.Nil(t, err) |
| 212 | } |
| 213 | |
| 214 | func TestDeleteDevice(t *testing.T) { |
| 215 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 216 | id := &voltha.ID{Id: "DeleteDevice"} |
| 217 | response, err := stub.DeleteDevice(ctx, id) |
| 218 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 219 | assert.Equal(t, &empty.Empty{}, response) |
| 220 | assert.Nil(t, err) |
| 221 | } |
| 222 | |
| 223 | func TestEnableLogicalDevicePort(t *testing.T) { |
| 224 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 225 | id := &voltha.LogicalPortId{Id: "EnableLogicalDevicePort"} |
| 226 | response, err := stub.EnableLogicalDevicePort(ctx, id) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 227 | if e, ok := status.FromError(err); ok { |
| 228 | log.Infow("response", log.Fields{"error": err, "errorcode": e.Code(), "msg": e.Message()}) |
| 229 | } |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 230 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 231 | assert.Equal(t, &empty.Empty{}, response) |
| 232 | assert.Nil(t, err) |
| 233 | } |
| 234 | |
| 235 | func TestDisableLogicalDevicePort(t *testing.T) { |
| 236 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 237 | id := &voltha.LogicalPortId{Id: "DisableLogicalDevicePort"} |
| 238 | response, err := stub.DisableLogicalDevicePort(ctx, id) |
| 239 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 240 | assert.Equal(t, &empty.Empty{}, response) |
| 241 | assert.Nil(t, err) |
| 242 | } |
| 243 | |
| 244 | func TestUpdateLogicalDeviceFlowGroupTable(t *testing.T) { |
| 245 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 246 | flow := &openflow_13.FlowGroupTableUpdate{Id: "UpdateLogicalDeviceFlowGroupTable"} |
| 247 | response, err := stub.UpdateLogicalDeviceFlowGroupTable(ctx, flow) |
| 248 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 249 | assert.Equal(t, &empty.Empty{}, response) |
| 250 | assert.Nil(t, err) |
| 251 | } |
| 252 | |
| 253 | func TestGetImageDownloadStatus(t *testing.T) { |
| 254 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 255 | img := &voltha.ImageDownload{Id: "GetImageDownloadStatus"} |
| 256 | response, err := stub.GetImageDownloadStatus(ctx, img) |
| 257 | assert.Nil(t, response) |
| 258 | st, _ := status.FromError(err) |
| 259 | assert.Equal(t, "UnImplemented", st.Message()) |
| 260 | } |
| 261 | |
| 262 | // TODO: complete the remaining tests |
| 263 | |
| 264 | func shutdown() { |
| 265 | conn.Close() |
| 266 | } |
| 267 | |
| 268 | func TestMain(m *testing.M) { |
| 269 | setup() |
| 270 | code := m.Run() |
| 271 | shutdown() |
| 272 | os.Exit(code) |
| 273 | } |