func (s *upgradeSuite) TestPerformUpgrade(c *gc.C) { s.PatchValue(upgrades.UpgradeOperations, upgradeOperations) for i, test := range upgradeTests { c.Logf("%d: %s", i, test.about) var messages []string ctx := &mockContext{ messages: messages, } fromVersion := version.Zero if test.fromVersion != "" { fromVersion = version.MustParse(test.fromVersion) } toVersion := version.MustParse("1.18.0") if test.toVersion != "" { toVersion = version.MustParse(test.toVersion) } vers := version.Current vers.Number = toVersion s.PatchValue(&version.Current, vers) err := upgrades.PerformUpgrade(fromVersion, test.target, ctx) if test.err == "" { c.Check(err, gc.IsNil) } else { c.Check(err, gc.ErrorMatches, test.err) } c.Check(ctx.messages, jc.DeepEquals, test.expectedSteps) } }
func stateUpgradeOperations() []upgrades.Operation { steps := []upgrades.Operation{ &mockUpgradeOperation{ targetVersion: version.MustParse("1.11.0"), steps: []upgrades.Step{ newUpgradeStep("state step 1 - 1.11.0", upgrades.Controller), newUpgradeStep("state step 2 error", upgrades.Controller), newUpgradeStep("state step 3 - 1.11.0", upgrades.Controller), }, }, &mockUpgradeOperation{ targetVersion: version.MustParse("1.21.0"), steps: []upgrades.Step{ newUpgradeStep("state step 1 - 1.21.0", upgrades.DatabaseMaster), newUpgradeStep("state step 2 - 1.21.0", upgrades.Controller), }, }, &mockUpgradeOperation{ targetVersion: version.MustParse("1.22.0"), steps: []upgrades.Step{ newUpgradeStep("state step 1 - 1.22.0", upgrades.DatabaseMaster), newUpgradeStep("state step 2 - 1.22.0", upgrades.Controller), }, }, } return steps }
func (s *toolsSuite) TestFindToolsExactNotInStorage(c *gc.C) { mockToolsStorage := &mockToolsStorage{} s.PatchValue(&version.Current, version.MustParse("1.22-beta1")) s.testFindToolsExact(c, mockToolsStorage, false, true) s.PatchValue(&version.Current, version.MustParse("1.22.0")) s.testFindToolsExact(c, mockToolsStorage, false, false) }
func (s *machineUpgraderSuite) TestWatchAPIVersion(c *gc.C) { w, err := s.st.WatchAPIVersion(s.rawMachine.Tag().String()) c.Assert(err, jc.ErrorIsNil) wc := watchertest.NewNotifyWatcherC(c, w, s.BackingState.StartSync) defer wc.AssertStops() // Initial event wc.AssertOneChange() // One change noticing the new version vers := version.MustParse("10.20.34") err = statetesting.SetAgentVersion(s.BackingState, vers) c.Assert(err, jc.ErrorIsNil) wc.AssertOneChange() // Setting the version to the same value doesn't trigger a change err = statetesting.SetAgentVersion(s.BackingState, vers) c.Assert(err, jc.ErrorIsNil) wc.AssertNoChange() // Another change noticing another new version vers = version.MustParse("10.20.35") err = statetesting.SetAgentVersion(s.BackingState, vers) c.Assert(err, jc.ErrorIsNil) wc.AssertOneChange() }
func twoDotOhDeprecation(replacement string) cmd.DeprecationCheck { return &versionDeprecation{ replacement: replacement, deprecate: version.MustParse("2.0-00"), obsolete: version.MustParse("3.0-00"), } }
func (s *UpgradeJujuSuite) TestBlockUpgradeJujuWithRealUpload(c *gc.C) { s.Reset(c) s.PatchValue(&version.Current, version.MustParse("1.99.99")) cmd := newUpgradeJujuCommand(map[int]version.Number{2: version.MustParse("1.99.99")}) // Block operation s.BlockAllChanges(c, "TestBlockUpgradeJujuWithRealUpload") _, err := coretesting.RunCommand(c, cmd, "--upload-tools") s.AssertBlocked(c, err, ".*TestBlockUpgradeJujuWithRealUpload.*") }
func (s *SimpleStreamsToolsSuite) TestPreferredStream(c *gc.C) { for i, test := range preferredStreamTests { c.Logf("\ntest %d", i) s.PatchValue(&version.Current, version.MustParse(test.currentVers)) var vers *version.Number if test.explicitVers != "" { v := version.MustParse(test.explicitVers) vers = &v } obtained := envtools.PreferredStream(vers, test.forceDevel, test.streamInConfig) c.Check(obtained, gc.Equals, test.expected) } }
func (s *UpgradeJujuSuite) TestUpgradeJujuWithRealUpload(c *gc.C) { s.Reset(c) s.PatchValue(&version.Current, version.MustParse("1.99.99")) cmd := newUpgradeJujuCommand(map[int]version.Number{2: version.MustParse("1.99.99")}) _, err := coretesting.RunCommand(c, cmd, "--upload-tools") c.Assert(err, jc.ErrorIsNil) vers := version.Binary{ Number: version.Current, Arch: arch.HostArch(), Series: series.HostSeries(), } vers.Build = 1 s.checkToolsUploaded(c, vers, vers.Number) }
func (s *EnvironmentVersionSuite) TestSuccess(c *gc.C) { vs := "1.22.1" s.fake.agentVersion = vs v, err := envcmd.GetEnvironmentVersion(s.fake) c.Assert(err, jc.ErrorIsNil) c.Assert(v.Compare(version.MustParse(vs)), gc.Equals, 0) }
func (*format_1_16Suite) TestReadConfReadsLegacyFormatAndWritesNew(c *gc.C) { dataDir := c.MkDir() formatPath := filepath.Join(dataDir, legacyFormatFilename) err := utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600) c.Assert(err, gc.IsNil) configPath := filepath.Join(dataDir, agentConfigFilename) err = utils.AtomicWriteFile(configPath, []byte(agentConfig1_16Contents), 0600) c.Assert(err, gc.IsNil) config, err := ReadConfig(configPath) c.Assert(err, gc.IsNil) c.Assert(config, gc.NotNil) // Test we wrote a currently valid config. config, err = ReadConfig(configPath) c.Assert(err, gc.IsNil) c.Assert(config, gc.NotNil) c.Assert(config.UpgradedToVersion(), jc.DeepEquals, version.MustParse("1.16.0")) c.Assert(config.Jobs(), gc.HasLen, 0) // Old format was deleted. assertFileNotExist(c, formatPath) // And new contents were written. data, err := ioutil.ReadFile(configPath) c.Assert(err, gc.IsNil) c.Assert(string(data), gc.Not(gc.Equals), agentConfig1_16Contents) }
func (s *steps121Suite) TestStateStepsFor121(c *gc.C) { expected := []string{ // Settings, and then environment UUID, related migrations should // come first as other upgrade steps may rely on them. "add the version field to all settings docs", "add environment uuid to state server doc", "set environment owner and server uuid", // It is important to keep the order of the following three steps: // 1.migrate machine instanceId, 2. Add env ID to machine docs, 3. // Add env ID to instanceData docs. If the order changes, bad things // will happen. "migrate machine instanceId into instanceData", "prepend the environment UUID to the ID of all machine docs", "prepend the environment UUID to the ID of all instanceData docs", "prepend the environment UUID to the ID of all containerRef docs", "prepend the environment UUID to the ID of all service docs", "prepend the environment UUID to the ID of all unit docs", "prepend the environment UUID to the ID of all reboot docs", "prepend the environment UUID to the ID of all relations docs", "prepend the environment UUID to the ID of all relationscopes docs", "prepend the environment UUID to the ID of all minUnit docs", "prepend the environment UUID to the ID of all cleanup docs", "prepend the environment UUID to the ID of all sequence docs", // Non-environment UUID upgrade steps follow. "rename the user LastConnection field to LastLogin", "add all users in state as environment users", "migrate custom image metadata into environment storage", "migrate tools into environment storage", "migrate individual unit ports to openedPorts collection", "create entries in meter status collection for existing units", "migrate machine jobs into ones with JobManageNetworking based on rules", } assertStateSteps(c, version.MustParse("1.21.0"), expected) }
func (s *format_1_16Suite) TestMissingAttributes(c *gc.C) { logDir, err := paths.LogDir(series.HostSeries()) c.Assert(err, jc.ErrorIsNil) realDataDir, err := paths.DataDir(series.HostSeries()) c.Assert(err, jc.ErrorIsNil) realDataDir = filepath.FromSlash(realDataDir) logPath := filepath.Join(logDir, "juju") logPath = filepath.FromSlash(logPath) dataDir := c.MkDir() formatPath := filepath.Join(dataDir, legacyFormatFilename) err = utils.AtomicWriteFile(formatPath, []byte(legacyFormatFileContents), 0600) c.Assert(err, jc.ErrorIsNil) configPath := filepath.Join(dataDir, agentConfigFilename) err = utils.AtomicWriteFile(configPath, []byte(configDataWithoutNewAttributes), 0600) c.Assert(err, jc.ErrorIsNil) readConfig, err := ReadConfig(configPath) c.Assert(err, jc.ErrorIsNil) c.Assert(readConfig.UpgradedToVersion(), gc.Equals, version.MustParse("1.16.0")) configLogDir := filepath.FromSlash(readConfig.LogDir()) configDataDir := filepath.FromSlash(readConfig.DataDir()) c.Assert(configLogDir, gc.Equals, logPath) c.Assert(configDataDir, gc.Equals, realDataDir) // Test data doesn't include a StateServerKey so StateServingInfo // should *not* be available _, available := readConfig.StateServingInfo() c.Assert(available, jc.IsFalse) }
func (s *metadataSuite) TestAsJSONBuffer(c *gc.C) { meta := backups.NewMetadata() meta.Origin = backups.Origin{ Environment: "asdf-zxcv-qwe", Machine: "0", Hostname: "myhost", Version: version.MustParse("1.21-alpha3"), } meta.Started = time.Date(2014, time.Month(9), 9, 11, 59, 34, 0, time.UTC) meta.SetID("20140909-115934.asdf-zxcv-qwe") err := meta.MarkComplete(10, "123af2cef") c.Assert(err, jc.ErrorIsNil) finished := meta.Started.Add(time.Minute) meta.Finished = &finished buf, err := meta.AsJSONBuffer() c.Assert(err, jc.ErrorIsNil) c.Check(buf.(*bytes.Buffer).String(), gc.Equals, `{`+ `"ID":"20140909-115934.asdf-zxcv-qwe",`+ `"Checksum":"123af2cef",`+ `"ChecksumFormat":"SHA-1, base64 encoded",`+ `"Size":10,`+ `"Stored":"0001-01-01T00:00:00Z",`+ `"Started":"2014-09-09T11:59:34Z",`+ `"Finished":"2014-09-09T12:00:34Z",`+ `"Notes":"",`+ `"Environment":"asdf-zxcv-qwe",`+ `"Machine":"0",`+ `"Hostname":"myhost",`+ `"Version":"1.21-alpha3"`+ `}`+"\n") }
func (s *steps125Suite) TestStepsFor125(c *gc.C) { expected := []string{ "remove Jujud.pass file on windows", "add juju registry key", } assertSteps(c, version.MustParse("1.25.0"), expected) }
// binary returns the tools metadata's binary version, // which may be used for map lookup. func (t *ToolsMetadata) binary() version.Binary { return version.Binary{ Number: version.MustParse(t.Version), Series: t.Release, Arch: t.Arch, } }
func (s *toolsSuite) TestFindAvailableToolsSpecificVersion(c *gc.C) { currentVersion := version.Binary{ Number: version.Current, Arch: arch.HostArch(), Series: series.HostSeries(), } currentVersion.Major = 2 currentVersion.Minor = 3 s.PatchValue(&version.Current, currentVersion.Number) var findToolsCalled int s.PatchValue(bootstrap.FindTools, func(_ environs.Environ, major, minor int, stream string, f tools.Filter) (tools.List, error) { c.Assert(f.Number.Major, gc.Equals, 10) c.Assert(f.Number.Minor, gc.Equals, 11) c.Assert(f.Number.Patch, gc.Equals, 12) c.Assert(stream, gc.Equals, "released") findToolsCalled++ return []*tools.Tools{ &tools.Tools{ Version: currentVersion, URL: "http://testing.invalid/tools.tar.gz", }, }, nil }) env := newEnviron("foo", useDefaultKeys, nil) toolsVersion := version.MustParse("10.11.12") result, err := bootstrap.FindAvailableTools(env, &toolsVersion, nil, nil, false) c.Assert(err, jc.ErrorIsNil) c.Assert(findToolsCalled, gc.Equals, 1) c.Assert(result, jc.DeepEquals, tools.List{ &tools.Tools{ Version: currentVersion, URL: "http://testing.invalid/tools.tar.gz", }, }) }
func (st *State) checkCanUpgrade(currentVersion, newVersion string) error { matchCurrent := "^" + regexp.QuoteMeta(currentVersion) + "-" matchNew := "^" + regexp.QuoteMeta(newVersion) + "-" // Get all machines and units with a different or empty version. sel := bson.D{{"$or", []bson.D{ {{"tools", bson.D{{"$exists", false}}}}, {{"$and", []bson.D{ {{"tools.version", bson.D{{"$not", bson.RegEx{matchCurrent, ""}}}}}, {{"tools.version", bson.D{{"$not", bson.RegEx{matchNew, ""}}}}}, }}}, }}} var agentTags []string for _, collection := range []*mgo.Collection{st.machines, st.units} { var doc struct { Id string `bson:"_id"` } iter := collection.Find(sel).Select(bson.D{{"_id", 1}}).Iter() for iter.Next(&doc) { switch collection.Name { case "machines": agentTags = append(agentTags, names.NewMachineTag(doc.Id).String()) case "units": agentTags = append(agentTags, names.NewUnitTag(doc.Id).String()) } } if err := iter.Close(); err != nil { return err } } if len(agentTags) > 0 { return newVersionInconsistentError(version.MustParse(currentVersion), agentTags) } return nil }
func (s *format_1_18Suite) TestMissingAttributes(c *gc.C) { logDir, err := paths.LogDir(series.HostSeries()) c.Assert(err, jc.ErrorIsNil) realDataDir, err := paths.DataDir(series.HostSeries()) c.Assert(err, jc.ErrorIsNil) realDataDir = filepath.FromSlash(realDataDir) logPath := filepath.Join(logDir, "juju") logPath = filepath.FromSlash(logPath) dataDir := c.MkDir() configPath := filepath.Join(dataDir, agentConfigFilename) err = utils.AtomicWriteFile(configPath, []byte(configData1_18WithoutUpgradedToVersion), 0600) c.Assert(err, jc.ErrorIsNil) readConfig, err := ReadConfig(configPath) c.Assert(err, jc.ErrorIsNil) c.Assert(readConfig.UpgradedToVersion(), gc.Equals, version.MustParse("1.16.0")) configLogDir := filepath.FromSlash(readConfig.LogDir()) configDataDir := filepath.FromSlash(readConfig.DataDir()) c.Assert(configLogDir, gc.Equals, logPath) c.Assert(configDataDir, gc.Equals, realDataDir) c.Assert(readConfig.PreferIPv6(), jc.IsFalse) // The api info doesn't have the environment tag set. apiInfo, ok := readConfig.APIInfo() c.Assert(ok, jc.IsTrue) c.Assert(apiInfo.EnvironTag.Id(), gc.Equals, "") }
func (s *AllowedTargetVersionSuite) TestAllowedTargetVersionSuite(c *gc.C) { cases := []allowedTest{ {current: "1.2.3", target: "1.3.3", allowed: true}, {current: "1.2.3", target: "1.2.3", allowed: true}, {current: "1.2.3", target: "2.2.3", allowed: true}, {current: "1.2.3", target: "1.1.3", allowed: false}, {current: "1.2.3", target: "1.2.2", allowed: true}, {current: "1.2.3", target: "0.2.3", allowed: false}, } for i, test := range cases { c.Logf("test case %d, %#v", i, test) current := version.MustParse(test.current) target := version.MustParse(test.target) c.Check(upgrader.AllowedTargetVersion(current, target), gc.Equals, test.allowed) } }
func (s *upgradeSuite) TestStateStepsNotAttemptedWhenNoStateTarget(c *gc.C) { stateCount := 0 stateUpgradeOperations := func() []upgrades.Operation { stateCount++ return nil } s.PatchValue(upgrades.StateUpgradeOperations, stateUpgradeOperations) apiCount := 0 upgradeOperations := func() []upgrades.Operation { apiCount++ return nil } s.PatchValue(upgrades.UpgradeOperations, upgradeOperations) fromVers := version.MustParse("1.18.0") ctx := new(mockContext) check := func(target upgrades.Target, expectedStateCallCount int) { stateCount = 0 apiCount = 0 err := upgrades.PerformUpgrade(fromVers, targets(target), ctx) c.Assert(err, jc.ErrorIsNil) c.Assert(stateCount, gc.Equals, expectedStateCallCount) c.Assert(apiCount, gc.Equals, 1) } check(upgrades.Controller, 1) check(upgrades.DatabaseMaster, 1) check(upgrades.AllMachines, 0) check(upgrades.HostMachine, 0) }
func (s *steps125Suite) TestStateStepsFor125(c *gc.C) { expected := []string{ "set hosted environment count to number of hosted environments", "tag machine instances", } assertStateSteps(c, version.MustParse("1.25.0"), expected) }
func (s *syncSuite) setUpTest(c *gc.C) { if runtime.GOOS == "windows" { c.Skip("issue 1403084: Currently does not work because of jujud problems") } s.FakeJujuHomeSuite.SetUpTest(c) s.ToolsFixture.SetUpTest(c) // It's important that this be v1.8.x to match the test data. s.PatchValue(&version.Current, version.MustParse("1.8.3")) // Create a source storage. baseDir := c.MkDir() stor, err := filestorage.NewFileStorageWriter(baseDir) c.Assert(err, jc.ErrorIsNil) s.storage = stor // Create a local tools directory. s.localStorage = c.MkDir() // Populate both local and default tools locations with the public tools. versionStrings := make([]string, len(vAll)) for i, vers := range vAll { versionStrings[i] = vers.String() } toolstesting.MakeTools(c, baseDir, "released", versionStrings) toolstesting.MakeTools(c, s.localStorage, "released", versionStrings) // Switch the default tools location. baseURL, err := s.storage.URL(storage.BaseToolsPath) c.Assert(err, jc.ErrorIsNil) s.PatchValue(&envtools.DefaultBaseURL, baseURL) }
func (s *steps123Suite) TestStepsFor123(c *gc.C) { expected := []string{ "add environment UUID to agent config", "add Stopped field to uniter state", } assertSteps(c, version.MustParse("1.23.0"), expected) }
func (s *UpgradeJujuSuite) TestUpgradeJuju(c *gc.C) { for i, test := range upgradeJujuTests { c.Logf("\ntest %d: %s", i, test.about) s.Reset(c) tools.DefaultBaseURL = "" // Set up apparent CLI version and initialize the command. s.PatchValue(&version.Current, version.MustParseBinary(test.currentVersion)) com := &UpgradeJujuCommand{} if err := coretesting.InitCommand(envcmd.Wrap(com), test.args); err != nil { if test.expectInitErr != "" { c.Check(err, gc.ErrorMatches, test.expectInitErr) } else { c.Check(err, jc.ErrorIsNil) } continue } // Set up state and environ, and run the command. toolsDir := c.MkDir() updateAttrs := map[string]interface{}{ "agent-version": test.agentVersion, "agent-metadata-url": "file://" + toolsDir + "/tools", } err := s.State.UpdateEnvironConfig(updateAttrs, nil, nil) c.Assert(err, jc.ErrorIsNil) versions := make([]version.Binary, len(test.tools)) for i, v := range test.tools { versions[i] = version.MustParseBinary(v) } if len(versions) > 0 { stor, err := filestorage.NewFileStorageWriter(toolsDir) c.Assert(err, jc.ErrorIsNil) envtesting.MustUploadFakeToolsVersions(stor, s.Environ.Config().AgentStream(), versions...) } err = com.Run(coretesting.Context(c)) if test.expectErr != "" { c.Check(err, gc.ErrorMatches, test.expectErr) continue } else if !c.Check(err, jc.ErrorIsNil) { continue } // Check expected changes to environ/state. cfg, err := s.State.EnvironConfig() c.Check(err, jc.ErrorIsNil) agentVersion, ok := cfg.AgentVersion() c.Check(ok, jc.IsTrue) c.Check(agentVersion, gc.Equals, version.MustParse(test.expectVersion)) for _, uploaded := range test.expectUploaded { // Substitute latest LTS for placeholder in expected series for uploaded tools uploaded = strings.Replace(uploaded, "%LTS%", config.LatestLtsSeries(), 1) vers := version.MustParseBinary(uploaded) s.checkToolsUploaded(c, vers, agentVersion) } } }
func (s *bootstrapSuite) TestBootstrapSpecificVersionClientMajorMismatch(c *gc.C) { // bootstrap using a specified version only works if the patch number is different. // The bootstrap client major and minor versions need to match the tools asked for. toolsVersion := version.MustParse("10.11.12") err, bootstrapCount, _ := s.setupBootstrapSpecificVersion(c, 1, 11, &toolsVersion) c.Assert(strings.Replace(err.Error(), "\n", "", -1), gc.Matches, ".* no tools are available .*") c.Assert(bootstrapCount, gc.Equals, 0) }
func (s *productSpecSuite) TestIndexIdNoStream(c *gc.C) { toolsConstraint := tools.NewVersionedToolsConstraint(version.MustParse("1.13.0"), simplestreams.LookupParams{ Series: []string{"precise"}, Arches: []string{"amd64"}, }) ids := toolsConstraint.IndexIds() c.Assert(ids, gc.HasLen, 0) }
func (s *steps118Suite) TestStateStepsFor118(c *gc.C) { expected := []string{ "update rsyslog port", "remove deprecated environment config settings", "migrate local provider agent config", } assertStateSteps(c, version.MustParse("1.18.0"), expected) }
func (s *upgradeSuite) checkContextRestriction(c *gc.C, expectedPanic string) { fromVersion := version.MustParse("1.20.0") type fakeAgentConfigSetter struct{ agent.ConfigSetter } ctx := upgrades.NewContext(fakeAgentConfigSetter{}, nil, new(state.State)) c.Assert( func() { upgrades.PerformUpgrade(fromVersion, targets(upgrades.Controller), ctx) }, gc.PanicMatches, expectedPanic, ) }
func (s *upgradeSuite) TestAreUpgradesDefined(c *gc.C) { s.PatchValue(upgrades.StateUpgradeOperations, stateUpgradeOperations) s.PatchValue(upgrades.UpgradeOperations, upgradeOperations) for i, test := range areUpgradesDefinedTests { c.Logf("%d: %s", i, test.about) fromVersion := version.Zero if test.fromVersion != "" { fromVersion = version.MustParse(test.fromVersion) } toVersion := version.MustParse("1.18.0") if test.toVersion != "" { toVersion = version.MustParse(test.toVersion) } s.PatchValue(&version.Current, toVersion) result := upgrades.AreUpgradesDefined(fromVersion) c.Check(result, gc.Equals, test.expected) } }
func (s *productSpecSuite) TestProductId(c *gc.C) { toolsConstraint := tools.NewVersionedToolsConstraint(version.MustParse("1.13.0"), simplestreams.LookupParams{ Series: []string{"precise"}, Arches: []string{"amd64"}, }) ids, err := toolsConstraint.ProductIds() c.Assert(err, jc.ErrorIsNil) c.Assert(ids, gc.DeepEquals, []string{"com.ubuntu.juju:12.04:amd64"}) }