github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/tests/rkt_interactive_test.go (about)

     1  // Copyright 2015 The rkt 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  // +build host coreos src kvm
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"strings"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/rkt/rkt/tests/testutils"
    27  )
    28  
    29  var interactiveTests = []struct {
    30  	testName     string
    31  	aciBuildArgs []string
    32  	rktArgs      string
    33  	say          string
    34  	expect       string
    35  }{
    36  	{
    37  		`Check tty without interactive`,
    38  		[]string{"--exec=/inspect --check-tty"},
    39  		`--debug --insecure-options=image run --mds-register=false ^INTERACTIVE^`,
    40  		``,
    41  		`stdin is not a terminal`,
    42  	},
    43  	{
    44  		`Check tty without interactive (with parameter)`,
    45  		[]string{"--exec=/inspect"},
    46  		`--debug --insecure-options=image run --mds-register=false ^INTERACTIVE^ -- --check-tty`,
    47  		``,
    48  		`stdin is not a terminal`,
    49  	},
    50  	{
    51  		`Check tty with interactive`,
    52  		[]string{"--exec=/inspect --check-tty"},
    53  		`--debug --insecure-options=image run --mds-register=false --interactive ^INTERACTIVE^`,
    54  		``,
    55  		`stdin is a terminal`,
    56  	},
    57  	{
    58  		`Check tty with interactive (with parameter)`,
    59  		[]string{"--exec=/inspect"},
    60  		`--debug --insecure-options=image run --mds-register=false --interactive ^INTERACTIVE^ -- --check-tty`,
    61  		``,
    62  		`stdin is a terminal`,
    63  	},
    64  	{
    65  		`Reading from stdin`,
    66  		[]string{"--exec=/inspect --read-stdin"},
    67  		`--debug --insecure-options=image run --mds-register=false --interactive ^INTERACTIVE^`,
    68  		`Saluton`,
    69  		`Received text: Saluton`,
    70  	},
    71  	{
    72  		`Reading from stdin (with parameter)`,
    73  		[]string{"--exec=/inspect"},
    74  		`--debug --insecure-options=image run --mds-register=false --interactive ^INTERACTIVE^ -- --read-stdin`,
    75  		`Saluton`,
    76  		`Received text: Saluton`,
    77  	},
    78  }
    79  
    80  func TestInteractive(t *testing.T) {
    81  	ctx := testutils.NewRktRunCtx()
    82  	defer ctx.Cleanup()
    83  
    84  	for i, tt := range interactiveTests {
    85  		t.Logf("Running test #%v: %v", i, tt.testName)
    86  
    87  		aciFileName := patchTestACI("rkt-inspect-interactive.aci", tt.aciBuildArgs...)
    88  		defer os.Remove(aciFileName)
    89  
    90  		rktCmd := fmt.Sprintf("%s %s", ctx.Cmd(), tt.rktArgs)
    91  		rktCmd = strings.Replace(rktCmd, "^INTERACTIVE^", aciFileName, -1)
    92  		child := spawnOrFail(t, rktCmd)
    93  		if tt.say != "" {
    94  			if err := expectTimeoutWithOutput(child, "Enter text:", time.Minute); err != nil {
    95  				t.Fatalf("Waited for the prompt but not found #%v: %v", i, err)
    96  			}
    97  
    98  			if err := child.SendLine(tt.say); err != nil {
    99  				t.Fatalf("Failed to send %q on the prompt #%v: %v", tt.say, i, err)
   100  			}
   101  		}
   102  
   103  		if err := expectTimeoutWithOutput(child, tt.expect, time.Minute); err != nil {
   104  			t.Fatalf("Expected %q but not found #%v: %v", tt.expect, i, err)
   105  		}
   106  
   107  		if err := child.Wait(); err != nil {
   108  			t.Fatalf("rkt didn't terminate correctly: %v", err)
   109  		}
   110  	}
   111  }