github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/plugins/juju-metadata/signmetadata_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/juju/cmd"
    14  	"github.com/juju/loggo"
    15  	jc "github.com/juju/testing/checkers"
    16  	gc "gopkg.in/check.v1"
    17  
    18  	"github.com/juju/juju/environs/simplestreams"
    19  	sstesting "github.com/juju/juju/environs/simplestreams/testing"
    20  	coretesting "github.com/juju/juju/testing"
    21  )
    22  
    23  type SignMetadataSuite struct {
    24  	coretesting.BaseSuite
    25  }
    26  
    27  var _ = gc.Suite(&SignMetadataSuite{})
    28  
    29  func (s *SignMetadataSuite) SetUpTest(c *gc.C) {
    30  	s.BaseSuite.SetUpTest(c)
    31  	loggo.GetLogger("").SetLogLevel(loggo.INFO)
    32  }
    33  
    34  var expectedLoggingOutput = `signing 2 file\(s\) in .*subdir1.*
    35  signing file .*file1\.json.*
    36  signing file .*file2\.json.*
    37  signing 1 file\(s\) in .*subdir2.*
    38  signing file .*file3\.json.*
    39  `
    40  
    41  func makeFileNames(topLevel string) []string {
    42  	return []string{
    43  		filepath.Join(topLevel, "subdir1", "file1.json"),
    44  		filepath.Join(topLevel, "subdir1", "file2.json"),
    45  		filepath.Join(topLevel, "subdir1", "subdir2", "file3.json"),
    46  	}
    47  }
    48  
    49  func setupJsonFiles(c *gc.C, topLevel string) {
    50  	err := os.MkdirAll(filepath.Join(topLevel, "subdir1", "subdir2"), 0700)
    51  	c.Assert(err, jc.ErrorIsNil)
    52  	content := []byte("hello world")
    53  	filenames := makeFileNames(topLevel)
    54  	for _, filename := range filenames {
    55  		err = ioutil.WriteFile(filename, content, 0644)
    56  		c.Assert(err, jc.ErrorIsNil)
    57  	}
    58  }
    59  
    60  func assertSignedFile(c *gc.C, filename string) {
    61  	r, err := os.Open(filename)
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	defer r.Close()
    64  	data, err := simplestreams.DecodeCheckSignature(r, sstesting.SignedMetadataPublicKey)
    65  	c.Assert(err, jc.ErrorIsNil)
    66  	c.Assert(string(data), gc.Equals, "hello world\n")
    67  }
    68  
    69  func assertSignedFiles(c *gc.C, topLevel string) {
    70  	filenames := makeFileNames(topLevel)
    71  	for _, filename := range filenames {
    72  		filename = strings.Replace(filename, ".json", ".sjson", -1)
    73  		assertSignedFile(c, filename)
    74  	}
    75  }
    76  
    77  func (s *SignMetadataSuite) TestSignMetadata(c *gc.C) {
    78  	topLevel := c.MkDir()
    79  	keyfile := filepath.Join(topLevel, "privatekey.asc")
    80  	err := ioutil.WriteFile(keyfile, []byte(sstesting.SignedMetadataPrivateKey), 0644)
    81  	c.Assert(err, jc.ErrorIsNil)
    82  	setupJsonFiles(c, topLevel)
    83  
    84  	ctx := coretesting.Context(c)
    85  	code := cmd.Main(
    86  		newSignMetadataCommand(), ctx, []string{"-d", topLevel, "-k", keyfile, "-p", sstesting.PrivateKeyPassphrase})
    87  	c.Assert(code, gc.Equals, 0)
    88  	output := ctx.Stdout.(*bytes.Buffer).String()
    89  	c.Assert(output, gc.Matches, expectedLoggingOutput)
    90  	assertSignedFiles(c, topLevel)
    91  }
    92  
    93  func runSignMetadata(c *gc.C, args ...string) error {
    94  	_, err := coretesting.RunCommand(c, newSignMetadataCommand(), args...)
    95  	return err
    96  }
    97  
    98  func (s *SignMetadataSuite) TestSignMetadataErrors(c *gc.C) {
    99  	err := runSignMetadata(c, "")
   100  	c.Assert(err, gc.ErrorMatches, `directory must be specified`)
   101  	err = runSignMetadata(c, "-d", "foo")
   102  	c.Assert(err, gc.ErrorMatches, `keyfile must be specified`)
   103  }