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