github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/daemon/api_console_conf_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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 daemon_test
    21  
    22  import (
    23  	"bytes"
    24  	"net/http"
    25  	"sort"
    26  	"time"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/daemon"
    31  	"github.com/snapcore/snapd/overlord/configstate/config"
    32  	"github.com/snapcore/snapd/overlord/snapstate"
    33  	"github.com/snapcore/snapd/overlord/state"
    34  )
    35  
    36  var _ = Suite(&consoleConfSuite{})
    37  
    38  type consoleConfSuite struct {
    39  	apiBaseSuite
    40  }
    41  
    42  func (s *consoleConfSuite) TestPostConsoleConfStartRoutine(c *C) {
    43  	t0 := time.Now()
    44  	d := s.daemonWithOverlordMock(c)
    45  	snapMgr, err := snapstate.Manager(d.Overlord().State(), d.Overlord().TaskRunner())
    46  	c.Assert(err, IsNil)
    47  	d.Overlord().AddManager(snapMgr)
    48  
    49  	st := d.Overlord().State()
    50  
    51  	body := bytes.NewBuffer(nil)
    52  	req, err := http.NewRequest("POST", "/v2/internal/console-conf-start", body)
    53  	c.Assert(err, IsNil)
    54  
    55  	// no changes in state, no changes in response
    56  	rsp := s.syncReq(c, req, nil)
    57  	c.Assert(rsp.Result, DeepEquals, &daemon.ConsoleConfStartRoutineResult{})
    58  
    59  	// we did set the refresh.hold time back 20 minutes though
    60  	st.Lock()
    61  	defer st.Unlock()
    62  
    63  	tr := config.NewTransaction(st)
    64  	var t1 time.Time
    65  	err = tr.Get("core", "refresh.hold", &t1)
    66  	c.Assert(err, IsNil)
    67  
    68  	c.Assert(t0.Add(20*time.Minute).After(t1), Equals, false)
    69  
    70  	// if we add some changes to state that are in progress, then they are
    71  	// returned
    72  
    73  	// now make some auto-refresh changes to make sure we get those figured out
    74  	chg0 := st.NewChange("auto-refresh", "auto-refresh-the-things")
    75  	chg0.AddTask(st.NewTask("nop", "do nothing"))
    76  
    77  	// make it in doing state
    78  	chg0.SetStatus(state.DoingStatus)
    79  	chg0.Set("snap-names", []string{"doing-snap"})
    80  
    81  	// this one will be picked up too
    82  	chg1 := st.NewChange("auto-refresh", "auto-refresh-the-things")
    83  	chg1.AddTask(st.NewTask("nop", "do nothing"))
    84  	chg1.SetStatus(state.DoStatus)
    85  	chg1.Set("snap-names", []string{"do-snap"})
    86  
    87  	// this one won't, it's Done
    88  	chg2 := st.NewChange("auto-refresh", "auto-refresh-the-things")
    89  	chg2.AddTask(st.NewTask("nop", "do nothing"))
    90  	chg2.SetStatus(state.DoneStatus)
    91  	chg2.Set("snap-names", []string{"done-snap"})
    92  
    93  	// nor this one, it's Undone
    94  	chg3 := st.NewChange("auto-refresh", "auto-refresh-the-things")
    95  	chg3.AddTask(st.NewTask("nop", "do nothing"))
    96  	chg3.SetStatus(state.UndoneStatus)
    97  	chg3.Set("snap-names", []string{"undone-snap"})
    98  
    99  	st.Unlock()
   100  	defer st.Lock()
   101  
   102  	req2, err := http.NewRequest("POST", "/v2/internal/console-conf-start", body)
   103  	c.Assert(err, IsNil)
   104  	rsp2 := s.syncReq(c, req2, nil)
   105  	c.Assert(rsp2.Result, FitsTypeOf, &daemon.ConsoleConfStartRoutineResult{})
   106  	res := rsp2.Result.(*daemon.ConsoleConfStartRoutineResult)
   107  	sort.Strings(res.ActiveAutoRefreshChanges)
   108  	sort.Strings(res.ActiveAutoRefreshSnaps)
   109  	expChgs := []string{chg0.ID(), chg1.ID()}
   110  	sort.Strings(expChgs)
   111  	c.Assert(res, DeepEquals, &daemon.ConsoleConfStartRoutineResult{
   112  		ActiveAutoRefreshChanges: expChgs,
   113  		ActiveAutoRefreshSnaps:   []string{"do-snap", "doing-snap"},
   114  	})
   115  }