github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/resource/util_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package resource_test
     5  
     6  import (
     7  	"bytes"
     8  	"strings"
     9  
    10  	jujucmd "github.com/juju/cmd"
    11  	"github.com/juju/cmd/cmdtesting"
    12  	jc "github.com/juju/testing/checkers"
    13  	gc "gopkg.in/check.v1"
    14  	charmresource "gopkg.in/juju/charm.v6/resource"
    15  )
    16  
    17  func charmRes(c *gc.C, name, suffix, description, content string) charmresource.Resource {
    18  	if content == "" {
    19  		content = name
    20  	}
    21  
    22  	fp, err := charmresource.GenerateFingerprint(strings.NewReader(content))
    23  	c.Assert(err, jc.ErrorIsNil)
    24  
    25  	res := charmresource.Resource{
    26  		Meta: charmresource.Meta{
    27  			Name:        name,
    28  			Type:        charmresource.TypeFile,
    29  			Path:        name + suffix,
    30  			Description: description,
    31  		},
    32  		Origin:      charmresource.OriginStore,
    33  		Revision:    1,
    34  		Fingerprint: fp,
    35  		Size:        int64(len(content)),
    36  	}
    37  	err = res.Validate()
    38  	c.Assert(err, jc.ErrorIsNil)
    39  	return res
    40  }
    41  
    42  func newCharmResources(c *gc.C, names ...string) []charmresource.Resource {
    43  	var resources []charmresource.Resource
    44  	for _, name := range names {
    45  		var description string
    46  		parts := strings.SplitN(name, ":", 2)
    47  		if len(parts) == 2 {
    48  			name = parts[0]
    49  			description = parts[1]
    50  		}
    51  
    52  		res := charmRes(c, name, ".tgz", description, "")
    53  		resources = append(resources, res)
    54  	}
    55  	return resources
    56  }
    57  
    58  func runCmd(c *gc.C, command jujucmd.Command, args ...string) (code int, stdout string, stderr string) {
    59  	ctx := cmdtesting.Context(c)
    60  	code = jujucmd.Main(command, ctx, args)
    61  	stdout = string(ctx.Stdout.(*bytes.Buffer).Bytes())
    62  	stderr = string(ctx.Stderr.(*bytes.Buffer).Bytes())
    63  	return code, stdout, stderr
    64  }