github.com/containerd/Containerd@v1.4.13/runtime/v2/shim/shim_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     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 shim
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"runtime"
    23  	"testing"
    24  )
    25  
    26  func TestRuntimeWithEmptyMaxEnvProcs(t *testing.T) {
    27  	var oldGoMaxProcs = runtime.GOMAXPROCS(0)
    28  	defer runtime.GOMAXPROCS(oldGoMaxProcs)
    29  
    30  	os.Setenv("GOMAXPROCS", "")
    31  	setRuntime()
    32  
    33  	var currentGoMaxProcs = runtime.GOMAXPROCS(0)
    34  	if currentGoMaxProcs != 2 {
    35  		t.Fatal("the max number of procs should be 2")
    36  	}
    37  }
    38  
    39  func TestRuntimeWithNonEmptyMaxEnvProcs(t *testing.T) {
    40  	os.Setenv("GOMAXPROCS", "not_empty")
    41  	setRuntime()
    42  	var oldGoMaxProcs2 = runtime.GOMAXPROCS(0)
    43  	if oldGoMaxProcs2 != runtime.NumCPU() {
    44  		t.Fatal("the max number CPU should be equal to available CPUs")
    45  	}
    46  }
    47  
    48  func TestShimOptWithValue(t *testing.T) {
    49  	ctx := context.TODO()
    50  	ctx = context.WithValue(ctx, OptsKey{}, Opts{Debug: true})
    51  
    52  	o := ctx.Value(OptsKey{})
    53  	if o == nil {
    54  		t.Fatal("opts nil")
    55  	}
    56  	op, ok := o.(Opts)
    57  	if !ok {
    58  		t.Fatal("opts not of type Opts")
    59  	}
    60  	if !op.Debug {
    61  		t.Fatal("opts.Debug should be true")
    62  	}
    63  }