github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_prepare_image_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 main_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	snap "github.com/snapcore/snapd/cmd/snap"
    30  	"github.com/snapcore/snapd/image"
    31  )
    32  
    33  type SnapPrepareImageSuite struct {
    34  	BaseSnapSuite
    35  }
    36  
    37  var _ = Suite(&SnapPrepareImageSuite{})
    38  
    39  func (s *SnapPrepareImageSuite) TestPrepareImageCore(c *C) {
    40  	var opts *image.Options
    41  	prep := func(o *image.Options) error {
    42  		opts = o
    43  		return nil
    44  	}
    45  	r := snap.MockImagePrepare(prep)
    46  	defer r()
    47  
    48  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "model", "prepare-dir"})
    49  	c.Assert(err, IsNil)
    50  	c.Assert(rest, DeepEquals, []string{})
    51  
    52  	c.Check(opts, DeepEquals, &image.Options{
    53  		ModelFile:  "model",
    54  		PrepareDir: "prepare-dir",
    55  	})
    56  }
    57  
    58  func (s *SnapPrepareImageSuite) TestPrepareImageClassic(c *C) {
    59  	var opts *image.Options
    60  	prep := func(o *image.Options) error {
    61  		opts = o
    62  		return nil
    63  	}
    64  	r := snap.MockImagePrepare(prep)
    65  	defer r()
    66  
    67  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "--classic", "model", "prepare-dir"})
    68  	c.Assert(err, IsNil)
    69  	c.Assert(rest, DeepEquals, []string{})
    70  
    71  	c.Check(opts, DeepEquals, &image.Options{
    72  		Classic:    true,
    73  		ModelFile:  "model",
    74  		PrepareDir: "prepare-dir",
    75  	})
    76  }
    77  
    78  func (s *SnapPrepareImageSuite) TestPrepareImageClassicArch(c *C) {
    79  	var opts *image.Options
    80  	prep := func(o *image.Options) error {
    81  		opts = o
    82  		return nil
    83  	}
    84  	r := snap.MockImagePrepare(prep)
    85  	defer r()
    86  
    87  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "--classic", "--arch", "i386", "model", "prepare-dir"})
    88  	c.Assert(err, IsNil)
    89  	c.Assert(rest, DeepEquals, []string{})
    90  
    91  	c.Check(opts, DeepEquals, &image.Options{
    92  		Classic:      true,
    93  		Architecture: "i386",
    94  		ModelFile:    "model",
    95  		PrepareDir:   "prepare-dir",
    96  	})
    97  }
    98  
    99  func (s *SnapPrepareImageSuite) TestPrepareImageClassicWideCohort(c *C) {
   100  	var opts *image.Options
   101  	prep := func(o *image.Options) error {
   102  		opts = o
   103  		return nil
   104  	}
   105  	r := snap.MockImagePrepare(prep)
   106  	defer r()
   107  
   108  	os.Setenv("UBUNTU_STORE_COHORT_KEY", "is-six-centuries")
   109  
   110  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "--classic", "model", "prepare-dir"})
   111  	c.Assert(err, IsNil)
   112  	c.Assert(rest, DeepEquals, []string{})
   113  
   114  	c.Check(opts, DeepEquals, &image.Options{
   115  		Classic:       true,
   116  		WideCohortKey: "is-six-centuries",
   117  		ModelFile:     "model",
   118  		PrepareDir:    "prepare-dir",
   119  	})
   120  
   121  	os.Unsetenv("UBUNTU_STORE_COHORT_KEY")
   122  }
   123  
   124  func (s *SnapPrepareImageSuite) TestPrepareImageExtraSnaps(c *C) {
   125  	var opts *image.Options
   126  	prep := func(o *image.Options) error {
   127  		opts = o
   128  		return nil
   129  	}
   130  	r := snap.MockImagePrepare(prep)
   131  	defer r()
   132  
   133  	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"})
   134  	c.Assert(err, IsNil)
   135  	c.Assert(rest, DeepEquals, []string{})
   136  
   137  	c.Check(opts, DeepEquals, &image.Options{
   138  		ModelFile:    "model",
   139  		Channel:      "candidate",
   140  		PrepareDir:   "prepare-dir",
   141  		Snaps:        []string{"foo", "bar", "local.snap", "local2.snap", "store-snap"},
   142  		SnapChannels: map[string]string{"bar": "t/edge"},
   143  	})
   144  }
   145  
   146  func (s *SnapPrepareImageSuite) TestPrepareImageCustomize(c *C) {
   147  	var opts *image.Options
   148  	prep := func(o *image.Options) error {
   149  		opts = o
   150  		return nil
   151  	}
   152  	r := snap.MockImagePrepare(prep)
   153  	defer r()
   154  
   155  	tmpdir := c.MkDir()
   156  	customizeFile := filepath.Join(tmpdir, "custo.json")
   157  	err := ioutil.WriteFile(customizeFile, []byte(`{
   158    "console-conf": "disabled",
   159    "cloud-init-user-data": "cloud-init-user-data"
   160  }`), 0644)
   161  	c.Assert(err, IsNil)
   162  
   163  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"prepare-image", "model", "prepare-dir", "--customize", customizeFile})
   164  	c.Assert(err, IsNil)
   165  	c.Assert(rest, DeepEquals, []string{})
   166  
   167  	c.Check(opts, DeepEquals, &image.Options{
   168  		ModelFile:  "model",
   169  		PrepareDir: "prepare-dir",
   170  		Customizations: image.Customizations{
   171  			ConsoleConf:       "disabled",
   172  			CloudInitUserData: "cloud-init-user-data",
   173  		},
   174  	})
   175  }