forked from hudl/fargo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata_test.go
86 lines (81 loc) · 2.57 KB
/
metadata_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package fargo_test
// MIT Licensed (see README.md) - Copyright (c) 2013 Hudl <@Hudl>
import (
"encoding/xml"
"github.com/hudl/fargo"
. "github.com/smartystreets/goconvey/convey"
"strconv"
"testing"
)
func TestGetInt(t *testing.T) {
Convey("Given an instance", t, func() {
instance := new(fargo.Instance)
Convey("With metadata", func() {
metadata := new(fargo.InstanceMetadata)
instance.Metadata = *metadata
Convey("That has a single integer value", func() {
key := "d"
value := 1
metadata.Raw = []byte("<" + key + ">" + strconv.Itoa(value) + "</" + key + ">")
Convey("GetInt should return that value", func() {
actualValue, err := metadata.GetInt(key)
So(err, ShouldBeNil)
So(actualValue, ShouldEqual, value)
})
})
})
})
}
func TestGetFloat(t *testing.T) {
Convey("Given an instance", t, func() {
instance := new(fargo.Instance)
Convey("With metadata", func() {
metadata := new(fargo.InstanceMetadata)
instance.Metadata = *metadata
Convey("That has a float64 value", func() {
key := "d"
value := 1.9
metadata.Raw = []byte("<" + key + ">" + strconv.FormatFloat(value, 'f', -1, 64) + "</" + key + ">")
Convey("GetFloat64 should return that value", func() {
actualValue, err := metadata.GetFloat64(key)
So(err, ShouldBeNil)
So(actualValue, ShouldEqual, value)
})
})
Convey("That has a float32 value", func() {
key := "d"
value := 1.9
metadata.Raw = []byte("<" + key + ">" + strconv.FormatFloat(value, 'f', -1, 32) + "</" + key + ">")
Convey("GetFloat32 should return that value", func() {
actualValue, err := metadata.GetFloat32(key)
So(err, ShouldBeNil)
So(actualValue, ShouldEqual, float32(1.9))
})
})
})
})
}
func TestSerializeMeta(t *testing.T) {
Convey("Given an instance", t, func() {
instance := new(fargo.Instance)
Convey("With metadata", func() {
instance.SetMetadataString("test", "value")
Convey("Serializing results in correct JSON", func() {
b, err := instance.Metadata.MarshalJSON()
So(err, ShouldBeNil)
So(string(b), ShouldEqual, "{\"test\":\"value\"}")
})
Convey("Serializing results in correct XML", func() {
b, err := xml.Marshal(instance.Metadata)
So(err, ShouldBeNil)
So(string(b), ShouldEqual, "<InstanceMetadata><test>value</test></InstanceMetadata>")
})
Convey("Blank metadata results in blank XML", func() {
metadata := new(fargo.InstanceMetadata)
b, err := xml.Marshal(metadata)
So(err, ShouldBeNil)
So(string(b), ShouldEqual, "<InstanceMetadata></InstanceMetadata>")
})
})
})
}