github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/container-utils/containerutils_test.go (about)

     1  // Copyright 2019-2021 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 containerutils
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	runtimeclient "github.com/inspektor-gadget/inspektor-gadget/pkg/container-utils/runtime-client"
    25  	containerutilsTypes "github.com/inspektor-gadget/inspektor-gadget/pkg/container-utils/types"
    26  	"github.com/inspektor-gadget/inspektor-gadget/pkg/types"
    27  )
    28  
    29  func newRuntimeClient(t *testing.T, runtime types.RuntimeName, sPath string) (runtimeclient.ContainerRuntimeClient, error) {
    30  	config := &containerutilsTypes.RuntimeConfig{
    31  		Name:       runtime,
    32  		SocketPath: sPath,
    33  	}
    34  	rc, err := NewContainerRuntimeClient(config)
    35  	t.Cleanup(func() {
    36  		if rc != nil {
    37  			rc.Close()
    38  		}
    39  	})
    40  	return rc, err
    41  }
    42  
    43  func TestNewContainerRuntimeClient(t *testing.T) {
    44  	t.Parallel()
    45  
    46  	nonExistingSocketPath := filepath.Join(t.TempDir(), "non-existing-socket")
    47  	for _, runtime := range AvailableRuntimes {
    48  		t.Run(runtime, func(t *testing.T) {
    49  			runtime := types.String2RuntimeName(runtime)
    50  			t.Parallel()
    51  
    52  			t.Run("WithNonExistingSocketPath", func(t *testing.T) {
    53  				t.Parallel()
    54  
    55  				rc, err := newRuntimeClient(t, runtime, nonExistingSocketPath)
    56  				require.Nil(t, err)
    57  				require.NotNil(t, rc)
    58  			})
    59  
    60  			t.Run("WithDefaultSocketPath", func(t *testing.T) {
    61  				t.Parallel()
    62  
    63  				rc, err := newRuntimeClient(t, runtime, "")
    64  				require.Nil(t, err)
    65  				require.NotNil(t, rc)
    66  			})
    67  		})
    68  	}
    69  }
    70  
    71  func TestParseOCIState(t *testing.T) {
    72  	t.Parallel()
    73  
    74  	match, err := filepath.Glob("testdata/*.input")
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	for _, inputFile := range match {
    80  		t.Logf("Parsing OCI state from file %s", inputFile)
    81  		stateBuf, err := os.ReadFile(inputFile)
    82  		if err != nil {
    83  			t.Fatal(err)
    84  		}
    85  		ID, PID, err := ParseOCIState(stateBuf)
    86  		if err != nil {
    87  			t.Errorf("Cannot parse file %s: %s", inputFile, err)
    88  		}
    89  		if ID != "92646e8e819a27d43a9435cd195dc1f38a0c5ff897b4ca660fcbfbfe7502b47a" {
    90  			t.Errorf("Cannot get ID in %s", inputFile)
    91  		}
    92  		if PID != 210223 {
    93  			t.Errorf("Cannot get PID in %s", inputFile)
    94  		}
    95  	}
    96  }