github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/testing/containers/factory.go (about)

     1  // Copyright 2023-2024 The Inspektor Gadget authors
     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 containers
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/inspektor-gadget/inspektor-gadget/pkg/container-utils/testutils"
    21  	"github.com/inspektor-gadget/inspektor-gadget/pkg/types"
    22  )
    23  
    24  type ContainerFactory interface {
    25  	NewContainer(name, cmd string, opts ...containerOption) *TestContainer
    26  }
    27  
    28  // NewContainerFactory returns a new instance of a ContainerFactory based on the
    29  // container runtime. The returned factory can be used to create new container
    30  // instances which can be used in tests.
    31  func NewContainerFactory(containerRuntime string) (ContainerFactory, error) {
    32  	switch types.String2RuntimeName(containerRuntime) {
    33  	case types.RuntimeNameDocker:
    34  		return &DockerManager{}, nil
    35  	case types.RuntimeNameContainerd:
    36  		return &ContainerdManager{}, nil
    37  	default:
    38  		return nil, fmt.Errorf("unknown container runtime %q", containerRuntime)
    39  	}
    40  }
    41  
    42  // TestContainer is a wrapper around testutils.Container that implements the
    43  // missing functions from the TestStep interface. This allows to use the
    44  // container as a step in a test.
    45  type TestContainer struct {
    46  	testutils.Container
    47  	cOptions
    48  }