pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/netutil/netutil_linux_test.go (about)

     1  package netutil
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"io/ioutil"
    12  	"os"
    13  	"testing"
    14  
    15  	. "pkg.re/essentialkaos/check.v1"
    16  )
    17  
    18  // ////////////////////////////////////////////////////////////////////////////////// //
    19  
    20  func Test(t *testing.T) { TestingT(t) }
    21  
    22  type NetUtilSuite struct{}
    23  
    24  // ////////////////////////////////////////////////////////////////////////////////// //
    25  
    26  var _ = Suite(&NetUtilSuite{})
    27  
    28  // ////////////////////////////////////////////////////////////////////////////////// //
    29  
    30  func (s *NetUtilSuite) TestCommon(c *C) {
    31  	c.Assert(getDefaultRouteInterface(), Not(Equals), "")
    32  
    33  	procRouteFile = "/tmp/not-exist"
    34  
    35  	c.Assert(getDefaultRouteInterface(), Equals, "")
    36  
    37  	procRouteFile = s.CreateTestFile(c, "Iface   Destination     Gateway         Flags   RefCnt  Use     Metric  Mask            MTU     Window  IRTT\neth0    0070652E        00000000        0001    0       0       0       00F0FFFF        0       0       0")
    38  
    39  	c.Assert(getDefaultRouteInterface(), Equals, "")
    40  }
    41  
    42  func (s *NetUtilSuite) TestGetIP(c *C) {
    43  	c.Assert(GetIP(), Not(Equals), "")
    44  }
    45  
    46  func (s *NetUtilSuite) TestGetIP6(c *C) {
    47  	if os.Getenv("CI") == "" {
    48  		c.Assert(GetIP6(), Not(Equals), "")
    49  	}
    50  }
    51  
    52  // ////////////////////////////////////////////////////////////////////////////////// //
    53  
    54  func (s *NetUtilSuite) CreateTestFile(c *C, data string) string {
    55  	tmpDir := c.MkDir()
    56  	tmpFile := tmpDir + "/test.file"
    57  
    58  	if ioutil.WriteFile(tmpFile, []byte(data), 0644) != nil {
    59  		c.Fatal("Can't create temporary file")
    60  	}
    61  
    62  	return tmpFile
    63  }