<commit_before>package net.dump247.docker;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Integration tests for {@link DockerClient}.
 * <p/>
 * Requires that a docker API be serving requests from local port 49000.
 * An easy method is to use https://github.com/dotcloud/docker/blob/master/Dockerfile
 * and add the following to /etc/default/docker: <em><pre>DOCKER_OPTS="-H tcp://0.0.0.0:49000"</pre></em>.
 */
public class DockerClientIT {
    private static final URI API_URL = URI.create("http://localhost:49000");
    private static final String UBUNTU = "ubuntu";

    private DockerClient _client;

    private List<String> _containerIds;
    private List<String> _imageIds;

    @BeforeClass
    public static void setUpFixture() throws Exception {
        // Ensure ubuntu image is available for all tests so tests run faster
        // Tests for the pull functionality should use a different image
        new DockerClient(API_URL).pullImage(UBUNTU);
    }

    @AfterClass
    public static void tearDownFixture() throws Exception {
        new DockerClient(API_URL).removeImage(UBUNTU);
    }

    @Before
    public void setUp() {
        _client = new DockerClient(API_URL);
        _containerIds = new ArrayList<String>();
        _imageIds = new ArrayList<String>();
    }

    @After
    public void tearDown() throws Exception {
        for (String containerId : _containerIds) {
            _client.removeContainer(containerId);
        }

        for (String imageId : _imageIds) {
            if (!imageId.equals(UBUNTU)) {
                _client.removeImage(imageId);
            }
        }
    }

    @Test
    public void version() throws Exception {
        DockerVersionResponse response = _client.version();
        assertTrue("Version=" + response.getVersion(), response.getVersion().matches("^\\d+\\.\\d+\\.\\d+$"));
        assertTrue("GitCommit=" + response.getGitCommit(), response.getGitCommit().matches("^[a-f0-9]{7}$"));
        assertTrue("GoVersion=" + response.getGoVersion(), response.getGoVersion().matches("^go\\d+\\.\\d+$"));
        assertTrue("Arch=" + response.getArch(), response.getArch().length() > 0);
        assertTrue("KernelVersion=" + response.getKernelVersion(), response.getKernelVersion().length() > 0);
        assertTrue("Os=" + response.getOs(), response.getOs().length() > 0);
    }

    @Test(expected = ImageNotFoundException.class)
    public void pullImage_that_does_not_exist() throws Exception {
        _client.pullImage("no-such-image");
    }

    @Test<commit_msg>Integration test for pull image progress listener<commit_after>package net.dump247.docker;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * Integration tests for {@link DockerClient}.
 * <p/>
 * Requires that a docker API be serving requests from local port 49000.
 * An easy method is to use https://github.com/dotcloud/docker/blob/master/Dockerfile
 * and add the following to /etc/default/docker: <em><pre>DOCKER_OPTS="-H tcp://0.0.0.0:49000"</pre></em>.
 */
public class DockerClientIT {
    private static final URI API_URL = URI.create("http://localhost:49000");
    private static final String UBUNTU = "ubuntu";

    private DockerClient _client;

    private List<String> _containerIds;
    private List<String> _imageIds;

    @BeforeClass
    public static void setUpFixture() throws Exception {
        // Ensure ubuntu image is available for all tests so tests run faster
        // Tests for the pull functionality should use a different image
        new DockerClient(API_URL).pullImage(UBUNTU);
    }

    @AfterClass
    public static void tearDownFixture() throws Exception {
        new DockerClient(API_URL).removeImage(UBUNTU);
    }

    @Before
    public void setUp() {
        _client = new DockerClient(API_URL);
        _containerIds = new ArrayList<String>();
        _imageIds = new ArrayList<String>();
    }

    @After
    public void tearDown() throws Exception {
        for (String containerId : _containerIds) {
            _client.removeContainer(containerId);
        }

        for (String imageId : _imageIds) {
            if (!imageId.equals(UBUNTU)) {
                _client.removeImage(imageId);
            }
        }
    }

    @Test
    public void version() throws Exception {
        DockerVersionResponse response = _client.version();
        assertTrue("Version=" + response.getVersion(), response.getVersion().matches("^\\d+\\.\\d+\\.\\d+$"));
        assertTrue("GitCommit=" + response.getGitCommit(), response.getGitCommit().matches("^[a-f0-9]{7}$"));
        assertTrue("GoVersion=" + response.getGoVersion(), response.getGoVersion().matches("^go\\d+\\.\\d+$"));
        assertTrue("Arch=" + response.getArch(), response.getArch().length() > 0);
        assertTrue("KernelVersion=" + response.getKernelVersion(), response.getKernelVersion().length() > 0);
        assertTrue("Os=" + response.getOs(), response.getOs().length() > 0);
    }

    @Test(expected = ImageNotFoundException.class)
    public void pullImage_that_does_not_exist() throws Exception {
        _client.pullImage("no-such-image");
    }

    @Test
    public void pullImage_events() throws Exception {
        final List<ProgressEvent> events = new ArrayList<ProgressEvent>();

        _imageIds.add("stackbrew/ubuntu");
        _client.pullImage("stackbrew/ubuntu", new ProgressListener() {
            @Override
            public void progress(final ProgressEvent event) {
                events.add(event);
            }
        });

        int progressEvents = 0;
        int finalProgressEvents = 0;

        int index = 0;
        for (ProgressEvent event : events) {
            // All events should be success events (no errors)
            assertEquals("index: " + index, ProgressEvent.Code.Ok, event.getCode());

            // Always should have a status message
            assertTrue(event.getStatusMessage().length() > 0);

            // Status message should not equal detail message (detail message is optional)
            assertFalse(event.getDetailMessage().equals(event.getStatusMessage()));

            // ID is not empty
            assertTrue(event.getId().length() > 0);

            // Progress info should be valid
            assertTrue("index: " + index, event.getTotal() >= 0);
            assertTrue("index: " + index, event.getCurrent() >= 0);
            assertTrue("index: " + index, event.getTotal() >= event.getCurrent());

            if (event.getTotal() > 0) {
                progressEvents += 1;

                if (event.getTotal() == event.getCurrent()) {
                    finalProgressEvents += 1;
                }
            }
        }

        assertTrue(progressEvents > 0);
        assertTrue(finalProgressEvents > 0);
    }

    @Test
{'fraction_non_alphanumeric': 0.12556772642265562, 'fraction_numerical': 0.009083622762489982, 'mean_word_length': 2.877265665458312, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6439119', 'n_tokens_mistral': 2129, 'n_tokens_neox': 2027, 'n_words': 504}