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