|
| 1 | +import { expect } from 'chai'; |
| 2 | +import request = require('request'); |
| 3 | +import { ReadableStreamBuffer, WritableStreamBuffer } from 'stream-buffers'; |
| 4 | +import { anyFunction, anything, capture, instance, mock, reset, verify, when } from 'ts-mockito'; |
| 5 | + |
| 6 | +import { KubeConfig } from './config'; |
| 7 | +import { Cluster, Context, User } from './config_types'; |
| 8 | +import { DefaultRequest, Watch } from './watch'; |
| 9 | + |
| 10 | +describe('Watch', () => { |
| 11 | + it('should construct correctly', () => { |
| 12 | + const kc = new KubeConfig(); |
| 13 | + const watch = new Watch(kc); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should watch correctly', () => { |
| 17 | + const kc = new KubeConfig(); |
| 18 | + const server = 'foo.company.com'; |
| 19 | + kc.clusters = [ |
| 20 | + { |
| 21 | + name: 'cluster', |
| 22 | + server, |
| 23 | + } as Cluster, |
| 24 | + ] as Cluster[]; |
| 25 | + kc.contexts = [ |
| 26 | + { |
| 27 | + cluster: 'cluster', |
| 28 | + user: 'user', |
| 29 | + } as Context, |
| 30 | + ] as Context[]; |
| 31 | + kc.users = [ |
| 32 | + { |
| 33 | + name: 'user', |
| 34 | + } as User, |
| 35 | + ]; |
| 36 | + const fakeRequestor = mock(DefaultRequest); |
| 37 | + const watch = new Watch(kc, instance(fakeRequestor)); |
| 38 | + |
| 39 | + const obj1 = { |
| 40 | + type: 'ADDED', |
| 41 | + object: { |
| 42 | + foo: 'bar', |
| 43 | + }, |
| 44 | + }; |
| 45 | + |
| 46 | + const obj2 = { |
| 47 | + type: 'MODIFIED', |
| 48 | + object: { |
| 49 | + baz: 'blah', |
| 50 | + }, |
| 51 | + }; |
| 52 | + |
| 53 | + const fakeRequest = { |
| 54 | + pipe: (stream) => { |
| 55 | + stream.write(JSON.stringify(obj1) + '\n'); |
| 56 | + stream.write(JSON.stringify(obj2) + '\n'); |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + when(fakeRequestor.webRequest(anything(), anyFunction())).thenReturn(fakeRequest); |
| 61 | + |
| 62 | + const path = '/some/path/to/object'; |
| 63 | + |
| 64 | + const receivedTypes: string[] = []; |
| 65 | + const receivedObjects: string[] = []; |
| 66 | + let doneCalled = false; |
| 67 | + let doneErr: any; |
| 68 | + |
| 69 | + watch.watch(path, {}, (phase: string, obj: string) => { |
| 70 | + receivedTypes.push(phase); |
| 71 | + receivedObjects.push(obj); |
| 72 | + }, (err: any) => { |
| 73 | + doneCalled = true; |
| 74 | + doneErr = err; |
| 75 | + }); |
| 76 | + |
| 77 | + verify(fakeRequestor.webRequest(anything(), anyFunction())); |
| 78 | + |
| 79 | + const [opts, doneCallback] = capture(fakeRequestor.webRequest).last(); |
| 80 | + const reqOpts: request.OptionsWithUri = opts as request.OptionsWithUri; |
| 81 | + |
| 82 | + expect(reqOpts.uri).to.equal(`${server}${path}`); |
| 83 | + expect(reqOpts.method).to.equal('GET'); |
| 84 | + expect(reqOpts.json).to.equal(true); |
| 85 | + |
| 86 | + expect(receivedTypes).to.deep.equal([obj1.type, obj2.type]); |
| 87 | + expect(receivedObjects).to.deep.equal([obj1.object, obj2.object]); |
| 88 | + |
| 89 | + expect(doneCalled).to.equal(false); |
| 90 | + |
| 91 | + doneCallback(null, null, null); |
| 92 | + |
| 93 | + expect(doneCalled).to.equal(true); |
| 94 | + expect(doneErr).to.equal(null); |
| 95 | + |
| 96 | + const errIn = {error: 'err'}; |
| 97 | + doneCallback(errIn, null, null); |
| 98 | + expect(doneErr).to.deep.equal(errIn); |
| 99 | + }); |
| 100 | +}); |
0 commit comments