github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap-repair/cmd_run_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 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 main_test
    21  
    22  import (
    23  	"os"
    24  	"path/filepath"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/asserts"
    29  	"github.com/snapcore/snapd/asserts/sysdb"
    30  	repair "github.com/snapcore/snapd/cmd/snap-repair"
    31  	"github.com/snapcore/snapd/dirs"
    32  	"github.com/snapcore/snapd/osutil"
    33  	"github.com/snapcore/snapd/release"
    34  )
    35  
    36  func (r *repairSuite) TestNonRoot(c *C) {
    37  	restore := repair.MockOsGetuid(func() int { return 1000 })
    38  	defer restore()
    39  	restore = release.MockOnClassic(false)
    40  	defer restore()
    41  
    42  	origArgs := os.Args
    43  	defer func() { os.Args = origArgs }()
    44  	os.Args = []string{"snap-repair", "run"}
    45  	err := repair.Run()
    46  	c.Assert(err, ErrorMatches, "must be run as root")
    47  }
    48  
    49  func (r *repairSuite) TestRun(c *C) {
    50  	restore := repair.MockOsGetuid(func() int { return 0 })
    51  	defer restore()
    52  	restore = release.MockOnClassic(false)
    53  	defer restore()
    54  
    55  	r1 := sysdb.InjectTrusted(r.storeSigning.Trusted)
    56  	defer r1()
    57  	r2 := repair.MockTrustedRepairRootKeys([]*asserts.AccountKey{r.repairRootAcctKey})
    58  	defer r2()
    59  
    60  	r.freshState(c)
    61  
    62  	const script = `#!/bin/sh
    63  echo "happy output"
    64  echo "done" >&$SNAP_REPAIR_STATUS_FD
    65  exit 0
    66  `
    67  	seqRepairs := r.signSeqRepairs(c, []string{makeMockRepair(script)})
    68  	mockServer := makeMockServer(c, &seqRepairs, false)
    69  	defer mockServer.Close()
    70  
    71  	repair.MockBaseURL(mockServer.URL)
    72  
    73  	origArgs := os.Args
    74  	defer func() { os.Args = origArgs }()
    75  	os.Args = []string{"snap-repair", "run"}
    76  	err := repair.Run()
    77  	c.Check(err, IsNil)
    78  	c.Check(r.Stdout(), HasLen, 0)
    79  
    80  	c.Check(osutil.FileExists(filepath.Join(dirs.SnapRepairRunDir, "canonical", "1", "r0.done")), Equals, true)
    81  }
    82  
    83  func (r *repairSuite) TestRunAlreadyLocked(c *C) {
    84  	err := os.MkdirAll(dirs.SnapRunRepairDir, 0700)
    85  	c.Assert(err, IsNil)
    86  	flock, err := osutil.NewFileLock(filepath.Join(dirs.SnapRunRepairDir, "lock"))
    87  	c.Assert(err, IsNil)
    88  	err = flock.Lock()
    89  	c.Assert(err, IsNil)
    90  	defer flock.Close() // Close unlocks too
    91  
    92  	err = repair.ParseArgs([]string{"run"})
    93  	c.Check(err, ErrorMatches, `cannot run, another snap-repair run already executing`)
    94  }