2015-01-24 19:32:33 -08:00
|
|
|
// Copyright 2015 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-10-17 15:36:22 -07:00
|
|
|
|
2014-08-15 18:14:34 -07:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-01-22 17:35:39 -08:00
|
|
|
"net"
|
|
|
|
"reflect"
|
2014-08-15 18:14:34 -07:00
|
|
|
"testing"
|
|
|
|
|
2015-01-22 17:35:39 -08:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
2014-08-15 18:14:34 -07:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata"
|
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata/test"
|
|
|
|
"github.com/coreos/coreos-cloudinit/pkg"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestType(t *testing.T) {
|
|
|
|
want := "digitalocean-metadata-service"
|
|
|
|
if kind := (metadataService{}).Type(); kind != want {
|
|
|
|
t.Fatalf("bad type: want %q, got %q", want, kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchMetadata(t *testing.T) {
|
|
|
|
for _, tt := range []struct {
|
|
|
|
root string
|
|
|
|
metadataPath string
|
|
|
|
resources map[string]string
|
2015-01-22 17:35:39 -08:00
|
|
|
expect datasource.Metadata
|
2014-08-15 18:14:34 -07:00
|
|
|
clientErr error
|
|
|
|
expectErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
root: "/",
|
|
|
|
metadataPath: "v1.json",
|
|
|
|
resources: map[string]string{
|
|
|
|
"/v1.json": "bad",
|
|
|
|
},
|
|
|
|
expectErr: fmt.Errorf("invalid character 'b' looking for beginning of value"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
root: "/",
|
|
|
|
metadataPath: "v1.json",
|
|
|
|
resources: map[string]string{
|
|
|
|
"/v1.json": `{
|
|
|
|
"droplet_id": 1,
|
|
|
|
"user_data": "hello",
|
|
|
|
"vendor_data": "hello",
|
|
|
|
"public_keys": [
|
|
|
|
"publickey1",
|
|
|
|
"publickey2"
|
|
|
|
],
|
|
|
|
"region": "nyc2",
|
|
|
|
"interfaces": {
|
|
|
|
"public": [
|
|
|
|
{
|
|
|
|
"ipv4": {
|
|
|
|
"ip_address": "192.168.1.2",
|
|
|
|
"netmask": "255.255.255.0",
|
|
|
|
"gateway": "192.168.1.1"
|
|
|
|
},
|
|
|
|
"ipv6": {
|
|
|
|
"ip_address": "fe00::",
|
|
|
|
"cidr": 126,
|
|
|
|
"gateway": "fe00::"
|
|
|
|
},
|
|
|
|
"mac": "ab:cd:ef:gh:ij",
|
|
|
|
"type": "public"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
},
|
2015-01-22 17:35:39 -08:00
|
|
|
expect: datasource.Metadata{
|
|
|
|
PublicIPv4: net.ParseIP("192.168.1.2"),
|
|
|
|
PublicIPv6: net.ParseIP("fe00::"),
|
|
|
|
SSHPublicKeys: map[string]string{
|
|
|
|
"0": "publickey1",
|
|
|
|
"1": "publickey2",
|
|
|
|
},
|
2015-01-23 11:31:57 -08:00
|
|
|
NetworkConfig: []byte(`{
|
|
|
|
"droplet_id": 1,
|
|
|
|
"user_data": "hello",
|
|
|
|
"vendor_data": "hello",
|
|
|
|
"public_keys": [
|
|
|
|
"publickey1",
|
|
|
|
"publickey2"
|
|
|
|
],
|
|
|
|
"region": "nyc2",
|
|
|
|
"interfaces": {
|
|
|
|
"public": [
|
|
|
|
{
|
|
|
|
"ipv4": {
|
|
|
|
"ip_address": "192.168.1.2",
|
|
|
|
"netmask": "255.255.255.0",
|
|
|
|
"gateway": "192.168.1.1"
|
|
|
|
},
|
|
|
|
"ipv6": {
|
|
|
|
"ip_address": "fe00::",
|
|
|
|
"cidr": 126,
|
|
|
|
"gateway": "fe00::"
|
|
|
|
},
|
|
|
|
"mac": "ab:cd:ef:gh:ij",
|
|
|
|
"type": "public"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`),
|
2015-01-22 17:35:39 -08:00
|
|
|
},
|
2014-08-15 18:14:34 -07:00
|
|
|
},
|
|
|
|
{
|
2014-10-23 11:46:08 -07:00
|
|
|
clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
|
|
|
|
expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
|
2014-08-15 18:14:34 -07:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
service := &metadataService{
|
|
|
|
MetadataService: metadata.MetadataService{
|
|
|
|
Root: tt.root,
|
2014-10-23 11:46:08 -07:00
|
|
|
Client: &test.HttpClient{Resources: tt.resources, Err: tt.clientErr},
|
2014-08-15 18:14:34 -07:00
|
|
|
MetadataPath: tt.metadataPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
metadata, err := service.FetchMetadata()
|
|
|
|
if Error(err) != Error(tt.expectErr) {
|
|
|
|
t.Fatalf("bad error (%q): want %q, got %q", tt.resources, tt.expectErr, err)
|
|
|
|
}
|
2015-01-22 17:35:39 -08:00
|
|
|
if !reflect.DeepEqual(tt.expect, metadata) {
|
|
|
|
t.Fatalf("bad fetch (%q): want %#q, got %#q", tt.resources, tt.expect, metadata)
|
2014-08-15 18:14:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(err error) string {
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|