github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/bugtool/cmd/helper_test.go (about)

     1  // Copyright 2017-2019 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 cmd
    18  
    19  import (
    20  	"archive/tar"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	. "gopkg.in/check.v1"
    28  )
    29  
    30  func Test(t *testing.T) {
    31  	TestingT(t)
    32  }
    33  
    34  type BugtoolSuite struct{}
    35  
    36  var (
    37  	_ = Suite(&BugtoolSuite{})
    38  
    39  	baseDir, tmpDir string
    40  )
    41  
    42  type dummyTarWriter struct{}
    43  
    44  func (t *dummyTarWriter) Write(p []byte) (n int, err error) {
    45  	// Ignore / discard all output.
    46  	return len(p), nil
    47  }
    48  
    49  func (t *dummyTarWriter) WriteHeader(h *tar.Header) error {
    50  	// Ignore / discard all output.
    51  	return nil
    52  }
    53  
    54  type logWrapper struct {
    55  	logf func(format string, args ...interface{})
    56  }
    57  
    58  func (l *logWrapper) Write(p []byte) (n int, err error) {
    59  	l.logf("%s", p)
    60  	return len(p), nil
    61  }
    62  
    63  func (b *BugtoolSuite) SetUpSuite(c *C) {
    64  	var err error
    65  	baseDir, err = ioutil.TempDir("", "cilium_test_bugtool_base")
    66  	c.Assert(err, IsNil)
    67  	tmpDir, err = ioutil.TempDir("", "cilium_test_bugtool_tmp")
    68  	c.Assert(err, IsNil)
    69  }
    70  
    71  func (b *BugtoolSuite) TearDownSuite(c *C) {
    72  	os.RemoveAll(baseDir)
    73  	os.RemoveAll(tmpDir)
    74  }
    75  
    76  // TestWalkPath tests that with various different error types, we can safely
    77  // back out and continue with the filepath walk. This allows gathering of other
    78  // information in a bugtool run when there's an issue with a particular file.
    79  func (b *BugtoolSuite) TestWalkPath(c *C) {
    80  	w := newWalker(baseDir, tmpDir, &dummyTarWriter{}, &logWrapper{c.Logf})
    81  	c.Assert(w, NotNil)
    82  
    83  	// Invalid paths
    84  	invalidFile := "doesnotexist"
    85  	err := w.walkPath(invalidFile, nil, nil)
    86  	c.Assert(err, IsNil)
    87  	err = w.walkPath(invalidFile, nil, fmt.Errorf("ignore me please"))
    88  	c.Assert(err, IsNil)
    89  
    90  	// Invalid symlink
    91  	invalidLink := filepath.Join(baseDir, "totes_real_link")
    92  	err = os.Symlink(invalidFile, invalidLink)
    93  	c.Assert(err, IsNil)
    94  	_, err = os.Stat(invalidLink)
    95  	c.Assert(err, NotNil)
    96  	info, err := os.Lstat(invalidLink)
    97  	c.Assert(err, IsNil)
    98  	err = w.walkPath(invalidLink, info, nil)
    99  	c.Assert(err, IsNil)
   100  
   101  	// With real file
   102  	realFile, err := ioutil.TempFile(baseDir, "test")
   103  	c.Assert(err, IsNil)
   104  	info, err = os.Stat(realFile.Name())
   105  	c.Assert(err, IsNil)
   106  	err = w.walkPath(realFile.Name(), info, nil)
   107  	c.Assert(err, IsNil)
   108  
   109  	// With real link to real file
   110  	realLink := filepath.Join(baseDir, "test_link")
   111  	err = os.Symlink(realFile.Name(), realLink)
   112  	c.Assert(err, IsNil)
   113  	info, err = os.Lstat(realLink)
   114  	c.Assert(err, IsNil)
   115  	err = w.walkPath(realLink, info, nil)
   116  	c.Assert(err, IsNil)
   117  
   118  	// With directory
   119  	nestedDir, err := ioutil.TempDir(baseDir, "nested")
   120  	c.Assert(err, IsNil)
   121  	info, err = os.Stat(nestedDir)
   122  	c.Assert(err, IsNil)
   123  	err = w.walkPath(nestedDir, info, nil)
   124  	c.Assert(err, IsNil)
   125  }