github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/metadata/containers_test.go (about)

     1  /*
     2  Copyright 2017 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package metadata
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/Mirantis/virtlet/pkg/metadata/fake"
    25  	"github.com/Mirantis/virtlet/pkg/metadata/types"
    26  )
    27  
    28  func TestSetGetContainerInfo(t *testing.T) {
    29  	sandboxes := fake.GetSandboxes(2)
    30  	containers := fake.GetContainersConfig(sandboxes)
    31  
    32  	store := setUpTestStore(t, sandboxes, containers, nil)
    33  
    34  	for _, container := range containers {
    35  		containerInfo, err := store.Container(container.ContainerID).Retrieve()
    36  		if err != nil {
    37  			t.Fatal(err)
    38  		}
    39  		if containerInfo == nil {
    40  			t.Fatal(fmt.Errorf("containerInfo of container %q not found in Virtlet metadata store", container.ContainerID))
    41  		}
    42  
    43  		if containerInfo.Id != container.ContainerID {
    44  			t.Errorf("Expected %s, instead got %s", container.ContainerID, containerInfo.Id)
    45  		}
    46  
    47  		if containerInfo.Config.PodSandboxID != container.SandboxID {
    48  			t.Errorf("Expected %s, instead got %s", container.SandboxID, containerInfo.Config.PodSandboxID)
    49  		}
    50  
    51  		if containerInfo.Config.Image != container.Image {
    52  			t.Errorf("Expected %s, instead got %s", container.Image, containerInfo.Config.Image)
    53  		}
    54  
    55  		if !reflect.DeepEqual(containerInfo.Config.ContainerLabels, container.Labels) {
    56  			t.Errorf("Expected %v, instead got %v", container.Labels, containerInfo.Config.ContainerLabels)
    57  		}
    58  
    59  		if !reflect.DeepEqual(containerInfo.Config.ContainerAnnotations, container.Annotations) {
    60  			t.Errorf("Expected %v, instead got %v", container.Annotations, containerInfo.Config.ContainerAnnotations)
    61  		}
    62  	}
    63  }
    64  
    65  func TestGetImagesInUse(t *testing.T) {
    66  	sandboxes := fake.GetSandboxes(2)
    67  	containers := fake.GetContainersConfig(sandboxes)
    68  
    69  	store := setUpTestStore(t, sandboxes, containers, nil)
    70  
    71  	expectedImagesInUse := map[string]bool{"testImage": true}
    72  	imagesInUse, err := store.ImagesInUse()
    73  	if err != nil {
    74  		t.Fatalf("ImagesInUse(): %v", err)
    75  	}
    76  	if !reflect.DeepEqual(imagesInUse, expectedImagesInUse) {
    77  		t.Errorf("bad result from ImagesInUse(): expected %#v, got #%v", expectedImagesInUse, imagesInUse)
    78  	}
    79  }
    80  
    81  func TestRemoveContainer(t *testing.T) {
    82  	sandboxes := fake.GetSandboxes(2)
    83  	containers := fake.GetContainersConfig(sandboxes)
    84  
    85  	store := setUpTestStore(t, sandboxes, containers, nil)
    86  
    87  	for _, container := range containers {
    88  		podContainers, err := store.ListPodContainers(container.SandboxID)
    89  		if err != nil {
    90  			t.Fatal(err)
    91  		}
    92  		if len(podContainers) != 1 || podContainers[0].GetID() != container.ContainerID {
    93  			t.Errorf("Unexpected container list length: %d != 1", len(podContainers))
    94  		}
    95  		if err := store.Container(container.ContainerID).Save(func(c *types.ContainerInfo) (*types.ContainerInfo, error) {
    96  			return nil, nil
    97  		}); err != nil {
    98  			t.Fatal(err)
    99  		}
   100  		podContainers, err = store.ListPodContainers(container.SandboxID)
   101  		if err != nil {
   102  			t.Fatal(err)
   103  		}
   104  		if len(podContainers) != 0 {
   105  			t.Errorf("Unexpected container list length: %d != 0", len(podContainers))
   106  		}
   107  	}
   108  }