66
77import com .beust .jcommander .JCommander ;
88
9- import java .io .BufferedWriter ;
10- import java .io .FileInputStream ;
11- import java .io .FileNotFoundException ;
12- import java .io .FileWriter ;
13- import java .io .IOException ;
9+ import java .io .*;
1410import java .lang .management .ManagementFactory ;
11+ import java .nio .charset .StandardCharsets ;
1512import java .nio .file .Files ;
1613import java .nio .file .Path ;
1714import java .util .ArrayList ;
3330import org .datadog .jmxfetch .util .CustomLogger ;
3431import org .datadog .jmxfetch .util .MetricsAssert ;
3532import org .datadog .jmxfetch .util .LogLevel ;
33+ import org .junit .Rule ;
34+ import org .junit .rules .TemporaryFolder ;
3635
36+ /**
37+ * Config utility
38+ */
3739final class ConfigUtil {
38- public static Path writeConfigYamlToTemp (String content , String yamlName ) throws IOException {
39- Path tempDirectory = Files .createTempDirectory ("temp-dir" );
40- // Create a temporary file within the temporary directory
41- Path tempFile = Files .createTempFile (tempDirectory , yamlName , ".yaml" );
40+ public final Path tempConfdDir ;
4241
43- // Write the contents of the file to the temporary file
44- BufferedWriter writer = new BufferedWriter (new FileWriter (tempFile .toFile ()));
45- writer .write (content );
46- writer .close ();
47-
48- return tempFile ;
42+ public ConfigUtil (File file ) {
43+ this .tempConfdDir = file .toPath ();
4944 }
5045
51- public static String concatWithNewlines (String ... lines ) {
52- StringBuilder sb = new StringBuilder ();
53- for (String line : lines ) {
54- sb .append (line ).append (System .lineSeparator ());
46+ public Path makeTempYamlConfigFile (String yamlName , List <String > lines ) {
47+ try {
48+ return Files .write (
49+ Files .createTempFile (tempConfdDir , yamlName , ".yaml" ),
50+ lines ,
51+ StandardCharsets .UTF_8
52+ );
53+ } catch (IOException e ) {
54+ throw new UncheckedIOException (e );
5555 }
56- return sb .toString ();
5756 }
5857}
5958
@@ -66,9 +65,12 @@ public class TestCommon {
6665 List <Map <String , Object >> metrics = new ArrayList <>();
6766 List <Map <String , Object >> serviceChecks ;
6867
68+ @ Rule
69+ public TemporaryFolder temporaryFolder = new TemporaryFolder ();
70+
6971 /** Setup logger. */
7072 @ BeforeClass
71- public static void init () throws Exception {
73+ public static void init () {
7274 String level = System .getProperty ("tests.log_level" );
7375 if (level == null ) {
7476 level = "ALL" ;
@@ -132,10 +134,7 @@ protected void initApplication(String yamlFileName, String autoDiscoveryPipeFile
132134 // We initialize the main app that will collect these metrics using JMX
133135 String confdDirectory =
134136 Thread .currentThread ().getContextClassLoader ().getResource (yamlFileName ).getPath ();
135- confdDirectory =
136- new String (
137- confdDirectory .substring (
138- 0 , confdDirectory .length () - yamlFileName .length ()));
137+ confdDirectory = confdDirectory .substring (0 , confdDirectory .length () - yamlFileName .length ());
139138 List <String > params = new ArrayList <String >();
140139 boolean sdEnabled = (autoDiscoveryPipeFile .length () > 0 );
141140 params .add ("--reporter" );
@@ -154,7 +153,7 @@ protected void initApplication(String yamlFileName, String autoDiscoveryPipeFile
154153 params .add (5 , "/foo" ); // could be anything we're stubbing it out
155154 params .add (6 , "--sd_enabled" );
156155 }
157- new JCommander (appConfig , params .toArray (new String [params . size () ]));
156+ new JCommander (appConfig , params .toArray (new String [0 ]));
158157
159158 if (sdEnabled ) {
160159 String autoDiscoveryPipe =
@@ -179,7 +178,7 @@ protected void initApplication(String yamlFileName, String autoDiscoveryPipeFile
179178 app .init (false );
180179 }
181180
182- protected void initApplication (String yamlFileName ) throws FileNotFoundException , IOException {
181+ protected void initApplication (String yamlFileName ) throws IOException {
183182 initApplication (yamlFileName , "" );
184183 }
185184
@@ -188,31 +187,37 @@ protected void initApplication(String yamlFileName) throws FileNotFoundException
188187 * The configuration can be specified as a yaml literal with each arg
189188 * representing one line of the Yaml file
190189 * Does not support any SD/AD features.
190+ *
191+ * @param yamlConfigContents the YAML configuration contents, each representing a configuration file
191192 */
192- protected void initApplicationWithYamlLines (String ... yamlLines )
193- throws IOException {
194- String yamlConfig = ConfigUtil . concatWithNewlines ( yamlLines );
195- Path tempFile = ConfigUtil . writeConfigYamlToTemp ( yamlConfig , "config" );
193+ protected void initApplicationWithYamlLines (Path ... yamlConfigs ) {
194+ if ( yamlConfigs == null || yamlConfigs . length == 0 ) {
195+ throw new IllegalArgumentException ( "yamlConfigContents cannot be null or empty" );
196+ }
196197
197- String confdDirectory = tempFile .getParent ().toString ();
198- String yamlFileName = tempFile .getFileName ().toString ();
198+ String confdDirectory = yamlConfigs [0 ].getParent ().toString ();
199199
200- List <String > params = new ArrayList <String >();
200+ List <String > params = new ArrayList <>();
201201 params .add ("--reporter" );
202202 params .add ("console" );
203203
204- if ( confdDirectory != null ) {
204+ for ( Path yamlConfig : yamlConfigs ) {
205205 params .add ("-c" );
206- params .add (yamlFileName );
207- params .add ("--conf_directory" );
208- params .add (confdDirectory );
209- params .add ("collect" );
206+ params .add (yamlConfig .getFileName ().toString ());
210207 }
211- new JCommander (appConfig , params .toArray (new String [params .size ()]));
208+
209+ params .add ("--conf_directory" );
210+ params .add (confdDirectory );
211+ params .add ("collect" );
212+
213+ new JCommander (appConfig , params .toArray (new String [0 ]));
212214 this .app = new App (appConfig );
213215 app .init (false );
214216 }
215217
218+ ConfigUtil getConfigUtil () {
219+ return new ConfigUtil (temporaryFolder .getRoot ());
220+ }
216221
217222 /** Run a JMXFetch iteration. */
218223 protected void run () {
@@ -255,7 +260,7 @@ protected List<Map<String, Object>> getServiceChecks() {
255260 * @param additionalTags metric tags inherited from the bean properties
256261 * @param countTags number of metric tags
257262 * @param metricType type of the metric (gauge, histogram, ...)
258- * @return fail if the metric was not found
263+ * @throws AssertionError if the metric was not found
259264 */
260265 public void assertMetric (
261266 String name ,
0 commit comments