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 18, 2019
1 parent b039054 commit b68cd50
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 99 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kube-icinga",
"version": "1.0.0",
"version": "2.0.0",
"description": "Icinga2 autodiscovery service for kubernetes",
"main": "main.js",
"scripts": {
Expand Down
38 changes: 0 additions & 38 deletions src/icinga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,50 +207,12 @@ export default class Icinga {
});
}

/**
* Cleanup all previously deployed icinga kubernetes objects
*/
/*public async cleanup(): Promise<any> {
this.logger.info('start cleanup, removing all kubernetes objects from icinga');
return new Promise((resolve, reject) => {
this.icingaClient.getServiceFiltered({filter: 'service.vars._kubernetes == true'}, async (err, result) => {
if (err) {
return reject(err);
}
let handlers = [];
for (const service of result) {
handlers.push(this.deleteService(service.attrs.host_name, service.attrs.name));
}
await Promise.all(handlers);
resolve(true);
});
this.icingaClient.getHostFiltered({filter: 'host.vars._kubernetes == true'}, async (err, result) => {
if (err) {
return reject(err);
}
let handlers = [];
for (const host of result) {
handlers.push(this.deleteHost(host.attrs.name));
}
await Promise.all(handlers);
resolve(true);
});
});
}*/

/**
* Delete services by filter
*/
public async deleteServicesByFilter(filter: string): Promise<any> {
return new Promise((resolve, reject) => {
this.icingaClient.getServiceFiltered({filter: filter}, async (err, result) => {
console.log(err, result);
if (err) {
return reject(err);
}
Expand Down
7 changes: 3 additions & 4 deletions src/kube/ingress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ export default class Ingress extends Resource {
if (!this.options.attachToNodes) {
await this.applyHost(hostname, hostname, definition, this.options.hostTemplates);
}

let service = this.prepareResource(definition);
let templates = this.options.serviceTemplates;
templates = templates.concat(this.prepareTemplates(definition));

if (this.options.applyServices) {
await this.icinga.applyServiceGroup(definition.metadata.namespace);

Expand Down Expand Up @@ -145,7 +144,7 @@ export default class Ingress extends Resource {
let hostname = this.getHostname(definition);
return this.icinga.deleteHost(hostname);
}

return this.icinga.deleteServicesByFilter('service.vars.kubernetes.metadata.uid=="'+definition.metadata.uid+'"');
}

Expand All @@ -167,7 +166,7 @@ export default class Ingress extends Resource {
await this.deleteObject(object.object).catch((err) => {
this.logger.error('failed to remove objects', {error: err})
});
}
}

if (object.type == 'ADDED' || object.type == 'MODIFIED') {
this.prepareObject(object.object).catch((err) => {
Expand Down
10 changes: 0 additions & 10 deletions tests/icinga.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
/*
public hasCheckCommand(command: string): Promise<boolean> {
public applyHostGroup(name: string): Promise<any> {
public applyServiceGroup(name: string): Promise<boolean> {
public applyHost(name: string, address: string, definition, templates: string[]=[]): Promise<boolean> {
public applyService(host: string, name: string, definition, templates: string[]=[]) {
public deleteService(host: string, name: string): Promise<boolean> {
public deleteHost(name: string): Promise<boolean> {
public async cleanup(): Promise<any> {
*/
import * as IcingaApi from 'icinga2-api';
import IcingaClient from '../src/icinga';
import Logger from '../src/logger';
Expand Down
124 changes: 107 additions & 17 deletions tests/kube/ingress.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Ingress from '../../src/kube/ingress';
import Node from '../../src/kube/node';
import Icinga from '../../src/icinga';
import {LoggerInstance} from 'winston';
import Logger from '../../src/logger';
jest.mock('../../src/icinga');
jest.mock('kubernetes-client');
jest.mock('../../src/logger');

const template = {
"apiVersion": "extensions/v1beta1",
Expand All @@ -12,6 +13,7 @@ const template = {
"annotations": {},
"name": "foo",
"namespace": "foobar",
"uid": "xyz"
},
"spec": {
"rules": [
Expand Down Expand Up @@ -56,11 +58,99 @@ beforeEach(() => {
});

describe('kubernetes ingresses', () => {
var instance: Ingress;
describe('ingress watch stream', () => {
it('create icinga ingress object', async () => {
let instance = new Ingress(Logger, Node, Icinga);
var resource = {
type: 'ADDED',
object: fixture
};

Icinga.deleteServicesByFilter = jest.fn();
Icinga.applyHost = jest.fn();

var bindings = {};
var json = {
on: 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('modify ingress object delete and create', async () => {
let instance = new Ingress(Logger, Node, Icinga);
var resource = {
type: 'MODIFIED',
object: fixture
};

Icinga.applyHost = jest.fn();
Icinga.deleteServicesByFilter = function(definition) {
expect(definition).toEqual('service.vars.kubernetes.metadata.uid==\"xyz\"');
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);
});

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

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

Icinga.applyHost = jest.fn();
Icinga.deleteServicesByFilter = function(definition) {
expect(definition).toEqual('service.vars.kubernetes.metadata.uid==\"xyz\"');
return new Promise((resolve,reject) => {
resolve(true);
});
};


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

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

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

describe('add ingress object with dummy host', () => {
it('create icinga host object', () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: false
});
Icinga.applyHost = jest.fn();
Expand All @@ -72,7 +162,7 @@ describe('kubernetes ingresses', () => {
});

it('create icinga host object with dynamic host', () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: false,
hostName: null
});
Expand All @@ -85,7 +175,7 @@ describe('kubernetes ingresses', () => {
});

it('create icinga host object with custom definitions', () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: false,
hostDefinition: {
'vars.foo': 'bar',
Expand All @@ -101,7 +191,7 @@ describe('kubernetes ingresses', () => {
});

it('create icinga host object with templates', () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: false,
hostTemplates: ['foo', 'bar']
});
Expand All @@ -113,7 +203,7 @@ describe('kubernetes ingresses', () => {
});

it('do not create icinga host object while attachToNodes is enabled', () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: false,
attachToNodes: true
});
Expand All @@ -126,7 +216,7 @@ describe('kubernetes ingresses', () => {

describe('add ingress object namespace as service group', () => {
it('create service group per default', async () => {
let instance = new Ingress(LoggerInstance, Node, Icinga);
let instance = new Ingress(Logger, Node, Icinga);

Icinga.applyHost = jest.fn();
Icinga.applyService = jest.fn();
Expand All @@ -137,7 +227,7 @@ describe('kubernetes ingresses', () => {
});

it('do not create servicegroup if applyServices is disabled', () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: false
});

Expand All @@ -150,7 +240,7 @@ describe('kubernetes ingresses', () => {

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

Icinga.applyServiceGroup = jest.fn();
Icinga.applyService = jest.fn();
Expand All @@ -166,7 +256,7 @@ describe('kubernetes ingresses', () => {
});

it('create all service objects with dynamic hosts', async () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
hostName: null
});

Expand All @@ -180,7 +270,7 @@ describe('kubernetes ingresses', () => {
}

it('create all service objects with custom service definition', async () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: true,
serviceDefinition: {
'check_command': 'tcp',
Expand All @@ -199,7 +289,7 @@ describe('kubernetes ingresses', () => {
});

it('create all service objects with templates', async () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: true,
serviceTemplates: ['foo', 'bar']
});
Expand All @@ -213,7 +303,7 @@ describe('kubernetes ingresses', () => {
});

it('create service objects for tls enabled ingresses', async () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: true,
serviceTemplates: ['foo', 'bar']
});
Expand All @@ -235,7 +325,7 @@ describe('kubernetes ingresses', () => {

describe('kubernetes annotations', () => {
it('check_command/templates annotation', async () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: true
});

Expand All @@ -254,7 +344,7 @@ describe('kubernetes ingresses', () => {
});

it('use annotation instead global definition', async () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: true,
serviceDefinition: {
check_command: 'foo'
Expand All @@ -272,7 +362,7 @@ describe('kubernetes ingresses', () => {
});

it('definiton merge', async () => {
let instance = new Ingress(LoggerInstance, Node, Icinga, {
let instance = new Ingress(Logger, Node, Icinga, {
applyServices: true,
serviceDefinition: {
check_command: 'foo'
Expand Down
Loading

0 comments on commit b68cd50

Please sign in to comment.