github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 25 var _ = gc.Suite(&SignMetadataSuite{}) 26 27 func (s *SignMetadataSuite) SetUpTest(c *gc.C) { 28 loggo.GetLogger("").SetLogLevel(loggo.INFO) 29 } 30 31 func (s *SignMetadataSuite) TearDownTest(c *gc.C) { 32 loggo.ResetLoggers() 33 } 34 35 var expectedLoggingOutput = `signing 2 file\(s\) in .*subdir1.* 36 signing file .*file1\.json.* 37 signing file .*file2\.json.* 38 signing 1 file\(s\) in .*subdir2.* 39 signing file .*file3\.json.* 40 ` 41 42 func makeFileNames(topLevel string) []string { 43 return []string{ 44 filepath.Join(topLevel, "subdir1", "file1.json"), 45 filepath.Join(topLevel, "subdir1", "file2.json"), 46 filepath.Join(topLevel, "subdir1", "subdir2", "file3.json"), 47 } 48 } 49 50 func setupJsonFiles(c *gc.C, topLevel string) { 51 err := os.MkdirAll(filepath.Join(topLevel, "subdir1", "subdir2"), 0700) 52 c.Assert(err, jc.ErrorIsNil) 53 content := []byte("hello world") 54 filenames := makeFileNames(topLevel) 55 for _, filename := range filenames { 56 err = ioutil.WriteFile(filename, content, 0644) 57 c.Assert(err, jc.ErrorIsNil) 58 } 59 } 60 61 func assertSignedFile(c *gc.C, filename string) { 62 r, err := os.Open(filename) 63 c.Assert(err, jc.ErrorIsNil) 64 defer r.Close() 65 data, err := simplestreams.DecodeCheckSignature(r, sstesting.SignedMetadataPublicKey) 66 c.Assert(err, jc.ErrorIsNil) 67 c.Assert(string(data), gc.Equals, "hello world\n") 68 } 69 70 func assertSignedFiles(c *gc.C, topLevel string) { 71 filenames := makeFileNames(topLevel) 72 for _, filename := range filenames { 73 filename = strings.Replace(filename, ".json", ".sjson", -1) 74 assertSignedFile(c, filename) 75 } 76 } 77 78 func (s *SignMetadataSuite) TestSignMetadata(c *gc.C) { 79 topLevel := c.MkDir() 80 keyfile := filepath.Join(topLevel, "privatekey.asc") 81 err := ioutil.WriteFile(keyfile, []byte(sstesting.SignedMetadataPrivateKey), 0644) 82 c.Assert(err, jc.ErrorIsNil) 83 setupJsonFiles(c, topLevel) 84 85 ctx := coretesting.Context(c) 86 code := cmd.Main( 87 newSignMetadataCommand(), ctx, []string{"-d", topLevel, "-k", keyfile, "-p", sstesting.PrivateKeyPassphrase}) 88 c.Assert(code, gc.Equals, 0) 89 output := ctx.Stdout.(*bytes.Buffer).String() 90 c.Assert(output, gc.Matches, expectedLoggingOutput) 91 assertSignedFiles(c, topLevel) 92 } 93 94 func runSignMetadata(c *gc.C, args ...string) error { 95 _, err := coretesting.RunCommand(c, newSignMetadataCommand(), args...) 96 return err 97 } 98 99 func (s *SignMetadataSuite) TestSignMetadataErrors(c *gc.C) { 100 err := runSignMetadata(c, "") 101 c.Assert(err, gc.ErrorMatches, `directory must be specified`) 102 err = runSignMetadata(c, "-d", "foo") 103 c.Assert(err, gc.ErrorMatches, `keyfile must be specified`) 104 }