|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import expect from 'expect.js'; |
| 8 | +import { |
| 9 | + ES_INDEX_NAME, |
| 10 | + ES_TYPE_NAME |
| 11 | +} from './constants'; |
| 12 | + |
| 13 | +export default function ({ getService }) { |
| 14 | + const supertest = getService('supertest'); |
| 15 | + const chance = getService('chance'); |
| 16 | + const es = getService('es'); |
| 17 | + |
| 18 | + describe('set_tag', () => { |
| 19 | + it('should create an empty tag', async () => { |
| 20 | + const tagId = 'production'; |
| 21 | + await supertest |
| 22 | + .put( |
| 23 | + `/api/beats/tag/${tagId}` |
| 24 | + ) |
| 25 | + .set('kbn-xsrf', 'xxx') |
| 26 | + .send() |
| 27 | + .expect(201); |
| 28 | + |
| 29 | + const esResponse = await es.get({ |
| 30 | + index: ES_INDEX_NAME, |
| 31 | + type: ES_TYPE_NAME, |
| 32 | + id: `tag:${tagId}` |
| 33 | + }); |
| 34 | + |
| 35 | + const tagInEs = esResponse._source; |
| 36 | + |
| 37 | + expect(tagInEs.type).to.be('tag'); |
| 38 | + expect(tagInEs.tag.id).to.be(tagId); |
| 39 | + expect(tagInEs.tag.configuration_blocks).to.be.an(Array); |
| 40 | + expect(tagInEs.tag.configuration_blocks.length).to.be(0); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should create a tag with one configuration block', async () => { |
| 44 | + const tagId = 'production'; |
| 45 | + await supertest |
| 46 | + .put( |
| 47 | + `/api/beats/tag/${tagId}` |
| 48 | + ) |
| 49 | + .set('kbn-xsrf', 'xxx') |
| 50 | + .send({ |
| 51 | + configuration_blocks: [ |
| 52 | + { |
| 53 | + type: 'output', |
| 54 | + block_yml: 'elasticsearch:\n hosts: [\"localhost:9200\"]\n username: "..."' |
| 55 | + } |
| 56 | + ] |
| 57 | + }) |
| 58 | + .expect(201); |
| 59 | + |
| 60 | + const esResponse = await es.get({ |
| 61 | + index: ES_INDEX_NAME, |
| 62 | + type: ES_TYPE_NAME, |
| 63 | + id: `tag:${tagId}` |
| 64 | + }); |
| 65 | + |
| 66 | + const tagInEs = esResponse._source; |
| 67 | + |
| 68 | + expect(tagInEs.type).to.be('tag'); |
| 69 | + expect(tagInEs.tag.id).to.be(tagId); |
| 70 | + expect(tagInEs.tag.configuration_blocks).to.be.an(Array); |
| 71 | + expect(tagInEs.tag.configuration_blocks.length).to.be(1); |
| 72 | + expect(tagInEs.tag.configuration_blocks[0].type).to.be('output'); |
| 73 | + expect(tagInEs.tag.configuration_blocks[0].block_yml).to.be('elasticsearch:\n hosts: [\"localhost:9200\"]\n username: "..."'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should create a tag with two configuration blocks', async () => { |
| 77 | + const tagId = 'production'; |
| 78 | + await supertest |
| 79 | + .put( |
| 80 | + `/api/beats/tag/${tagId}` |
| 81 | + ) |
| 82 | + .set('kbn-xsrf', 'xxx') |
| 83 | + .send({ |
| 84 | + configuration_blocks: [ |
| 85 | + { |
| 86 | + type: 'filebeat.inputs', |
| 87 | + block_yml: 'file:\n path: "/var/log/some.log"]\n' |
| 88 | + }, |
| 89 | + { |
| 90 | + type: 'output', |
| 91 | + block_yml: 'elasticsearch:\n hosts: [\"localhost:9200\"]\n username: "..."' |
| 92 | + } |
| 93 | + ] |
| 94 | + }) |
| 95 | + .expect(201); |
| 96 | + |
| 97 | + const esResponse = await es.get({ |
| 98 | + index: ES_INDEX_NAME, |
| 99 | + type: ES_TYPE_NAME, |
| 100 | + id: `tag:${tagId}` |
| 101 | + }); |
| 102 | + |
| 103 | + const tagInEs = esResponse._source; |
| 104 | + |
| 105 | + expect(tagInEs.type).to.be('tag'); |
| 106 | + expect(tagInEs.tag.id).to.be(tagId); |
| 107 | + expect(tagInEs.tag.configuration_blocks).to.be.an(Array); |
| 108 | + expect(tagInEs.tag.configuration_blocks.length).to.be(2); |
| 109 | + expect(tagInEs.tag.configuration_blocks[0].type).to.be('filebeat.inputs'); |
| 110 | + expect(tagInEs.tag.configuration_blocks[0].block_yml).to.be('file:\n path: "/var/log/some.log"]\n'); |
| 111 | + expect(tagInEs.tag.configuration_blocks[1].type).to.be('output'); |
| 112 | + expect(tagInEs.tag.configuration_blocks[1].block_yml).to.be('elasticsearch:\n hosts: [\"localhost:9200\"]\n username: "..."'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should fail when creating a tag with two configuration blocks of type output', async () => { |
| 116 | + const tagId = 'production'; |
| 117 | + await supertest |
| 118 | + .put( |
| 119 | + `/api/beats/tag/${tagId}` |
| 120 | + ) |
| 121 | + .set('kbn-xsrf', 'xxx') |
| 122 | + .send({ |
| 123 | + configuration_blocks: [ |
| 124 | + { |
| 125 | + type: 'output', |
| 126 | + block_yml: 'logstash:\n hosts: ["localhost:9000"]\n' |
| 127 | + }, |
| 128 | + { |
| 129 | + type: 'output', |
| 130 | + block_yml: 'elasticsearch:\n hosts: [\"localhost:9200\"]\n username: "..."' |
| 131 | + } |
| 132 | + ] |
| 133 | + }) |
| 134 | + .expect(400); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should fail when creating a tag with an invalid configuration block type', async () => { |
| 138 | + const tagId = 'production'; |
| 139 | + await supertest |
| 140 | + .put( |
| 141 | + `/api/beats/tag/${tagId}` |
| 142 | + ) |
| 143 | + .set('kbn-xsrf', 'xxx') |
| 144 | + .send({ |
| 145 | + configuration_blocks: [ |
| 146 | + { |
| 147 | + type: chance.word(), |
| 148 | + block_yml: 'logstash:\n hosts: ["localhost:9000"]\n' |
| 149 | + } |
| 150 | + ] |
| 151 | + }) |
| 152 | + .expect(400); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should update an existing tag', async () => { |
| 156 | + const tagId = 'production'; |
| 157 | + await supertest |
| 158 | + .put( |
| 159 | + `/api/beats/tag/${tagId}` |
| 160 | + ) |
| 161 | + .set('kbn-xsrf', 'xxx') |
| 162 | + .send({ |
| 163 | + configuration_blocks: [ |
| 164 | + { |
| 165 | + type: 'filebeat.inputs', |
| 166 | + block_yml: 'file:\n path: "/var/log/some.log"]\n' |
| 167 | + }, |
| 168 | + { |
| 169 | + type: 'output', |
| 170 | + block_yml: 'elasticsearch:\n hosts: [\"localhost:9200\"]\n username: "..."' |
| 171 | + } |
| 172 | + ] |
| 173 | + }) |
| 174 | + .expect(201); |
| 175 | + |
| 176 | + await supertest |
| 177 | + .put( |
| 178 | + `/api/beats/tag/${tagId}` |
| 179 | + ) |
| 180 | + .set('kbn-xsrf', 'xxx') |
| 181 | + .send({ |
| 182 | + configuration_blocks: [ |
| 183 | + { |
| 184 | + type: 'output', |
| 185 | + block_yml: 'logstash:\n hosts: ["localhost:9000"]\n' |
| 186 | + } |
| 187 | + ] |
| 188 | + }) |
| 189 | + .expect(200); |
| 190 | + |
| 191 | + const esResponse = await es.get({ |
| 192 | + index: ES_INDEX_NAME, |
| 193 | + type: ES_TYPE_NAME, |
| 194 | + id: `tag:${tagId}` |
| 195 | + }); |
| 196 | + |
| 197 | + const tagInEs = esResponse._source; |
| 198 | + |
| 199 | + expect(tagInEs.type).to.be('tag'); |
| 200 | + expect(tagInEs.tag.id).to.be(tagId); |
| 201 | + expect(tagInEs.tag.configuration_blocks).to.be.an(Array); |
| 202 | + expect(tagInEs.tag.configuration_blocks.length).to.be(1); |
| 203 | + expect(tagInEs.tag.configuration_blocks[0].type).to.be('output'); |
| 204 | + expect(tagInEs.tag.configuration_blocks[0].block_yml).to.be('logstash:\n hosts: ["localhost:9000"]\n'); |
| 205 | + }); |
| 206 | + }); |
| 207 | +} |
0 commit comments