github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap-repair/cmd_done_retry_skip_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  	"io/ioutil"
    24  	"os"
    25  	"strconv"
    26  	"syscall"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	repair "github.com/snapcore/snapd/cmd/snap-repair"
    31  )
    32  
    33  func (r *repairSuite) TestStatusNoStatusFdEnv(c *C) {
    34  	for _, s := range []string{"done", "skip", "retry"} {
    35  		err := repair.ParseArgs([]string{s})
    36  		c.Check(err, ErrorMatches, "cannot find SNAP_REPAIR_STATUS_FD environment")
    37  	}
    38  }
    39  
    40  func (r *repairSuite) TestStatusBadStatusFD(c *C) {
    41  	for _, s := range []string{"done", "skip", "retry"} {
    42  		os.Setenv("SNAP_REPAIR_STATUS_FD", "123456789")
    43  		defer os.Unsetenv("SNAP_REPAIR_STATUS_FD")
    44  
    45  		err := repair.ParseArgs([]string{s})
    46  		c.Check(err, ErrorMatches, `write <snap-repair-status-fd>: bad file descriptor`)
    47  	}
    48  }
    49  
    50  func (r *repairSuite) TestStatusUnparsableStatusFD(c *C) {
    51  	for _, s := range []string{"done", "skip", "retry"} {
    52  		os.Setenv("SNAP_REPAIR_STATUS_FD", "xxx")
    53  		defer os.Unsetenv("SNAP_REPAIR_STATUS_FD")
    54  
    55  		err := repair.ParseArgs([]string{s})
    56  		c.Check(err, ErrorMatches, `cannot parse SNAP_REPAIR_STATUS_FD environment: strconv.*: parsing "xxx": invalid syntax`)
    57  	}
    58  }
    59  
    60  func (r *repairSuite) TestStatusHappy(c *C) {
    61  	for _, s := range []string{"done", "skip", "retry"} {
    62  		rp, wp, err := os.Pipe()
    63  		c.Assert(err, IsNil)
    64  		defer rp.Close()
    65  		defer wp.Close()
    66  
    67  		fd, e := syscall.Dup(int(wp.Fd()))
    68  		c.Assert(e, IsNil)
    69  		wp.Close()
    70  
    71  		os.Setenv("SNAP_REPAIR_STATUS_FD", strconv.Itoa(fd))
    72  		defer os.Unsetenv("SNAP_REPAIR_STATUS_FD")
    73  
    74  		err = repair.ParseArgs([]string{s})
    75  		c.Check(err, IsNil)
    76  
    77  		status, err := ioutil.ReadAll(rp)
    78  		c.Assert(err, IsNil)
    79  		c.Check(string(status), Equals, s+"\n")
    80  	}
    81  }