github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/command/exec/exec_test.go (about) 1 // Copyright 2018 Authors of Cilium 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 !privileged_tests 16 17 package exec 18 19 import ( 20 "context" 21 "testing" 22 "time" 23 24 "github.com/cilium/cilium/pkg/checker" 25 "github.com/cilium/cilium/pkg/logging" 26 27 "github.com/sirupsen/logrus" 28 . "gopkg.in/check.v1" 29 ) 30 31 const ( 32 timeout = 250 * time.Millisecond 33 ) 34 35 // Hook up gocheck into the "go test" runner. 36 type ExecTestSuite struct{} 37 38 var ( 39 _ = Suite(&ExecTestSuite{}) 40 fooLog = logging.DefaultLogger.WithField("foo", "bar") 41 ) 42 43 func Test(t *testing.T) { 44 TestingT(t) 45 } 46 47 func (s *ExecTestSuite) TestWithTimeout(c *C) { 48 cmd := WithTimeout(timeout, "sleep", "inf") 49 err := cmd.Start() 50 c.Assert(err, IsNil) 51 err = cmd.Wait() 52 c.Assert(err, ErrorMatches, "signal: killed") 53 } 54 55 func (s *ExecTestSuite) TestWithCancel(c *C) { 56 cmd, cancel := WithCancel(context.Background(), "sleep", "inf") 57 c.Assert(cancel, NotNil) 58 err := cmd.Start() 59 c.Assert(err, IsNil) 60 cancel() 61 } 62 63 func (s *ExecTestSuite) TestCanceled(c *C) { 64 cmd, cancel := WithCancel(context.Background(), "sleep", "inf") 65 c.Assert(cancel, NotNil) 66 cancel() 67 _, err := cmd.CombinedOutput(fooLog, true) 68 c.Assert(err, ErrorMatches, ".*: context canceled") 69 } 70 71 func (s *ExecTestSuite) TestCombinedOutput(c *C) { 72 cmd := CommandContext(context.Background(), "echo", "foo") 73 out, err := cmd.CombinedOutput(fooLog, true) 74 c.Assert(err, IsNil) 75 c.Assert(string(out), Equals, "foo\n") 76 } 77 78 func (s *ExecTestSuite) TestCombinedOutputFailedTimeout(c *C) { 79 cmd := WithTimeout(timeout, "sleep", "inf") 80 time.Sleep(timeout) 81 _, err := cmd.CombinedOutput(fooLog, true) 82 c.Assert(err, ErrorMatches, "Command execution failed for .*: context deadline exceeded") 83 } 84 85 // LoggingHook is a simple hook which saves Warn messages to a slice of strings. 86 type LoggingHook struct { 87 Lines []string 88 } 89 90 func (h *LoggingHook) Levels() []logrus.Level { 91 // CombinedOutput logs stdout and stderr on WarnLevel. 92 return []logrus.Level{ 93 logrus.WarnLevel, 94 } 95 } 96 97 func (h *LoggingHook) Fire(entry *logrus.Entry) error { 98 serializedEntry, err := entry.String() 99 if err != nil { 100 return err 101 } 102 h.Lines = append(h.Lines, serializedEntry) 103 return nil 104 } 105 106 func (s *ExecTestSuite) TestWithFilters(c *C) { 107 hook := &LoggingHook{} 108 logging.DefaultLogger.Hooks.Add(hook) 109 logging.DefaultLogger.SetLevel(logrus.WarnLevel) 110 defer logging.DefaultLogger.SetLevel(logging.LevelStringToLogrusLevel[logging.DefaultLogLevelStr]) 111 112 // This command will print the following output to stderr: 113 // 114 // cat: /some/non/existing/file: No such file or directory 115 // cat: /non/existing/file/filtered/out: No such file or directory 116 // 117 // But the second message should be filtered out from logging. 118 cmd := CommandContext(context.Background(), 119 "cat", 120 "/non/existing/file", 121 "/non/existing/file/filtered/out").WithFilters("/filtered/out") 122 scopedLog := logging.DefaultLogger.WithField("foo", "bar") 123 out, err := cmd.CombinedOutput(scopedLog, true) 124 c.Assert(err, ErrorMatches, "exit status 1") 125 126 // Both errors be returned by CombinedOutput. 127 expectedOut := `cat: /non/existing/file: No such file or directory 128 cat: /non/existing/file/filtered/out: No such file or directory 129 ` 130 c.Assert(string(out), Equals, expectedOut) 131 132 // The last error message should be filtered out from logging. 133 logLines := []string{ 134 "level=warning msg=\"cat: /non/existing/file: No such file or directory\" foo=bar\n", 135 } 136 c.Assert(hook.Lines, checker.DeepEquals, logLines) 137 }