github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/sandbox/cgroup/process_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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 cgroup_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/sandbox/cgroup"
    30  )
    31  
    32  func (s *cgroupSuite) mockPidCgroup(c *C, text string) int {
    33  	f := filepath.Join(s.rootDir, "proc/333/cgroup")
    34  	c.Assert(os.MkdirAll(filepath.Dir(f), 0755), IsNil)
    35  	c.Assert(ioutil.WriteFile(f, []byte(text), 0755), IsNil)
    36  	return 333
    37  }
    38  
    39  func (s *cgroupSuite) TestSnapNameFromPidHappy(c *C) {
    40  	pid := s.mockPidCgroup(c, string(mockCgroup)) // defined in cgroup_test.go
    41  	name, err := cgroup.SnapNameFromPid(pid)
    42  	c.Assert(err, IsNil)
    43  	c.Check(name, Equals, "hello-world")
    44  }
    45  
    46  func (s *cgroupSuite) TestSnapNameFromPidUnhappy(c *C) {
    47  	pid := s.mockPidCgroup(c, "1:freezer:/\n")
    48  	name, err := cgroup.SnapNameFromPid(pid)
    49  	c.Assert(err, ErrorMatches, "cannot find a snap for pid .*")
    50  	c.Check(name, Equals, "")
    51  }
    52  
    53  func (s *cgroupSuite) TestSnapNameFromPidEmptyName(c *C) {
    54  	pid := s.mockPidCgroup(c, "1:freezer:/snap./\n")
    55  	name, err := cgroup.SnapNameFromPid(pid)
    56  	c.Assert(err, ErrorMatches, "snap name in cgroup path is empty")
    57  	c.Check(name, Equals, "")
    58  }
    59  
    60  func (s *cgroupSuite) TestSnapNameFromPidTracking(c *C) {
    61  	pid := s.mockPidCgroup(c, "1:name=systemd:/user.slice/user-1000.slice/user@1000.service/apps.slice/snap.foo.bar.00000-1111-3333.service\n")
    62  	name, err := cgroup.SnapNameFromPid(pid)
    63  	c.Assert(err, IsNil)
    64  	c.Check(name, Equals, "foo")
    65  }
    66  
    67  func (s *cgroupSuite) TestSnapNameFromPidWithoutSources(c *C) {
    68  	restore := cgroup.MockVersion(cgroup.V2, nil)
    69  	defer restore()
    70  
    71  	pid := s.mockPidCgroup(c, "")
    72  	name, err := cgroup.SnapNameFromPid(pid)
    73  	c.Assert(err, ErrorMatches, "not supported")
    74  	c.Check(name, Equals, "")
    75  }