github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/shim/runtimeoptions/runtimeoptions_test.go (about)

     1  // Copyright 2020 The gVisor 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  //     https://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 runtimeoptions
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  
    21  	shim "github.com/containerd/containerd/runtime/v1/shim/v1"
    22  	"github.com/containerd/typeurl"
    23  	"github.com/gogo/protobuf/proto"
    24  )
    25  
    26  func TestCreateTaskRequest(t *testing.T) {
    27  	// Serialize the top-level message.
    28  	const encodedText = `options: <
    29    type_url: "cri.runtimeoptions.v1.Options"
    30    value: "\n\010type_url\022\013config_path"
    31  >`
    32  	got := &shim.CreateTaskRequest{} // Should have raw options.
    33  	if err := proto.UnmarshalText(encodedText, got); err != nil {
    34  		t.Fatalf("unable to unmarshal text: %v", err)
    35  	}
    36  	var textBuffer bytes.Buffer
    37  	if err := proto.MarshalText(&textBuffer, got); err != nil {
    38  		t.Errorf("unable to marshal text: %v", err)
    39  	}
    40  	t.Logf("got: %s", string(textBuffer.Bytes()))
    41  
    42  	// Check the options.
    43  	wantOptions := &Options{}
    44  	wantOptions.TypeUrl = "type_url"
    45  	wantOptions.ConfigPath = "config_path"
    46  	gotMessage, err := typeurl.UnmarshalAny(got.Options)
    47  	if err != nil {
    48  		t.Fatalf("unable to unmarshal any: %v", err)
    49  	}
    50  	gotOptions, ok := gotMessage.(*Options)
    51  	if !ok {
    52  		t.Fatalf("got %v, want %v", gotMessage, wantOptions)
    53  	}
    54  	if !proto.Equal(gotOptions, wantOptions) {
    55  		t.Fatalf("got %v, want %v", gotOptions, wantOptions)
    56  	}
    57  }