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 | */ |
| 16 | package grpc |
| 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" |
| 27 | "google.golang.org/grpc/metadata" |
| 28 | "google.golang.org/grpc/status" |
| 29 | "os" |
| 30 | "testing" |
| 31 | ) |
| 32 | |
| 33 | var conn *grpc.ClientConn |
| 34 | var stub voltha.VolthaServiceClient |
| 35 | var testMode string |
| 36 | |
| 37 | func setup() { |
| 38 | var err error |
| 39 | if _, err = log.SetLogger(log.JSON, 3, log.Fields{"instanceId": "testing"}); err != nil { |
| 40 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 41 | } |
| 42 | conn, err = grpc.Dial("localhost:50057", grpc.WithInsecure()) |
| 43 | if err != nil { |
| 44 | log.Fatalf("did not connect: %s", err) |
| 45 | } |
| 46 | |
| 47 | stub = voltha.NewVolthaServiceClient(conn) |
| 48 | testMode = common.TestModeKeys_api_test.String() |
| 49 | } |
| 50 | |
| 51 | func TestGetDevice(t *testing.T) { |
| 52 | var id common.ID |
| 53 | id.Id = "anyid" |
| 54 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 55 | response, err := stub.GetDevice(ctx, &id) |
| 56 | assert.Nil(t, response) |
| 57 | st, _ := status.FromError(err) |
| 58 | assert.Equal(t, "UnImplemented", st.Message()) |
| 59 | |
| 60 | } |
| 61 | |
| 62 | func TestUpdateLogLevelError(t *testing.T) { |
| 63 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 64 | level := voltha.Logging{Level: common.LogLevel_ERROR} |
| 65 | response, err := stub.UpdateLogLevel(ctx, &level) |
| 66 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 67 | assert.Equal(t, &empty.Empty{}, response) |
| 68 | assert.Nil(t, err) |
| 69 | } |
| 70 | |
| 71 | func TestGetVoltha(t *testing.T) { |
| 72 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 73 | response, err := stub.GetVoltha(ctx, &empty.Empty{}) |
| 74 | assert.Nil(t, response) |
| 75 | st, _ := status.FromError(err) |
| 76 | assert.Equal(t, "UnImplemented", st.Message()) |
| 77 | } |
| 78 | |
| 79 | func TestUpdateLogLevelDebug(t *testing.T) { |
| 80 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 81 | level := voltha.Logging{Level: common.LogLevel_DEBUG} |
| 82 | response, err := stub.UpdateLogLevel(ctx, &level) |
| 83 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 84 | assert.Equal(t, &empty.Empty{}, response) |
| 85 | assert.Nil(t, err) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | func TestGetCoreInstance(t *testing.T) { |
| 90 | id := &voltha.ID{Id: "getCoreInstance"} |
| 91 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 92 | response, err := stub.GetCoreInstance(ctx, id) |
| 93 | assert.Nil(t, response) |
| 94 | st, _ := status.FromError(err) |
| 95 | assert.Equal(t, "UnImplemented", st.Message()) |
| 96 | } |
| 97 | |
| 98 | func TestGetLogicalDevice(t *testing.T) { |
| 99 | id := &voltha.ID{Id: "getLogicalDevice"} |
| 100 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 101 | response, err := stub.GetLogicalDevice(ctx, id) |
| 102 | assert.Nil(t, response) |
| 103 | st, _ := status.FromError(err) |
| 104 | assert.Equal(t, "UnImplemented", st.Message()) |
| 105 | } |
| 106 | |
| 107 | func TestGetLogicalDevicePort(t *testing.T) { |
| 108 | id := &voltha.LogicalPortId{Id: "GetLogicalDevicePort"} |
| 109 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 110 | response, err := stub.GetLogicalDevicePort(ctx, id) |
| 111 | assert.Nil(t, response) |
| 112 | st, _ := status.FromError(err) |
| 113 | assert.Equal(t, "UnImplemented", st.Message()) |
| 114 | } |
| 115 | |
| 116 | func TestListLogicalDevicePorts(t *testing.T) { |
| 117 | id := &voltha.ID{Id: "listLogicalDevicePorts"} |
| 118 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 119 | response, err := stub.ListLogicalDevicePorts(ctx, id) |
| 120 | assert.Nil(t, response) |
| 121 | st, _ := status.FromError(err) |
| 122 | assert.Equal(t, "UnImplemented", st.Message()) |
| 123 | } |
| 124 | |
| 125 | func TestListLogicalDeviceFlows(t *testing.T) { |
| 126 | id := &voltha.ID{Id: "ListLogicalDeviceFlows"} |
| 127 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 128 | response, err := stub.ListLogicalDeviceFlows(ctx, id) |
| 129 | assert.Nil(t, response) |
| 130 | st, _ := status.FromError(err) |
| 131 | assert.Equal(t, "UnImplemented", st.Message()) |
| 132 | } |
| 133 | |
| 134 | func TestListLogicalDeviceFlowGroups(t *testing.T) { |
| 135 | id := &voltha.ID{Id: "ListLogicalDeviceFlowGroups"} |
| 136 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 137 | response, err := stub.ListLogicalDeviceFlowGroups(ctx, id) |
| 138 | assert.Nil(t, response) |
| 139 | st, _ := status.FromError(err) |
| 140 | assert.Equal(t, "UnImplemented", st.Message()) |
| 141 | } |
| 142 | |
| 143 | func TestListDevices(t *testing.T) { |
| 144 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 145 | response, err := stub.ListDevices(ctx, &empty.Empty{}) |
| 146 | assert.Nil(t, response) |
| 147 | st, _ := status.FromError(err) |
| 148 | assert.Equal(t, "UnImplemented", st.Message()) |
| 149 | } |
| 150 | |
| 151 | func TestListAdapters(t *testing.T) { |
| 152 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 153 | response, err := stub.ListAdapters(ctx, &empty.Empty{}) |
| 154 | assert.Nil(t, response) |
| 155 | st, _ := status.FromError(err) |
| 156 | assert.Equal(t, "UnImplemented", st.Message()) |
| 157 | } |
| 158 | |
| 159 | func TestListLogicalDevices(t *testing.T) { |
| 160 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 161 | response, err := stub.ListLogicalDevices(ctx, &empty.Empty{}) |
| 162 | assert.Nil(t, response) |
| 163 | st, _ := status.FromError(err) |
| 164 | assert.Equal(t, "UnImplemented", st.Message()) |
| 165 | } |
| 166 | |
| 167 | func TestListCoreInstances(t *testing.T) { |
| 168 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 169 | response, err := stub.ListCoreInstances(ctx, &empty.Empty{}) |
| 170 | assert.Nil(t, response) |
| 171 | st, _ := status.FromError(err) |
| 172 | assert.Equal(t, "UnImplemented", st.Message()) |
| 173 | } |
| 174 | |
| 175 | func TestCreateDevice(t *testing.T) { |
| 176 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 177 | device := &voltha.Device{Id: "newdevice"} |
| 178 | response, err := stub.CreateDevice(ctx, device) |
| 179 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 180 | assert.Equal(t, &voltha.Device{Id: "newdevice"}, response) |
| 181 | assert.Nil(t, err) |
| 182 | } |
| 183 | |
| 184 | func TestEnableDevice(t *testing.T) { |
| 185 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 186 | id := &voltha.ID{Id: "enabledevice"} |
| 187 | response, err := stub.EnableDevice(ctx, id) |
| 188 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 189 | assert.Equal(t, &empty.Empty{}, response) |
| 190 | assert.Nil(t, err) |
| 191 | } |
| 192 | |
| 193 | func TestDisableDevice(t *testing.T) { |
| 194 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 195 | id := &voltha.ID{Id: "DisableDevice"} |
| 196 | response, err := stub.DisableDevice(ctx, id) |
| 197 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 198 | assert.Equal(t, &empty.Empty{}, response) |
| 199 | assert.Nil(t, err) |
| 200 | } |
| 201 | |
| 202 | func TestRebootDevice(t *testing.T) { |
| 203 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 204 | id := &voltha.ID{Id: "RebootDevice"} |
| 205 | response, err := stub.RebootDevice(ctx, id) |
| 206 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 207 | assert.Equal(t, &empty.Empty{}, response) |
| 208 | assert.Nil(t, err) |
| 209 | } |
| 210 | |
| 211 | func TestDeleteDevice(t *testing.T) { |
| 212 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 213 | id := &voltha.ID{Id: "DeleteDevice"} |
| 214 | response, err := stub.DeleteDevice(ctx, id) |
| 215 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 216 | assert.Equal(t, &empty.Empty{}, response) |
| 217 | assert.Nil(t, err) |
| 218 | } |
| 219 | |
| 220 | func TestEnableLogicalDevicePort(t *testing.T) { |
| 221 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 222 | id := &voltha.LogicalPortId{Id: "EnableLogicalDevicePort"} |
| 223 | response, err := stub.EnableLogicalDevicePort(ctx, id) |
| 224 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 225 | assert.Equal(t, &empty.Empty{}, response) |
| 226 | assert.Nil(t, err) |
| 227 | } |
| 228 | |
| 229 | func TestDisableLogicalDevicePort(t *testing.T) { |
| 230 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 231 | id := &voltha.LogicalPortId{Id: "DisableLogicalDevicePort"} |
| 232 | response, err := stub.DisableLogicalDevicePort(ctx, id) |
| 233 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 234 | assert.Equal(t, &empty.Empty{}, response) |
| 235 | assert.Nil(t, err) |
| 236 | } |
| 237 | |
| 238 | func TestUpdateLogicalDeviceFlowGroupTable(t *testing.T) { |
| 239 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 240 | flow := &openflow_13.FlowGroupTableUpdate{Id: "UpdateLogicalDeviceFlowGroupTable"} |
| 241 | response, err := stub.UpdateLogicalDeviceFlowGroupTable(ctx, flow) |
| 242 | log.Infow("response", log.Fields{"res": response, "error": err}) |
| 243 | assert.Equal(t, &empty.Empty{}, response) |
| 244 | assert.Nil(t, err) |
| 245 | } |
| 246 | |
| 247 | func TestGetImageDownloadStatus(t *testing.T) { |
| 248 | ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs(testMode, "true")) |
| 249 | img := &voltha.ImageDownload{Id: "GetImageDownloadStatus"} |
| 250 | response, err := stub.GetImageDownloadStatus(ctx, img) |
| 251 | assert.Nil(t, response) |
| 252 | st, _ := status.FromError(err) |
| 253 | assert.Equal(t, "UnImplemented", st.Message()) |
| 254 | } |
| 255 | |
| 256 | // TODO: complete the remaining tests |
| 257 | |
| 258 | func shutdown() { |
| 259 | conn.Close() |
| 260 | } |
| 261 | |
| 262 | func TestMain(m *testing.M) { |
| 263 | setup() |
| 264 | code := m.Run() |
| 265 | shutdown() |
| 266 | os.Exit(code) |
| 267 | } |