Skip to content

Commit 59570b9

Browse files
compute.snapshot
1 parent eb071a8 commit 59570b9

File tree

2 files changed

+65
-116
lines changed

2 files changed

+65
-116
lines changed

lib/compute/snapshot.js

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ function Snapshot(scope, name) {
152152
ServiceObject.call(this, config);
153153

154154
this.compute = isDisk ? scope.compute : scope;
155+
this.name = name;
155156
}
156157

157158
nodeutil.inherits(Snapshot, ServiceObject);

test/compute/snapshot.js

+64-116
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,39 @@
1717
'use strict';
1818

1919
var assert = require('assert');
20-
var Snapshot = require('../../lib/compute/snapshot.js');
20+
var mockery = require('mockery');
21+
var nodeutil = require('util');
22+
23+
var ServiceObject = require('../../lib/common/service-object.js');
24+
25+
function FakeServiceObject() {
26+
this.calledWith_ = arguments;
27+
ServiceObject.apply(this, arguments);
28+
}
29+
30+
nodeutil.inherits(FakeServiceObject, ServiceObject);
2131

2232
describe('Snapshot', function() {
33+
var Snapshot;
34+
var snapshot;
35+
2336
var COMPUTE = {};
2437
var SNAPSHOT_NAME = 'snapshot-name';
2538

26-
var snapshot;
39+
before(function() {
40+
mockery.registerMock('../common/service-object.js', FakeServiceObject);
41+
mockery.enable({
42+
useCleanCache: true,
43+
warnOnUnregistered: false
44+
});
45+
46+
Snapshot = require('../../lib/compute/snapshot.js');
47+
});
48+
49+
after(function() {
50+
mockery.deregisterAll();
51+
mockery.disable();
52+
});
2753

2854
beforeEach(function() {
2955
snapshot = new Snapshot(COMPUTE, SNAPSHOT_NAME);
@@ -38,32 +64,55 @@ describe('Snapshot', function() {
3864
assert.strictEqual(snapshot.name, SNAPSHOT_NAME);
3965
});
4066

41-
it('should default metadata to an empty object', function() {
42-
assert.strictEqual(typeof snapshot.metadata, 'object');
43-
assert.strictEqual(Object.keys(snapshot.metadata).length, 0);
67+
it('should inherit from ServiceObject', function() {
68+
var calledWith = snapshot.calledWith_[0];
69+
70+
assert.strictEqual(calledWith.parent, COMPUTE);
71+
assert.strictEqual(calledWith.baseUrl, '/global/snapshots');
72+
assert.strictEqual(calledWith.id, SNAPSHOT_NAME);
73+
assert.deepEqual(calledWith.methods, {
74+
exists: true,
75+
get: true,
76+
getMetadata: true
77+
});
78+
});
79+
80+
it('should allow creating for a Disk object snapshot', function(done) {
81+
var scope = {
82+
constructor: {
83+
name: 'Disk'
84+
},
85+
createSnapshot: function() {
86+
assert.strictEqual(this, scope);
87+
done();
88+
}
89+
};
90+
91+
var snapshot = new Snapshot(scope, SNAPSHOT_NAME);
92+
93+
var calledWith = snapshot.calledWith_[0];
94+
assert.strictEqual(calledWith.methods.create, true);
95+
96+
calledWith.createMethod(); // (scope.createSnapshot)
4497
});
4598
});
4699

47100
describe('delete', function() {
48-
it('should make the correct API request', function(done) {
49-
snapshot.makeReq_ = function(method, path, query, body) {
50-
assert.strictEqual(method, 'DELETE');
51-
assert.strictEqual(path, '');
52-
assert.strictEqual(query, null);
53-
assert.strictEqual(body, null);
54-
101+
it('should call ServiceObject.delete', function(done) {
102+
FakeServiceObject.prototype.delete = function() {
103+
assert.strictEqual(this, snapshot);
55104
done();
56105
};
57106

58-
snapshot.delete(assert.ifError);
107+
snapshot.delete();
59108
});
60109

61110
describe('error', function() {
62111
var error = new Error('Error.');
63112
var apiResponse = { a: 'b', c: 'd' };
64113

65114
beforeEach(function() {
66-
snapshot.makeReq_ = function(method, path, query, body, callback) {
115+
FakeServiceObject.prototype.delete = function(callback) {
67116
callback(error, apiResponse);
68117
};
69118
});
@@ -88,7 +137,7 @@ describe('Snapshot', function() {
88137
var apiResponse = { name: 'operation-name' };
89138

90139
beforeEach(function() {
91-
snapshot.makeReq_ = function(method, path, query, body, callback) {
140+
FakeServiceObject.prototype.delete = function(callback) {
92141
callback(null, apiResponse);
93142
};
94143
});
@@ -119,105 +168,4 @@ describe('Snapshot', function() {
119168
});
120169
});
121170
});
122-
123-
describe('getMetadata', function() {
124-
it('should make the correct API request', function(done) {
125-
snapshot.makeReq_ = function(method, path, query, body) {
126-
assert.strictEqual(method, 'GET');
127-
assert.strictEqual(path, '');
128-
assert.strictEqual(query, null);
129-
assert.strictEqual(body, null);
130-
131-
done();
132-
};
133-
134-
snapshot.getMetadata(assert.ifError);
135-
});
136-
137-
describe('error', function() {
138-
var error = new Error('Error.');
139-
var apiResponse = { a: 'b', c: 'd' };
140-
141-
beforeEach(function() {
142-
snapshot.makeReq_ = function(method, path, query, body, callback) {
143-
callback(error, apiResponse);
144-
};
145-
});
146-
147-
it('should execute callback with error and API response', function(done) {
148-
snapshot.getMetadata(function(err, metadata, apiResponse_) {
149-
assert.strictEqual(err, error);
150-
assert.strictEqual(metadata, null);
151-
assert.strictEqual(apiResponse_, apiResponse);
152-
done();
153-
});
154-
});
155-
156-
it('should not require a callback', function() {
157-
assert.doesNotThrow(function() {
158-
snapshot.getMetadata();
159-
});
160-
});
161-
});
162-
163-
describe('success', function() {
164-
var apiResponse = { a: 'b', c: 'd' };
165-
166-
beforeEach(function() {
167-
snapshot.makeReq_ = function(method, path, query, body, callback) {
168-
callback(null, apiResponse);
169-
};
170-
});
171-
172-
it('should update the metadata to the API response', function(done) {
173-
snapshot.getMetadata(function(err) {
174-
assert.ifError(err);
175-
assert.strictEqual(snapshot.metadata, apiResponse);
176-
done();
177-
});
178-
});
179-
180-
it('should exec callback with metadata and API response', function(done) {
181-
snapshot.getMetadata(function(err, metadata, apiResponse_) {
182-
assert.ifError(err);
183-
assert.strictEqual(metadata, apiResponse);
184-
assert.strictEqual(apiResponse_, apiResponse);
185-
done();
186-
});
187-
});
188-
189-
it('should not require a callback', function() {
190-
assert.doesNotThrow(function() {
191-
snapshot.getMetadata();
192-
});
193-
});
194-
});
195-
});
196-
197-
describe('makeReq_', function() {
198-
it('should make the correct request to Compute', function(done) {
199-
var expectedPathPrefix = '/global/snapshots/' + snapshot.name;
200-
201-
var method = 'POST';
202-
var path = '/test';
203-
var query = {
204-
a: 'b',
205-
c: 'd'
206-
};
207-
var body = {
208-
a: 'b',
209-
c: 'd'
210-
};
211-
212-
snapshot.compute.makeReq_ = function(method_, path_, query_, body_, cb) {
213-
assert.strictEqual(method_, method);
214-
assert.strictEqual(path_, expectedPathPrefix + path);
215-
assert.strictEqual(query_, query);
216-
assert.strictEqual(body_, body);
217-
cb();
218-
};
219-
220-
snapshot.makeReq_(method, path, query, body, done);
221-
});
222-
});
223171
});

0 commit comments

Comments
 (0)