github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap/cmd_create_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 main_test
    21  
    22  import (
    23  	"fmt"
    24  	"net/http"
    25  
    26  	"gopkg.in/check.v1"
    27  	"gopkg.in/yaml.v2"
    28  
    29  	snap "github.com/snapcore/snapd/cmd/snap"
    30  )
    31  
    32  func (s *SnapSuite) TestCreateCohort(c *check.C) {
    33  	n := 0
    34  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    35  		n++
    36  		fmt.Fprintln(w, `{
    37  "type": "sync",
    38  "status-code": 200,
    39  "status": "OK",
    40  "result": {"foo": "what", "bar": "this"}}`)
    41  
    42  	})
    43  
    44  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"create-cohort", "foo", "bar"})
    45  	c.Assert(err, check.IsNil)
    46  	c.Check(rest, check.HasLen, 0)
    47  
    48  	var v map[string]map[string]map[string]string
    49  	c.Assert(yaml.Unmarshal(s.stdout.Bytes(), &v), check.IsNil)
    50  	c.Check(v, check.DeepEquals, map[string]map[string]map[string]string{
    51  		"cohorts": {
    52  			"foo": {"cohort-key": "what"},
    53  			"bar": {"cohort-key": "this"},
    54  		},
    55  	})
    56  	c.Check(n, check.Equals, 1)
    57  }
    58  
    59  func (s *SnapSuite) TestCreateCohortNoSnaps(c *check.C) {
    60  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    61  		panic("shouldn't be called")
    62  	})
    63  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"create-cohort"})
    64  	c.Check(err, check.ErrorMatches, "the required argument .* was not provided")
    65  }
    66  
    67  func (s *SnapSuite) TestCreateCohortNotFound(c *check.C) {
    68  	n := 0
    69  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    70  		n++
    71  		fmt.Fprintln(w, `{"type": "error", "result": {"message": "snap not found", "kind": "snap-not-found"}, "status-code": 404}`)
    72  	})
    73  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"create-cohort", "foo", "bar"})
    74  	c.Check(err, check.ErrorMatches, "cannot create cohorts: snap not found")
    75  	c.Check(n, check.Equals, 1)
    76  }
    77  
    78  func (s *SnapSuite) TestCreateCohortError(c *check.C) {
    79  	n := 0
    80  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    81  		n++
    82  		fmt.Fprintln(w, `{"type": "error", "result": {"message": "something went wrong"}}`)
    83  	})
    84  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"create-cohort", "foo", "bar"})
    85  	c.Check(err, check.ErrorMatches, "cannot create cohorts: something went wrong")
    86  	c.Check(n, check.Equals, 1)
    87  }