github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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) TestRun(c *C) {
    37  	defer release.MockOnClassic(false)()
    38  
    39  	r1 := sysdb.InjectTrusted(r.storeSigning.Trusted)
    40  	defer r1()
    41  	r2 := repair.MockTrustedRepairRootKeys([]*asserts.AccountKey{r.repairRootAcctKey})
    42  	defer r2()
    43  
    44  	r.freshState(c)
    45  
    46  	const script = `#!/bin/sh
    47  echo "happy output"
    48  echo "done" >&$SNAP_REPAIR_STATUS_FD
    49  exit 0
    50  `
    51  	seqRepairs := r.signSeqRepairs(c, []string{makeMockRepair(script)})
    52  	mockServer := makeMockServer(c, &seqRepairs, false)
    53  	defer mockServer.Close()
    54  
    55  	repair.MockBaseURL(mockServer.URL)
    56  
    57  	origArgs := os.Args
    58  	defer func() { os.Args = origArgs }()
    59  	os.Args = []string{"snap-repair", "run"}
    60  	err := repair.Run()
    61  	c.Check(err, IsNil)
    62  	c.Check(r.Stdout(), HasLen, 0)
    63  
    64  	c.Check(osutil.FileExists(filepath.Join(dirs.SnapRepairRunDir, "canonical", "1", "r0.done")), Equals, true)
    65  }
    66  
    67  func (r *repairSuite) TestRunAlreadyLocked(c *C) {
    68  	err := os.MkdirAll(dirs.SnapRunRepairDir, 0700)
    69  	c.Assert(err, IsNil)
    70  	flock, err := osutil.NewFileLock(filepath.Join(dirs.SnapRunRepairDir, "lock"))
    71  	c.Assert(err, IsNil)
    72  	err = flock.Lock()
    73  	c.Assert(err, IsNil)
    74  	defer flock.Unlock()
    75  
    76  	err = repair.ParseArgs([]string{"run"})
    77  	c.Check(err, ErrorMatches, `cannot run, another snap-repair run already executing`)
    78  }