github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/common/flags_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package common 5 6 import ( 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 11 "github.com/juju/errors" 12 jc "github.com/juju/testing/checkers" 13 gc "gopkg.in/check.v1" 14 15 "github.com/juju/juju/testing" 16 ) 17 18 type FlagsSuite struct { 19 testing.FakeJujuXDGDataHomeSuite 20 } 21 22 var _ = gc.Suite(&FlagsSuite{}) 23 24 func (*FlagsSuite) TestConfigFlagSet(c *gc.C) { 25 var f ConfigFlag 26 c.Assert(f.Set("a.yaml"), jc.ErrorIsNil) 27 assertConfigFlag(c, f, []string{"a.yaml"}, nil) 28 c.Assert(f.Set("b.yaml"), jc.ErrorIsNil) 29 assertConfigFlag(c, f, []string{"a.yaml", "b.yaml"}, nil) 30 c.Assert(f.Set("k1=v1"), jc.ErrorIsNil) 31 assertConfigFlag(c, f, []string{"a.yaml", "b.yaml"}, map[string]interface{}{"k1": "v1"}) 32 c.Assert(f.Set("k1==v2"), jc.ErrorIsNil) 33 assertConfigFlag(c, f, []string{"a.yaml", "b.yaml"}, map[string]interface{}{"k1": "=v2"}) 34 c.Assert(f.Set("k2=3"), jc.ErrorIsNil) 35 assertConfigFlag(c, f, []string{"a.yaml", "b.yaml"}, map[string]interface{}{"k1": "=v2", "k2": 3}) 36 } 37 38 func (*FlagsSuite) TestConfigFlagSetErrors(c *gc.C) { 39 var f ConfigFlag 40 c.Assert(f.Set(""), gc.ErrorMatches, "empty string not valid") 41 c.Assert(f.Set("x=!"), gc.ErrorMatches, "yaml: did not find URI escaped octet") 42 } 43 44 func (*FlagsSuite) TestConfigFlagString(c *gc.C) { 45 var f ConfigFlag 46 c.Assert(f.String(), gc.Equals, "") 47 f.files = append(f.files, "a.yaml") 48 c.Assert(f.String(), gc.Equals, "a.yaml") 49 f.files = append(f.files, "b.yaml") 50 c.Assert(f.String(), gc.Equals, "a.yaml b.yaml") 51 f.files = append(f.files, "x=y") 52 c.Assert(f.String(), gc.Equals, "a.yaml b.yaml x=y") 53 f.files = append(f.files, "zz=y") 54 c.Assert(f.String(), gc.Equals, "a.yaml b.yaml x=y zz=y") 55 } 56 57 func (*FlagsSuite) TestConfigFlagReadAttrs(c *gc.C) { 58 tmpdir := c.MkDir() 59 configFile1 := filepath.Join(tmpdir, "config-1.yaml") 60 configFile2 := filepath.Join(tmpdir, "config-2.yaml") 61 err := ioutil.WriteFile(configFile1, []byte(`over: "'n'out"`+"\n"), 0644) 62 c.Assert(err, jc.ErrorIsNil) 63 err = ioutil.WriteFile(configFile2, []byte(`over: "'n'under"`+"\n"), 0644) 64 c.Assert(err, jc.ErrorIsNil) 65 66 var f ConfigFlag 67 assertConfigFlagReadAttrs(c, f, map[string]interface{}{}) 68 f.files = append(f.files, configFile1) 69 assertConfigFlagReadAttrs(c, f, map[string]interface{}{"over": "'n'out"}) 70 f.files = append(f.files, configFile2) 71 assertConfigFlagReadAttrs(c, f, map[string]interface{}{"over": "'n'under"}) 72 f.attrs = map[string]interface{}{"over": "ridden"} 73 assertConfigFlagReadAttrs(c, f, map[string]interface{}{"over": "ridden"}) 74 } 75 76 func (*FlagsSuite) TestConfigFlagReadAttrsErrors(c *gc.C) { 77 tmpdir := c.MkDir() 78 configFile := filepath.Join(tmpdir, "config.yaml") 79 80 var f ConfigFlag 81 f.files = append(f.files, configFile) 82 ctx := testing.Context(c) 83 attrs, err := f.ReadAttrs(ctx) 84 c.Assert(errors.Cause(err), jc.Satisfies, os.IsNotExist) 85 c.Assert(attrs, gc.IsNil) 86 } 87 88 func assertConfigFlag(c *gc.C, f ConfigFlag, files []string, attrs map[string]interface{}) { 89 c.Assert(f.files, jc.DeepEquals, files) 90 c.Assert(f.attrs, jc.DeepEquals, attrs) 91 } 92 93 func assertConfigFlagReadAttrs(c *gc.C, f ConfigFlag, expect map[string]interface{}) { 94 ctx := testing.Context(c) 95 attrs, err := f.ReadAttrs(ctx) 96 c.Assert(err, jc.ErrorIsNil) 97 c.Assert(attrs, jc.DeepEquals, expect) 98 }