github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/daemon/api_cohort_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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  	"context"
    24  	"errors"
    25  	"net/http"
    26  	"strings"
    27  
    28  	"gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/daemon"
    31  	"github.com/snapcore/snapd/dirs"
    32  	"github.com/snapcore/snapd/overlord"
    33  	"github.com/snapcore/snapd/overlord/snapstate"
    34  	"github.com/snapcore/snapd/store/storetest"
    35  )
    36  
    37  var _ = check.Suite(&cohortSuite{})
    38  
    39  type cohortSuite struct {
    40  	storetest.Store
    41  	d *daemon.Daemon
    42  
    43  	snaps []string
    44  	coh   map[string]string
    45  	err   error
    46  }
    47  
    48  func (s *cohortSuite) CreateCohorts(_ context.Context, snaps []string) (map[string]string, error) {
    49  	s.snaps = snaps[:]
    50  	return s.coh, s.err
    51  }
    52  
    53  func (s *cohortSuite) SetUpTest(c *check.C) {
    54  	s.snaps = nil
    55  	s.coh = nil
    56  	s.err = nil
    57  
    58  	o := overlord.Mock()
    59  	s.d = daemon.NewWithOverlord(o)
    60  
    61  	st := o.State()
    62  	st.Lock()
    63  	defer st.Unlock()
    64  	snapstate.ReplaceStore(st, s)
    65  	dirs.SetRootDir(c.MkDir())
    66  }
    67  
    68  func (s *cohortSuite) TestCreateCohort(c *check.C) {
    69  	s.coh = map[string]string{
    70  		"foo": "cohort for foo",
    71  		"bar": "cohort for bar",
    72  	}
    73  
    74  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create", "snaps": ["foo","bar"]}]`))
    75  	c.Assert(err, check.IsNil)
    76  
    77  	rsp := daemon.CohortsCmd.POST(daemon.CohortsCmd, req, nil)
    78  	c.Check(rsp, check.DeepEquals, &daemon.Resp{
    79  		Status: 200,
    80  		Type:   "sync",
    81  		Result: s.coh,
    82  	})
    83  }
    84  
    85  func (s *cohortSuite) TestCreateCohortNoSnaps(c *check.C) {
    86  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create"}]`))
    87  	c.Assert(err, check.IsNil)
    88  
    89  	rsp := daemon.CohortsCmd.POST(daemon.CohortsCmd, req, nil)
    90  	c.Check(rsp, check.DeepEquals, &daemon.Resp{
    91  		Status: 200,
    92  		Type:   "sync",
    93  		Result: map[string]string{},
    94  	})
    95  }
    96  
    97  func (s *cohortSuite) TestCreateCohortBadAction(c *check.C) {
    98  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "pupate", "snaps": ["foo","bar"]}]`))
    99  	c.Assert(err, check.IsNil)
   100  
   101  	rsp := daemon.CohortsCmd.POST(daemon.CohortsCmd, req, nil)
   102  	c.Check(rsp, check.DeepEquals, &daemon.Resp{
   103  		Status: 400,
   104  		Type:   "error",
   105  		Result: &daemon.ErrorResult{Message: `unknown cohort action "pupate"`},
   106  	})
   107  }
   108  
   109  func (s *cohortSuite) TestCreateCohortError(c *check.C) {
   110  	s.err = errors.New("something went wrong")
   111  
   112  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create", "snaps": ["foo","bar"]}]`))
   113  	c.Assert(err, check.IsNil)
   114  
   115  	rsp := daemon.CohortsCmd.POST(daemon.CohortsCmd, req, nil)
   116  	c.Check(rsp, check.DeepEquals, &daemon.Resp{
   117  		Status: 500,
   118  		Type:   "error",
   119  		Result: &daemon.ErrorResult{Message: `something went wrong`},
   120  	})
   121  }
   122  
   123  func (s *cohortSuite) TestCreateBadBody1(c *check.C) {
   124  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create", "snaps": ["foo","bar"]`))
   125  	c.Assert(err, check.IsNil)
   126  
   127  	rsp := daemon.CohortsCmd.POST(daemon.CohortsCmd, req, nil)
   128  	c.Check(rsp, check.DeepEquals, &daemon.Resp{
   129  		Status: 400,
   130  		Type:   "error",
   131  		Result: &daemon.ErrorResult{Message: `cannot decode request body into cohort instruction: unexpected EOF`},
   132  	})
   133  }
   134  
   135  func (s *cohortSuite) TestCreateBadBody2(c *check.C) {
   136  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create", "snaps": ["foo","bar"]}xx`))
   137  	c.Assert(err, check.IsNil)
   138  
   139  	rsp := daemon.CohortsCmd.POST(daemon.CohortsCmd, req, nil)
   140  	c.Check(rsp, check.DeepEquals, &daemon.Resp{
   141  		Status: 400,
   142  		Type:   "error",
   143  		Result: &daemon.ErrorResult{Message: `spurious content after cohort instruction`},
   144  	})
   145  }