Skip to content

Commit

Permalink
added new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raffis committed Feb 21, 2019
1 parent 2644785 commit e5dcc57
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
47 changes: 47 additions & 0 deletions tests/icinga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,51 @@ describe('icinga', () => {
expect(calls[0][0]).toBe('bar');
});
});


describe('delete services by filter', () => {
it('delete service filtered', async () => {
icinga.deleteService = jest.fn();
IcingaApi.getServiceFiltered = jest.fn()
.mockImplementation((host, cb) => cb(null, [
{attrs: {name: "foo", host_name: "foo"}},
{attrs: {name: "bar", host_name: "bar"}},
]));

await expect(icinga.deleteServicesByFilter('vars.bar==true')).resolves.toEqual(true);
expect(icinga.deleteService.mock.calls.length).toBe(2);
});

it('get services filtered failed', async () => {
IcingaApi.deleteService = jest.fn();
IcingaApi.getServiceFiltered = jest.fn()
.mockImplementation((host, cb) => cb({Statuscode: 500}, null));

await expect(icinga.deleteServicesByFilter('vars.bar==true')).rejects.toEqual({Statuscode: 500});
expect(IcingaApi.deleteService.mock.calls.length).toBe(0);
});
});

describe('delete hosts by filter', () => {
it('delete host filtered', async () => {
icinga.deleteHost = jest.fn();
IcingaApi.getHostFiltered = jest.fn()
.mockImplementation((host, cb) => cb(null, [
{attrs: {name: "foo"}},
{attrs: {name: "bar"}},
]));

await expect(icinga.deleteHostsByFilter('vars.bar==true')).resolves.toEqual(true);
expect(icinga.deleteHost.mock.calls.length).toBe(2);
});

it('get hosts filtered failed', async () => {
IcingaApi.deleteHost = jest.fn();
IcingaApi.getHostFiltered = jest.fn()
.mockImplementation((host, cb) => cb({Statuscode: 500}, null));

await expect(icinga.deleteHostsByFilter('vars.bar==true')).rejects.toEqual({Statuscode: 500});
expect(IcingaApi.deleteHost.mock.calls.length).toBe(0);
});
});
});
62 changes: 62 additions & 0 deletions tests/kube/ingress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,41 @@ describe('kubernetes ingresses', () => {
await bindings.data(resource);
expect(Icinga.applyHost.mock.calls.length).toBe(1);
});

it('modify ingress object delete and create host', async () => {
let instance = new Ingress(Logger, Node, Icinga, {
hostName: null
});

var resource = {
type: 'MODIFIED',
object: fixture
};

Icinga.applyHost = jest.fn();
Icinga.deleteServicesByFilter = jest.fn();
Icinga.deleteHost = function(name) {
expect(name).toEqual('ingress-foobar-foo');
return new Promise((resolve,reject) => {
resolve(true);
});
};

var bindings = {};
var json = {
on: async function(name, callback) {
bindings[name] = callback;
}
};

await instance.kubeListener(() => {
return json;
});

await bindings.data(resource);
expect(Icinga.applyHost.mock.calls.length).toBe(1);
expect(Icinga.deleteServicesByFilter.mock.calls.length).toBe(0);
});

it('delete ingress object delete', async () => {
let instance = new Ingress(Logger, Node, Icinga);
Expand Down Expand Up @@ -321,6 +356,33 @@ describe('kubernetes ingresses', () => {
expect(calls[1][2]['vars.http_ssl']).toBe(true);
});

it('attach services to kube workers if attachToNodes is enabled', async () => {
let instance = new Ingress(Logger, Node, Icinga, {
attachToNodes: true
});

Node.getWorkerNodes = function() {
return ['foo', 'bar'];
};

Icinga.applyService = jest.fn();
Icinga.applyServiceGroup = jest.fn();
Icinga.applyHost = jest.fn();

await instance.prepareObject(fixture);
const calls = Icinga.applyService.mock.calls;
expect(Icinga.applyHost.mock.instances.length).toBe(0);
expect(Icinga.applyService.mock.instances.length).toBe(4);

expect(calls[0][0]).toBe('foo');
expect(calls[1][0]).toBe('bar');
expect(calls[2][0]).toBe('foo');
expect(calls[3][0]).toBe('bar');
expect(calls[0][1]).toBe('foobar.example.org-http--');
expect(calls[1][1]).toBe('foobar.example.org-http--');
expect(calls[2][1]).toBe('barfoo.example.org-http--foo');
expect(calls[3][1]).toBe('barfoo.example.org-http--foo');
});
});

describe('kubernetes annotations', () => {
Expand Down
61 changes: 60 additions & 1 deletion tests/kube/volume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,42 @@ describe('kubernetes volumes', () => {
await bindings.data(resource);
expect(Icinga.applyHost.mock.calls.length).toBe(1);
});

it('modify ingress object delete and create host', async () => {
let instance = new Volume(Logger, Node, Icinga, {
hostName: null
});

var resource = {
type: 'MODIFIED',
object: fixture
};

Icinga.applyHost = jest.fn();
Icinga.deleteServicesByFilter = jest.fn();
Icinga.deleteHost = function(name) {
expect(name).toEqual('volume-generic-nimble-fad5684e-22fb-11e9-94e3-0050568fe3c2');
return new Promise((resolve,reject) => {
resolve(true);
});
};

var bindings = {};
var json = {
on: async function(name, callback) {
bindings[name] = callback;
}
};

await instance.kubeListener(() => {
return json;
});

await bindings.data(resource);
expect(Icinga.applyHost.mock.calls.length).toBe(1);
expect(Icinga.deleteServicesByFilter.mock.calls.length).toBe(0);
});

it('delete volume object delete', async () => {
let instance = new Volume(Logger, Node, Icinga);

Expand Down Expand Up @@ -233,7 +268,7 @@ describe('kubernetes volumes', () => {
});
});

describe('add all volume object http path rules as service objects', () => {
describe('add all volume objects as service objects', () => {
it('create service object', async () => {
let instance = new Volume(Logger, Node, Icinga);

Expand Down Expand Up @@ -288,6 +323,30 @@ describe('kubernetes volumes', () => {
expect(Icinga.applyService.mock.instances.length).toBe(1);
expect(calls[0][3]).toEqual(['foo', 'bar']);
});

it('attach services to kube workers if attachToNodes is enabled', async () => {
let instance = new Volume(Logger, Node, Icinga, {
attachToNodes: true
});

Node.getWorkerNodes = function() {
return ['foo', 'bar'];
};

Icinga.applyService = jest.fn();
Icinga.applyServiceGroup = jest.fn();
Icinga.applyHost = jest.fn();

await instance.prepareObject(fixture);
const calls = Icinga.applyService.mock.calls;
expect(Icinga.applyHost.mock.instances.length).toBe(0);
expect(Icinga.applyService.mock.instances.length).toBe(2);

expect(calls[0][0]).toBe('foo');
expect(calls[1][0]).toBe('bar');
expect(calls[0][1]).toBe('generic-nimble-fad5684e-22fb-11e9-94e3-0050568fe3c2');
expect(calls[1][1]).toBe('generic-nimble-fad5684e-22fb-11e9-94e3-0050568fe3c2');
});
});

describe('kubernetes annotations', () => {
Expand Down

0 comments on commit e5dcc57

Please sign in to comment.