github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/snap/squashfs/export_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 squashfs
    21  
    22  import (
    23  	"os"
    24  	"os/exec"
    25  	"time"
    26  
    27  	"gopkg.in/check.v1"
    28  )
    29  
    30  var (
    31  	FromRaw                   = fromRaw
    32  	NewUnsquashfsStderrWriter = newUnsquashfsStderrWriter
    33  )
    34  
    35  const (
    36  	SuperblockSize = superblockSize
    37  	MaxErrPaths    = maxErrPaths
    38  )
    39  
    40  func (s stat) User() string  { return s.user }
    41  func (s stat) Group() string { return s.group }
    42  
    43  func MockLink(newLink func(string, string) error) (restore func()) {
    44  	oldLink := osLink
    45  	osLink = newLink
    46  	return func() {
    47  		osLink = oldLink
    48  	}
    49  }
    50  
    51  func MockCommandFromSystemSnap(f func(string, ...string) (*exec.Cmd, error)) (restore func()) {
    52  	oldCommandFromSystemSnap := snapdtoolCommandFromSystemSnap
    53  	snapdtoolCommandFromSystemSnap = f
    54  	return func() {
    55  		snapdtoolCommandFromSystemSnap = oldCommandFromSystemSnap
    56  	}
    57  }
    58  
    59  // Alike compares to os.FileInfo to determine if they are sufficiently
    60  // alike to say they refer to the same thing.
    61  func Alike(a, b os.FileInfo, c *check.C, comment check.CommentInterface) {
    62  	c.Check(a, check.NotNil, comment)
    63  	c.Check(b, check.NotNil, comment)
    64  	if a == nil || b == nil {
    65  		return
    66  	}
    67  
    68  	// the .Name() of the root will be different on non-squashfs things
    69  	_, asq := a.(*stat)
    70  	_, bsq := b.(*stat)
    71  	if !((asq && a.Name() == "/") || (bsq && b.Name() == "/")) {
    72  		c.Check(a.Name(), check.Equals, b.Name(), comment)
    73  	}
    74  
    75  	c.Check(a.Mode(), check.Equals, b.Mode(), comment)
    76  	if a.Mode().IsRegular() {
    77  		c.Check(a.Size(), check.Equals, b.Size(), comment)
    78  	}
    79  	am := a.ModTime().UTC().Truncate(time.Minute)
    80  	bm := b.ModTime().UTC().Truncate(time.Minute)
    81  	c.Check(am.Equal(bm), check.Equals, true, check.Commentf("%s != %s (%s)", am, bm, comment))
    82  }
    83  
    84  func MockIsRootWritableOverlay(new func() (string, error)) (restore func()) {
    85  	old := isRootWritableOverlay
    86  	isRootWritableOverlay = new
    87  	return func() {
    88  		isRootWritableOverlay = old
    89  	}
    90  }