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