github.com/lalkh/containerd@v1.4.3/pkg/process/io_test.go (about)

     1  // +build linux
     2  
     3  /*
     4     Copyright The containerd Authors.
     5  
     6     Licensed under the Apache License, Version 2.0 (the "License");
     7     you may not use this file except in compliance with the License.
     8     You may obtain a copy of the License at
     9  
    10         http://www.apache.org/licenses/LICENSE-2.0
    11  
    12     Unless required by applicable law or agreed to in writing, software
    13     distributed under the License is distributed on an "AS IS" BASIS,
    14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15     See the License for the specific language governing permissions and
    16     limitations under the License.
    17  */
    18  
    19  package process
    20  
    21  import (
    22  	"context"
    23  	"io/ioutil"
    24  	"net/url"
    25  	"testing"
    26  
    27  	"github.com/containerd/containerd/namespaces"
    28  )
    29  
    30  func TestNewBinaryIO(t *testing.T) {
    31  	ctx := namespaces.WithNamespace(context.Background(), "test")
    32  	uri, _ := url.Parse("binary:///bin/echo?test")
    33  
    34  	before := descriptorCount(t)
    35  
    36  	io, err := NewBinaryIO(ctx, "1", uri)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	err = io.Close()
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	after := descriptorCount(t)
    47  	if before != after-1 { // one descriptor must be closed from shim logger side
    48  		t.Fatalf("some descriptors weren't closed (%d != %d)", before, after)
    49  	}
    50  }
    51  
    52  func TestNewBinaryIOCleanup(t *testing.T) {
    53  	ctx := namespaces.WithNamespace(context.Background(), "test")
    54  	uri, _ := url.Parse("binary:///not/existing")
    55  
    56  	before := descriptorCount(t)
    57  	_, err := NewBinaryIO(ctx, "2", uri)
    58  	if err == nil {
    59  		t.Fatal("error expected for invalid binary")
    60  	}
    61  
    62  	after := descriptorCount(t)
    63  	if before != after {
    64  		t.Fatalf("some descriptors weren't closed (%d != %d)", before, after)
    65  	}
    66  }
    67  
    68  func descriptorCount(t *testing.T) int {
    69  	t.Helper()
    70  	files, _ := ioutil.ReadDir("/proc/self/fd")
    71  	return len(files)
    72  }