diff --git a/lib/promscrape/discovery/gce/gce.go b/lib/promscrape/discovery/gce/gce.go index 91b52b263b..e849049296 100644 --- a/lib/promscrape/discovery/gce/gce.go +++ b/lib/promscrape/discovery/gce/gce.go @@ -54,6 +54,11 @@ func (z *ZoneYAML) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } +// MarshalYAML implements yaml.Marshaler +func (z ZoneYAML) MarshalYAML() (interface{}, error) { + return z.zones, nil +} + // GetLabels returns gce labels according to sdc. func (sdc *SDConfig) GetLabels(baseDir string) ([]map[string]string, error) { cfg, err := getAPIConfig(sdc) diff --git a/lib/promscrape/discovery/gce/gce_test.go b/lib/promscrape/discovery/gce/gce_test.go new file mode 100644 index 0000000000..254a5bddcd --- /dev/null +++ b/lib/promscrape/discovery/gce/gce_test.go @@ -0,0 +1,27 @@ +package gce + +import ( + "testing" + + "gopkg.in/yaml.v2" +) + +func TestMarshallingSDConfigWithZoneYAML(t *testing.T) { + sdConfig := SDConfig{ + Project: "test-project", + Zone: ZoneYAML{ + zones: []string{"zone-a", "zone-b"}, + }, + } + + data, err := yaml.Marshal(sdConfig) + if err != nil { + t.Fatalf("unexpected non-nil error") + } + + strData := string(data) + expected := "project: test-project\nzone:\n- zone-a\n- zone-b\n" + if strData != expected { + t.Fatalf("unexpected marshal:\ngot \n%vwant\n%v", strData, expected) + } +}