github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_prepare_image_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  	. "gopkg.in/check.v1"
    24  	"os"
    25  
    26  	snap "github.com/snapcore/snapd/cmd/snap"
    27  	"github.com/snapcore/snapd/image"
    28  )
    29  
    30  type SnapPrepareImageSuite struct {
    31  	BaseSnapSuite
    32  }
    33  
    34  var _ = Suite(&SnapPrepareImageSuite{})
    35  
    36  func (s *SnapPrepareImageSuite) TestPrepareImageCore(c *C) {
    37  	var opts *image.Options
    38  	prep := func(o *image.Options) error {
    39  		opts = o
    40  		return nil
    41  	}
    42  	r := snap.MockImagePrepare(prep)
    43  	defer r()
    44  
    45  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "model", "prepare-dir"})
    46  	c.Assert(err, IsNil)
    47  	c.Assert(rest, DeepEquals, []string{})
    48  
    49  	c.Check(opts, DeepEquals, &image.Options{
    50  		ModelFile:  "model",
    51  		PrepareDir: "prepare-dir",
    52  	})
    53  }
    54  
    55  func (s *SnapPrepareImageSuite) TestPrepareImageClassic(c *C) {
    56  	var opts *image.Options
    57  	prep := func(o *image.Options) error {
    58  		opts = o
    59  		return nil
    60  	}
    61  	r := snap.MockImagePrepare(prep)
    62  	defer r()
    63  
    64  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "--classic", "model", "prepare-dir"})
    65  	c.Assert(err, IsNil)
    66  	c.Assert(rest, DeepEquals, []string{})
    67  
    68  	c.Check(opts, DeepEquals, &image.Options{
    69  		Classic:    true,
    70  		ModelFile:  "model",
    71  		PrepareDir: "prepare-dir",
    72  	})
    73  }
    74  
    75  func (s *SnapPrepareImageSuite) TestPrepareImageClassicArch(c *C) {
    76  	var opts *image.Options
    77  	prep := func(o *image.Options) error {
    78  		opts = o
    79  		return nil
    80  	}
    81  	r := snap.MockImagePrepare(prep)
    82  	defer r()
    83  
    84  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "--classic", "--arch", "i386", "model", "prepare-dir"})
    85  	c.Assert(err, IsNil)
    86  	c.Assert(rest, DeepEquals, []string{})
    87  
    88  	c.Check(opts, DeepEquals, &image.Options{
    89  		Classic:      true,
    90  		Architecture: "i386",
    91  		ModelFile:    "model",
    92  		PrepareDir:   "prepare-dir",
    93  	})
    94  }
    95  
    96  func (s *SnapPrepareImageSuite) TestPrepareImageClassicWideCohort(c *C) {
    97  	var opts *image.Options
    98  	prep := func(o *image.Options) error {
    99  		opts = o
   100  		return nil
   101  	}
   102  	r := snap.MockImagePrepare(prep)
   103  	defer r()
   104  
   105  	os.Setenv("UBUNTU_STORE_COHORT_KEY", "is-six-centuries")
   106  
   107  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "--classic", "model", "prepare-dir"})
   108  	c.Assert(err, IsNil)
   109  	c.Assert(rest, DeepEquals, []string{})
   110  
   111  	c.Check(opts, DeepEquals, &image.Options{
   112  		Classic:       true,
   113  		WideCohortKey: "is-six-centuries",
   114  		ModelFile:     "model",
   115  		PrepareDir:    "prepare-dir",
   116  	})
   117  
   118  	os.Unsetenv("UBUNTU_STORE_COHORT_KEY")
   119  }
   120  
   121  func (s *SnapPrepareImageSuite) TestPrepareImageExtraSnaps(c *C) {
   122  	var opts *image.Options
   123  	prep := func(o *image.Options) error {
   124  		opts = o
   125  		return nil
   126  	}
   127  	r := snap.MockImagePrepare(prep)
   128  	defer r()
   129  
   130  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "model", "prepare-dir", "--channel", "candidate", "--snap", "foo", "--snap", "bar=t/edge", "--snap", "local.snap", "--extra-snaps", "local2.snap", "--extra-snaps", "store-snap"})
   131  	c.Assert(err, IsNil)
   132  	c.Assert(rest, DeepEquals, []string{})
   133  
   134  	c.Check(opts, DeepEquals, &image.Options{
   135  		ModelFile:    "model",
   136  		Channel:      "candidate",
   137  		PrepareDir:   "prepare-dir",
   138  		Snaps:        []string{"foo", "bar", "local.snap", "local2.snap", "store-snap"},
   139  		SnapChannels: map[string]string{"bar": "t/edge"},
   140  	})
   141  }