github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/osutil/uname_linux_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2018 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package osutil_test
    21  
    22  import (
    23  	"bytes"
    24  	"errors"
    25  	"os/exec"
    26  	"syscall"
    27  
    28  	"gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/osutil"
    31  )
    32  
    33  type unameSuite struct{}
    34  
    35  var _ = check.Suite(unameSuite{})
    36  
    37  func ucmd1(c *check.C, arg string) string {
    38  	out, err := exec.Command("uname", arg).CombinedOutput()
    39  	c.Assert(err, check.IsNil)
    40  	return string(bytes.TrimSpace(out))
    41  }
    42  
    43  func (unameSuite) TestUname(c *check.C) {
    44  	c.Check(osutil.KernelVersion(), check.Equals, ucmd1(c, "-r"))
    45  	c.Check(osutil.MachineName(), check.Equals, ucmd1(c, "-m"))
    46  }
    47  
    48  func (unameSuite) TestUnameErrorMeansUnknown(c *check.C) {
    49  	restore := osutil.MockUname(func(buf *syscall.Utsname) error {
    50  		return errors.New("bzzzt")
    51  	})
    52  	defer restore()
    53  
    54  	c.Check(osutil.KernelVersion(), check.Equals, "unknown")
    55  	c.Check(osutil.MachineName(), check.Equals, "unknown")
    56  }
    57  
    58  func (unameSuite) TestKernelVersionStopsAtZeroes(c *check.C) {
    59  	restore := osutil.MockUname(func(buf *syscall.Utsname) error {
    60  		buf.Release[0] = 'f'
    61  		buf.Release[1] = 'o'
    62  		buf.Release[2] = 'o'
    63  		buf.Release[3] = 0
    64  		buf.Release[4] = 'u'
    65  		buf.Release[5] = 'n'
    66  		buf.Release[6] = 'u'
    67  		buf.Release[7] = 's'
    68  		buf.Release[8] = 'e'
    69  		buf.Release[9] = 'd'
    70  		return nil
    71  	})
    72  	defer restore()
    73  
    74  	c.Check(osutil.KernelVersion(), check.Equals, "foo")
    75  }
    76  
    77  func (unameSuite) TestMachineNameStopsAtZeroes(c *check.C) {
    78  	restore := osutil.MockUname(func(buf *syscall.Utsname) error {
    79  		buf.Machine[0] = 'a'
    80  		buf.Machine[1] = 'r'
    81  		buf.Machine[2] = 'm'
    82  		buf.Machine[3] = 'v'
    83  		buf.Machine[4] = '7'
    84  		buf.Machine[5] = 'a'
    85  		buf.Machine[6] = 0
    86  		buf.Machine[7] = 'u'
    87  		buf.Machine[8] = 'n'
    88  		buf.Machine[9] = 'u'
    89  		buf.Machine[10] = 's'
    90  		buf.Machine[11] = 'e'
    91  		buf.Machine[12] = 'd'
    92  		return nil
    93  	})
    94  	defer restore()
    95  	c.Check(osutil.MachineName(), check.Equals, "armv7a")
    96  }