|
17 | 17 | 'use strict'; |
18 | 18 |
|
19 | 19 | var assert = require('assert'); |
| 20 | +var extend = require('extend'); |
| 21 | +var format = require('string-format-obj'); |
| 22 | + |
| 23 | +var Network = require('../../lib/compute/network.js'); |
20 | 24 |
|
21 | 25 | describe('Network', function() { |
| 26 | + var network; |
| 27 | + |
| 28 | + var COMPUTE = { projectId: 'project-id' }; |
| 29 | + var NETWORK_NAME = 'network-name'; |
| 30 | + var NETWORK_FULL_NAME = format('projects/{pId}/global/networks/{name}', { |
| 31 | + pId: COMPUTE.projectId, |
| 32 | + name: NETWORK_NAME |
| 33 | + }); |
| 34 | + |
| 35 | + beforeEach(function() { |
| 36 | + network = new Network(COMPUTE, NETWORK_NAME); |
| 37 | + }); |
| 38 | + |
22 | 39 | describe('instantiation', function() { |
23 | | - assert(true); |
| 40 | + it('should localize the compute instance', function() { |
| 41 | + assert.strictEqual(network.compute, COMPUTE); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should localize the name', function() { |
| 45 | + assert.strictEqual(network.name, NETWORK_NAME); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should default metadata to an empty object', function() { |
| 49 | + assert.strictEqual(typeof network.metadata, 'object'); |
| 50 | + assert.strictEqual(Object.keys(network.metadata).length, 0); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should format the network name', function() { |
| 54 | + var formatName_ = Network.formatName_; |
| 55 | + var formattedName = 'projects/a/global/networks/b'; |
| 56 | + |
| 57 | + Network.formatName_ = function(compute, name) { |
| 58 | + Network.formatName_ = formatName_; |
| 59 | + |
| 60 | + assert.strictEqual(compute, COMPUTE); |
| 61 | + assert.strictEqual(name, NETWORK_NAME); |
| 62 | + |
| 63 | + return formattedName; |
| 64 | + }; |
| 65 | + |
| 66 | + var network = new Network(COMPUTE, NETWORK_NAME); |
| 67 | + assert(network.formattedName, formattedName); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('formatName_', function() { |
| 72 | + it('should format the name', function() { |
| 73 | + var formattedName_ = Network.formatName_(COMPUTE, NETWORK_NAME); |
| 74 | + assert.strictEqual(formattedName_, NETWORK_FULL_NAME); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('createFirewall', function() { |
| 79 | + it('should make the correct call to Compute', function(done) { |
| 80 | + var name = 'firewall-name'; |
| 81 | + var config = { a: 'b', c: 'd' }; |
| 82 | + var expectedConfig = extend({}, config, { |
| 83 | + network: network.formattedName |
| 84 | + }); |
| 85 | + |
| 86 | + network.compute.createFirewall = function(name_, config_, callback) { |
| 87 | + assert.strictEqual(name_, name); |
| 88 | + assert.deepEqual(config_, expectedConfig); |
| 89 | + callback(); |
| 90 | + }; |
| 91 | + |
| 92 | + network.createFirewall(name, config, done); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('delete', function() { |
| 97 | + it('should make the correct API request', function(done) { |
| 98 | + network.makeReq_ = function(method, path, query, body) { |
| 99 | + assert.strictEqual(method, 'DELETE'); |
| 100 | + assert.strictEqual(path, ''); |
| 101 | + assert.strictEqual(query, null); |
| 102 | + assert.strictEqual(body, null); |
| 103 | + done(); |
| 104 | + }; |
| 105 | + |
| 106 | + network.delete(assert.ifError); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('error', function() { |
| 110 | + var error = new Error('Error.'); |
| 111 | + var apiResponse = { a: 'b', c: 'd' }; |
| 112 | + |
| 113 | + beforeEach(function() { |
| 114 | + network.makeReq_ = function(method, path, query, body, callback) { |
| 115 | + callback(error, apiResponse); |
| 116 | + }; |
| 117 | + }); |
| 118 | + |
| 119 | + it('should return an error if the request fails', function(done) { |
| 120 | + network.delete(function(err, operation, apiResponse_) { |
| 121 | + assert.strictEqual(err, error); |
| 122 | + assert.strictEqual(operation, null); |
| 123 | + assert.strictEqual(apiResponse_, apiResponse); |
| 124 | + done(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should not require a callback', function() { |
| 129 | + assert.doesNotThrow(function() { |
| 130 | + network.delete(); |
| 131 | + }); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('success', function() { |
| 136 | + var apiResponse = { |
| 137 | + name: 'op-name' |
| 138 | + }; |
| 139 | + |
| 140 | + beforeEach(function() { |
| 141 | + network.makeReq_ = function(method, path, query, body, callback) { |
| 142 | + callback(null, apiResponse); |
| 143 | + }; |
| 144 | + }); |
| 145 | + |
| 146 | + it('should execute callback with Operation & Response', function(done) { |
| 147 | + var operation = {}; |
| 148 | + |
| 149 | + network.compute.operation = function(name) { |
| 150 | + assert.strictEqual(name, apiResponse.name); |
| 151 | + return operation; |
| 152 | + }; |
| 153 | + |
| 154 | + network.delete(function(err, operation_, apiResponse_) { |
| 155 | + assert.ifError(err); |
| 156 | + assert.strictEqual(operation_, operation); |
| 157 | + assert.strictEqual(operation_.metadata, apiResponse); |
| 158 | + assert.strictEqual(apiResponse_, apiResponse); |
| 159 | + done(); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should not require a callback', function() { |
| 164 | + assert.doesNotThrow(function() { |
| 165 | + network.delete(); |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + }); |
| 170 | + |
| 171 | + describe('firewall', function() { |
| 172 | + it('should return a Firewall with the correct metadata', function() { |
| 173 | + var name = 'firewall-name'; |
| 174 | + var firewall = {}; |
| 175 | + |
| 176 | + network.compute.firewall = function(name_) { |
| 177 | + assert.strictEqual(name_, name); |
| 178 | + return firewall; |
| 179 | + }; |
| 180 | + |
| 181 | + var firewallInstance = network.firewall(name); |
| 182 | + assert.deepEqual(firewallInstance.metadata, { |
| 183 | + network: network.formattedName |
| 184 | + }); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('getFirewalls', function() { |
| 189 | + it('should make the correct call to Compute', function(done) { |
| 190 | + var options = { a: 'b', c: 'd' }; |
| 191 | + var expectedOptions = extend({}, options, { |
| 192 | + filter: 'network eq .*' + network.formattedName |
| 193 | + }); |
| 194 | + |
| 195 | + network.compute.getFirewalls = function(options, callback) { |
| 196 | + assert.deepEqual(options, expectedOptions); |
| 197 | + callback(); |
| 198 | + }; |
| 199 | + |
| 200 | + network.getFirewalls(options, done); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should not require options', function(done) { |
| 204 | + network.compute.getFirewalls = function(options, callback) { |
| 205 | + callback(); |
| 206 | + }; |
| 207 | + |
| 208 | + network.getFirewalls(done); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should not require any arguments', function(done) { |
| 212 | + network.compute.getFirewalls = function(options, callback) { |
| 213 | + assert.deepEqual(options, { |
| 214 | + filter: 'network eq .*' + network.formattedName |
| 215 | + }); |
| 216 | + assert.strictEqual(typeof callback, 'undefined'); |
| 217 | + done(); |
| 218 | + }; |
| 219 | + |
| 220 | + network.getFirewalls(); |
| 221 | + }); |
| 222 | + |
| 223 | + it('should return the result of calling Compute', function() { |
| 224 | + var resultOfGetFirewalls = {}; |
| 225 | + |
| 226 | + network.compute.getFirewalls = function() { |
| 227 | + return resultOfGetFirewalls; |
| 228 | + }; |
| 229 | + |
| 230 | + assert.strictEqual(network.getFirewalls(), resultOfGetFirewalls); |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + describe('getMetadata', function() { |
| 235 | + it('should make the correct API request', function(done) { |
| 236 | + network.makeReq_ = function(method, path, query, body) { |
| 237 | + assert.strictEqual(method, 'GET'); |
| 238 | + assert.strictEqual(path, ''); |
| 239 | + assert.strictEqual(query, null); |
| 240 | + assert.strictEqual(body, null); |
| 241 | + |
| 242 | + done(); |
| 243 | + }; |
| 244 | + |
| 245 | + network.getMetadata(assert.ifError); |
| 246 | + }); |
| 247 | + |
| 248 | + describe('error', function() { |
| 249 | + var error = new Error('Error.'); |
| 250 | + var apiResponse = { a: 'b', c: 'd' }; |
| 251 | + |
| 252 | + beforeEach(function() { |
| 253 | + network.makeReq_ = function(method, path, query, body, callback) { |
| 254 | + callback(error, apiResponse); |
| 255 | + }; |
| 256 | + }); |
| 257 | + |
| 258 | + it('should execute callback with error and API response', function(done) { |
| 259 | + network.getMetadata(function(err, metadata, apiResponse_) { |
| 260 | + assert.strictEqual(err, error); |
| 261 | + assert.strictEqual(metadata, null); |
| 262 | + assert.strictEqual(apiResponse_, apiResponse); |
| 263 | + done(); |
| 264 | + }); |
| 265 | + }); |
| 266 | + |
| 267 | + it('should not require a callback', function() { |
| 268 | + assert.doesNotThrow(function() { |
| 269 | + network.getMetadata(); |
| 270 | + }); |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
| 274 | + describe('success', function() { |
| 275 | + var apiResponse = { a: 'b', c: 'd' }; |
| 276 | + |
| 277 | + beforeEach(function() { |
| 278 | + network.makeReq_ = function(method, path, query, body, callback) { |
| 279 | + callback(null, apiResponse); |
| 280 | + }; |
| 281 | + }); |
| 282 | + |
| 283 | + it('should update the metadata to the API response', function(done) { |
| 284 | + network.getMetadata(function(err) { |
| 285 | + assert.ifError(err); |
| 286 | + assert.strictEqual(network.metadata, apiResponse); |
| 287 | + done(); |
| 288 | + }); |
| 289 | + }); |
| 290 | + |
| 291 | + it('should exec callback with metadata and API response', function(done) { |
| 292 | + network.getMetadata(function(err, metadata, apiResponse_) { |
| 293 | + assert.ifError(err); |
| 294 | + assert.strictEqual(metadata, apiResponse); |
| 295 | + assert.strictEqual(apiResponse_, apiResponse); |
| 296 | + done(); |
| 297 | + }); |
| 298 | + }); |
| 299 | + |
| 300 | + it('should not require a callback', function() { |
| 301 | + assert.doesNotThrow(function() { |
| 302 | + network.getMetadata(); |
| 303 | + }); |
| 304 | + }); |
| 305 | + }); |
| 306 | + }); |
| 307 | + |
| 308 | + describe('makeReq_', function() { |
| 309 | + it('should make the correct request to Compute', function(done) { |
| 310 | + var expectedPathPrefix = '/global/networks/' + network.name; |
| 311 | + |
| 312 | + var method = 'POST'; |
| 313 | + var path = '/test'; |
| 314 | + var query = { |
| 315 | + a: 'b', |
| 316 | + c: 'd' |
| 317 | + }; |
| 318 | + var body = { |
| 319 | + a: 'b', |
| 320 | + c: 'd' |
| 321 | + }; |
| 322 | + |
| 323 | + network.compute.makeReq_ = function(method_, path_, query_, body_, cb) { |
| 324 | + assert.strictEqual(method_, method); |
| 325 | + assert.strictEqual(path_, expectedPathPrefix + path); |
| 326 | + assert.strictEqual(query_, query); |
| 327 | + assert.strictEqual(body_, body); |
| 328 | + cb(); |
| 329 | + }; |
| 330 | + |
| 331 | + network.makeReq_(method, path, query, body, done); |
| 332 | + }); |
24 | 333 | }); |
25 | 334 | }); |
0 commit comments