github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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.req(c, req, nil).(*daemon.Resp)
    57  	c.Check(rsp.Type, Equals, daemon.ResponseTypeSync)
    58  	c.Assert(rsp.Result, DeepEquals, &daemon.ConsoleConfStartRoutineResult{})
    59  
    60  	// we did set the refresh.hold time back 20 minutes though
    61  	st.Lock()
    62  	defer st.Unlock()
    63  
    64  	tr := config.NewTransaction(st)
    65  	var t1 time.Time
    66  	err = tr.Get("core", "refresh.hold", &t1)
    67  	c.Assert(err, IsNil)
    68  
    69  	c.Assert(t0.Add(20*time.Minute).After(t1), Equals, false)
    70  
    71  	// if we add some changes to state that are in progress, then they are
    72  	// returned
    73  
    74  	// now make some auto-refresh changes to make sure we get those figured out
    75  	chg0 := st.NewChange("auto-refresh", "auto-refresh-the-things")
    76  	chg0.AddTask(st.NewTask("nop", "do nothing"))
    77  
    78  	// make it in doing state
    79  	chg0.SetStatus(state.DoingStatus)
    80  	chg0.Set("snap-names", []string{"doing-snap"})
    81  
    82  	// this one will be picked up too
    83  	chg1 := st.NewChange("auto-refresh", "auto-refresh-the-things")
    84  	chg1.AddTask(st.NewTask("nop", "do nothing"))
    85  	chg1.SetStatus(state.DoStatus)
    86  	chg1.Set("snap-names", []string{"do-snap"})
    87  
    88  	// this one won't, it's Done
    89  	chg2 := st.NewChange("auto-refresh", "auto-refresh-the-things")
    90  	chg2.AddTask(st.NewTask("nop", "do nothing"))
    91  	chg2.SetStatus(state.DoneStatus)
    92  	chg2.Set("snap-names", []string{"done-snap"})
    93  
    94  	// nor this one, it's Undone
    95  	chg3 := st.NewChange("auto-refresh", "auto-refresh-the-things")
    96  	chg3.AddTask(st.NewTask("nop", "do nothing"))
    97  	chg3.SetStatus(state.UndoneStatus)
    98  	chg3.Set("snap-names", []string{"undone-snap"})
    99  
   100  	st.Unlock()
   101  	defer st.Lock()
   102  
   103  	req2, err := http.NewRequest("POST", "/v2/internal/console-conf-start", body)
   104  	c.Assert(err, IsNil)
   105  	rsp2 := s.req(c, req2, nil).(*daemon.Resp)
   106  	c.Check(rsp2.Type, Equals, daemon.ResponseTypeSync)
   107  	c.Assert(rsp2.Result, FitsTypeOf, &daemon.ConsoleConfStartRoutineResult{})
   108  	res := rsp2.Result.(*daemon.ConsoleConfStartRoutineResult)
   109  	sort.Strings(res.ActiveAutoRefreshChanges)
   110  	sort.Strings(res.ActiveAutoRefreshSnaps)
   111  	expChgs := []string{chg0.ID(), chg1.ID()}
   112  	sort.Strings(expChgs)
   113  	c.Assert(res, DeepEquals, &daemon.ConsoleConfStartRoutineResult{
   114  		ActiveAutoRefreshChanges: expChgs,
   115  		ActiveAutoRefreshSnaps:   []string{"do-snap", "doing-snap"},
   116  	})
   117  }