github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/common/charms/common_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charms_test
     5  
     6  import (
     7  	"github.com/juju/charm/v12"
     8  	"github.com/juju/charm/v12/resource"
     9  	"github.com/juju/version/v2"
    10  	"go.uber.org/mock/gomock"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	basemocks "github.com/juju/juju/api/base/mocks"
    14  	apicommoncharms "github.com/juju/juju/api/common/charms"
    15  	"github.com/juju/juju/rpc/params"
    16  	coretesting "github.com/juju/juju/testing"
    17  )
    18  
    19  type suite struct {
    20  	coretesting.BaseSuite
    21  }
    22  
    23  var _ = gc.Suite(&suite{})
    24  
    25  func (s *suite) TestCharmInfo(c *gc.C) {
    26  	ctrl := gomock.NewController(c)
    27  	defer ctrl.Finish()
    28  
    29  	mockFacadeCaller := basemocks.NewMockFacadeCaller(ctrl)
    30  
    31  	url := "local:quantal/dummy-1"
    32  	args := params.CharmURL{URL: url}
    33  	info := new(params.Charm)
    34  
    35  	params := params.Charm{
    36  		Revision: 1,
    37  		URL:      url,
    38  		Config: map[string]params.CharmOption{
    39  			"config": {
    40  				Type:        "type",
    41  				Description: "config-type option",
    42  			},
    43  		},
    44  		LXDProfile: &params.CharmLXDProfile{
    45  			Description: "LXDProfile",
    46  			Devices: map[string]map[string]string{
    47  				"tun": {
    48  					"path": "/dev/net/tun",
    49  					"type": "unix-char",
    50  				},
    51  			},
    52  		},
    53  		Meta: &params.CharmMeta{
    54  			Name:           "dummy",
    55  			Description:    "cockroachdb",
    56  			MinJujuVersion: "2.9.0",
    57  			Resources: map[string]params.CharmResourceMeta{
    58  				"cockroachdb-image": {
    59  					Type:        "oci-image",
    60  					Description: "OCI image used for cockroachdb",
    61  				},
    62  			},
    63  			Containers: map[string]params.CharmContainer{
    64  				"cockroachdb": {
    65  					Resource: "cockroachdb-image",
    66  					Mounts: []params.CharmMount{
    67  						{
    68  							Storage:  "database",
    69  							Location: "/cockroach/cockroach-data",
    70  						},
    71  					},
    72  					Uid: 5000,
    73  					Gid: 5001,
    74  				},
    75  			},
    76  			Storage: map[string]params.CharmStorage{
    77  				"database": {
    78  					Type: "filesystem",
    79  				},
    80  			},
    81  			CharmUser: "root",
    82  		},
    83  		Manifest: &params.CharmManifest{
    84  			Bases: []params.CharmBase{
    85  				{
    86  					Name:    "ubuntu",
    87  					Channel: "20.04/stable",
    88  				},
    89  			},
    90  		},
    91  	}
    92  
    93  	mockFacadeCaller.EXPECT().FacadeCall("CharmInfo", args, info).SetArg(2, params).Return(nil)
    94  
    95  	client := apicommoncharms.NewCharmInfoClient(mockFacadeCaller)
    96  	got, err := client.CharmInfo(url)
    97  	c.Assert(err, gc.IsNil)
    98  
    99  	want := &apicommoncharms.CharmInfo{
   100  		Revision: 1,
   101  		URL:      url,
   102  		Config: &charm.Config{
   103  			Options: map[string]charm.Option{
   104  				"config": {
   105  					Type:        "type",
   106  					Description: "config-type option",
   107  				},
   108  			},
   109  		},
   110  		LXDProfile: &charm.LXDProfile{
   111  			Description: "LXDProfile",
   112  			Config:      map[string]string{},
   113  			Devices: map[string]map[string]string{
   114  				"tun": {
   115  					"path": "/dev/net/tun",
   116  					"type": "unix-char",
   117  				},
   118  			},
   119  		},
   120  		Meta: &charm.Meta{
   121  			Name:           "dummy",
   122  			Description:    "cockroachdb",
   123  			MinJujuVersion: version.MustParse("2.9.0"),
   124  			Resources: map[string]resource.Meta{
   125  				"cockroachdb-image": {
   126  					Type:        resource.TypeContainerImage,
   127  					Description: "OCI image used for cockroachdb",
   128  				},
   129  			},
   130  			Containers: map[string]charm.Container{
   131  				"cockroachdb": {
   132  					Resource: "cockroachdb-image",
   133  					Mounts: []charm.Mount{
   134  						{
   135  							Storage:  "database",
   136  							Location: "/cockroach/cockroach-data",
   137  						},
   138  					},
   139  					Uid: 5000,
   140  					Gid: 5001,
   141  				},
   142  			},
   143  			Storage: map[string]charm.Storage{
   144  				"database": {
   145  					Type: "filesystem",
   146  				},
   147  			},
   148  			CharmUser: charm.RunAsRoot,
   149  		},
   150  		Manifest: &charm.Manifest{
   151  			Bases: []charm.Base{
   152  				{
   153  					Name: "ubuntu",
   154  					Channel: charm.Channel{
   155  						Risk:  "stable",
   156  						Track: "20.04",
   157  					},
   158  					Architectures: []string{},
   159  				},
   160  			},
   161  		},
   162  	}
   163  	c.Assert(got, gc.DeepEquals, want)
   164  }
   165  
   166  func (s *suite) TestApplicationCharmInfo(c *gc.C) {
   167  	ctrl := gomock.NewController(c)
   168  	defer ctrl.Finish()
   169  
   170  	mockFacadeCaller := basemocks.NewMockFacadeCaller(ctrl)
   171  
   172  	args := params.Entity{Tag: "application-foobar"}
   173  	info := new(params.Charm)
   174  
   175  	params := params.Charm{
   176  		Revision: 1,
   177  		URL:      "ch:foobar",
   178  		Meta: &params.CharmMeta{
   179  			Name:           "foobar",
   180  			MinJujuVersion: "2.9.0",
   181  		},
   182  		// The rest of the field conversions are tested by TestCharmInfo
   183  	}
   184  
   185  	mockFacadeCaller.EXPECT().FacadeCall("ApplicationCharmInfo", args, info).SetArg(2, params).Return(nil)
   186  
   187  	client := apicommoncharms.NewApplicationCharmInfoClient(mockFacadeCaller)
   188  	got, err := client.ApplicationCharmInfo("foobar")
   189  	c.Assert(err, gc.IsNil)
   190  
   191  	want := &apicommoncharms.CharmInfo{
   192  		Revision: 1,
   193  		URL:      "ch:foobar",
   194  		Meta: &charm.Meta{
   195  			Name:           "foobar",
   196  			MinJujuVersion: version.MustParse("2.9.0"),
   197  		},
   198  	}
   199  	c.Assert(got, gc.DeepEquals, want)
   200  }