示例#1
0
func (s *UpgradeJujuSuite) TestUpgradeJuju(c *C) {
	oldVersion := version.Current
	putTools = testPutTools
	defer func() {
		version.Current = oldVersion
		putTools = environs.PutTools
	}()

	for i, test := range upgradeJujuTests {
		c.Logf("%d. %s", i, test.about)
		// Set up the test preconditions.
		s.Reset(c)
		for _, v := range test.private {
			upload(s.Conn.Environ.Storage(), v)
		}
		for _, v := range test.public {
			storage := s.Conn.Environ.PublicStorage().(environs.Storage)
			upload(storage, v)
		}
		version.Current = version.MustParseBinary(test.currentVersion)
		err := SetAgentVersion(s.State, version.MustParse(test.agentVersion), false)
		c.Assert(err, IsNil)

		// Run the command
		com := &UpgradeJujuCommand{}
		err = com.Init(newFlagSet(), test.args)
		if test.expectInitErr != "" {
			c.Check(err, ErrorMatches, test.expectInitErr)
			continue
		}
		err = com.Run(&cmd.Context{c.MkDir(), nil, ioutil.Discard, ioutil.Discard})
		if test.expectErr != "" {
			c.Check(err, ErrorMatches, test.expectErr)
			continue
		}
		c.Assert(err, IsNil)
		cfg, err := s.State.EnvironConfig()
		c.Check(err, IsNil)
		c.Check(cfg.AgentVersion(), Equals, version.MustParse(test.expectVersion))
		c.Check(cfg.Development(), Equals, test.expectDevelopment)

		if test.expectUploaded != "" {
			p := environs.ToolsStoragePath(version.MustParseBinary(test.expectUploaded))
			r, err := s.Conn.Environ.Storage().Get(p)
			c.Assert(err, IsNil)
			data, err := ioutil.ReadAll(r)
			c.Check(err, IsNil)
			c.Check(string(data), Equals, test.expectUploaded)
			r.Close()
		}
	}
}
示例#2
0
func (s *BootstrapSuite) TestAutoSync(c *gc.C) {
	// Prepare a mock storage for testing and store the
	// dummy tools in there.
	restore := createToolsStore(c)
	defer restore()

	// Change the tools location to be the test location and also
	// the version and ensure their later restoring.
	origVersion := version.Current
	version.Current.Number = version.MustParse("1.2.3")
	defer func() { version.Current = origVersion }()

	// Create home with dummy provider and remove all
	// of its tools.
	env, fake := makeEmptyFakeHome(c)
	defer fake.Restore()

	// Bootstrap the environment now detects the missing
	// tools and automatically synchronizes them from the
	// storage above.
	ctx := coretesting.Context(c)
	code := cmd.Main(&BootstrapCommand{}, ctx, nil)
	c.Check(code, gc.Equals, 0)

	// Now check the available tools which are the 1.0.0 tools.
	checkTools(c, env, v100All)
}
示例#3
0
func (s *UpgraderSuite) TestDelayedStop(c *C) {
	defer dummy.SetStorageDelay(0)
	tools := s.primeTools(c, version.MustParseBinary("2.0.3-foo-bar"))
	s.uploadTools(c, version.MustParseBinary("2.0.5-foo-bar"))
	s.uploadTools(c, version.MustParseBinary("2.0.6-foo-bar"))
	s.uploadTools(c, version.MustParseBinary("2.0.6-foo-bar"))
	s.uploadTools(c, version.MustParseBinary("2.0.7-foo-bar"))
	s.poisonVersion(version.MustParseBinary("2.0.7-foo-bar"))

	for i, test := range delayedStopTests {
		c.Logf("%d. %v", i, test.about)
		upgraderKillDelay = test.upgraderKillDelay
		dummy.SetStorageDelay(test.storageDelay)
		proposed := version.MustParse(test.propose)
		s.proposeVersion(c, proposed, true)
		u := s.startUpgrader(c, tools)
		t0 := time.Now()
		err := u.Stop()
		d := time.Now().Sub(t0)
		if d > 100*time.Millisecond {
			c.Errorf("upgrader took took too long: %v", d)
		}
		if test.err == "" {
			c.Check(err, IsNil)
		} else {
			c.Check(err, ErrorMatches, test.err)
		}
	}
}
示例#4
0
func (s *syncToolsSuite) SetUpTest(c *C) {
	s.LoggingSuite.SetUpTest(c)
	s.origVersion = version.Current
	// It's important that this be v1 to match the test data.
	version.Current.Number = version.MustParse("1.2.3")

	// Create a target environments.yaml and make sure its environment is empty.
	s.home = testing.MakeFakeHome(c, `
environments:
    test-target:
        type: dummy
        state-server: false
        authorized-keys: "not-really-one"
`)
	var err error
	s.targetEnv, err = environs.NewFromName("test-target")
	c.Assert(err, IsNil)
	envtesting.RemoveAllTools(c, s.targetEnv)

	// Create a source storage.
	s.storage, err = envtesting.NewEC2HTTPTestStorage("127.0.0.1")
	c.Assert(err, IsNil)

	// Create a local tools directory.
	s.localStorage = c.MkDir()

	// Populate both with the public tools.
	for _, vers := range vAll {
		s.storage.PutBinary(vers)
		putBinary(c, s.localStorage, vers)
	}

	s.origLocation = defaultToolsLocation
	defaultToolsLocation = s.storage.Location()
}
示例#5
0
func toolsStoragePath(vers string) string {
	return environs.ToolsStoragePath(version.Binary{
		Number: version.MustParse(vers),
		Series: version.Current.Series,
		Arch:   version.Current.Arch,
	})
}
示例#6
0
func (suite) TestNumberBSON(c *C) {
	type doc struct {
		Version version.Number
	}
	v := doc{version.MustParse("1.2.3")}
	data, err := bson.Marshal(&v)
	c.Assert(err, IsNil)
	var nv doc
	err = bson.Unmarshal(data, &nv)
	c.Assert(err, IsNil)
	c.Assert(v, Equals, nv)
}
示例#7
0
func (s *upgraderSuite) TestWatchAPIVersion(c *C) {
	w, err := s.st.WatchAPIVersion(s.rawMachine.Tag())
	c.Assert(err, IsNil)
	defer statetesting.AssertStop(c, w)
	wc := statetesting.NewNotifyWatcherC(c, s.BackingState, w)
	// Initial event
	wc.AssertOneChange()
	vers := version.MustParse("10.20.34")
	err = statetesting.SetAgentVersion(s.BackingState, vers)
	c.Assert(err, IsNil)
	// One change noticing the new version
	wc.AssertOneChange()
	// Setting the version to the same value doesn't trigger a change
	err = statetesting.SetAgentVersion(s.BackingState, vers)
	c.Assert(err, IsNil)
	wc.AssertNoChange()
	vers = version.MustParse("10.20.35")
	err = statetesting.SetAgentVersion(s.BackingState, vers)
	c.Assert(err, IsNil)
	wc.AssertOneChange()
	statetesting.AssertStop(c, w)
	wc.AssertClosed()
}
示例#8
0
func (suite) TestNumberMarshalUnmarshal(c *C) {
	for _, m := range marshallers {
		c.Logf("encoding %v", m.name)
		type doc struct {
			Version version.Number
		}
		v := doc{version.MustParse("1.2.3")}
		data, err := m.marshal(&v)
		c.Assert(err, IsNil)
		var nv doc
		err = m.unmarshal(data, &nv)
		c.Assert(err, IsNil)
		c.Assert(v, Equals, nv)
	}
}
示例#9
0
func (s *upgraderSuite) TestWatchAPIVersion(c *C) {
	args := params.Entities{
		Entities: []params.Entity{{Tag: s.rawMachine.Tag()}},
	}
	results, err := s.upgrader.WatchAPIVersion(args)
	c.Assert(err, IsNil)
	c.Check(results.Results, HasLen, 1)
	c.Check(results.Results[0].NotifyWatcherId, Not(Equals), "")
	c.Check(results.Results[0].Error, IsNil)
	resource := s.resources.Get(results.Results[0].NotifyWatcherId)
	c.Check(resource, NotNil)

	w := resource.(state.NotifyWatcher)
	wc := statetesting.NewNotifyWatcherC(c, s.State, w)
	wc.AssertNoChange()

	err = statetesting.SetAgentVersion(s.State, version.MustParse("3.4.567.8"))
	c.Assert(err, IsNil)
	wc.AssertOneChange()
	statetesting.AssertStop(c, w)
	wc.AssertClosed()
}
示例#10
0
func (s *BootstrapSuite) TestAutoSyncLocalSource(c *gc.C) {
	// Prepare a tools directory for testing and store the
	// dummy tools in there.
	source := createToolsSource(c)

	// Change the version and ensure its later restoring.
	origVersion := version.Current
	version.Current.Number = version.MustParse("1.2.3")
	defer func() {
		version.Current = origVersion
	}()

	// Create home with dummy provider and remove all
	// of its tools.
	env, fake := makeEmptyFakeHome(c)
	defer fake.Restore()

	// Bootstrap the environment with an invalid source.
	// The command returns with an error.
	ctx := coretesting.Context(c)
	code := cmd.Main(&BootstrapCommand{}, ctx, []string{"--source", c.MkDir()})
	c.Check(code, gc.Equals, 1)

	// Now check that there are no tools available.
	_, err := environs.FindAvailableTools(env, version.Current.Major)
	c.Assert(err, gc.ErrorMatches, "no tools available")

	// Bootstrap the environment with the valid source. This time
	// the bootstrapping has to show no error, because the tools
	// are automatically synchronized.
	ctx = coretesting.Context(c)
	code = cmd.Main(&BootstrapCommand{}, ctx, []string{"--source", source})
	c.Check(code, gc.Equals, 0)

	// Now check the available tools which are the 1.0.0 tools.
	checkTools(c, env, v100All)
}
示例#11
0
func (s *UpgradeJujuSuite) TestUpgradeJuju(c *C) {
	oldVersion := version.Current
	uploadTools = mockUploadTools
	defer func() {
		version.Current = oldVersion
		uploadTools = tools.Upload
	}()

	for i, test := range upgradeJujuTests {
		c.Logf("\ntest %d: %s", i, test.about)
		s.Reset(c)

		// Set up apparent CLI version and initialize the command.
		version.Current = version.MustParseBinary(test.currentVersion)
		com := &UpgradeJujuCommand{}
		if err := coretesting.InitCommand(com, test.args); err != nil {
			if test.expectInitErr != "" {
				c.Check(err, ErrorMatches, test.expectInitErr)
			} else {
				c.Check(err, IsNil)
			}
			continue
		}

		// Set up state and environ, and run the command.
		cfg, err := s.State.EnvironConfig()
		c.Assert(err, IsNil)
		cfg, err = cfg.Apply(map[string]interface{}{
			"agent-version": test.agentVersion,
			"development":   test.development,
		})
		c.Assert(err, IsNil)
		err = s.State.SetEnvironConfig(cfg)
		c.Assert(err, IsNil)
		for _, v := range test.private {
			vers := version.MustParseBinary(v)
			envtesting.MustUploadFakeToolsVersion(s.Conn.Environ.Storage(), vers)
		}
		for _, v := range test.public {
			vers := version.MustParseBinary(v)
			storage := s.Conn.Environ.PublicStorage().(environs.Storage)
			envtesting.MustUploadFakeToolsVersion(storage, vers)
		}
		err = com.Run(coretesting.Context(c))
		if test.expectErr != "" {
			c.Check(err, ErrorMatches, test.expectErr)
			continue
		} else if !c.Check(err, IsNil) {
			continue
		}

		// Check expected changes to environ/state.
		cfg, err = s.State.EnvironConfig()
		c.Check(err, IsNil)
		agentVersion, ok := cfg.AgentVersion()
		c.Check(ok, Equals, true)
		c.Check(agentVersion, Equals, version.MustParse(test.expectVersion))
		c.Check(cfg.Development(), Equals, test.development)

		for _, uploaded := range test.expectUploaded {
			vers := version.MustParseBinary(uploaded)
			r, err := s.Conn.Environ.Storage().Get(tools.StorageName(vers))
			if !c.Check(err, IsNil) {
				continue
			}
			data, err := ioutil.ReadAll(r)
			r.Close()
			c.Check(err, IsNil)
			c.Check(string(data), Equals, uploaded)
		}
	}
}
示例#12
0
		"state-server":    false,
		"authorized-keys": "i-am-a-key",
		"ca-cert":         testing.CACert,
		"ca-private-key":  "",
	}
	for k, v := range attrs {
		final[k] = v
	}
	env, err := environs.NewFromAttrs(final)
	c.Assert(err, IsNil)
	s.env = env
	envtesting.RemoveAllTools(c, s.env)
}

var (
	v100    = version.MustParse("1.0.0")
	v100p64 = version.MustParseBinary("1.0.0-precise-amd64")
	v100p32 = version.MustParseBinary("1.0.0-precise-i386")
	v100p   = []version.Binary{v100p64, v100p32}

	v100q64 = version.MustParseBinary("1.0.0-quantal-amd64")
	v100q32 = version.MustParseBinary("1.0.0-quantal-i386")
	v100q   = []version.Binary{v100q64, v100q32}
	v100all = append(v100p, v100q...)

	v1001    = version.MustParse("1.0.0.1")
	v1001p64 = version.MustParseBinary("1.0.0.1-precise-amd64")
	v100Xall = append(v100all, v1001p64)

	v110    = version.MustParse("1.1.0")
	v110p64 = version.MustParseBinary("1.1.0-precise-amd64")
示例#13
0
func (s *UpgraderSuite) TestUpgrader(c *C) {
	currentTools := s.primeTools(c, version.MustParseBinary("2.0.0-foo-bar"))
	// Remove the tools from the storage so that we're sure that the
	// uploader isn't trying to fetch them.
	resp, err := http.Get(currentTools.URL)
	c.Assert(err, IsNil)
	err = environs.UnpackTools(s.DataDir(), currentTools, resp.Body)
	c.Assert(err, IsNil)
	s.removeVersion(c, currentTools.Binary)

	var (
		u            *Upgrader
		upgraderDone <-chan error
	)

	defer func() {
		if u != nil {
			c.Assert(u.Stop(), IsNil)
		}
	}()

	uploaded := make(map[version.Number]*state.Tools)
	for i, test := range upgraderTests {
		c.Logf("%d. %s; current version: %v", i, test.about, version.Current)
		for _, v := range test.upload {
			vers := version.Current
			vers.Number = version.MustParse(v)
			tools := s.uploadTools(c, vers)
			uploaded[vers.Number] = tools
		}
		if test.current != "" {
			version.Current = version.MustParseBinary(test.current)
			currentTools, err = environs.ReadTools(s.DataDir(), version.Current)
			c.Assert(err, IsNil)
		}
		if u == nil {
			u = s.startUpgrader(c, currentTools)
		}
		if test.propose != "" {
			s.proposeVersion(c, version.MustParse(test.propose), test.devVersion)
			s.State.StartSync()
		}
		if test.upgradeTo == "" {
			s.State.StartSync()
			assertNothingHappens(c, upgraderDone)
		} else {
			ug := waitDeath(c, u)
			tools := uploaded[version.MustParse(test.upgradeTo)]
			c.Check(ug.NewTools, DeepEquals, tools)
			c.Check(ug.OldTools.Binary, Equals, version.Current)
			c.Check(ug.DataDir, Equals, s.DataDir())
			c.Check(ug.AgentName, Equals, "testagent")

			// Check that the upgraded version was really downloaded.
			data, err := ioutil.ReadFile(filepath.Join(environs.ToolsDir(s.DataDir(), tools.Binary), "jujud"))
			c.Assert(err, IsNil)
			c.Assert(string(data), Equals, "jujud contents "+tools.Binary.String())

			u, upgraderDone = nil, nil
			currentTools = tools
			version.Current = tools.Binary
		}
	}
}
示例#14
0
		},

		setMachineStatus{"0", params.StatusStarted, ""},
		expect{
			"simulate the MA started and set the machine status",
			M{
				"machines": M{
					"0": machine0,
				},
				"services": M{},
			},
		},

		setTools{"0", &tools.Tools{
			Binary: version.Binary{
				Number: version.MustParse("1.2.3"),
				Series: "gutsy",
				Arch:   "ppc",
			},
			URL: "http://canonical.com/",
		}},
		expect{
			"simulate the MA setting the version",
			M{
				"machines": M{
					"0": M{
						"dns-name":      "dummyenv-0.dns",
						"instance-id":   "dummyenv-0",
						"agent-version": "1.2.3",
						"agent-state":   "started",
						"series":        "series",
示例#15
0
		t2001precise.Binary: t2001precise.URL,
	})
}

var newestTests = []struct {
	src    tools.List
	expect tools.List
	number version.Number
}{{
	src:    nil,
	expect: nil,
	number: version.Zero,
}, {
	src:    tools.List{t100precise},
	expect: tools.List{t100precise},
	number: version.MustParse("1.0.0"),
}, {
	src:    t100all,
	expect: t100all,
	number: version.MustParse("1.0.0"),
}, {
	src:    extend(t100all, t190all, t200all),
	expect: t200all,
	number: version.MustParse("2.0.0"),
}, {
	src:    tAll,
	expect: tools.List{t2001precise},
	number: version.MustParse("2.0.0.1"),
}}

func (s *ListSuite) TestNewest(c *gc.C) {
示例#16
0
func (s *UpgraderSuite) TearDownTest(c *C) {
	version.Current = s.oldVersion
	s.JujuConnSuite.TearDownTest(c)
}

func (s *UpgraderSuite) TestUpgraderStop(c *C) {
	u := s.startUpgrader(c, &state.Tools{Binary: version.Current})
	err := u.Stop()
	c.Assert(err, IsNil)
}

var _ worker.Worker = (*Upgrader)(nil)

var (
	v200     = version.MustParse("2.0.0")
	t200p64  = version.MustParseBinary("2.0.0-precise-amd64")
	t200p32  = version.MustParseBinary("2.0.0-precise-i386")
	t200q64  = version.MustParseBinary("2.0.0-quantal-amd64")
	t2007p64 = version.MustParseBinary("2.0.0.7-precise-amd64")
	t200all  = []version.Binary{t200p64, t200p32, t200q64, t2007p64}

	v214     = version.MustParse("2.1.4")
	t214p64  = version.MustParseBinary("2.1.4-precise-amd64")
	v2144    = version.MustParse("2.1.4.4")
	t2144p64 = version.MustParseBinary("2.1.4.4-precise-amd64")
	t2144p32 = version.MustParseBinary("2.1.4.4-precise-i386")
	t2144q64 = version.MustParseBinary("2.1.4.4-quantal-amd64")
	t214all  = []version.Binary{t214p64, t2144p64, t2144p32, t2144q64}
	t2all    = append(t200all, t214all...)
示例#17
0
var findToolsTests = []struct {
	version        version.Number // version to assume is current for the test.
	flags          environs.ToolsSearchFlags
	contents       []string // names in private storage.
	publicContents []string // names in public storage.
	expect         string   // the name we expect to find (if no error).
	err            string   // the error we expect to find (if not blank).
}{{
	// current version should be satisfied by current tools path.
	version:  version.Current.Number,
	flags:    environs.CompatVersion,
	contents: []string{environs.ToolsStoragePath(version.Current)},
	expect:   environs.ToolsStoragePath(version.Current),
}, {
	// highest version of tools is chosen.
	version: version.MustParse("0.0.0"),
	flags:   environs.HighestVersion | environs.DevVersion | environs.CompatVersion,
	contents: []string{
		toolsStoragePath("0.0.9"),
		toolsStoragePath("0.1.9"),
	},
	expect: toolsStoragePath("0.1.9"),
}, {
	// fall back to public storage when nothing found in private.
	version: version.MustParse("1.0.2"),
	flags:   environs.DevVersion | environs.CompatVersion,
	contents: []string{
		toolsStoragePath("0.0.9"),
	},
	publicContents: []string{
		toolsStoragePath("1.0.0"),
示例#18
0
func (test configTest) check(c *gc.C, home *testing.FakeHome) {
	cfg, err := config.New(test.attrs)
	if test.err != "" {
		c.Check(cfg, gc.IsNil)
		c.Assert(err, gc.ErrorMatches, test.err)
		return
	}
	c.Assert(err, gc.IsNil)

	typ, _ := test.attrs["type"].(string)
	name, _ := test.attrs["name"].(string)
	c.Assert(cfg.Type(), gc.Equals, typ)
	c.Assert(cfg.Name(), gc.Equals, name)
	agentVersion, ok := cfg.AgentVersion()
	if s := test.attrs["agent-version"]; s != nil {
		c.Assert(ok, jc.IsTrue)
		c.Assert(agentVersion, gc.Equals, version.MustParse(s.(string)))
	} else {
		c.Assert(ok, jc.IsFalse)
		c.Assert(agentVersion, gc.Equals, version.Zero)
	}

	if statePort, _ := test.attrs["state-port"].(int); statePort != 0 {
		c.Assert(cfg.StatePort(), gc.Equals, statePort)
	}
	if apiPort, _ := test.attrs["api-port"].(int); apiPort != 0 {
		c.Assert(cfg.APIPort(), gc.Equals, apiPort)
	}

	dev, _ := test.attrs["development"].(bool)
	c.Assert(cfg.Development(), gc.Equals, dev)

	if series, _ := test.attrs["default-series"].(string); series != "" {
		c.Assert(cfg.DefaultSeries(), gc.Equals, series)
	} else {
		c.Assert(cfg.DefaultSeries(), gc.Equals, config.DefaultSeries)
	}

	if m, _ := test.attrs["firewall-mode"].(string); m != "" {
		c.Assert(cfg.FirewallMode(), gc.Equals, config.FirewallMode(m))
	}

	if secret, _ := test.attrs["admin-secret"].(string); secret != "" {
		c.Assert(cfg.AdminSecret(), gc.Equals, secret)
	}

	if path, _ := test.attrs["authorized-keys-path"].(string); path != "" {
		c.Assert(cfg.AuthorizedKeys(), gc.Equals, home.FileContents(c, path))
		c.Assert(cfg.AllAttrs()["authorized-keys-path"], gc.IsNil)
	} else if keys, _ := test.attrs["authorized-keys"].(string); keys != "" {
		c.Assert(cfg.AuthorizedKeys(), gc.Equals, keys)
	} else {
		// Content of all the files that are read by default.
		want := "dsa\nrsa\nidentity\n"
		c.Assert(cfg.AuthorizedKeys(), gc.Equals, want)
	}

	cert, certPresent := cfg.CACert()
	if path, _ := test.attrs["ca-cert-path"].(string); path != "" {
		c.Assert(certPresent, jc.IsTrue)
		c.Assert(string(cert), gc.Equals, home.FileContents(c, path))
	} else if v, ok := test.attrs["ca-cert"].(string); v != "" {
		c.Assert(certPresent, jc.IsTrue)
		c.Assert(string(cert), gc.Equals, v)
	} else if ok {
		c.Check(cert, gc.HasLen, 0)
		c.Assert(certPresent, jc.IsFalse)
	} else if home.FileExists(".juju/my-name-cert.pem") {
		c.Assert(certPresent, jc.IsTrue)
		c.Assert(string(cert), gc.Equals, home.FileContents(c, "my-name-cert.pem"))
	} else {
		c.Check(cert, gc.HasLen, 0)
		c.Assert(certPresent, jc.IsFalse)
	}

	key, keyPresent := cfg.CAPrivateKey()
	if path, _ := test.attrs["ca-private-key-path"].(string); path != "" {
		c.Assert(keyPresent, jc.IsTrue)
		c.Assert(string(key), gc.Equals, home.FileContents(c, path))
	} else if v, ok := test.attrs["ca-private-key"].(string); v != "" {
		c.Assert(keyPresent, jc.IsTrue)
		c.Assert(string(key), gc.Equals, v)
	} else if ok {
		c.Check(key, gc.HasLen, 0)
		c.Assert(keyPresent, jc.IsFalse)
	} else if home.FileExists(".juju/my-name-private-key.pem") {
		c.Assert(keyPresent, jc.IsTrue)
		c.Assert(string(key), gc.Equals, home.FileContents(c, "my-name-private-key.pem"))
	} else {
		c.Check(key, gc.HasLen, 0)
		c.Assert(keyPresent, jc.IsFalse)
	}

	if v, ok := test.attrs["ssl-hostname-verification"]; ok {
		c.Assert(cfg.SSLHostnameVerification(), gc.Equals, v)
	}
}