This repository was archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathElasticsearchIntegrationTest.scala
101 lines (84 loc) · 3.42 KB
/
ElasticsearchIntegrationTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.sumologic.elasticsearch_test
import java.io.File
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest
import org.elasticsearch.client.transport.TransportClient
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.transport.{InetSocketTransportAddress, LocalTransportAddress}
import org.elasticsearch.node.NodeBuilder
import org.scalatest.{BeforeAndAfterAll, Suite}
import scala.util.{Random, Try}
/**
* Created by Russell Cohen on 11/2/15.
*
* A mixin trait for UTs requiring Elasticsearch. The trait will manage a global Elasticsearch instance across all tests.
*
* You should use the index `IndexName` provided by the trait as it is guaranteed to be absent when the test starts
* and will be cleaned up when the test is complete.
*/
trait ElasticsearchIntegrationTest extends BeforeAndAfterAll {
this: Suite =>
import ElasticsearchIntegrationTest._
lazy val endpoint = globalEndpoint
lazy val IndexName = allocateNewIndexName
def allocateNewIndexName = synchronized {
s"index-${r.nextLong()}"
}
override def beforeAll(): Unit = {
esNode
client
endpoint
Try(delete(IndexName))
super.beforeAll()
}
override def afterAll(): Unit = {
Try(delete(IndexName))
super.afterAll()
}
def refresh(): Unit = esNode.client().admin().indices().prepareRefresh().execute().actionGet()
def delete(index: String) = Try(client.admin().indices().delete(new DeleteIndexRequest(index)).actionGet())
}
object ElasticsearchIntegrationTest {
private val r = new Random()
private lazy val esNodeSettings = Settings.builder().put("path.home", createTempDir("elasticsearch-test")).build()
private lazy val esNode = NodeBuilder.nodeBuilder().local(true).settings(esNodeSettings).node()
private lazy val settings = Settings.builder().put("node.local", "true").build()
lazy val client = esNode.client()
lazy val globalEndpoint = {
val nodeInfos = client.admin().cluster().prepareNodesInfo().clear().setSettings(true).setHttp(true).get()
val nodeAddress =
nodeInfos.getNodes.map(_.getHttp.address()).head.publishAddress().asInstanceOf[InetSocketTransportAddress]
val host = nodeAddress.address().getHostString
val port = nodeAddress.address().getPort
(host, port)
}
def createTempDir(name: String = "TempDir") = {
val tempFile = File.createTempFile(name, f"${System.currentTimeMillis()}")
val tempDir = new File(
f"${
tempFile.getParentFile.getAbsolutePath
}${File.separator}$name-file-${System.currentTimeMillis()}"
)
tempDir.mkdir()
tempFile.delete()
tempDir.deleteOnExit()
tempDir
}
}