github.com/google/cadvisor@v0.49.1/integration/tests/api/event_test.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package api
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	info "github.com/google/cadvisor/info/v1"
    25  	"github.com/google/cadvisor/integration/framework"
    26  )
    27  
    28  func TestStreamingEventInformationIsReturned(t *testing.T) {
    29  	// TODO(vmarmol): De-flake and re-enable.
    30  	t.Skip()
    31  
    32  	fm := framework.New(t)
    33  	defer fm.Cleanup()
    34  
    35  	// Watch for container deletions
    36  	einfo := make(chan *info.Event)
    37  	go func() {
    38  		err := fm.Cadvisor().Client().EventStreamingInfo("?deletion_events=true&stream=true&subcontainers=true", einfo)
    39  		require.NoError(t, err)
    40  	}()
    41  
    42  	// Create a short-lived container.
    43  	containerID := fm.Docker().RunBusybox("sleep", "2")
    44  
    45  	// Wait for the deletion event.
    46  	timeout := time.After(30 * time.Second)
    47  	done := false
    48  	for !done {
    49  		select {
    50  		case ev := <-einfo:
    51  			if ev.EventType == info.EventContainerDeletion {
    52  				if strings.Contains(ev.ContainerName, containerID) {
    53  					done = true
    54  				}
    55  			}
    56  		case <-timeout:
    57  			t.Errorf(
    58  				"timeout happened before destruction event was detected for container %q", containerID)
    59  			done = true
    60  		}
    61  	}
    62  
    63  	// We should have already received a creation event.
    64  	waitForStaticEvent(containerID, "?creation_events=true&subcontainers=true", t, fm, info.EventContainerCreation)
    65  }
    66  
    67  func waitForStaticEvent(containerID string, urlRequest string, t *testing.T, fm framework.Framework, typeEvent info.EventType) {
    68  	einfo, err := fm.Cadvisor().Client().EventStaticInfo(urlRequest)
    69  	require.NoError(t, err)
    70  	found := false
    71  	for _, ev := range einfo {
    72  		if ev.EventType == typeEvent {
    73  			if strings.Contains(ev.ContainerName, containerID) {
    74  				found = true
    75  				break
    76  			}
    77  		}
    78  	}
    79  	require.True(t, found)
    80  }