<commit_before>package container

import (
	"bytes"
	"testing"
	"time"

	"github.com/fsouza/go-dockerclient"
	mesos "github.com/mesos/mesos-go/mesosproto"
	. "github.com/smartystreets/goconvey/convey"
)

const (
	prelude = "LO, praise of the prowess of people-kings of spear-armed Danes, in days long sped"
	ending  = "Thus made their mourning the men of Geatland, for their hero's passing his hearth-companions"
)

func Test_PullImage(t *testing.T) {
	Convey("PullImage()", t, func() {
		image := "foo/foo:foo"
		taskInfo := &mesos.TaskInfo{
			Container: &mesos.ContainerInfo{
				Docker: &mesos.ContainerInfo_DockerInfo{
					Image: &image,
				},
			},
		}

		dockerClient := &MockDockerClient{}

		Convey("passes the right params", func() {
			err := PullImage(dockerClient, taskInfo, &docker.AuthConfiguration{})

			So(dockerClient.validOptions, ShouldBeTrue)
			So(err, ShouldBeNil)
		})

		Convey("bubbles up errors", func() {
			dockerClient.PullImageShouldError = true
			err := PullImage(dockerClient, taskInfo, &docker.AuthConfiguration{})

			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "Something went wrong")
		})
	})
}

func Test_CheckImage(t *testing.T) {
	Convey("CheckImage()", t, func() {
		image := "gonitro/sidecar:latest"
		images := []docker.APIImages{
			{
				RepoTags: []string{image, "sidecar", "sidecar:latest"},
			},
		}

		taskInfo := &mesos.TaskInfo{
			Container: &mesos.ContainerInfo{
				Docker: &mesos.ContainerInfo_DockerInfo{
					Image: &image,
				},
			},
		}

		dockerClient := &MockDockerClient{Images: images}

		Convey("handles errors", func() {
			dockerClient.ListImagesShouldError = true
			So(CheckImage(dockerClient, taskInfo), ShouldBeFalse)
		})

		Convey("matches the image", func() {
			So(CheckImage(dockerClient, taskInfo), ShouldBeTrue)
		})

		Convey("handles missing images", func() {
			wrong := "wrong"
			taskInfo.Container.Docker.Image = &wrong
			So(CheckImage(dockerClient, taskInfo), ShouldBeFalse)
		})

	})
}

func Test_StopContainer(t *testing.T) {
	Convey("When stopping containers", t, func() {
		dockerClient := &MockDockerClient{
			StopContainerShouldError: true,
			StopContainerMaxFails:    1,
			Container: &docker.Container{
				State: docker.State{
					Status: "running",
				},
			},
		}

		Convey("retries stopping the container", func() {
			err := StopContainer(dockerClient, "someid", 0)
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldNotContainSubstring, "Unable to kill")
			So(dockerClient.stopContainerFails, ShouldEqual, 2)
		})

		Convey("returns an error when it really won't stop", func() {
			dockerClient.StopContainerMaxFails = 2
			err := StopContainer(dockerClient, "someid", 0)
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "Unable to kill")
			So(dockerClient.stopContainerFails, ShouldEqual, 2)

		})
	})
}

func Test_GetLogs(t *testing.T) {
	Convey("Fetches the logs from a task", t, func() {
		taskId := "nginx-2392676-1479746266455-1-dev_singularity_sick_sing-DEFAULT"
		dockerClient := &MockDockerClient{
			LogOutputString: prelude,
			LogErrorString: ending,
		}

		stdout := bytes.NewBuffer(make([]byte, 0, 256))
		stderr := bytes.NewBuffer(make([]byte, 0, 256))
<commit_msg>Disable log output in testing<commit_after>package container

import (
	"bytes"
	"io/ioutil"
	"testing"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/fsouza/go-dockerclient"
	mesos "github.com/mesos/mesos-go/mesosproto"
	. "github.com/smartystreets/goconvey/convey"
)

const (
	prelude = "LO, praise of the prowess of people-kings of spear-armed Danes, in days long sped"
	ending  = "Thus made their mourning the men of Geatland, for their hero's passing his hearth-companions"
)

func init() {
	log.SetOutput(ioutil.Discard)
}

func Test_PullImage(t *testing.T) {
	Convey("PullImage()", t, func() {

		image := "foo/foo:foo"
		taskInfo := &mesos.TaskInfo{
			Container: &mesos.ContainerInfo{
				Docker: &mesos.ContainerInfo_DockerInfo{
					Image: &image,
				},
			},
		}

		dockerClient := &MockDockerClient{}

		Convey("passes the right params", func() {
			err := PullImage(dockerClient, taskInfo, &docker.AuthConfiguration{})

			So(dockerClient.validOptions, ShouldBeTrue)
			So(err, ShouldBeNil)
		})

		Convey("bubbles up errors", func() {
			dockerClient.PullImageShouldError = true
			err := PullImage(dockerClient, taskInfo, &docker.AuthConfiguration{})

			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "Something went wrong")
		})
	})
}

func Test_CheckImage(t *testing.T) {
	Convey("CheckImage()", t, func() {
		image := "gonitro/sidecar:latest"
		images := []docker.APIImages{
			{
				RepoTags: []string{image, "sidecar", "sidecar:latest"},
			},
		}

		taskInfo := &mesos.TaskInfo{
			Container: &mesos.ContainerInfo{
				Docker: &mesos.ContainerInfo_DockerInfo{
					Image: &image,
				},
			},
		}

		dockerClient := &MockDockerClient{Images: images}

		Convey("handles errors", func() {
			dockerClient.ListImagesShouldError = true
			So(CheckImage(dockerClient, taskInfo), ShouldBeFalse)
		})

		Convey("matches the image", func() {
			So(CheckImage(dockerClient, taskInfo), ShouldBeTrue)
		})

		Convey("handles missing images", func() {
			wrong := "wrong"
			taskInfo.Container.Docker.Image = &wrong
			So(CheckImage(dockerClient, taskInfo), ShouldBeFalse)
		})

	})
}

func Test_StopContainer(t *testing.T) {
	Convey("When stopping containers", t, func() {
		dockerClient := &MockDockerClient{
			StopContainerShouldError: true,
			StopContainerMaxFails:    1,
			Container: &docker.Container{
				State: docker.State{
					Status: "running",
				},
			},
		}

		Convey("retries stopping the container", func() {
			err := StopContainer(dockerClient, "someid", 0)
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldNotContainSubstring, "Unable to kill")
			So(dockerClient.stopContainerFails, ShouldEqual, 2)
		})

		Convey("returns an error when it really won't stop", func() {
			dockerClient.StopContainerMaxFails = 2
			err := StopContainer(dockerClient, "someid", 0)
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "Unable to kill")
			So(dockerClient.stopContainerFails, ShouldEqual, 2)

		})
	})
}

func Test_GetLogs(t *testing.T) {
	Convey("Fetches the logs from a task", t, func() {
		taskId := "nginx-2392676-1479746266455-1-dev_singularity_sick_sing-DEFAULT"
		dockerClient := &MockDockerClient{
			LogOutputString: prelude,
			LogErrorString:  ending,
		}

		stdout := bytes.NewBuffer(make([]byte, 0, 256))
		stderr := bytes.NewBuffer(make([]byte, 0, 256))

{'fraction_non_alphanumeric': 0.13875816001214514, 'fraction_numerical': 0.010626992561105207, 'mean_word_length': 4.36919315403423, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '786001', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2255, 'n_words': 472}