/*
 * 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 org.apache.flink.runtime.jobmanager;

import static org.junit.Assert.*;

import java.net.InetAddress;
import java.net.ServerSocket;

import org.apache.flink.configuration.ConfigConstants;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.net.NetUtils;
import org.junit.Test;

/**
 * Tests that verify the startup behavior of the JobManager in failure
 * situations, when the JobManager cannot be started.
 */
public class JobManagerStartupTest {

	/**
	 * Verifies that the JobManager fails fast (and with expressive error message)
	 * when the port to listen is already in use.
	 */
	@Test
	public void testStartupWithPortInUse() {
		
		ServerSocket portOccupier;
		final int portNum;
		
		try {
			portNum = NetUtils.getAvailablePort();
			portOccupier = new ServerSocket(portNum, 10, InetAddress.getByName("localhost"));
		}
		catch (Throwable t) {
			// could not find free port, or open a connection there
			return;
		}
		
		try {
			JobManager.runJobManager(new Configuration(), ExecutionMode.CLUSTER(), "localhost", portNum);
			fail("this should throw an exception");
		}
		catch (Exception e) {
			// expected
			if(!e.getMessage().contains("Address already in use")) {
				e.printStackTrace();
				fail("Received wrong exception");
			}
		}
		finally {
			try {
				portOccupier.close();
			}
			catch (Throwable t) {}
		}
	}

	/**
	 * Verifies that the JobManager fails fast (and with expressive error message)
	 * when one of its components (here the BLOB server) fails to start properly.
	 */
	@Test
	public void testJobManagerStartupFails() {
		final int portNum;
		try {
			portNum = NetUtils.getAvailablePort();
		}
		catch (Throwable t) {
			// skip test if we cannot find a free port
			return;
		}
		Configuration failConfig = new Configuration();
		failConfig.setString(ConfigConstants.BLOB_STORAGE_DIRECTORY_KEY, "/does-not-exist-no-sir");

		try {
			JobManager.runJobManager(failConfig, ExecutionMode.CLUSTER(), "localhost", portNum);
			fail("this should fail with an exception");
		}
		catch (Exception e) {
			// expected
		}
	}
}

@@ -881,16 +881,37 @@
 ert.*;%0A%0A
+import java.io.File;%0A
 import j
@@ -962,16 +962,52 @@
 ocket;%0A%0A
+import com.google.common.io.Files;%0A%0A
 import o
@@ -1151,16 +1151,66 @@
 etUtils;
+%0A%0Aimport org.junit.After;%0Aimport org.junit.Before;
 %0Aimport 
@@ -1397,16 +1397,541 @@
 Test %7B%0A%0A
+%09private final static String DOES_NOT_EXISTS_NO_SIR = %22does-not-exist-no-sir%22;%0A%0A%09private File blobStorageDirectory;%0A%0A%09@Before%0A%09public void before() %7B%0A%09%09// Prepare test directory%0A%09%09blobStorageDirectory = Files.createTempDir();%0A%0A%09%09assertTrue(blobStorageDirectory.setExecutable(true, false));%0A%09%09assertTrue(blobStorageDirectory.setReadable(true, false));%0A%09%09assertTrue(blobStorageDirectory.setWritable(false, false));%0A%09%7D%0A%0A%09@After%0A%09public void after() %7B%0A%09%09// Cleanup test directory%0A%09%09assertTrue(blobStorageDirectory.delete());%0A%09%7D%0A%0A
 %09/**%0A%09 *
@@ -3243,16 +3243,119 @@
 tion();%0A
+%09%09String nonExistDirectory = new File(blobStorageDirectory, DOES_NOT_EXISTS_NO_SIR).getAbsolutePath();%0A
 %09%09failCo
@@ -3417,32 +3417,25 @@
 EY, 
-%22/does-not-exist-no-sir%22
+nonExistDirectory
 );%0A%0A

Fix test case at JobManagerStartupTest to avoid hang on certain users