2014-10-18 02:36:22 +04:00
|
|
|
/*
|
|
|
|
Copyright 2014 CoreOS, Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-07-31 01:23:10 +04:00
|
|
|
package ec2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2014-08-16 05:13:37 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata/test"
|
2014-07-31 01:23:10 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/pkg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestType(t *testing.T) {
|
|
|
|
want := "ec2-metadata-service"
|
|
|
|
if kind := (metadataService{}).Type(); kind != want {
|
|
|
|
t.Fatalf("bad type: want %q, got %q", want, kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchAttributes(t *testing.T) {
|
|
|
|
for _, s := range []struct {
|
|
|
|
resources map[string]string
|
|
|
|
err error
|
|
|
|
tests []struct {
|
|
|
|
path string
|
|
|
|
val []string
|
|
|
|
}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
resources: map[string]string{
|
|
|
|
"/": "a\nb\nc/",
|
|
|
|
"/c/": "d\ne/",
|
|
|
|
"/c/e/": "f",
|
|
|
|
"/a": "1",
|
|
|
|
"/b": "2",
|
|
|
|
"/c/d": "3",
|
|
|
|
"/c/e/f": "4",
|
|
|
|
},
|
|
|
|
tests: []struct {
|
|
|
|
path string
|
|
|
|
val []string
|
|
|
|
}{
|
|
|
|
{"/", []string{"a", "b", "c/"}},
|
|
|
|
{"/b", []string{"2"}},
|
|
|
|
{"/c/d", []string{"3"}},
|
|
|
|
{"/c/e/", []string{"f"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-08-16 05:13:37 +04:00
|
|
|
err: fmt.Errorf("test error"),
|
2014-07-31 01:23:10 +04:00
|
|
|
tests: []struct {
|
|
|
|
path string
|
|
|
|
val []string
|
|
|
|
}{
|
|
|
|
{"", nil},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
2014-08-16 05:13:37 +04:00
|
|
|
service := metadataService{metadata.MetadataService{
|
2014-10-23 22:46:08 +04:00
|
|
|
Client: &test.HttpClient{Resources: s.resources, Err: s.err},
|
2014-08-16 05:13:37 +04:00
|
|
|
}}
|
2014-07-31 01:23:10 +04:00
|
|
|
for _, tt := range s.tests {
|
2014-08-16 05:13:37 +04:00
|
|
|
attrs, err := service.fetchAttributes(tt.path)
|
2014-07-31 01:23:10 +04:00
|
|
|
if err != s.err {
|
|
|
|
t.Fatalf("bad error for %q (%q): want %q, got %q", tt.path, s.resources, s.err, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(attrs, tt.val) {
|
|
|
|
t.Fatalf("bad fetch for %q (%q): want %q, got %q", tt.path, s.resources, tt.val, attrs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchAttribute(t *testing.T) {
|
|
|
|
for _, s := range []struct {
|
|
|
|
resources map[string]string
|
|
|
|
err error
|
|
|
|
tests []struct {
|
|
|
|
path string
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
resources: map[string]string{
|
|
|
|
"/": "a\nb\nc/",
|
|
|
|
"/c/": "d\ne/",
|
|
|
|
"/c/e/": "f",
|
|
|
|
"/a": "1",
|
|
|
|
"/b": "2",
|
|
|
|
"/c/d": "3",
|
|
|
|
"/c/e/f": "4",
|
|
|
|
},
|
|
|
|
tests: []struct {
|
|
|
|
path string
|
|
|
|
val string
|
|
|
|
}{
|
|
|
|
{"/a", "1"},
|
|
|
|
{"/b", "2"},
|
|
|
|
{"/c/d", "3"},
|
|
|
|
{"/c/e/f", "4"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2014-08-16 05:13:37 +04:00
|
|
|
err: fmt.Errorf("test error"),
|
2014-07-31 01:23:10 +04:00
|
|
|
tests: []struct {
|
|
|
|
path string
|
|
|
|
val string
|
|
|
|
}{
|
|
|
|
{"", ""},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
2014-08-16 05:13:37 +04:00
|
|
|
service := metadataService{metadata.MetadataService{
|
2014-10-23 22:46:08 +04:00
|
|
|
Client: &test.HttpClient{Resources: s.resources, Err: s.err},
|
2014-08-16 05:13:37 +04:00
|
|
|
}}
|
2014-07-31 01:23:10 +04:00
|
|
|
for _, tt := range s.tests {
|
2014-08-16 05:13:37 +04:00
|
|
|
attr, err := service.fetchAttribute(tt.path)
|
2014-07-31 01:23:10 +04:00
|
|
|
if err != s.err {
|
|
|
|
t.Fatalf("bad error for %q (%q): want %q, got %q", tt.path, s.resources, s.err, err)
|
|
|
|
}
|
|
|
|
if attr != tt.val {
|
|
|
|
t.Fatalf("bad fetch for %q (%q): want %q, got %q", tt.path, s.resources, tt.val, attr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchMetadata(t *testing.T) {
|
|
|
|
for _, tt := range []struct {
|
2014-08-16 05:13:37 +04:00
|
|
|
root string
|
|
|
|
metadataPath string
|
|
|
|
resources map[string]string
|
|
|
|
expect []byte
|
|
|
|
clientErr error
|
|
|
|
expectErr error
|
2014-07-31 01:23:10 +04:00
|
|
|
}{
|
|
|
|
{
|
2014-08-16 05:13:37 +04:00
|
|
|
root: "/",
|
|
|
|
metadataPath: "2009-04-04/meta-data",
|
2014-07-31 01:23:10 +04:00
|
|
|
resources: map[string]string{
|
|
|
|
"/2009-04-04/meta-data/public-keys": "bad\n",
|
|
|
|
},
|
|
|
|
expectErr: fmt.Errorf("malformed public key: \"bad\""),
|
|
|
|
},
|
|
|
|
{
|
2014-08-16 05:13:37 +04:00
|
|
|
root: "/",
|
|
|
|
metadataPath: "2009-04-04/meta-data",
|
2014-07-31 01:23:10 +04:00
|
|
|
resources: map[string]string{
|
|
|
|
"/2009-04-04/meta-data/hostname": "host",
|
|
|
|
"/2009-04-04/meta-data/local-ipv4": "1.2.3.4",
|
|
|
|
"/2009-04-04/meta-data/public-ipv4": "5.6.7.8",
|
|
|
|
"/2009-04-04/meta-data/public-keys": "0=test1\n",
|
|
|
|
"/2009-04-04/meta-data/public-keys/0": "openssh-key",
|
|
|
|
"/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
|
|
|
|
"/2009-04-04/meta-data/network_config/content_path": "path",
|
|
|
|
},
|
|
|
|
expect: []byte(`{"hostname":"host","local-ipv4":"1.2.3.4","network_config":{"content_path":"path"},"public-ipv4":"5.6.7.8","public_keys":{"test1":"key"}}`),
|
|
|
|
},
|
|
|
|
{
|
2014-10-23 22:46:08 +04:00
|
|
|
clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
|
|
|
|
expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
|
2014-07-31 01:23:10 +04:00
|
|
|
},
|
|
|
|
} {
|
2014-08-16 05:13:37 +04:00
|
|
|
service := &metadataService{metadata.MetadataService{
|
|
|
|
Root: tt.root,
|
2014-10-23 22:46:08 +04:00
|
|
|
Client: &test.HttpClient{Resources: tt.resources, Err: tt.clientErr},
|
2014-08-16 05:13:37 +04:00
|
|
|
MetadataPath: tt.metadataPath,
|
|
|
|
}}
|
2014-07-31 01:23:10 +04:00
|
|
|
metadata, err := service.FetchMetadata()
|
|
|
|
if Error(err) != Error(tt.expectErr) {
|
|
|
|
t.Fatalf("bad error (%q): want %q, got %q", tt.resources, tt.expectErr, err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(metadata, tt.expect) {
|
|
|
|
t.Fatalf("bad fetch (%q): want %q, got %q", tt.resources, tt.expect, metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(err error) string {
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|