gitee.com/mysnapcore/mysnapd@v0.1.0/daemon/api_debug_seeding_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  	"net/http"
    24  	"time"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"gitee.com/mysnapcore/mysnapd/daemon"
    29  	"gitee.com/mysnapcore/mysnapd/overlord/state"
    30  )
    31  
    32  var _ = Suite(&seedingDebugSuite{})
    33  
    34  type seedingDebugSuite struct {
    35  	apiBaseSuite
    36  }
    37  
    38  func (s *seedingDebugSuite) SetUpTest(c *C) {
    39  	s.apiBaseSuite.SetUpTest(c)
    40  	s.daemonWithOverlordMock()
    41  }
    42  
    43  func (s *seedingDebugSuite) getSeedingDebug(c *C) interface{} {
    44  	req, err := http.NewRequest("GET", "/v2/debug?aspect=seeding", nil)
    45  	c.Assert(err, IsNil)
    46  
    47  	rsp := s.syncReq(c, req, nil)
    48  	c.Assert(rsp.Type, Equals, daemon.ResponseTypeSync)
    49  	return rsp.Result
    50  }
    51  
    52  func (s *seedingDebugSuite) TestNoData(c *C) {
    53  	data := s.getSeedingDebug(c)
    54  	c.Check(data, NotNil)
    55  	c.Check(data, DeepEquals, &daemon.SeedingInfo{})
    56  }
    57  
    58  func (s *seedingDebugSuite) TestSeedingDebug(c *C) {
    59  	seeded := true
    60  	preseeded := true
    61  	key1 := "foo"
    62  	key2 := "bar"
    63  
    64  	preseedStartTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:00Z")
    65  	c.Assert(err, IsNil)
    66  	preseedTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:01Z")
    67  	c.Assert(err, IsNil)
    68  	seedRestartTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:03Z")
    69  	c.Assert(err, IsNil)
    70  	seedTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:07Z")
    71  	c.Assert(err, IsNil)
    72  
    73  	st := s.d.Overlord().State()
    74  	st.Lock()
    75  
    76  	st.Set("preseeded", preseeded)
    77  	st.Set("seeded", seeded)
    78  
    79  	st.Set("preseed-system-key", key1)
    80  	st.Set("seed-restart-system-key", key2)
    81  
    82  	st.Set("preseed-start-time", preseedStartTime)
    83  	st.Set("seed-restart-time", seedRestartTime)
    84  
    85  	st.Set("preseed-time", preseedTime)
    86  	st.Set("seed-time", seedTime)
    87  
    88  	st.Unlock()
    89  
    90  	data := s.getSeedingDebug(c)
    91  	c.Check(data, DeepEquals, &daemon.SeedingInfo{
    92  		Seeded:               true,
    93  		Preseeded:            true,
    94  		PreseedSystemKey:     "foo",
    95  		SeedRestartSystemKey: "bar",
    96  		PreseedStartTime:     &preseedStartTime,
    97  		PreseedTime:          &preseedTime,
    98  		SeedRestartTime:      &seedRestartTime,
    99  		SeedTime:             &seedTime,
   100  	})
   101  }
   102  
   103  func (s *seedingDebugSuite) TestSeedingDebugSeededNoTimes(c *C) {
   104  	seedTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:07Z")
   105  	c.Assert(err, IsNil)
   106  
   107  	st := s.d.Overlord().State()
   108  	st.Lock()
   109  
   110  	// only set seed-time and seeded
   111  	st.Set("seed-time", seedTime)
   112  	st.Set("seeded", true)
   113  
   114  	st.Unlock()
   115  
   116  	data := s.getSeedingDebug(c)
   117  	c.Check(data, DeepEquals, &daemon.SeedingInfo{
   118  		Seeded:   true,
   119  		SeedTime: &seedTime,
   120  	})
   121  }
   122  
   123  func (s *seedingDebugSuite) TestSeedingDebugPreseededStillSeeding(c *C) {
   124  	preseedStartTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:00Z")
   125  	c.Assert(err, IsNil)
   126  	preseedTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:01Z")
   127  	c.Assert(err, IsNil)
   128  	seedRestartTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:03Z")
   129  	c.Assert(err, IsNil)
   130  
   131  	st := s.d.Overlord().State()
   132  	st.Lock()
   133  
   134  	st.Set("preseeded", true)
   135  	st.Set("seeded", false)
   136  
   137  	st.Set("preseed-system-key", "foo")
   138  	st.Set("seed-restart-system-key", "bar")
   139  
   140  	st.Set("preseed-start-time", preseedStartTime)
   141  	st.Set("seed-restart-time", seedRestartTime)
   142  
   143  	st.Set("preseed-time", preseedTime)
   144  
   145  	st.Unlock()
   146  
   147  	data := s.getSeedingDebug(c)
   148  	c.Check(data, DeepEquals, &daemon.SeedingInfo{
   149  		Seeded:               false,
   150  		Preseeded:            true,
   151  		PreseedSystemKey:     "foo",
   152  		SeedRestartSystemKey: "bar",
   153  		PreseedStartTime:     &preseedStartTime,
   154  		PreseedTime:          &preseedTime,
   155  		SeedRestartTime:      &seedRestartTime,
   156  	})
   157  }
   158  
   159  func (s *seedingDebugSuite) TestSeedingDebugPreseededSeedError(c *C) {
   160  	preseedStartTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:00Z")
   161  	c.Assert(err, IsNil)
   162  	preseedTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:01Z")
   163  	c.Assert(err, IsNil)
   164  	seedRestartTime, err := time.Parse(time.RFC3339, "2020-01-01T10:00:03Z")
   165  	c.Assert(err, IsNil)
   166  
   167  	st := s.d.Overlord().State()
   168  	st.Lock()
   169  
   170  	st.Set("preseeded", true)
   171  	st.Set("seeded", false)
   172  
   173  	st.Set("preseed-system-key", "foo")
   174  	st.Set("seed-restart-system-key", "bar")
   175  
   176  	st.Set("preseed-start-time", preseedStartTime)
   177  	st.Set("seed-restart-time", seedRestartTime)
   178  
   179  	st.Set("preseed-time", preseedTime)
   180  
   181  	chg1 := st.NewChange("seed", "tentative 1")
   182  	t11 := st.NewTask("seed task", "t11")
   183  	t12 := st.NewTask("seed task", "t12")
   184  	chg1.AddTask(t11)
   185  	chg1.AddTask(t12)
   186  	t11.SetStatus(state.UndoneStatus)
   187  	t11.Errorf("t11: undone")
   188  	t12.SetStatus(state.ErrorStatus)
   189  	t12.Errorf("t12: fail")
   190  
   191  	// ensure different spawn time
   192  	time.Sleep(50 * time.Millisecond)
   193  	chg2 := st.NewChange("seed", "tentative 2")
   194  	t21 := st.NewTask("seed task", "t21")
   195  	chg2.AddTask(t21)
   196  	t21.SetStatus(state.ErrorStatus)
   197  	t21.Errorf("t21: error")
   198  
   199  	chg3 := st.NewChange("seed", "tentative 3")
   200  	t31 := st.NewTask("seed task", "t31")
   201  	chg3.AddTask(t31)
   202  	t31.SetStatus(state.DoingStatus)
   203  
   204  	st.Unlock()
   205  
   206  	data := s.getSeedingDebug(c)
   207  	c.Check(data, DeepEquals, &daemon.SeedingInfo{
   208  		Seeded:               false,
   209  		Preseeded:            true,
   210  		PreseedSystemKey:     "foo",
   211  		SeedRestartSystemKey: "bar",
   212  		PreseedStartTime:     &preseedStartTime,
   213  		PreseedTime:          &preseedTime,
   214  		SeedRestartTime:      &seedRestartTime,
   215  		SeedError: `cannot perform the following tasks:
   216  - t12 (t12: fail)`,
   217  	})
   218  }