github.com/containerd/Containerd@v1.4.13/cio/io_unix_test.go (about) 1 // +build !windows 2 3 /* 4 Copyright The containerd Authors. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 package cio 20 21 import ( 22 "context" 23 "fmt" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 "testing" 28 29 "gotest.tools/v3/assert" 30 ) 31 32 func TestOpenFifos(t *testing.T) { 33 scenarios := []*FIFOSet{ 34 { 35 Config: Config{ 36 Stdin: "", 37 Stdout: filepath.Join("This/does/not/exist", "test-stdout"), 38 Stderr: filepath.Join("This/does/not/exist", "test-stderr"), 39 }, 40 }, 41 { 42 Config: Config{ 43 Stdin: filepath.Join("This/does/not/exist", "test-stdin"), 44 Stdout: "", 45 Stderr: filepath.Join("This/does/not/exist", "test-stderr"), 46 }, 47 }, 48 { 49 Config: Config{ 50 Stdin: "", 51 Stdout: "", 52 Stderr: filepath.Join("This/does/not/exist", "test-stderr"), 53 }, 54 }, 55 } 56 for _, scenario := range scenarios { 57 _, err := openFifos(context.Background(), scenario) 58 assert.Assert(t, err != nil, scenario) 59 } 60 } 61 62 // TestOpenFifosWithTerminal tests openFifos should not open stderr if terminal 63 // is set. 64 func TestOpenFifosWithTerminal(t *testing.T) { 65 var ctx, cancel = context.WithCancel(context.Background()) 66 defer cancel() 67 68 ioFifoDir, err := ioutil.TempDir("", fmt.Sprintf("cio-%s", t.Name())) 69 if err != nil { 70 t.Fatalf("unexpected error during creating temp dir: %v", err) 71 } 72 defer os.RemoveAll(ioFifoDir) 73 74 cfg := Config{ 75 Stdout: filepath.Join(ioFifoDir, "test-stdout"), 76 Stderr: filepath.Join(ioFifoDir, "test-stderr"), 77 } 78 79 // Without terminal, pipes.Stderr should not be nil 80 { 81 p, err := openFifos(ctx, NewFIFOSet(cfg, nil)) 82 if err != nil { 83 t.Fatalf("unexpected error during openFifos: %v", err) 84 } 85 86 if p.Stderr == nil { 87 t.Fatalf("unexpected empty stderr pipe") 88 } 89 } 90 91 // With terminal, pipes.Stderr should be nil 92 { 93 cfg.Terminal = true 94 p, err := openFifos(ctx, NewFIFOSet(cfg, nil)) 95 if err != nil { 96 t.Fatalf("unexpected error during openFifos: %v", err) 97 } 98 99 if p.Stderr != nil { 100 t.Fatalf("unexpected stderr pipe") 101 } 102 } 103 }