Esempio n. 1
0
func (matcher *MatchYAMLMatcher) Match(actual interface{}) (success bool, err error) {
	actualString, expectedString, err := matcher.prettyPrint(actual)
	if err != nil {
		return false, err
	}

	var aval interface{}
	var eval interface{}

	// this is guarded by prettyPrint
	candiedyaml.Unmarshal([]byte(actualString), &aval)
	candiedyaml.Unmarshal([]byte(expectedString), &eval)

	return reflect.DeepEqual(aval, eval), nil
}
Esempio n. 2
0
func TestSliceOrMapYaml(t *testing.T) {
	str := `{foos: [bar=baz, far=faz]}`

	s := StructSliceorMap{}
	yaml.Unmarshal([]byte(str), &s)

	assert.Equal(t, SliceorMap{"bar": "baz", "far": "faz"}, s.Foos)

	d, err := yaml.Marshal(&s)
	assert.Nil(t, err)

	s2 := StructSliceorMap{}
	yaml.Unmarshal(d, &s2)

	assert.Equal(t, SliceorMap{"bar": "baz", "far": "faz"}, s2.Foos)
}
Esempio n. 3
0
File: disk.go Progetto: pirater/os
func readMetadata() datasource.Metadata {
	metadata := datasource.Metadata{}
	if metaDataBytes, err := ioutil.ReadFile(MetaDataFile); err == nil {
		yaml.Unmarshal(metaDataBytes, &metadata)
	}
	return metadata
}
Esempio n. 4
0
func (c Client) ResolveManifestVersions(yaml []byte) ([]byte, error) {
	m := manifest{}
	err := candiedyaml.Unmarshal(yaml, &m)
	if err != nil {
		return nil, err
	}

	for i, r := range m.Releases {
		if r.Version == "latest" {
			release, err := c.Release(r.Name)
			if err != nil {
				return nil, err
			}
			r.Version = release.Latest()
			m.Releases[i] = r
		}
	}

	for i, pool := range m.ResourcePools {
		if pool.Stemcell.Version == "latest" {
			stemcell, err := c.Stemcell(pool.Stemcell.Name)
			if err != nil {
				return nil, err
			}
			pool.Stemcell.Version = stemcell.Latest()
			m.ResourcePools[i] = pool
		}
	}

	return candiedyaml.Marshal(m)
}
Esempio n. 5
0
func (cfg *Config) Initialize(file []byte, authDisabled bool) error {
	err := candiedyaml.Unmarshal(file, &cfg)
	if err != nil {
		return err
	}

	if cfg.LogGuid == "" {
		return errors.New("No log_guid specified")
	}

	if !authDisabled && cfg.OAuth.TokenEndpoint == "" {
		return errors.New("No token endpoint specified")
	}

	if !authDisabled && cfg.OAuth.TokenEndpoint != "" && cfg.OAuth.Port == -1 {
		return errors.New("Routing API requires TLS enabled to get OAuth token")
	}

	err = cfg.process()

	if err != nil {
		return err
	}

	return nil
}
Esempio n. 6
0
func getServices(urls []string, key string) ([]string, error) {
	result := []string{}

	for _, url := range urls {
		indexUrl := fmt.Sprintf("%s/index.yml", url)
		content, err := LoadResource(indexUrl, true)
		if err != nil {
			log.Errorf("Failed to load %s: %v", indexUrl, err)
			continue
		}

		services := make(map[string][]string)
		err = yaml.Unmarshal(content, &services)
		if err != nil {
			log.Errorf("Failed to unmarshal %s: %v", indexUrl, err)
			continue
		}

		if list, ok := services[key]; ok {
			result = append(result, list...)
		}
	}

	return result, nil
}
Esempio n. 7
0
File: merge.go Progetto: pirater/os
func mergeProject(p *Project, bytes []byte) (map[string]*ServiceConfig, error) {
	configs := make(map[string]*ServiceConfig)

	datas := make(rawServiceMap)
	if err := yaml.Unmarshal(bytes, &datas); err != nil {
		return nil, err
	}

	if err := interpolate(p.context.EnvironmentLookup, &datas); err != nil {
		return nil, err
	}

	for name, data := range datas {
		data, err := parse(p.context.ConfigLookup, p.context.EnvironmentLookup, p.File, data, datas)
		if err != nil {
			logrus.Errorf("Failed to parse service %s: %v", name, err)
			return nil, err
		}

		datas[name] = data
	}

	if err := utils.Convert(datas, &configs); err != nil {
		return nil, err
	}

	adjustValues(configs)
	return configs, nil
}
Esempio n. 8
0
func TestStringorsliceYaml(t *testing.T) {
	str := `{foo: [bar, baz]}`

	s := StructStringorslice{}
	yaml.Unmarshal([]byte(str), &s)

	assert.Equal(t, Stringorslice{"bar", "baz"}, s.Foo)

	d, err := yaml.Marshal(&s)
	assert.Nil(t, err)

	s2 := StructStringorslice{}
	yaml.Unmarshal(d, &s2)

	assert.Equal(t, Stringorslice{"bar", "baz"}, s2.Foo)
}
Esempio n. 9
0
func FromYAML(yaml []byte) (Manifest, error) {
	var m Manifest
	if err := candiedyaml.Unmarshal(yaml, &m); err != nil {
		return m, err
	}
	return m, nil
}
Esempio n. 10
0
func unmarshalOrReturnString(value string) (result interface{}) {
	value = strings.Replace(value, "\n", magicString, -1)
	if err := yaml.Unmarshal([]byte(value), &result); err != nil {
		result = value
	}
	result = reverseReplacement(result)
	return
}
Esempio n. 11
0
func TestUnmarshalCommand(t *testing.T) {
	s := &StructCommand{}
	err := yaml.Unmarshal([]byte(sampleStructCommand), s)

	assert.Nil(t, err)
	assert.Equal(t, Command{"bash"}, s.Command)
	assert.Nil(t, s.Entrypoint)
	bytes, err := yaml.Marshal(s)
	assert.Nil(t, err)

	s2 := &StructCommand{}
	err = yaml.Unmarshal(bytes, s2)

	assert.Nil(t, err)
	assert.Equal(t, Command{"bash"}, s2.Command)
	assert.Nil(t, s2.Entrypoint)
}
Esempio n. 12
0
func TestUnmarshalEmptyCommand(t *testing.T) {
	s := &StructCommand{}
	err := yaml.Unmarshal([]byte(sampleEmptyCommand), s)

	assert.Nil(t, err)
	assert.Nil(t, s.Command)

	bytes, err := yaml.Marshal(s)
	assert.Nil(t, err)
	assert.Equal(t, "{}", strings.TrimSpace(string(bytes)))

	s2 := &StructCommand{}
	err = yaml.Unmarshal(bytes, s2)

	assert.Nil(t, err)
	assert.Nil(t, s2.Command)
}
Esempio n. 13
0
func TestUnmarshalSlice(t *testing.T) {
	expected := &structStringorslice{Foo: NewStringorslice("a", "b", "c")}
	b := []byte("foo: [a, b, c]\n")
	s := &structStringorslice{}
	err := yaml.Unmarshal(b, s)
	assert.Equal(t, expected, s)
	assert.Nil(t, err)
}
Esempio n. 14
0
func TestUnmarshalEmpty(t *testing.T) {
	expected := &structStringorslice{Foo: NewStringorslice()}
	b := []byte("{}\n")
	s := &structStringorslice{}
	err := yaml.Unmarshal(b, s)
	assert.Equal(t, expected, s)
	assert.Nil(t, err)
}
Esempio n. 15
0
func testInvalidInterpolatedConfig(t *testing.T, interpolatedConfig string) {
	interpolatedConfigBytes := []byte(interpolatedConfig)
	interpolatedData := make(RawServiceMap)
	yaml.Unmarshal(interpolatedConfigBytes, &interpolatedData)

	err := Interpolate(new(MockEnvironmentLookup), &interpolatedData)

	assert.NotNil(t, err)
}
Esempio n. 16
0
File: util.go Progetto: pirater/os
func Convert(from, to interface{}) error {
	bytes, err := yaml.Marshal(from)
	if err != nil {
		log.WithFields(log.Fields{"from": from, "err": err}).Warn("Error serializing to YML")
		return err
	}

	return yaml.Unmarshal(bytes, to)
}
Esempio n. 17
0
File: os.go Progetto: datawolf/os
func parseBody(body []byte) (*Images, error) {
	update := &Images{}
	err := yaml.Unmarshal(body, update)
	if err != nil {
		return nil, err
	}

	return update, nil
}
Esempio n. 18
0
func (c *Config) initConfigFromFile(path string) error {
	var e error

	b, e := ioutil.ReadFile(path)
	if e != nil {
		return e
	}

	return candiedyaml.Unmarshal(b, &c)
}
Esempio n. 19
0
func Parse(sourceName string, source []byte) (Node, error) {
	var parsed interface{}

	err := candiedyaml.Unmarshal(source, &parsed)
	if err != nil {
		return nil, err
	}

	return sanitize(sourceName, parsed)
}
Esempio n. 20
0
func TestMaporsliceYaml(t *testing.T) {
	str := `{foo: {bar: baz, far: 1}}`

	s := StructMaporslice{}
	yaml.Unmarshal([]byte(str), &s)

	assert.Equal(t, 2, len(s.Foo))
	assert.True(t, contains(s.Foo, "bar=baz"))
	assert.True(t, contains(s.Foo, "far=1"))

	d, err := yaml.Marshal(&s)
	assert.Nil(t, err)

	s2 := StructMaporslice{}
	yaml.Unmarshal(d, &s2)

	assert.Equal(t, 2, len(s2.Foo))
	assert.True(t, contains(s2.Foo, "bar=baz"))
	assert.True(t, contains(s2.Foo, "far=1"))
}
Esempio n. 21
0
func (c Client) ScanAndFix(yaml []byte) error {
	var manifest struct {
		Name string
		Jobs []struct {
			Name      string
			Instances int
		}
	}
	err := candiedyaml.Unmarshal(yaml, &manifest)
	if err != nil {
		return err
	}

	jobs := make(map[string][]int)
	for _, j := range manifest.Jobs {
		if j.Instances > 0 {
			var indices []int
			for i := 0; i < j.Instances; i++ {
				indices = append(indices, i)
			}
			jobs[j.Name] = indices
		}
	}

	requestBody, err := json.Marshal(map[string]interface{}{
		"jobs": jobs,
	})
	if err != nil {
		return err
	}

	request, err := http.NewRequest("PUT", fmt.Sprintf("%s/deployments/%s/scan_and_fix", c.config.URL, manifest.Name), bytes.NewBuffer(requestBody))
	if err != nil {
		return err
	}
	request.SetBasicAuth(c.config.Username, c.config.Password)
	request.Header.Set("Content-Type", "application/json")

	response, err := transport.RoundTrip(request)
	if err != nil {
		return err
	}

	if response.StatusCode != http.StatusFound {
		return fmt.Errorf("unexpected response %d %s", response.StatusCode, http.StatusText(response.StatusCode))
	}

	err = c.checkTask(response.Header.Get("Location"))
	if err != nil {
		return err
	}

	return nil
}
Esempio n. 22
0
func TestMarshalConfig(t *testing.T) {
	config := newTestConfig()
	bytes, err := yaml.Marshal(config)
	assert.Nil(t, err)

	config2 := TestConfig{}

	err = yaml.Unmarshal(bytes, &config2)
	assert.Nil(t, err)

	assert.Equal(t, config, config2)
}
Esempio n. 23
0
File: util.go Progetto: pirater/os
// Convert converts a struct (src) to another one (target) using yaml marshalling/unmarshalling.
// If the structure are not compatible, this will throw an error as the unmarshalling will fail.
func Convert(src, target interface{}) error {
	newBytes, err := yaml.Marshal(src)
	if err != nil {
		return err
	}

	err = yaml.Unmarshal(newBytes, target)
	if err != nil {
		logrus.Errorf("Failed to unmarshall: %v\n%s", err, string(newBytes))
	}
	return err
}
Esempio n. 24
0
func TestMarshalServiceConfig(t *testing.T) {
	configPtr := newTestConfig().SystemContainers["udev"]
	bytes, err := yaml.Marshal(configPtr)
	assert.Nil(t, err)

	configPtr2 := &ServiceConfig{}

	err = yaml.Unmarshal(bytes, configPtr2)
	assert.Nil(t, err)

	assert.Equal(t, configPtr, configPtr2)
}
Esempio n. 25
0
File: disk.go Progetto: pirater/os
func readConfig(bytes []byte, substituteMetadataVars bool, files ...string) (map[interface{}]interface{}, error) {
	// You can't just overlay yaml bytes on to maps, it won't merge, but instead
	// just override the keys and not merge the map values.
	left := make(map[interface{}]interface{})
	metadata := readMetadata()
	for _, file := range files {
		content, err := readConfigFile(file)
		if err != nil {
			return nil, err
		}
		if len(content) == 0 {
			continue
		}
		if substituteMetadataVars {
			content = substituteVars(content, metadata)
		}

		right := make(map[interface{}]interface{})
		err = yaml.Unmarshal(content, &right)
		if err != nil {
			return nil, err
		}

		left = util.MapsUnion(left, right)
	}

	if bytes != nil && len(bytes) > 0 {
		right := make(map[interface{}]interface{})
		if substituteMetadataVars {
			bytes = substituteVars(bytes, metadata)
		}
		if err := yaml.Unmarshal(bytes, &right); err != nil {
			return nil, err
		}

		left = util.MapsUnion(left, right)
	}

	return left, nil
}
Esempio n. 26
0
func composeToCloudConfig(bytes []byte) ([]byte, error) {
	compose := make(map[interface{}]interface{})
	err := yaml.Unmarshal(bytes, &compose)
	if err != nil {
		return nil, err
	}

	return yaml.Marshal(map[interface{}]interface{}{
		"rancher": map[interface{}]interface{}{
			"services": compose,
		},
	})
}
Esempio n. 27
0
func testInterpolatedConfig(t *testing.T, expectedConfig, interpolatedConfig string, envVariables map[string]string) {
	for k, v := range envVariables {
		os.Setenv(k, v)
	}

	expectedConfigBytes := []byte(expectedConfig)
	interpolatedConfigBytes := []byte(interpolatedConfig)

	expectedData := make(RawServiceMap)
	interpolatedData := make(RawServiceMap)

	yaml.Unmarshal(expectedConfigBytes, &expectedData)
	yaml.Unmarshal(interpolatedConfigBytes, &interpolatedData)

	_ = Interpolate(MockEnvironmentLookup{envVariables}, &interpolatedData)

	for k := range envVariables {
		os.Unsetenv(k)
	}

	assert.Equal(t, expectedData, interpolatedData)
}
Esempio n. 28
0
func TestStr2SliceOrMapPtrMap(t *testing.T) {
	s := map[string]*StructSliceorMap{"udav": {
		Foos: SliceorMap{"io.rancher.os.bar": "baz", "io.rancher.os.far": "true"},
		Bars: []string{},
	}}
	d, err := yaml.Marshal(&s)
	assert.Nil(t, err)

	s2 := map[string]*StructSliceorMap{}
	yaml.Unmarshal(d, &s2)

	assert.Equal(t, s, s2)
}
Esempio n. 29
0
func (matcher *MatchYAMLMatcher) prettyPrint(actual interface{}) (actualFormatted, expectedFormatted string, err error) {
	actualString, aok := toString(actual)
	expectedString, eok := toString(matcher.YAMLToMatch)

	if !(aok && eok) {
		return "", "", fmt.Errorf("MatchYAMLMatcher matcher requires a string or stringer.  Got:\n%s", format.Object(actual, 1))
	}

	var adata interface{}
	if err := candiedyaml.Unmarshal([]byte(actualString), &adata); err != nil {
		return "", "", err
	}
	abuf, _ := candiedyaml.Marshal(adata)

	var edata interface{}
	if err := candiedyaml.Unmarshal([]byte(expectedString), &edata); err != nil {
		return "", "", err
	}
	ebuf, _ := candiedyaml.Marshal(edata)

	return string(abuf), string(ebuf), nil
}
Esempio n. 30
0
// MergeServices merges a compose file into an existing set of service configs
func MergeServices(existingServices *Configs, environmentLookup EnvironmentLookup, resourceLookup ResourceLookup, file string, bytes []byte) (map[string]*ServiceConfig, error) {
	configs := make(map[string]*ServiceConfig)

	datas := make(RawServiceMap)
	if err := yaml.Unmarshal(bytes, &datas); err != nil {
		return nil, err
	}

	if err := Interpolate(environmentLookup, &datas); err != nil {
		return nil, err
	}

	if err := validate(datas); err != nil {
		return nil, err
	}

	for name, data := range datas {
		data, err := parse(resourceLookup, environmentLookup, file, data, datas)
		if err != nil {
			logrus.Errorf("Failed to parse service %s: %v", name, err)
			return nil, err
		}

		if serviceConfig, ok := existingServices.Get(name); ok {
			var rawExistingService RawService
			if err := utils.Convert(serviceConfig, &rawExistingService); err != nil {
				return nil, err
			}

			data = mergeConfig(rawExistingService, data)
		}

		datas[name] = data
	}

	for name, data := range datas {
		err := validateServiceConstraints(data, name)
		if err != nil {
			return nil, err
		}
	}

	if err := utils.Convert(datas, &configs); err != nil {
		return nil, err
	}

	adjustValues(configs)

	return configs, nil
}