/*
 * Copyright 2005-2014 Red Hat, Inc.
 *
 * Red Hat 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 io.fabric8.arquillian.kubernetes.await;

import io.fabric8.arquillian.kubernetes.Session;
import io.fabric8.arquillian.utils.Util;
import io.fabric8.kubernetes.api.KubernetesClient;
import io.fabric8.kubernetes.api.KubernetesHelper;
import io.fabric8.kubernetes.api.PodStatus;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.utils.Filter;
import io.fabric8.utils.Objects;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import static io.fabric8.arquillian.kubernetes.Constants.ARQ_KEY;

public class SessionPodsAreReady implements Callable<Boolean> {

    private final Session session;
    private final KubernetesClient kubernetesClient;

    public SessionPodsAreReady(KubernetesClient kubernetesClient, Session session) {
        this.session = session;
        this.kubernetesClient = kubernetesClient;
    }

    @Override
    public Boolean call() throws Exception {
        boolean result = true;
        Map<String, String> labels = Collections.singletonMap(ARQ_KEY, session.getId());
        Filter<Pod> podFilter = KubernetesHelper.createPodFilter(labels);
        List<Pod> pods = Util.findPods(kubernetesClient, podFilter);

        if (pods.isEmpty()) {
            result = false;
            session.getLogger().warn("No pods are available yet, waiting...");
        }

        for (Pod pod : pods) {
            result = result && Objects.equal(PodStatus.OK, KubernetesHelper.getPodStatus(pod));
            if (!result) {
                session.getLogger().warn("Waiting for pods to reach 'running' state...");
            }
        }
        return result;
    }

}

@@ -907,16 +907,122 @@
 Status;%0A
+import io.fabric8.kubernetes.api.model.Container;%0Aimport io.fabric8.kubernetes.api.model.ContainerStatus;%0A
 import i
@@ -1236,16 +1236,17 @@
 llable;%0A
+%0A
 import s
@@ -2277,81 +2277,585 @@
     
-session.getLogger().warn(%22Waiting for pods to reach 'running' state...%22);
+if (pod.getCurrentState().getInfo() != null) %7B%0A                    for (Map.Entry%3CString, ContainerStatus%3E entry : pod.getCurrentState().getInfo().entrySet()) %7B%0A                        String containerId = entry.getKey();%0A                        ContainerStatus status = entry.getValue();%0A                        if (status.getState().getWaiting() != null) %7B%0A                            session.getLogger().warn(%22Waiting for container:%22 + containerId + %22. Reason:%22 + status.getState().getWaiting().getReason());%0A                        %7D%0A                    %7D%0A                %7D
 %0A   

Add more verbose logging when a pod is in "waiting" status.