github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/cmd/juju/synctools_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 "errors" 8 "time" 9 10 "github.com/juju/loggo" 11 gc "launchpad.net/gocheck" 12 13 "launchpad.net/juju-core/cmd" 14 "launchpad.net/juju-core/environs" 15 "launchpad.net/juju-core/environs/configstore" 16 "launchpad.net/juju-core/environs/sync" 17 "launchpad.net/juju-core/provider/dummy" 18 coretesting "launchpad.net/juju-core/testing" 19 jc "launchpad.net/juju-core/testing/checkers" 20 "launchpad.net/juju-core/testing/testbase" 21 ) 22 23 type syncToolsSuite struct { 24 testbase.LoggingSuite 25 home *coretesting.FakeHome 26 configStore configstore.Storage 27 localStorage string 28 29 origSyncTools func(*sync.SyncContext) error 30 } 31 32 var _ = gc.Suite(&syncToolsSuite{}) 33 34 func (s *syncToolsSuite) SetUpTest(c *gc.C) { 35 s.LoggingSuite.SetUpTest(c) 36 37 // Create a target environments.yaml and make sure its environment is empty. 38 s.home = coretesting.MakeFakeHome(c, ` 39 environments: 40 test-target: 41 type: dummy 42 state-server: false 43 authorized-keys: "not-really-one" 44 `) 45 var err error 46 s.configStore, err = configstore.Default() 47 c.Assert(err, gc.IsNil) 48 s.origSyncTools = syncTools 49 } 50 51 func (s *syncToolsSuite) TearDownTest(c *gc.C) { 52 syncTools = s.origSyncTools 53 dummy.Reset() 54 s.home.Restore() 55 s.LoggingSuite.TearDownTest(c) 56 } 57 58 func (s *syncToolsSuite) Reset(c *gc.C) { 59 s.TearDownTest(c) 60 s.SetUpTest(c) 61 } 62 63 func runSyncToolsCommand(c *gc.C, args ...string) (*cmd.Context, error) { 64 return coretesting.RunCommand(c, &SyncToolsCommand{}, args) 65 } 66 67 func wait(signal chan struct{}) error { 68 select { 69 case <-signal: 70 return nil 71 case <-time.After(25 * time.Millisecond): 72 return errors.New("timeout") 73 } 74 } 75 76 var syncToolsCommandTests = []struct { 77 description string 78 args []string 79 sctx *sync.SyncContext 80 }{ 81 { 82 description: "environment as only argument", 83 args: []string{"-e", "test-target"}, 84 sctx: &sync.SyncContext{}, 85 }, 86 { 87 description: "specifying also the synchronization source", 88 args: []string{"-e", "test-target", "--source", "/foo/bar"}, 89 sctx: &sync.SyncContext{ 90 Source: "/foo/bar", 91 }, 92 }, 93 { 94 description: "synchronize all version including development", 95 args: []string{"-e", "test-target", "--all", "--dev"}, 96 sctx: &sync.SyncContext{ 97 AllVersions: true, 98 Dev: true, 99 }, 100 }, 101 { 102 description: "just make a dry run", 103 args: []string{"-e", "test-target", "--dry-run"}, 104 sctx: &sync.SyncContext{ 105 DryRun: true, 106 }, 107 }, 108 { 109 description: "specific public", 110 args: []string{"-e", "test-target", "--public"}, 111 sctx: &sync.SyncContext{ 112 Public: true, 113 }, 114 }, 115 { 116 description: "specify version", 117 args: []string{"-e", "test-target", "--version", "1.2"}, 118 sctx: &sync.SyncContext{ 119 MajorVersion: 1, 120 MinorVersion: 2, 121 }, 122 }, 123 } 124 125 func (s *syncToolsSuite) TestSyncToolsCommand(c *gc.C) { 126 for i, test := range syncToolsCommandTests { 127 c.Logf("test %d: %s", i, test.description) 128 targetEnv, err := environs.PrepareFromName("test-target", nullContext(), s.configStore) 129 c.Assert(err, gc.IsNil) 130 called := false 131 syncTools = func(sctx *sync.SyncContext) error { 132 c.Assert(sctx.AllVersions, gc.Equals, test.sctx.AllVersions) 133 c.Assert(sctx.MajorVersion, gc.Equals, test.sctx.MajorVersion) 134 c.Assert(sctx.MinorVersion, gc.Equals, test.sctx.MinorVersion) 135 c.Assert(sctx.DryRun, gc.Equals, test.sctx.DryRun) 136 c.Assert(sctx.Dev, gc.Equals, test.sctx.Dev) 137 c.Assert(sctx.Public, gc.Equals, test.sctx.Public) 138 c.Assert(sctx.Source, gc.Equals, test.sctx.Source) 139 c.Assert(dummy.IsSameStorage(sctx.Target, targetEnv.Storage()), jc.IsTrue) 140 called = true 141 return nil 142 } 143 ctx, err := runSyncToolsCommand(c, test.args...) 144 c.Assert(err, gc.IsNil) 145 c.Assert(ctx, gc.NotNil) 146 c.Assert(called, jc.IsTrue) 147 s.Reset(c) 148 } 149 } 150 151 func (s *syncToolsSuite) TestSyncToolsCommandTargetDirectory(c *gc.C) { 152 called := false 153 dir := c.MkDir() 154 syncTools = func(sctx *sync.SyncContext) error { 155 c.Assert(sctx.AllVersions, gc.Equals, false) 156 c.Assert(sctx.DryRun, gc.Equals, false) 157 c.Assert(sctx.Dev, gc.Equals, false) 158 c.Assert(sctx.Source, gc.Equals, "") 159 url, err := sctx.Target.URL("") 160 c.Assert(err, gc.IsNil) 161 c.Assert(url, gc.Equals, "file://"+dir) 162 called = true 163 return nil 164 } 165 ctx, err := runSyncToolsCommand(c, "-e", "test-target", "--local-dir", dir) 166 c.Assert(err, gc.IsNil) 167 c.Assert(ctx, gc.NotNil) 168 c.Assert(called, jc.IsTrue) 169 s.Reset(c) 170 } 171 172 func (s *syncToolsSuite) TestSyncToolsCommandDeprecatedDestination(c *gc.C) { 173 called := false 174 dir := c.MkDir() 175 syncTools = func(sctx *sync.SyncContext) error { 176 c.Assert(sctx.AllVersions, gc.Equals, false) 177 c.Assert(sctx.DryRun, gc.Equals, false) 178 c.Assert(sctx.Dev, gc.Equals, false) 179 c.Assert(sctx.Source, gc.Equals, "") 180 url, err := sctx.Target.URL("") 181 c.Assert(err, gc.IsNil) 182 c.Assert(url, gc.Equals, "file://"+dir) 183 called = true 184 return nil 185 } 186 // Register writer. 187 tw := &loggo.TestWriter{} 188 c.Assert(loggo.RegisterWriter("deprecated-tester", tw, loggo.DEBUG), gc.IsNil) 189 defer loggo.RemoveWriter("deprecated-tester") 190 // Add deprecated message to be checked. 191 messages := []jc.SimpleMessage{ 192 {loggo.WARNING, "Use of the --destination flag is deprecated in 1.18. Please use --local-dir instead."}, 193 } 194 // Run sync-tools command with --destination flag. 195 ctx, err := runSyncToolsCommand(c, "-e", "test-target", "--destination", dir) 196 c.Assert(err, gc.IsNil) 197 c.Assert(ctx, gc.NotNil) 198 c.Assert(called, jc.IsTrue) 199 // Check deprecated message was logged. 200 c.Check(tw.Log, jc.LogMatches, messages) 201 s.Reset(c) 202 }