github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/client/quota_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 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 client_test
    21  
    22  import (
    23  	"encoding/json"
    24  	"io/ioutil"
    25  
    26  	"gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/client"
    29  )
    30  
    31  func (cs *clientSuite) TestCreateQuotaGroupInvalidName(c *check.C) {
    32  	err := cs.cli.EnsureQuota("", "", nil, 0)
    33  	c.Check(err, check.ErrorMatches, `cannot create or update quota group without a name`)
    34  }
    35  
    36  func (cs *clientSuite) TestEnsureQuotaGroup(c *check.C) {
    37  	cs.rsp = `{
    38  		"type": "sync",
    39  		"status-code": 200
    40  	}`
    41  
    42  	c.Assert(cs.cli.EnsureQuota("foo", "bar", []string{"snap-a", "snap-b"}, 1001), check.IsNil)
    43  	c.Check(cs.req.Method, check.Equals, "POST")
    44  	c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas")
    45  	body, err := ioutil.ReadAll(cs.req.Body)
    46  	c.Assert(err, check.IsNil)
    47  	var req map[string]interface{}
    48  	err = json.Unmarshal(body, &req)
    49  	c.Assert(err, check.IsNil)
    50  	c.Assert(req, check.DeepEquals, map[string]interface{}{
    51  		"action":     "ensure",
    52  		"group-name": "foo",
    53  		"parent":     "bar",
    54  		"snaps":      []interface{}{"snap-a", "snap-b"},
    55  		"max-memory": float64(1001),
    56  	})
    57  }
    58  
    59  func (cs *clientSuite) TestEnsureQuotaGroupError(c *check.C) {
    60  	cs.status = 500
    61  	cs.rsp = `{"type": "error"}`
    62  	err := cs.cli.EnsureQuota("foo", "bar", []string{"snap-a"}, 1)
    63  	c.Check(err, check.ErrorMatches, `cannot create or update quota group: server error: "Internal Server Error"`)
    64  }
    65  
    66  func (cs *clientSuite) TestGetQuotaGroupInvalidName(c *check.C) {
    67  	_, err := cs.cli.GetQuotaGroup("")
    68  	c.Assert(err, check.ErrorMatches, `cannot get quota group without a name`)
    69  }
    70  
    71  func (cs *clientSuite) TestGetQuotaGroup(c *check.C) {
    72  	cs.rsp = `{
    73  		"type": "sync",
    74  		"status-code": 200,
    75  		"result": {"group-name":"foo", "parent":"bar", "subgroups":["foo-subgrp"], "snaps":["snap-a"], "max-memory":999, "current-memory":450}
    76  	}`
    77  
    78  	grp, err := cs.cli.GetQuotaGroup("foo")
    79  	c.Assert(err, check.IsNil)
    80  	c.Check(cs.req.Method, check.Equals, "GET")
    81  	c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas/foo")
    82  	c.Check(grp, check.DeepEquals, &client.QuotaGroupResult{
    83  		GroupName:     "foo",
    84  		Parent:        "bar",
    85  		Subgroups:     []string{"foo-subgrp"},
    86  		MaxMemory:     999,
    87  		CurrentMemory: 450,
    88  		Snaps:         []string{"snap-a"},
    89  	})
    90  }
    91  
    92  func (cs *clientSuite) TestGetQuotaGroupError(c *check.C) {
    93  	cs.status = 500
    94  	cs.rsp = `{"type": "error"}`
    95  	_, err := cs.cli.GetQuotaGroup("foo")
    96  	c.Check(err, check.ErrorMatches, `server error: "Internal Server Error"`)
    97  }
    98  
    99  func (cs *clientSuite) TestRemoveQuotaGroup(c *check.C) {
   100  	cs.rsp = `{
   101  		"type": "sync",
   102  		"status-code": 200
   103  	}`
   104  
   105  	err := cs.cli.RemoveQuotaGroup("foo")
   106  	c.Assert(err, check.IsNil)
   107  	c.Check(cs.req.Method, check.Equals, "POST")
   108  	c.Check(cs.req.URL.Path, check.Equals, "/v2/quotas")
   109  	body, err := ioutil.ReadAll(cs.req.Body)
   110  	c.Assert(err, check.IsNil)
   111  	var req map[string]interface{}
   112  	err = json.Unmarshal(body, &req)
   113  	c.Assert(err, check.IsNil)
   114  	c.Assert(req, check.DeepEquals, map[string]interface{}{
   115  		"action":     "remove",
   116  		"group-name": "foo",
   117  	})
   118  }
   119  
   120  func (cs *clientSuite) TestRemoveQuotaGroupError(c *check.C) {
   121  	cs.status = 500
   122  	cs.rsp = `{"type": "error"}`
   123  	err := cs.cli.RemoveQuotaGroup("foo")
   124  	c.Check(err, check.ErrorMatches, `server error: "Internal Server Error"`)
   125  }