github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/resource/cmd/util_test.go (about)

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