func (s *GetSuite) TestGetConfig(c *gc.C) { sch := s.AddTestingCharm(c, "dummy") svc := s.AddTestingService(c, "dummy-service", sch) err := svc.UpdateConfigSettings(charm.Settings{"title": "Nearly There"}) c.Assert(err, jc.ErrorIsNil) for _, t := range getTests { ctx := coretesting.Context(c) code := cmd.Main(envcmd.Wrap(&GetCommand{}), ctx, []string{t.service}) c.Check(code, gc.Equals, 0) c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "") // round trip via goyaml to avoid being sucked into a quagmire of // map[interface{}]interface{} vs map[string]interface{}. This is // also required if we add json support to this command. buf, err := goyaml.Marshal(t.expected) c.Assert(err, jc.ErrorIsNil) expected := make(map[string]interface{}) err = goyaml.Unmarshal(buf, &expected) c.Assert(err, jc.ErrorIsNil) actual := make(map[string]interface{}) err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual) c.Assert(err, jc.ErrorIsNil) c.Assert(actual, gc.DeepEquals, expected) } }
func (c *RelationSetCommand) readSettings(in io.Reader) (map[string]string, error) { data, err := ioutil.ReadAll(in) if err != nil { return nil, errors.Trace(err) } skipValidation := false // for debugging if !skipValidation { // Can this validation be done more simply or efficiently? var scalar string if err := goyaml.Unmarshal(data, &scalar); err != nil { return nil, errors.Trace(err) } if scalar != "" { return nil, errors.Errorf("expected YAML map, got %q", scalar) } var sequence []string if err := goyaml.Unmarshal(data, &sequence); err != nil { return nil, errors.Trace(err) } if len(sequence) != 0 { return nil, errors.Errorf("expected YAML map, got %#v", sequence) } } kvs := make(map[string]string) if err := goyaml.Unmarshal(data, kvs); err != nil { return nil, errors.Trace(err) } return kvs, nil }
func (cfg *conf) ParseFile(filename string) error { file, err := os.Open(filename) if err != nil { return fmt.Errorf("Failed to open file '%s': %s", filename, err.Error()) } defer file.Close() bytes, err := ioutil.ReadAll(file) if err != nil { return fmt.Errorf("Error on reading from file '%s': %s", filename, err.Error()) } err = yaml.Unmarshal(bytes, &(cfg.set)) if err != nil { return fmt.Errorf("Error on parsing configuration file '%s': %s", filename, err.Error()) } err = cfg.Validate() if err != nil { return fmt.Errorf("Failed to validate configuration: %s", err.Error()) } return nil }
func (s *storageGetSuite) TestOutputFormatKey(c *gc.C) { for i, t := range storageGetTests { c.Logf("test %d: %#v", i, t.args) hctx, _ := s.newHookContext() com, err := jujuc.NewCommand(hctx, cmdString("storage-get")) c.Assert(err, jc.ErrorIsNil) ctx := testing.Context(c) code := cmd.Main(com, ctx, t.args) c.Assert(code, gc.Equals, 0) c.Assert(bufferString(ctx.Stderr), gc.Equals, "") var out interface{} var outMap map[string]interface{} switch t.format { case formatYaml: c.Assert(goyaml.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil) out = outMap case formatJson: c.Assert(json.Unmarshal(bufferBytes(ctx.Stdout), &outMap), gc.IsNil) out = outMap default: out = string(bufferBytes(ctx.Stdout)) } c.Assert(out, gc.DeepEquals, t.out) } }
func loadConfig(agentConfig agent.Config) (*config, error) { config := &config{ storageDir: agentConfig.Value(StorageDir), storageAddr: agentConfig.Value(StorageAddr), authkey: agentConfig.Value(StorageAuthKey), } caCertPEM := agentConfig.Value(StorageCACert) if len(caCertPEM) > 0 { config.caCertPEM = caCertPEM } caKeyPEM := agentConfig.Value(StorageCAKey) if len(caKeyPEM) > 0 { config.caKeyPEM = caKeyPEM } hostnames := agentConfig.Value(StorageHostnames) if len(hostnames) > 0 { err := goyaml.Unmarshal([]byte(hostnames), &config.hostnames) if err != nil { return nil, err } } return config, nil }
func (s *migrateAgentEnvUUIDSuite) removeEnvUUIDFromAgentConfig(c *gc.C) { // Read the file in as simple map[string]interface{} and delete // the element, and write it back out again. // First step, read the file contents. filename := agent.ConfigPath(agent.DefaultDataDir, s.machine.Tag()) data, err := ioutil.ReadFile(filename) c.Assert(err, jc.ErrorIsNil) c.Logf("Data in:\n\n%s\n", data) // Parse it into the map. var content map[string]interface{} err = goyaml.Unmarshal(data, &content) c.Assert(err, jc.ErrorIsNil) // Remove the environment value, and marshal back into bytes. delete(content, "environment") data, err = goyaml.Marshal(content) c.Assert(err, jc.ErrorIsNil) // Write the yaml back out remembering to add the format prefix. data = append([]byte("# format 1.18\n"), data...) c.Logf("Data out:\n\n%s\n", data) err = ioutil.WriteFile(filename, data, 0644) c.Assert(err, jc.ErrorIsNil) // Reset test attributes. cfg, err := agent.ReadConfig(filename) c.Assert(err, jc.ErrorIsNil) s.ctx.realAgentConfig = cfg }
func ParseConfigData(data []byte) (*Config, error) { var cfg Config if err := yaml.Unmarshal([]byte(data), &cfg); err != nil { return nil, err } return &cfg, nil }
func (*cloudinitSuite) TestCloudInitConfigureBootstrapLogging(c *gc.C) { loggo.GetLogger("").SetLogLevel(loggo.INFO) instanceConfig := minimalInstanceConfig() instanceConfig.Config = minimalConfig(c) cloudcfg, err := cloudinit.New(instanceConfig.Series) c.Assert(err, jc.ErrorIsNil) udata, err := cloudconfig.NewUserdataConfig(&instanceConfig, cloudcfg) c.Assert(err, jc.ErrorIsNil) err = udata.Configure() c.Assert(err, jc.ErrorIsNil) data, err := cloudcfg.RenderYAML() c.Assert(err, jc.ErrorIsNil) configKeyValues := make(map[interface{}]interface{}) err = goyaml.Unmarshal(data, &configKeyValues) c.Assert(err, jc.ErrorIsNil) scripts := getScripts(configKeyValues) for i, script := range scripts { if strings.Contains(script, "bootstrap") { c.Logf("scripts[%d]: %q", i, script) } } expected := "jujud bootstrap-state --data-dir '.*' --env-config '.*'" + " --instance-id '.*' --constraints 'mem=2048M' --show-log" assertScriptMatch(c, scripts, expected, false) }
// ReadEnvironsBytes parses the contents of an environments.yaml file // and returns its representation. An environment with an unknown type // will only generate an error when New is called for that environment. // Attributes for environments with known types are checked. func ReadEnvironsBytes(data []byte) (*Environs, error) { var raw struct { Default string Environments map[string]map[string]interface{} } err := goyaml.Unmarshal(data, &raw) if err != nil { return nil, err } if raw.Default != "" && raw.Environments[raw.Default] == nil { return nil, fmt.Errorf("default environment %q does not exist", raw.Default) } if raw.Default == "" { // If there's a single environment, then we get the default // automatically. if len(raw.Environments) == 1 { for name := range raw.Environments { raw.Default = name break } } } for name, attrs := range raw.Environments { // store the name of the this environment in the config itself // so that providers can see it. attrs["name"] = name } return &Environs{raw.Default, raw.Environments}, nil }
func main() { flag.Parse() configs := &inception.Configs{} input, err := ioutil.ReadFile(*yml) if err != nil { log.Fatalf("Opening config failed: %v", err) } if err := yaml.Unmarshal(input, configs); err != nil { log.Fatalf("Parsing config failed: %v", err) } types, err := inception.Inception(*configs) if err != nil { log.Fatalf("Loading input failed: %v", err) } if err := os.Chdir(*out); err != nil { log.Fatalf("Changing to output directory %q failed: %v", *out, err) } if err := ioutil.WriteFile("Go2Elm.elm", []byte(Go2Elm), 0640); err != nil { log.Fatalf("Writing runtime failed: %v", err) } visitor := &Generator{} visitor.Visit(types) for pkg, buffer := range visitor.Buffers() { parts := strings.Split(pkg, ".") os.MkdirAll(filepath.Join(parts[0:len(parts)-1]...), 0750) if err := ioutil.WriteFile(filepath.Join(parts...)+".elm", buffer.Bytes(), 0640); err != nil { log.Fatalf("Writing output failed: %v", err) } } }
func (d *diskStore) readJENVFile(envName string) (*environInfo, error) { path := jenvFilename(d.dir, envName) data, err := ioutil.ReadFile(path) if err != nil { if os.IsNotExist(err) { return nil, errors.NotFoundf("environment %q", envName) } return nil, err } var info environInfo info.path = path if len(data) == 0 { return &info, nil } var values EnvironInfoData if err := goyaml.Unmarshal(data, &values); err != nil { return nil, errors.Annotatef(err, "error unmarshalling %q", path) } info.name = envName info.user = values.User info.credentials = values.Password info.environmentUUID = values.EnvironUUID info.caCert = values.CACert info.apiEndpoints = values.StateServers info.bootstrapConfig = values.Config info.initialized = true return &info, nil }
// ParseSettingsYAML returns settings derived from the supplied YAML data. The // YAML must unmarshal to a map of strings to settings data; the supplied key // must be present in the map, and must point to a map in which every value // must have, or be a string parseable to, the correct type for the associated // config option. Empty strings and nil values are both interpreted as nil. func (c *Config) ParseSettingsYAML(yamlData []byte, key string) (Settings, error) { var allSettings map[string]Settings if err := goyaml.Unmarshal(yamlData, &allSettings); err != nil { return nil, fmt.Errorf("cannot parse settings data: %v", err) } settings, ok := allSettings[key] if !ok { return nil, fmt.Errorf("no settings found for %q", key) } out := make(Settings) for name, value := range settings { option, err := c.option(name) if err != nil { return nil, err } // Accept string values for compatibility with python. if str, ok := value.(string); ok { if value, err = option.parse(name, str); err != nil { return nil, err } } else if value, err = option.validate(name, value); err != nil { return nil, err } out[name] = value } return out, nil }
// Set decodes the base64 value into yaml then expands that into a map. func (v *yamlBase64Value) Set(value string) error { decoded, err := base64.StdEncoding.DecodeString(value) if err != nil { return err } return goyaml.Unmarshal(decoded, v) }
// ReadYaml unmarshals the yaml contained in the file at path into obj. See // goyaml.Unmarshal. func ReadYaml(path string, obj interface{}) error { data, err := ioutil.ReadFile(path) if err != nil { return err } return goyaml.Unmarshal(data, obj) }
// ReadConfig reads a Config in YAML format. func ReadConfig(r io.Reader) (*Config, error) { data, err := ioutil.ReadAll(r) if err != nil { return nil, err } var config *Config if err := goyaml.Unmarshal(data, &config); err != nil { return nil, err } if config == nil { return nil, fmt.Errorf("invalid config: empty configuration") } for name, option := range config.Options { switch option.Type { case "string", "int", "float", "boolean": case "": // Missing type is valid in python. option.Type = "string" default: return nil, fmt.Errorf("invalid config: option %q has unknown type %q", name, option.Type) } def := option.Default if def == "" && option.Type == "string" { // Skip normal validation for compatibility with pyjuju. } else if option.Default, err = option.validate(name, def); err != nil { option.error(&err, name, def) return nil, fmt.Errorf("invalid config default: %v", err) } config.Options[name] = option } return config, nil }
func LoadYAML(path string, v interface{}) error { b, err := ioutil.ReadFile(path) if err != nil { return err } return yaml.Unmarshal(b, v) }
func (s assertYaml) step(c *gc.C, ctx *context) { data, err := ioutil.ReadFile(filepath.Join(ctx.path, s.path)) c.Assert(err, jc.ErrorIsNil) actual := make(map[string]interface{}) err = goyaml.Unmarshal(data, &actual) c.Assert(err, jc.ErrorIsNil) c.Assert(actual, gc.DeepEquals, s.expect) }
// TestCloudInit checks that the output from the various tests // in cloudinitTests is well formed. func (*cloudinitSuite) TestCloudInit(c *gc.C) { for i, test := range cloudinitTests { c.Logf("test %d", i) if test.setEnvConfig { test.cfg.Config = minimalConfig(c) } ci := coreCloudinit.New() udata, err := cloudinit.NewUserdataConfig(&test.cfg, ci) c.Assert(err, gc.IsNil) err = udata.Configure() c.Assert(err, gc.IsNil) c.Check(ci, gc.NotNil) // render the cloudinit config to bytes, and then // back to a map so we can introspect it without // worrying about internal details of the cloudinit // package. data, err := udata.Render() c.Assert(err, gc.IsNil) configKeyValues := make(map[interface{}]interface{}) err = goyaml.Unmarshal(data, &configKeyValues) c.Assert(err, gc.IsNil) c.Check(configKeyValues["apt_get_wrapper"], gc.DeepEquals, map[interface{}]interface{}{ "command": "eatmydata", "enabled": "auto", }) if test.cfg.EnableOSRefreshUpdate { c.Check(configKeyValues["apt_update"], gc.Equals, true) } else { c.Check(configKeyValues["apt_update"], gc.IsNil) } if test.cfg.EnableOSUpgrade { c.Check(configKeyValues["apt_upgrade"], gc.Equals, true) } else { c.Check(configKeyValues["apt_upgrade"], gc.IsNil) } scripts := getScripts(configKeyValues) assertScriptMatch(c, scripts, test.expectScripts, !test.inexactMatch) if test.cfg.Config != nil { checkEnvConfig(c, test.cfg.Config, configKeyValues, scripts) } checkPackage(c, configKeyValues, "curl", test.cfg.EnableOSRefreshUpdate) tag := names.NewMachineTag(test.cfg.MachineId).String() acfg := getAgentConfig(c, tag, scripts) c.Assert(acfg, jc.Contains, "AGENT_SERVICE_NAME: jujud-"+tag) c.Assert(acfg, jc.Contains, "upgradedToVersion: 1.2.3\n") source := "deb http://ubuntu-cloud.archive.canonical.com/ubuntu precise-updates/cloud-tools main" needCloudArchive := test.cfg.Series == "precise" checkAptSource(c, configKeyValues, source, cloudinit.CanonicalCloudArchiveSigningKey, needCloudArchive) } }
// Example_acctContentTypes verifies that the Accept header can be used // to control the format of the response and the Content-Type header // can be used to specify the format of the request. func Example_acctContentTypes() { _, stopper := startAdminServer() defer stopper.Stop() config := &proto.AcctConfig{} err := yaml.Unmarshal([]byte(testAcctConfig), config) if err != nil { fmt.Println(err) } testCases := []struct { contentType, accept string }{ {util.JSONContentType, util.JSONContentType}, {util.YAMLContentType, util.JSONContentType}, {util.JSONContentType, util.YAMLContentType}, {util.YAMLContentType, util.YAMLContentType}, } for i, test := range testCases { key := fmt.Sprintf("/test%d", i) var body []byte if test.contentType == util.JSONContentType { if body, err = json.MarshalIndent(config, "", " "); err != nil { fmt.Println(err) } } else { if body, err = yaml.Marshal(config); err != nil { fmt.Println(err) } } req, err := http.NewRequest("POST", fmt.Sprintf("%s://%s%s%s", testContext.RequestScheme(), testContext.Addr, acctPathPrefix, key), bytes.NewReader(body)) req.Header.Add(util.ContentTypeHeader, test.contentType) if _, err = sendAdminRequest(testContext, req); err != nil { fmt.Println(err) } req, err = http.NewRequest("GET", fmt.Sprintf("%s://%s%s%s", testContext.RequestScheme(), testContext.Addr, acctPathPrefix, key), nil) req.Header.Add(util.AcceptHeader, test.accept) if body, err = sendAdminRequest(testContext, req); err != nil { fmt.Println(err) } fmt.Println(string(body)) } // Output: // { // "cluster_id": "test" // } // { // "cluster_id": "test" // } // cluster_id: test // // cluster_id: test }
// NewYAMLSchema returns a schema based on the YAML input string. func NewYAMLSchema(in []byte) (*Schema, error) { s := &Schema{} if err := yaml.Unmarshal(in, s); err != nil { return nil, err } if err := s.Validate(); err != nil { return nil, err } return s, nil }
// ExampleAcctContentTypes verifies that the Accept header can be used // to control the format of the response and the Content-Type header // can be used to specify the format of the request. func ExampleAcctContentTypes() { httpServer := startAdminServer() defer httpServer.Close() config := &proto.AcctConfig{} err := yaml.Unmarshal([]byte(testAcctConfig), config) if err != nil { fmt.Println(err) } testCases := []struct { contentType, accept string }{ {"application/json", "application/json"}, {"text/yaml", "application/json"}, {"application/json", "text/yaml"}, {"text/yaml", "text/yaml"}, } for i, test := range testCases { key := fmt.Sprintf("/test%d", i) var body []byte if test.contentType == "application/json" { if body, err = json.MarshalIndent(config, "", " "); err != nil { fmt.Println(err) } } else { if body, err = yaml.Marshal(config); err != nil { fmt.Println(err) } } req, err := http.NewRequest("POST", fmt.Sprintf("%s://%s%s%s", adminScheme, testContext.Addr, acctPathPrefix, key), bytes.NewReader(body)) req.Header.Add("Content-Type", test.contentType) if _, err = sendAdminRequest(req); err != nil { fmt.Println(err) } req, err = http.NewRequest("GET", fmt.Sprintf("%s://%s%s%s", adminScheme, testContext.Addr, acctPathPrefix, key), nil) req.Header.Add("Accept", test.accept) if body, err = sendAdminRequest(req); err != nil { fmt.Println(err) } fmt.Println(string(body)) } // Output: // { // "cluster_id": "test" // } // { // "cluster_id": "test" // } // cluster_id: test // // cluster_id: test }
func (s *ConstraintsSuite) TestRoundtripYaml(c *gc.C) { for _, t := range constraintsRoundtripTests { c.Logf("test %s", t.Name) data, err := goyaml.Marshal(t.Value) c.Assert(err, jc.ErrorIsNil) var cons constraints.Value err = goyaml.Unmarshal(data, &cons) c.Check(err, jc.ErrorIsNil) c.Check(cons, jc.DeepEquals, t.Value) } }
func ReadYaml(r io.Reader) YamlHacker { data, err := ioutil.ReadAll(r) if err != nil { panic(err) } m := make(map[interface{}]interface{}) err = goyaml.Unmarshal(data, m) if err != nil { panic(err) } return YamlHacker(m) }
func (s *GetSuite) TestGetConfig(c *gc.C) { for _, t := range getTests { ctx := coretesting.Context(c) code := cmd.Main(envcmd.Wrap(service.NewGetCommand(s.fake)), ctx, []string{t.service}) c.Check(code, gc.Equals, 0) c.Assert(ctx.Stderr.(*bytes.Buffer).String(), gc.Equals, "") // round trip via goyaml to avoid being sucked into a quagmire of // map[interface{}]interface{} vs map[string]interface{}. This is // also required if we add json support to this command. buf, err := goyaml.Marshal(t.expected) c.Assert(err, jc.ErrorIsNil) expected := make(map[string]interface{}) err = goyaml.Unmarshal(buf, &expected) c.Assert(err, jc.ErrorIsNil) actual := make(map[string]interface{}) err = goyaml.Unmarshal(ctx.Stdout.(*bytes.Buffer).Bytes(), &actual) c.Assert(err, jc.ErrorIsNil) c.Assert(actual, gc.DeepEquals, expected) } }
func (s *BaseSuite) assertServerFileMatches(c *gc.C, serverfile, username, password string) { yaml, err := ioutil.ReadFile(serverfile) c.Assert(err, jc.ErrorIsNil) var content envcmd.ServerFile err = goyaml.Unmarshal(yaml, &content) c.Assert(err, jc.ErrorIsNil) c.Assert(content.Username, gc.Equals, username) c.Assert(content.Password, gc.Equals, password) c.Assert(content.CACert, gc.Equals, testing.CACert) c.Assert(content.Addresses, jc.DeepEquals, []string{"127.0.0.1:12345"}) }
func loadSponsors(filename string) error { cnt, err := ioutil.ReadFile(filename) if err != nil { return err } err = yaml.Unmarshal(cnt, &sponsors) if err != nil { return err } return nil }
// NewYAMLSchema returns a schema based on the YAML input string. func NewYAMLSchema(in []byte) (*Schema, error) { s := &Schema{} if err := yaml.Unmarshal(in, s); err != nil { return nil, err } // Sort tables. sort.Sort(s.Tables) if err := s.Validate(); err != nil { return nil, err } return s, nil }
func LoadRuleFromFile(filePath string) (*Rule, error) { data, err := ioutil.ReadFile(filePath) if err != nil { return nil, fmt.Errorf("Failed to read form-rule %s: %s", filePath, err.Error()) } r := &Rule{} err = yaml.Unmarshal(data, &r) if err != nil { return nil, fmt.Errorf("Failed to parse form-rule %s: %s", filePath, err.Error()) } return r, nil }
func loadState(r io.ReadCloser) (*BootstrapState, error) { defer r.Close() data, err := ioutil.ReadAll(r) if err != nil { return nil, fmt.Errorf("error reading %q: %v", StateFile, err) } var state BootstrapState err = goyaml.Unmarshal(data, &state) if err != nil { return nil, fmt.Errorf("error unmarshalling %q: %v", StateFile, err) } return &state, nil }
func loadEvent(filename string) error { cnt, err := ioutil.ReadFile(filename) if err != nil { return err } err = yaml.Unmarshal(cnt, &event) if err != nil { return err } return nil }