// AssertUploadFakeToolsVersions puts fake tools in the supplied storage for the supplied versions. func AssertUploadFakeToolsVersions(c *gc.C, stor storage.Storage, versions ...version.Binary) []*coretools.Tools { agentTools, err := UploadFakeToolsVersions(stor, versions...) c.Assert(err, gc.IsNil) err = envtools.MergeAndWriteMetadata(stor, agentTools, envtools.DoNotWriteMirrors) c.Assert(err, gc.IsNil) return agentTools }
func makeTools(c *gc.C, metadataDir, subdir string, versionStrings []string, withCheckSum bool) coretools.List { toolsDir := filepath.Join(metadataDir, storage.BaseToolsPath) if subdir != "" { toolsDir = filepath.Join(toolsDir, subdir) } c.Assert(os.MkdirAll(toolsDir, 0755), gc.IsNil) var toolsList coretools.List for _, versionString := range versionStrings { binary := version.MustParseBinary(versionString) path := filepath.Join(toolsDir, fmt.Sprintf("juju-%s.tgz", binary)) data := binary.String() err := ioutil.WriteFile(path, []byte(data), 0644) c.Assert(err, gc.IsNil) tool := &coretools.Tools{ Version: binary, URL: path, } if withCheckSum { tool.Size, tool.SHA256 = SHA256sum(c, path) } toolsList = append(toolsList, tool) } // Write the tools metadata. stor, err := filestorage.NewFileStorageWriter(metadataDir) c.Assert(err, gc.IsNil) err = tools.MergeAndWriteMetadata(stor, toolsList, false) c.Assert(err, gc.IsNil) return toolsList }
// primeAgent writes the configuration file and tools with version vers // for an agent with the given entity name. It returns the agent's // configuration and the current tools. func (s *agentSuite) primeAgent(c *gc.C, tag, password string, vers version.Binary) (agent.ConfigSetterWriter, *coretools.Tools) { stor := s.Conn.Environ.Storage() agentTools := envtesting.PrimeTools(c, stor, s.DataDir(), vers) err := envtools.MergeAndWriteMetadata(stor, coretools.List{agentTools}, envtools.DoNotWriteMirrors) c.Assert(err, gc.IsNil) tools1, err := agenttools.ChangeAgentTools(s.DataDir(), tag, vers) c.Assert(err, gc.IsNil) c.Assert(tools1, gc.DeepEquals, agentTools) stateInfo := s.StateInfo(c) apiInfo := s.APIInfo(c) conf, err := agent.NewAgentConfig( agent.AgentConfigParams{ DataDir: s.DataDir(), Tag: tag, UpgradedToVersion: vers.Number, Password: password, Nonce: state.BootstrapNonce, StateAddresses: stateInfo.Addrs, APIAddresses: apiInfo.Addrs, CACert: stateInfo.CACert, }) conf.SetPassword(password) c.Assert(conf.Write(), gc.IsNil) s.primeAPIHostPorts(c) return conf, agentTools }
// MustUploadFakeToolsVersions acts as UploadFakeToolsVersions, but panics on failure. func MustUploadFakeToolsVersions(stor storage.Storage, versions ...version.Binary) []*coretools.Tools { var agentTools coretools.List = make(coretools.List, len(versions)) for i, version := range versions { t, err := uploadFakeToolsVersion(stor, version) if err != nil { panic(err) } agentTools[i] = t } err := envtools.MergeAndWriteMetadata(stor, agentTools, envtools.DoNotWriteMirrors) if err != nil { panic(err) } return agentTools }
func (s *UpgraderSuite) TestUpgraderSetsTools(c *gc.C) { vers := version.MustParseBinary("5.4.3-precise-amd64") err := statetesting.SetAgentVersion(s.State, vers.Number) c.Assert(err, gc.IsNil) stor := s.Conn.Environ.Storage() agentTools := envtesting.PrimeTools(c, stor, s.DataDir(), vers) s.PatchValue(&version.Current, agentTools.Version) err = envtools.MergeAndWriteMetadata(stor, coretools.List{agentTools}, envtools.DoNotWriteMirrors) _, err = s.machine.AgentTools() c.Assert(err, jc.Satisfies, errors.IsNotFound) u := s.makeUpgrader() statetesting.AssertStop(c, u) s.machine.Refresh() gotTools, err := s.machine.AgentTools() c.Assert(err, gc.IsNil) envtesting.CheckTools(c, gotTools, agentTools) }
func (s *UpgraderSuite) TestChangeAgentTools(c *gc.C) { oldTools := &coretools.Tools{ Version: version.MustParseBinary("1.2.3-quantal-amd64"), } stor := s.Conn.Environ.Storage() newTools := envtesting.PrimeTools(c, stor, s.DataDir(), version.MustParseBinary("5.4.3-precise-amd64")) s.PatchValue(&version.Current, newTools.Version) err := envtools.MergeAndWriteMetadata(stor, coretools.List{newTools}, envtools.DoNotWriteMirrors) c.Assert(err, gc.IsNil) ugErr := &upgrader.UpgradeReadyError{ AgentName: "anAgent", OldTools: oldTools.Version, NewTools: newTools.Version, DataDir: s.DataDir(), } err = ugErr.ChangeAgentTools() c.Assert(err, gc.IsNil) link, err := os.Readlink(agenttools.ToolsDir(s.DataDir(), "anAgent")) c.Assert(err, gc.IsNil) c.Assert(link, gc.Equals, newTools.Version.String()) }
// UploadFakeToolsVersions puts fake tools in the supplied storage for the supplied versions. func UploadFakeToolsVersions(stor storage.Storage, versions ...version.Binary) ([]*coretools.Tools, error) { // Leave existing tools alone. existingTools := make(map[version.Binary]*coretools.Tools) existing, _ := envtools.ReadList(stor, 1, -1) for _, tools := range existing { existingTools[tools.Version] = tools } var agentTools coretools.List = make(coretools.List, len(versions)) for i, version := range versions { if tools, ok := existingTools[version]; ok { agentTools[i] = tools } else { t, err := uploadFakeToolsVersion(stor, version) if err != nil { return nil, err } agentTools[i] = t } } if err := envtools.MergeAndWriteMetadata(stor, agentTools, envtools.DoNotWriteMirrors); err != nil { return nil, err } return agentTools, nil }