github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap-repair/cmd_show_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  	"fmt"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	repair "github.com/snapcore/snapd/cmd/snap-repair"
    30  	"github.com/snapcore/snapd/dirs"
    31  )
    32  
    33  func (r *repairSuite) TestShowRepairSingle(c *C) {
    34  	makeMockRepairState(c)
    35  
    36  	err := repair.ParseArgs([]string{"show", "canonical-1"})
    37  	c.Check(err, IsNil)
    38  	c.Check(r.Stdout(), Equals, `repair: canonical-1
    39  revision: 3
    40  status: retry
    41  summary: repair one
    42  script:
    43    #!/bin/sh
    44    echo retry output
    45  output:
    46    retry output
    47  
    48  `)
    49  
    50  }
    51  
    52  func (r *repairSuite) TestShowRepairMultiple(c *C) {
    53  	makeMockRepairState(c)
    54  
    55  	// repair.ParseArgs() always appends to its internal slice:
    56  	// cmdShow.Positional.Repair. To workaround this we create a
    57  	// new cmdShow here
    58  	err := repair.NewCmdShow("canonical-1", "my-brand-1", "my-brand-2").Execute(nil)
    59  	c.Check(err, IsNil)
    60  	c.Check(r.Stdout(), Equals, `repair: canonical-1
    61  revision: 3
    62  status: retry
    63  summary: repair one
    64  script:
    65    #!/bin/sh
    66    echo retry output
    67  output:
    68    retry output
    69  
    70  repair: my-brand-1
    71  revision: 1
    72  status: done
    73  summary: my-brand repair one
    74  script:
    75    #!/bin/sh
    76    echo done output
    77  output:
    78    done output
    79  
    80  repair: my-brand-2
    81  revision: 2
    82  status: skip
    83  summary: my-brand repair two
    84  script:
    85    #!/bin/sh
    86    echo skip output
    87  output:
    88    skip output
    89  
    90  `)
    91  }
    92  
    93  func (r *repairSuite) TestShowRepairErrorNoRepairDir(c *C) {
    94  	dirs.SetRootDir(c.MkDir())
    95  
    96  	err := repair.NewCmdShow("canonical-1").Execute(nil)
    97  	c.Check(err, ErrorMatches, `cannot find repair "canonical-1"`)
    98  }
    99  
   100  func (r *repairSuite) TestShowRepairSingleWithoutScript(c *C) {
   101  	makeMockRepairState(c)
   102  	scriptPath := filepath.Join(dirs.SnapRepairRunDir, "canonical/1", "r3.script")
   103  	err := os.Remove(scriptPath)
   104  	c.Assert(err, IsNil)
   105  
   106  	err = repair.NewCmdShow("canonical-1").Execute(nil)
   107  	c.Check(err, IsNil)
   108  	c.Check(r.Stdout(), Equals, fmt.Sprintf(`repair: canonical-1
   109  revision: 3
   110  status: retry
   111  summary: repair one
   112  script:
   113    error: open %s: no such file or directory
   114  output:
   115    retry output
   116  
   117  `, scriptPath))
   118  
   119  }
   120  
   121  func (r *repairSuite) TestShowRepairSingleUnreadableOutput(c *C) {
   122  	makeMockRepairState(c)
   123  	scriptPath := filepath.Join(dirs.SnapRepairRunDir, "canonical/1", "r3.retry")
   124  	err := os.Chmod(scriptPath, 0000)
   125  	c.Assert(err, IsNil)
   126  	defer os.Chmod(scriptPath, 0644)
   127  
   128  	err = repair.NewCmdShow("canonical-1").Execute(nil)
   129  	c.Check(err, IsNil)
   130  	c.Check(r.Stdout(), Equals, fmt.Sprintf(`repair: canonical-1
   131  revision: 3
   132  status: retry
   133  summary: -
   134  script:
   135    #!/bin/sh
   136    echo retry output
   137  output:
   138    error: open %s: permission denied
   139  
   140  `, scriptPath))
   141  
   142  }