gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/embeddedbinary/test/helloworld_test.go (about)

     1  // Copyright 2023 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  //     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 helloworld_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"os/exec"
    21  	"testing"
    22  	"time"
    23  
    24  	"gvisor.dev/gvisor/pkg/test/testutil"
    25  )
    26  
    27  // TestHelloworld executes helloworld_bundler and verifies that its output
    28  // matches "Hello, gVisor!\n".
    29  func TestHelloworld(t *testing.T) {
    30  	ctx, ctxCancel := context.WithTimeout(context.Background(), 10*time.Second)
    31  	defer ctxCancel()
    32  	helloWorldPath, err := testutil.FindFile("tools/embeddedbinary/test/helloworld_bundler")
    33  	if err != nil {
    34  		t.Fatalf("Cannot find helloworld_bundler path: %v", err)
    35  	}
    36  	for _, mode := range []string{"exec", "fork"} {
    37  		t.Run(mode, func(t *testing.T) {
    38  			output, err := exec.CommandContext(ctx, helloWorldPath, fmt.Sprintf("--mode=%s", mode)).CombinedOutput()
    39  			outputStr := string(output)
    40  			if err != nil {
    41  				t.Fatalf("Failed to execute helloworld_bundler: %v; output:\n%v\n", err, outputStr)
    42  			}
    43  			want := "Hello, gVisor!\n"
    44  			if outputStr != want {
    45  				t.Fatalf("helloworld_bundler: got output %q want %q", outputStr, want)
    46  			}
    47  		})
    48  	}
    49  }