func TestStatsEngineWithNewContainers(t *testing.T) {
	// This should be a functional test. Upgrading to docker 1.6 breaks our ability to
	// read state.json file for containers.
	t.Skip("Skipping integ test as this is really a functional test")
	engine := NewDockerStatsEngine(&cfg)
	err := engine.initDockerClient()
	if err != nil {
		t.Error("Error initializing stats engine: ", err)
	}
	container, err := createGremlin(client)
	if err != nil {
		t.Fatal("Error creating container", err)
	}
	defer client.RemoveContainer(docker.RemoveContainerOptions{
		ID:    container.ID,
		Force: true,
	})

	resolver := newIntegContainerMetadataResolver()
	// Initialize mock interface so that task id is resolved only for the container
	// that was launched during the test.
	resolver.addToMap(container.ID)

	// Wait for containers from previous tests to transition states.
	time.Sleep(checkPointSleep)
	engine.resolver = resolver

	err = engine.Init()
	if err != nil {
		t.Error("Error initializing stats engine: ", err)
	}

	err = client.StartContainer(container.ID, nil)
	defer client.StopContainer(container.ID, defaultDockerTimeoutSeconds)
	if err != nil {
		t.Error("Error starting container: ", container.ID, " error: ", err)
	}

	// Wait for the stats collection go routine to start.
	time.Sleep(checkPointSleep)

	metadata, taskMetrics, err := engine.GetInstanceMetrics()
	if err != nil {
		t.Error("Error gettting instance metrics: ", err)
	}

	err = validateMetricsMetadata(metadata)
	if err != nil {
		t.Error("Error validating metadata: ", err)
	}

	if len(taskMetrics) != 1 {
		t.Error("Incorrect number of tasks. Expected: 1, got: ", len(taskMetrics))
	}
	taskMetric := taskMetrics[0]
	if *taskMetric.TaskDefinitionFamily != taskDefinitionFamily {
		t.Error("Excpected task definition family to be: ", taskDefinitionFamily, " got: ", *taskMetric.TaskDefinitionFamily)
	}
	if *taskMetric.TaskDefinitionVersion != taskDefinitionVersion {
		t.Error("Excpected task definition family to be: ", taskDefinitionVersion, " got: ", *taskMetric.TaskDefinitionVersion)
	}

	err = validateContainerMetrics(taskMetric.ContainerMetrics, 1)
	if err != nil {
		t.Error("Error validating container metrics: ", err)
	}

	err = client.StopContainer(container.ID, defaultDockerTimeoutSeconds)
	if err != nil {
		t.Error("Error stopping container: ", container.ID, " error: ", err)
	}

	time.Sleep(waitForCleanupSleep)

	// Should not contain any metrics after cleanup.
	err = validateIdleContainerMetrics(engine)
	if err != nil {
		t.Fatal("Error validating metadata: ", err)
	}
}
Exemplo n.º 2
0
func TestStatsEngineWithExistingContainers(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integ test in short mode")
	}
	engine := NewDockerStatsEngine(&cfg, dockerClient)

	// Create a container to get the container id.
	container, err := createGremlin(client)
	if err != nil {
		t.Fatal("Error creating container", err)
	}
	defer client.RemoveContainer(docker.RemoveContainerOptions{
		ID:    container.ID,
		Force: true,
	})
	resolver := newIntegContainerMetadataResolver()
	// Initialize mock interface so that task id is resolved only for the container
	// that was launched during the test.
	resolver.addToMap(container.ID)

	// Wait for containers from previous tests to transition states.
	time.Sleep(checkPointSleep)

	engine.resolver = resolver
	engine.cluster = defaultCluster
	engine.containerInstanceArn = defaultContainerInstance

	err = client.StartContainer(container.ID, nil)
	if err != nil {
		t.Error("Error starting container: ", container.ID, " error: ", err)
	}
	defer client.StopContainer(container.ID, defaultDockerTimeoutSeconds)

	// Simulate container start prior to listener initialization.
	time.Sleep(checkPointSleep)
	err = engine.Init()
	if err != nil {
		t.Error("Error initializing stats engine: ", err)
	}
	defer engine.unsubscribeContainerEvents()

	// Wait for the stats collection go routine to start.
	time.Sleep(checkPointSleep)

	metadata, taskMetrics, err := engine.GetInstanceMetrics()
	if err != nil {
		t.Error("Error gettting instance metrics: ", err)
	}
	err = validateMetricsMetadata(metadata)
	if err != nil {
		t.Error("Error validating metadata: ", err)
	}

	if len(taskMetrics) != 1 {
		t.Fatal("Incorrect number of tasks. Expected: 1, got: ", len(taskMetrics))
	}

	taskMetric := taskMetrics[0]
	if *taskMetric.TaskDefinitionFamily != taskDefinitionFamily {
		t.Error("Excpected task definition family to be: ", taskDefinitionFamily, " got: ", *taskMetric.TaskDefinitionFamily)
	}
	if *taskMetric.TaskDefinitionVersion != taskDefinitionVersion {
		t.Error("Excpected task definition family to be: ", taskDefinitionVersion, " got: ", *taskMetric.TaskDefinitionVersion)
	}
	err = validateContainerMetrics(taskMetric.ContainerMetrics, 1)
	if err != nil {
		t.Error("Error validating container metrics: ", err)
	}

	err = client.StopContainer(container.ID, defaultDockerTimeoutSeconds)
	if err != nil {
		t.Error("Error stopping container: ", container.ID, " error: ", err)
	}

	time.Sleep(waitForCleanupSleep)

	// Should not contain any metrics after cleanup.
	err = validateIdleContainerMetrics(engine)
	if err != nil {
		t.Fatal("Error validating idle metrics: ", err)
	}
}