github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/snapstate/handlers_discard_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 snapstate_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/overlord/snapstate"
    26  	"github.com/snapcore/snapd/overlord/state"
    27  	"github.com/snapcore/snapd/snap"
    28  )
    29  
    30  type discardSnapSuite struct {
    31  	baseHandlerSuite
    32  }
    33  
    34  var _ = Suite(&discardSnapSuite{})
    35  
    36  func (s *discardSnapSuite) TestDoDiscardSnapSuccess(c *C) {
    37  	s.state.Lock()
    38  	snapstate.Set(s.state, "foo", &snapstate.SnapState{
    39  		Sequence: []*snap.SideInfo{
    40  			{RealName: "foo", Revision: snap.R(3)},
    41  			{RealName: "foo", Revision: snap.R(33)},
    42  		},
    43  		Current:  snap.R(33),
    44  		SnapType: "app",
    45  	})
    46  	t := s.state.NewTask("discard-snap", "test")
    47  	t.Set("snap-setup", &snapstate.SnapSetup{
    48  		SideInfo: &snap.SideInfo{
    49  			RealName: "foo",
    50  			Revision: snap.R(33),
    51  		},
    52  	})
    53  	s.state.NewChange("dummy", "...").AddTask(t)
    54  
    55  	s.state.Unlock()
    56  
    57  	s.se.Ensure()
    58  	s.se.Wait()
    59  
    60  	s.state.Lock()
    61  	defer s.state.Unlock()
    62  	var snapst snapstate.SnapState
    63  	err := snapstate.Get(s.state, "foo", &snapst)
    64  	c.Assert(err, IsNil)
    65  
    66  	c.Check(snapst.Sequence, HasLen, 1)
    67  	c.Check(snapst.Current, Equals, snap.R(3))
    68  	c.Check(t.Status(), Equals, state.DoneStatus)
    69  }
    70  
    71  func (s *discardSnapSuite) TestDoDiscardSnapToEmpty(c *C) {
    72  	s.state.Lock()
    73  	snapstate.Set(s.state, "foo", &snapstate.SnapState{
    74  		Sequence: []*snap.SideInfo{
    75  			{RealName: "foo", Revision: snap.R(3)},
    76  		},
    77  		Current:  snap.R(3),
    78  		SnapType: "app",
    79  	})
    80  	t := s.state.NewTask("discard-snap", "test")
    81  	t.Set("snap-setup", &snapstate.SnapSetup{
    82  		SideInfo: &snap.SideInfo{
    83  			RealName: "foo",
    84  			Revision: snap.R(33),
    85  		},
    86  	})
    87  	s.state.NewChange("dummy", "...").AddTask(t)
    88  
    89  	s.state.Unlock()
    90  
    91  	s.se.Ensure()
    92  	s.se.Wait()
    93  
    94  	s.state.Lock()
    95  	defer s.state.Unlock()
    96  	var snapst snapstate.SnapState
    97  	err := snapstate.Get(s.state, "foo", &snapst)
    98  	c.Assert(err, Equals, state.ErrNoState)
    99  }
   100  
   101  func (s *discardSnapSuite) TestDoDiscardSnapErrorsForActive(c *C) {
   102  	s.state.Lock()
   103  	snapstate.Set(s.state, "foo", &snapstate.SnapState{
   104  		Sequence: []*snap.SideInfo{
   105  			{RealName: "foo", Revision: snap.R(3)},
   106  		},
   107  		Current:  snap.R(3),
   108  		Active:   true,
   109  		SnapType: "app",
   110  	})
   111  	t := s.state.NewTask("discard-snap", "test")
   112  	t.Set("snap-setup", &snapstate.SnapSetup{
   113  		SideInfo: &snap.SideInfo{
   114  			RealName: "foo",
   115  			Revision: snap.R(3),
   116  		},
   117  	})
   118  	chg := s.state.NewChange("dummy", "...")
   119  	chg.AddTask(t)
   120  
   121  	s.state.Unlock()
   122  
   123  	s.se.Ensure()
   124  	s.se.Wait()
   125  
   126  	s.state.Lock()
   127  	defer s.state.Unlock()
   128  
   129  	c.Check(chg.Status(), Equals, state.ErrorStatus)
   130  	c.Check(chg.Err(), ErrorMatches, `(?s).*internal error: cannot discard snap "foo": still active.*`)
   131  }
   132  
   133  func (s *discardSnapSuite) TestDoDiscardSnapNoErrorsForActive(c *C) {
   134  	s.state.Lock()
   135  	snapstate.Set(s.state, "foo", &snapstate.SnapState{
   136  		Sequence: []*snap.SideInfo{
   137  			{RealName: "foo", Revision: snap.R(3)},
   138  			{RealName: "foo", Revision: snap.R(33)},
   139  		},
   140  		Current:  snap.R(33),
   141  		Active:   true,
   142  		SnapType: "app",
   143  	})
   144  	t := s.state.NewTask("discard-snap", "test")
   145  	t.Set("snap-setup", &snapstate.SnapSetup{
   146  		SideInfo: &snap.SideInfo{
   147  			RealName: "foo",
   148  			Revision: snap.R(3),
   149  		},
   150  	})
   151  	chg := s.state.NewChange("dummy", "...")
   152  	chg.AddTask(t)
   153  
   154  	s.state.Unlock()
   155  
   156  	s.se.Ensure()
   157  	s.se.Wait()
   158  
   159  	s.state.Lock()
   160  	defer s.state.Unlock()
   161  
   162  	var snapst snapstate.SnapState
   163  	err := snapstate.Get(s.state, "foo", &snapst)
   164  	c.Assert(err, IsNil)
   165  
   166  	c.Assert(chg.Err(), IsNil)
   167  	c.Check(snapst.Sequence, HasLen, 1)
   168  	c.Check(snapst.Current, Equals, snap.R(33))
   169  	c.Check(t.Status(), Equals, state.DoneStatus)
   170  }