<commit_before>package docker

import (
	docker "github.com/fsouza/go-dockerclient"
	"github.com/stretchr/testify/assert"
	"testing"
)

var containers = []docker.APIContainers{
	docker.APIContainers{
		ID:      "bd1d34c0ebeeb62dfdcc57327aca15d2ef3cbc39a60e44aecb7085a8d1f89fd9",
		Image:   "33e91d7bac3e",
		Command: "/usr/sbin/sshd -D -o UseDNS=no -o UsePAM=no -o PasswordAuthentication=yes -o UsePrivilegeSeparation=no -o PidFile=/tmp/sshd.pid",
		Created: 1436541119,
		Status:  "Up 7 days",
		//Ports:      []docker.APIPort{PrivatePort: 22, PublicPort: 32782, Type: "tcp", IP: "0.0.0.0"},
		SizeRw:     0,
		SizeRootFs: 0,
		Names:      []string{"/elated_kirch"},
	},
}

// 2015/07/17 18:06:05 {bd1d34c0ebee   0 0 0  false false false [] map[22/tcp:{}] false false false [PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DEBIAN_FRONTEND=noninteractive] [/usr/sbin/sshd -D -o UseDNS=no -o UsePAM=no -o PasswordAuthentication=yes -o UsePrivilegeSeparation=no -o PidFile=/tmp/sshd.pid] [] 33e91d7bac3e map[]   [] false [] [] map[]}

var containerConfig = docker.Config{
	Hostname:     "bd1d34c0ebee",
	ExposedPorts: map[docker.Port]struct{}{"22/tcp": {}},
	Env:          []string{"SRV_NAME=microservice1", "SRV_TAG=tag1"},
	Image:        "33e91d7bac3e",
}

var containerDetails = docker.Container{
	ID:              "bd1d34c0ebeeb62dfdcc57327aca15d2ef3cbc39a60e44aecb7085a8d1f89fd9",
	Config:          &containerConfig,
	Image:           "brainly/eve-landing-pages",
	NetworkSettings: nil,
	Name:            "/elated_kirch",
}

var containerNetworkSettings = docker.NetworkSettings{
	IPAddress:   "172.17.1.225",
	IPPrefixLen: 16,
	Gateway:     "172.17.42.1",
	Bridge:      "",
	//PortMapping map[string]PortMapping `json:"PortMapping,omitempty" yaml:"PortMapping,omitempty"`
	//Ports       map[Port][]PortBinding,
}

func TestGetRunningContainers(t *testing.T) {
	client := fakeDockerClient{}

	containers, err := GetRunningContainers(client)
	assert.Nil(t, err)

	assert.Equal(t, len(containers), 1)
	assert.Equal(t, containers[0].Name, "/elated_kirch")
}

type fakeDockerClient struct{}

func (c fakeDockerClient) listContainers(opts docker.ListContainersOptions) ([]docker.APIContainers, error) {
	return containers, nil
}

func (c fakeDockerClient) inspectContainer(id string) (*docker.Container, error) {
	return &containerDetails, nil
}<commit_msg>Add more thorough tests for GetRunningContainers method<commit_after>package docker

import (
	docker "github.com/fsouza/go-dockerclient"
	"github.com/stretchr/testify/assert"
	"testing"
	"time"
)

var (
	// container A section
	containerA = docker.APIContainers{
		ID:         "bd1d34c0ebeeb62dfdcc57327aca15d2ef3cbc39a60e44aecb7085a8d1f89fd9",
		Image:      "33e91d7bac3e",
		Command:    "/usr/sbin/sshd -D -o UseDNS=no -o UsePAM=no -o PasswordAuthentication=yes -o UsePrivilegeSeparation=no -o PidFile=/tmp/sshd.pid",
		Created:    1436541119,
		Status:     "Up 7 days",
		SizeRw:     0,
		SizeRootFs: 0,
		Names:      []string{"/elated_kirch"},
	}

	containerAConfig = docker.Config{
		Hostname:     "bd1d34c0ebee",
		ExposedPorts: map[docker.Port]struct{}{"22/tcp": {}},
		Env:          []string{"SRV_NAME=microservice1", "SRV_TAG=tag1"},
		Image:        "brainly/eve-landing-pages",
	}

	containerADetails = docker.Container{
		ID:              "bd1d34c0ebeeb62dfdcc57327aca15d2ef3cbc39a60e44aecb7085a8d1f89fd9",
		Config:          &containerAConfig,
		Image:           "33e91d7bac3e",
		NetworkSettings: nil,
		Name:            "/elated_kirch",
		Created:         time.Unix(1436541119, 0),
	}

	// container B section
	containerB = docker.APIContainers{
		ID:         "f717f795bcccd674628b92f77a72f4b80b2c6b5da289846a0edbd21fb4c462db",
		Image:      "e5d4de01ea02",
		Command:    "/usr/local/bin/wiyd --daddy=John",
		Created:    1436541319,
		Status:     "Up 9 days",
		SizeRw:     0,
		SizeRootFs: 0,
		Names:      []string{"/naughty_heisenberg"},
	}

	containerBConfig = docker.Config{
		Hostname:     "f717f795bccc",
		ExposedPorts: map[docker.Port]struct{}{"9000/tcp": {}},
		Env:          []string{"SRV_NAME=microservice2", "SRV_TAG=tag2"},
		Image:        "brainly/eve-who-is-your-daddy",
	}

	containerBDetails = docker.Container{
		ID:              "f717f795bcccd674628b92f77a72f4b80b2c6b5da289846a0edbd21fb4c462db",
		Config:          &containerBConfig,
		Image:           "e5d4de01ea02",
		NetworkSettings: nil,
		Name:            "/naughty_heisenberg",
		Created:         time.Unix(1436541319, 0),
	}
)

func TestGetRunningContainers(t *testing.T) {
	client := fakeDockerClient{
		fakeContainers: []docker.APIContainers{containerA},
		fakeContainerDetails: map[string]*docker.Container{
			"bd1d34c0ebeeb62dfdcc57327aca15d2ef3cbc39a60e44aecb7085a8d1f89fd9": &containerADetails,
		},
	}

	expectedContainers := []ContainerInfo{
		ContainerInfo{
			ID:          "bd1d34c0ebeeb62dfdcc57327aca15d2ef3cbc39a60e44aecb7085a8d1f89fd9",
			Name:        "/elated_kirch",
			ImageName:   "eve-landing-pages",
			ImageID:     "33e91d7bac3e",
			ServiceName: "microservice1",
			Env: map[string]string{
				"SRV_NAME": "microservice1",
				"SRV_TAG":  "tag1",
			},
			Created:  time.Unix(1436541119, 0),
			Config:   &containerAConfig,
			TCPPorts: TCPPorts{"22"},
			UDPPorts: UDPPorts{},
		},
	}

	containers, err := GetRunningContainers(client)
	assert.Nil(t, err)

	assert.Equal(t, expectedContainers, containers)
}

func TestGetRunningContainersWithNoContainers(t *testing.T) {
	client := fakeDockerClient{}

	expectedContainers := []ContainerInfo{}

	containers, err := GetRunningContainers(client)
	assert.Nil(t, err)

	assert.Equal(t, expectedContainers, containers)
}

func TestGetRunningContainersWithTwoContainers(t *testing.T) {
	client := fakeDockerClient{
		fakeContainers: []docker.APIContainers{
			containerA,
			containerB,
		},
		fakeContainerDetails: map[string]*docker.Container{
			"bd1d34c0ebeeb62dfdcc57327aca15d2ef3cbc39a60e44aecb7085a8d1f89fd9": &containerADetails,
			"f717f795bcccd674628b92f77a72f4b80b2c6b5da289846a0edbd21fb4c462db": &containerBDetails,
		},
	}

	expectedContainers := []ContainerInfo{
		ContainerInfo{
			ID:          "bd1d34c0ebeeb62dfdcc57327aca15d2ef3cbc39a60e44aecb7085a8d1f89fd9",
			Name:        "/elated_kirch",
			ImageName:   "eve-landing-pages",
			ImageID:     "33e91d7bac3e",
			ServiceName: "microservice1",
			Env: map[string]string{
				"SRV_NAME": "microservice1",
				"SRV_TAG":  "tag1",
			},
			Created:  time.Unix(1436541119, 0),
			Config:   &containerAConfig,
			TCPPorts: TCPPorts{"22"},
			UDPPorts: UDPPorts{},
		},
		ContainerInfo{
			ID:          "f717f795bcccd674628b92f77a72f4b80b2c6b5da289846a0edbd21fb4c462db",
			Name:        "/naughty_heisenberg",
			ImageName:   "eve-who-is-your-daddy",
			ImageID:     "e5d4de01ea02",
			ServiceName: "microservice2",
			Env: map[string]string{
				"SRV_NAME": "microservice2",
				"SRV_TAG":  "tag2",
			},
			Created:  time.Unix(1436541319, 0),
			Config:   &containerBConfig,
			TCPPorts: TCPPorts{"9000"},
			UDPPorts: UDPPorts{},
		},
	}

	containers, err := GetRunningContainers(client)
	assert.Nil(t, err)

	assert.Equal(t, expectedContainers, containers)
}

type fakeDockerClient struct {
	fakeContainers []docker.APIContainers
	fakeContainerDetails map[string]*docker.Container
}



func (c fakeDockerClient) listContainers(opts docker.ListContainersOptions) ([]docker.APIContainers, error) {
	return c.fakeContainers, nil
}

func (c fakeDockerClient) inspectContainer(id string) (*docker.Container, error) {
	return c.fakeContainerDetails[id], nil
}
{'fraction_non_alphanumeric': 0.12918596955658504, 'fraction_numerical': 0.08497683653209795, 'mean_word_length': 4.196698762035763, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, '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': '6472155', 'n_tokens_mistral': 3493, 'n_tokens_neox': 2983, 'n_words': 451}