github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/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  )
    32  
    33  var _ = check.Suite(&cohortSuite{})
    34  
    35  type cohortSuite struct {
    36  	apiBaseSuite
    37  
    38  	snaps []string
    39  	coh   map[string]string
    40  }
    41  
    42  func (s *cohortSuite) CreateCohorts(_ context.Context, snaps []string) (map[string]string, error) {
    43  	s.pokeStateLock()
    44  
    45  	s.snaps = snaps[:]
    46  	return s.coh, s.err
    47  }
    48  
    49  func (s *cohortSuite) SetUpTest(c *check.C) {
    50  	s.apiBaseSuite.SetUpTest(c)
    51  
    52  	s.snaps = nil
    53  	s.coh = nil
    54  
    55  	s.daemonWithStore(c, s)
    56  }
    57  
    58  func (s *cohortSuite) TestCreateCohort(c *check.C) {
    59  	s.coh = map[string]string{
    60  		"foo": "cohort for foo",
    61  		"bar": "cohort for bar",
    62  	}
    63  
    64  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create", "snaps": ["foo","bar"]}]`))
    65  	c.Assert(err, check.IsNil)
    66  
    67  	rsp := s.syncReq(c, req, nil)
    68  	c.Check(rsp.Status, check.Equals, 200)
    69  	c.Check(rsp.Result, check.DeepEquals, s.coh)
    70  }
    71  
    72  func (s *cohortSuite) TestCreateCohortNoSnaps(c *check.C) {
    73  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create"}]`))
    74  	c.Assert(err, check.IsNil)
    75  
    76  	rsp := s.syncReq(c, req, nil)
    77  	c.Check(rsp.Status, check.Equals, 200)
    78  	c.Check(rsp.Result, check.DeepEquals, map[string]string{})
    79  }
    80  
    81  func (s *cohortSuite) TestCreateCohortBadAction(c *check.C) {
    82  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "pupate", "snaps": ["foo","bar"]}]`))
    83  	c.Assert(err, check.IsNil)
    84  
    85  	rsp := s.errorReq(c, req, nil)
    86  	c.Check(rsp.Status, check.Equals, 400)
    87  	c.Check(rsp.Result, check.DeepEquals, &daemon.ErrorResult{Message: `unknown cohort action "pupate"`})
    88  }
    89  
    90  func (s *cohortSuite) TestCreateCohortError(c *check.C) {
    91  	s.err = errors.New("something went wrong")
    92  
    93  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create", "snaps": ["foo","bar"]}]`))
    94  	c.Assert(err, check.IsNil)
    95  
    96  	rsp := s.errorReq(c, req, nil)
    97  	c.Check(rsp.Status, check.Equals, 500)
    98  	c.Check(rsp.Result, check.DeepEquals, &daemon.ErrorResult{Message: `something went wrong`})
    99  }
   100  
   101  func (s *cohortSuite) TestCreateBadBody1(c *check.C) {
   102  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create", "snaps": ["foo","bar"]`))
   103  	c.Assert(err, check.IsNil)
   104  
   105  	rsp := s.errorReq(c, req, nil)
   106  	c.Check(rsp.Status, check.Equals, 400)
   107  	c.Check(rsp.Result, check.DeepEquals, &daemon.ErrorResult{Message: `cannot decode request body into cohort instruction: unexpected EOF`})
   108  }
   109  
   110  func (s *cohortSuite) TestCreateBadBody2(c *check.C) {
   111  	req, err := http.NewRequest("POST", "/v2/cohorts", strings.NewReader(`{"action": "create", "snaps": ["foo","bar"]}xx`))
   112  	c.Assert(err, check.IsNil)
   113  
   114  	rsp := s.errorReq(c, req, nil)
   115  	c.Check(rsp.Status, check.Equals, 400)
   116  	c.Check(rsp.Result, check.DeepEquals, &daemon.ErrorResult{Message: `spurious content after cohort instruction`})
   117  }