Exemple #1
0
func TestIntOrStringMarshalYAML(t *testing.T) {
	{
		input := IntOrStringHolder{
			IOrS: IntOrString{
				Kind:   IntstrInt,
				IntVal: 123,
			},
		}
		result, err := yaml.Marshal(&input)
		if err != nil {
			t.Errorf("Failed to marshal: %v", err)
		}
		if string(result) != "val: 123\n" {
			t.Errorf("Failed to marshal int-typed IntOrString: %q", string(result))
		}
	}

	{
		input := IntOrStringHolder{
			IOrS: IntOrString{
				Kind:   IntstrString,
				StrVal: "123",
			},
		}
		result, err := yaml.Marshal(&input)
		if err != nil {
			t.Errorf("Failed to marshal: %v", err)
		}
		if string(result) != "val: \"123\"\n" {
			t.Errorf("Failed to marshal string-typed IntOrString: %q", string(result))
		}
	}
}
Exemple #2
0
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'.
func RunController(ctx api.Context, image, name string, replicas int, client client.Interface, portSpec string, servicePort int) error {
	// TODO replace ctx with a namespace string
	if servicePort > 0 && !util.IsDNSLabel(name) {
		return fmt.Errorf("service creation requested, but an invalid name for a service was provided (%s). Service names must be valid DNS labels.", name)
	}
	ports, err := portsFromString(portSpec)
	if err != nil {
		return err
	}
	controller := &api.ReplicationController{
		ObjectMeta: api.ObjectMeta{
			Name: name,
		},
		Spec: api.ReplicationControllerSpec{
			Replicas: replicas,
			Selector: map[string]string{
				"name": name,
			},
			Template: &api.PodTemplateSpec{
				ObjectMeta: api.ObjectMeta{
					Labels: map[string]string{
						"name": name,
					},
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name:  strings.ToLower(name),
							Image: image,
							Ports: ports,
						},
					},
				},
			},
		},
	}

	controllerOut, err := client.ReplicationControllers(api.Namespace(ctx)).Create(controller)
	if err != nil {
		return err
	}
	data, err := yaml.Marshal(controllerOut)
	if err != nil {
		return err
	}
	fmt.Print(string(data))

	if servicePort > 0 {
		svc, err := createService(ctx, name, servicePort, client)
		if err != nil {
			return err
		}
		data, err = yaml.Marshal(svc)
		if err != nil {
			return err
		}
		fmt.Printf(string(data))
	}
	return nil
}
Exemple #3
0
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'
func RunController(image, name string, replicas int, client client.Interface, portSpec string, servicePort int) error {
	controller := api.ReplicationController{
		JSONBase: api.JSONBase{
			ID: name,
		},
		DesiredState: api.ReplicationControllerState{
			Replicas: replicas,
			ReplicaSelector: map[string]string{
				"name": name,
			},
			PodTemplate: api.PodTemplate{
				DesiredState: api.PodState{
					Manifest: api.ContainerManifest{
						Version: "v1beta2",
						Containers: []api.Container{
							{
								Name:  strings.ToLower(name),
								Image: image,
								Ports: makePorts(portSpec),
							},
						},
					},
				},
				Labels: map[string]string{
					"name": name,
				},
			},
		},
		Labels: map[string]string{
			"name": name,
		},
	}

	controllerOut, err := client.CreateReplicationController(controller)
	if err != nil {
		return err
	}
	data, err := yaml.Marshal(controllerOut)
	if err != nil {
		return err
	}
	fmt.Print(string(data))

	if servicePort > 0 {
		svc, err := createService(name, servicePort, client)
		if err != nil {
			return err
		}
		data, err = yaml.Marshal(svc)
		if err != nil {
			return err
		}
		fmt.Printf(string(data))
	}
	return nil
}
Exemple #4
0
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'.
func RunController(image, name string, replicas int, client client.Interface, portSpec string, servicePort int) error {
	if servicePort > 0 && !util.IsDNSLabel(name) {
		return fmt.Errorf("Service creation requested, but an invalid name for a service was provided (%s). Service names must be valid DNS labels.", name)
	}
	controller := &api.ReplicationController{
		JSONBase: api.JSONBase{
			ID: name,
		},
		DesiredState: api.ReplicationControllerState{
			Replicas: replicas,
			ReplicaSelector: map[string]string{
				"replicationController": name,
			},
			PodTemplate: api.PodTemplate{
				DesiredState: api.PodState{
					Manifest: api.ContainerManifest{
						Version: "v1beta2",
						Containers: []api.Container{
							{
								Name:  strings.ToLower(name),
								Image: image,
								Ports: portsFromString(portSpec),
							},
						},
					},
				},
				Labels: map[string]string{
					"replicationController": name,
				},
			},
		},
	}

	controllerOut, err := client.CreateReplicationController(controller)
	if err != nil {
		return err
	}
	data, err := yaml.Marshal(controllerOut)
	if err != nil {
		return err
	}
	fmt.Print(string(data))

	if servicePort > 0 {
		svc, err := createService(name, servicePort, client)
		if err != nil {
			return err
		}
		data, err = yaml.Marshal(svc)
		if err != nil {
			return err
		}
		fmt.Printf(string(data))
	}
	return nil
}
Exemple #5
0
func (s *S) TestMarshalTypeCache(c *C) {
	var data []byte
	var err error
	func() {
		type T struct{ A int }
		data, err = yaml.Marshal(&T{})
		c.Assert(err, IsNil)
	}()
	func() {
		type T struct{ B int }
		data, err = yaml.Marshal(&T{})
		c.Assert(err, IsNil)
	}()
	c.Assert(string(data), Equals, "b: 0\n")
}
Exemple #6
0
func (s *S) TestMarshal(c *C) {
	for _, item := range marshalTests {
		data, err := yaml.Marshal(item.value)
		c.Assert(err, IsNil)
		c.Assert(string(data), Equals, item.data)
	}
}
func DoParseTest(t *testing.T, storage string, obj runtime.Object, codec runtime.Codec, p *Parser) {
	jsonData, _ := codec.Encode(obj)
	var tmp map[string]interface{}
	json.Unmarshal(jsonData, &tmp)
	yamlData, _ := yaml.Marshal(tmp)
	t.Logf("Intermediate yaml:\n%v\n", string(yamlData))
	t.Logf("Intermediate json:\n%v\n", string(jsonData))
	jsonGot, jsonErr := p.ToWireFormat(jsonData, storage, latest.Codec, codec)
	yamlGot, yamlErr := p.ToWireFormat(yamlData, storage, latest.Codec, codec)

	if jsonErr != nil {
		t.Errorf("json err: %#v", jsonErr)
	}
	if yamlErr != nil {
		t.Errorf("yaml err: %#v", yamlErr)
	}
	if string(jsonGot) != string(jsonData) {
		t.Errorf("json output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
			string(jsonGot), string(jsonData))
	}
	if string(yamlGot) != string(jsonData) {
		t.Errorf("yaml parsed output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
			string(yamlGot), string(jsonData))
	}
}
Exemple #8
0
func main() {
	if len(os.Args) != 3 {
		checkErr(fmt.Errorf(usage))
	}
	specFilename := os.Args[1]
	configFilename := os.Args[2]

	specData, err := ReadConfigData(specFilename)
	checkErr(err)

	spec := EnscopeSpec{}
	err = yaml.Unmarshal(specData, &spec)
	checkErr(err)

	configData, err := ReadConfigData(configFilename)
	checkErr(err)

	var data interface{}

	err = yaml.Unmarshal([]byte(configData), &data)
	checkErr(err)

	xData, err := enscope("", spec, data)
	checkErr(err)

	out, err := yaml.Marshal(xData)
	checkErr(err)

	fmt.Print(string(out))
}
func generateReplicationController(sname string, cname string, replicas int) {
	if replicas < 1 {
		replicas = 1
	}

	name := fmt.Sprintf("%s-%s", sname, cname)
	rc := []v1beta3.ReplicationController{{
		TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "ReplicationController"},
		ObjectMeta: v1beta3.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"service": sname,
				"track":   cname,
			},
		},
		Spec: v1beta3.ReplicationControllerSpec{
			Replicas: replicas,
			Selector: map[string]string{
				"service": sname,
				"track":   cname,
			},
			Template: v1beta3.ObjectReference{
				Kind:       "PodTemplate",
				Name:       name,
				APIVersion: "v1beta3",
			},
		},
	}}

	rcOutData, err := yaml.Marshal(rc)
	checkErr(err)

	fmt.Print(string(rcOutData))
}
Exemple #10
0
func generatePodTemplate(sname string, cname string, podSpec v1beta3.PodSpec) {
	name := fmt.Sprintf("%s-%s", sname, cname)
	pt := []v1beta3.PodTemplate{{
		TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "PodTemplate"},
		ObjectMeta: v1beta3.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"service": sname,
				"track":   cname,
			},
		},
		Spec: v1beta3.PodTemplateSpec{
			ObjectMeta: v1beta3.ObjectMeta{
				Labels: map[string]string{
					"service": sname,
					"track":   cname,
				},
			},
			Spec: podSpec,
		},
	}}

	ptOutData, err := yaml.Marshal(pt)
	checkErr(err)

	fmt.Print(string(ptOutData))
}
Exemple #11
0
func generateService(name string, portSpec string) {
	if portSpec == "" {
		return
	}

	servicePort, containerPort, err := portsFromString(portSpec)
	checkErr(err)

	svc := []v1beta3.Service{{
		TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "Service"},
		ObjectMeta: v1beta3.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"service": name,
			},
		},
		Spec: v1beta3.ServiceSpec{
			Port:          servicePort,
			ContainerPort: util.NewIntOrStringFromInt(containerPort),
			Selector: map[string]string{
				"service": name,
			},
		},
	}}

	svcOutData, err := yaml.Marshal(svc)
	checkErr(err)

	fmt.Print(string(svcOutData))
}
Exemple #12
0
func generateReplicationController(name string, image string, replicas int, ports []v1beta1.Port) {
	controller := []v1beta1.ReplicationController{{
		TypeMeta: v1beta1.TypeMeta{APIVersion: "v1beta1", Kind: "ReplicationController", ID: name},
		DesiredState: v1beta1.ReplicationControllerState{
			Replicas: replicas,
			ReplicaSelector: map[string]string{
				"simpleservice": name,
			},
			PodTemplate: v1beta1.PodTemplate{
				DesiredState: v1beta1.PodState{
					Manifest: v1beta1.ContainerManifest{
						Version: "v1beta2",
						Containers: []v1beta1.Container{
							{
								Name:  name,
								Image: image,
								Ports: ports,
							},
						},
					},
				},
				Labels: map[string]string{
					"simpleservice": name,
				},
			},
		},
		Labels: map[string]string{
			"simpleservice": name,
		},
	}}
	controllerOutData, err := yaml.Marshal(controller)
	checkErr(err)

	fmt.Print(string(controllerOutData))
}
Exemple #13
0
// ReMarshal parses interface{} into concrete types
func ReMarshal(config interface{}, target interface{}) error {
	data, err := yaml.Marshal(config)
	if err != nil {
		return err
	}
	return yaml.Unmarshal(data, target)
}
Exemple #14
0
func TestStatus(t *testing.T) {
	mc, mcli, f := fixture()
	mc.Err, mc.P = make([]error, 3), []string{"LM-X"}
	f.Build = 1
	out, err := mcli.Status()
	mc.Check(t)

	if out == nil || len(out) != 1 {
		t.Error("expected out to not be empty")
	}
	if err != nil && len(err) != 0 {
		t.Error("expected err to be empty")
	}
	y, e := yaml.Marshal(map[string][]pulse.BuildResult{fmt.Sprintf("%s (build 1)", mc.P[0]): {}})
	if e != nil {
		t.Error("expected err to be nil")
	}
	s, ok := out[0].(string)
	if !ok {
		t.Fatalf("expected out[0] to be of string type, was %T instead", out[0])
	}
	if string(y) != s {
		t.Errorf("expected %s, got %s", y, out[0])
	}
}
// PrintObj prints the data as YAML.
func (y *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
	output, err := yaml.Marshal(obj)
	if err != nil {
		return err
	}
	_, err = fmt.Fprint(w, string(output))
	return err
}
Exemple #16
0
func (s *S) TestUnmarshalWholeDocumentWithGetter(c *C) {
	obj := &typeWithGetter{}
	obj.tag = ""
	obj.value = map[string]string{"hello": "world!"}
	data, err := yaml.Marshal(obj)
	c.Assert(err, IsNil)
	c.Assert(string(data), Equals, "hello: world!\n")
}
Exemple #17
0
func buildfileForConfig(config map[string]interface{}) (string, error) {
	yml, err := yaml.Marshal(map[string]interface{}{
		"publish": config,
	})
	if err != nil {
		return "", err
	}
	return setUpWithDrone(string(yml))
}
Exemple #18
0
func (s *S) TestMashalWithGetter(c *C) {
	for _, item := range getterTests {
		obj := &typeWithGetterField{}
		obj.Field.tag = item.tag
		obj.Field.value = item.value
		data, err := yaml.Marshal(obj)
		c.Assert(err, IsNil)
		c.Assert(string(data), Equals, string(item.data))
	}
}
func (y *YAMLPrinter) Print(data string, w io.Writer) error {
	var obj interface{}
	if err := json.Unmarshal([]byte(data), &obj); err != nil {
		return err
	}
	output, err := yaml.Marshal(obj)
	if err != nil {
		return err
	}
	_, err = fmt.Fprint(w, string(output))
	return err
}
// PrintObj prints the data as YAML.
func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
	outObj, err := p.convertor.ConvertToVersion(obj, p.version)
	if err != nil {
		return err
	}
	output, err := yaml.Marshal(outObj)
	if err != nil {
		return err
	}
	_, err = fmt.Fprint(w, string(output))
	return err
}
Exemple #21
0
func (fileStore) Save(c *Creds) error {
	f, err := config(os.O_TRUNC | os.O_CREATE | os.O_WRONLY)
	if err != nil {
		return err
	}
	defer f.Close()
	b, err := yaml.Marshal(c)
	if err != nil {
		return err
	}
	_, err = io.Copy(f, bytes.NewBuffer(b))
	return err
}
Exemple #22
0
func (s Suite) ToTapY() ([]byte, error) {
	var ret []byte
	for _, doc := range s.Docs() {
		if b, err := yaml.Marshal(doc); err != nil {
			return nil, err
		} else {
			ret = append(ret, []byte("---\n")...)
			ret = append(ret, b...)
			ret = append(ret, '\n')
		}
	}
	return append(ret, []byte("...")...), nil
}
Exemple #23
0
func (renderer *YamlRenderer) Render(env Env) {
	log.Printf("[YAML RENDERER] Rendering to %s", *renderer.YamlFile)

	out, err := yaml.Marshal(env.Data)
	if err != nil {
		panic(err)
	}

	err = ioutil.WriteFile(*renderer.YamlFile, out, 0644)
	if err != nil {
		panic(err)
	}
}
Exemple #24
0
// StopController stops a controller named 'name' by setting replicas to zero
func StopController(name string, client client.ClientInterface) error {
	controller, err := client.GetReplicationController(name)
	if err != nil {
		return err
	}
	controller.DesiredState.Replicas = 0
	controllerOut, err := client.UpdateReplicationController(controller)
	if err != nil {
		return err
	}
	data, err := yaml.Marshal(controllerOut)
	if err != nil {
		return err
	}
	fmt.Print(string(data))
	return nil
}
Exemple #25
0
func generateService(name string, servicePort int, containerPort int) {
	svc := []v1beta1.Service{{
		TypeMeta:      v1beta1.TypeMeta{APIVersion: "v1beta1", Kind: "Service", ID: name},
		Port:          servicePort,
		ContainerPort: util.NewIntOrStringFromInt(containerPort),
		Labels: map[string]string{
			"simpleservice": name,
		},
		Selector: map[string]string{
			"simpleservice": name,
		},
	}}

	svcOutData, err := yaml.Marshal(svc)
	checkErr(err)

	fmt.Print(string(svcOutData))
}
Exemple #26
0
// ResizeController resizes a controller named 'name' by setting replicas to 'replicas'.
func ResizeController(ctx api.Context, name string, replicas int, client client.Interface) error {
	// TODO ctx is not needed, and should just be a namespace
	controller, err := client.ReplicationControllers(api.Namespace(ctx)).Get(name)
	if err != nil {
		return err
	}
	controller.Spec.Replicas = replicas
	controllerOut, err := client.ReplicationControllers(api.Namespace(ctx)).Update(controller)
	if err != nil {
		return err
	}
	data, err := yaml.Marshal(controllerOut)
	if err != nil {
		return err
	}
	fmt.Print(string(data))
	return nil
}
Exemple #27
0
// SetYAML implements the yaml.Setter interface.
func (re *RawExtension) SetYAML(tag string, value interface{}) bool {
	if value == nil {
		re.RawJSON = []byte("null")
		return true
	}
	// Why does the yaml package send value as a map[interface{}]interface{}?
	// It's especially frustrating because encoding/json does the right thing
	// by giving a []byte. So here we do the embarrasing thing of re-encode and
	// de-encode the right way.
	// TODO: Write a version of Decode that uses reflect to turn this value
	// into an API object.
	b, err := yaml.Marshal(value)
	if err != nil {
		panic("yaml can't reverse its own object")
	}
	re.RawJSON = b
	return true
}
Exemple #28
0
// SetYAML implements the yaml.Setter interface.
func CodecSetYAML(codec Codec, tag string, value interface{}) (Object, bool) {
	if value == nil {
		return nil, true
	}
	// Why does the yaml package send value as a map[interface{}]interface{}?
	// It's especially frustrating because encoding/json does the right thing
	// by giving a []byte. So here we do the embarrasing thing of re-encode and
	// de-encode the right way.
	// TODO: Write a version of Decode that uses reflect to turn this value
	// into an API object.
	b, err := yaml.Marshal(value)
	if err != nil {
		panic("yaml can't reverse its own object")
	}
	obj, err := codec.Decode(b)
	if err != nil {
		return nil, false
	}
	return obj, true
}
Exemple #29
0
// Fill the TaskInfo in the PodTask, should be called in PodScheduleFunc.
func (t *PodTask) FillTaskInfo(slaveId string, offer *mesos.Offer) {
	t.OfferIds = append(t.OfferIds, offer.GetId().GetValue())

	var err error

	// TODO(nnielsen): We only launch one pod per offer. We should be able to launch multiple.

	// TODO(nnielsen): Assign/aquire pod port.
	t.TaskInfo.TaskId = &mesos.TaskID{Value: proto.String(t.ID)}
	t.TaskInfo.SlaveId = &mesos.SlaveID{Value: proto.String(slaveId)}
	t.TaskInfo.Resources = []*mesos.Resource{
		mesos.ScalarResource("cpus", containerCpus),
		mesos.ScalarResource("mem", containerMem),
		rangeResource("ports", t.Ports()),
	}
	t.TaskInfo.Data, err = yaml.Marshal(&t.Pod.DesiredState.Manifest)
	if err != nil {
		log.Warningf("Failed to marshal the manifest")
	}
}
func TestIntOrStringMarshalYAML(t *testing.T) {
	cases := []struct {
		input  IntOrString
		result string
	}{
		{IntOrString{Kind: IntstrInt, IntVal: 123}, "val: 123\n"},
		{IntOrString{Kind: IntstrString, StrVal: "123"}, "val: \"123\"\n"},
	}

	for _, c := range cases {
		input := IntOrStringHolder{c.input}
		result, err := yaml.Marshal(&input)
		if err != nil {
			t.Errorf("Failed to marshal: %v", err)
		}
		if string(result) != c.result {
			t.Errorf("Failed to marshal IntOrString: got %q", string(result))
		}
	}
}