123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- // 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.
- package metadata
- import (
- "bytes"
- "fmt"
- "reflect"
- "testing"
- "github.com/rancher/os/config/cloudinit/datasource/metadata/test"
- "github.com/rancher/os/config/cloudinit/pkg"
- )
- func TestAvailabilityChanges(t *testing.T) {
- want := true
- if ac := (Service{}).AvailabilityChanges(); ac != want {
- t.Fatalf("bad AvailabilityChanges: want %t, got %t", want, ac)
- }
- }
- func TestIsAvailable(t *testing.T) {
- for _, tt := range []struct {
- root string
- apiVersion string
- resources map[string]string
- expect bool
- }{
- {
- root: "/",
- apiVersion: "2009-04-04",
- resources: map[string]string{
- "/2009-04-04": "",
- },
- expect: true,
- },
- {
- root: "/",
- resources: map[string]string{},
- expect: false,
- },
- } {
- service := &Service{
- Root: tt.root,
- Client: &test.HTTPClient{Resources: tt.resources, Err: nil},
- APIVersion: tt.apiVersion,
- }
- if a := service.IsAvailable(); a != tt.expect {
- t.Fatalf("bad isAvailable (%q): want %t, got %t", tt.resources, tt.expect, a)
- }
- }
- }
- func TestFetchUserdata(t *testing.T) {
- for _, tt := range []struct {
- root string
- userdataPath string
- resources map[string]string
- userdata []byte
- clientErr error
- expectErr error
- }{
- {
- root: "/",
- userdataPath: "2009-04-04/user-data",
- resources: map[string]string{
- "/2009-04-04/user-data": "hello",
- },
- userdata: []byte("hello"),
- },
- {
- root: "/",
- clientErr: pkg.ErrNotFound{Err: fmt.Errorf("test not found error")},
- userdata: []byte{},
- },
- {
- root: "/",
- clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test timeout error")},
- expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test timeout error")},
- },
- } {
- service := &Service{
- Root: tt.root,
- Client: &test.HTTPClient{Resources: tt.resources, Err: tt.clientErr},
- UserdataPath: tt.userdataPath,
- }
- data, err := service.FetchUserdata()
- if Error(err) != Error(tt.expectErr) {
- t.Fatalf("bad error (%q): want %q, got %q", tt.resources, tt.expectErr, err)
- }
- if !bytes.Equal(data, tt.userdata) {
- t.Fatalf("bad userdata (%q): want %q, got %q", tt.resources, tt.userdata, data)
- }
- }
- }
- func TestURLs(t *testing.T) {
- for _, tt := range []struct {
- root string
- userdataPath string
- metadataPath string
- expectRoot string
- userdata string
- metadata string
- }{
- {
- root: "/",
- userdataPath: "2009-04-04/user-data",
- metadataPath: "2009-04-04/meta-data",
- expectRoot: "/",
- userdata: "/2009-04-04/user-data",
- metadata: "/2009-04-04/meta-data",
- },
- {
- root: "http://169.254.169.254/",
- userdataPath: "2009-04-04/user-data",
- metadataPath: "2009-04-04/meta-data",
- expectRoot: "http://169.254.169.254/",
- userdata: "http://169.254.169.254/2009-04-04/user-data",
- metadata: "http://169.254.169.254/2009-04-04/meta-data",
- },
- } {
- service := &Service{
- Root: tt.root,
- UserdataPath: tt.userdataPath,
- MetadataPath: tt.metadataPath,
- }
- if url := service.UserdataURL(); url != tt.userdata {
- t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.userdata, url)
- }
- if url := service.MetadataURL(); url != tt.metadata {
- t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.metadata, url)
- }
- if url := service.ConfigRoot(); url != tt.expectRoot {
- t.Fatalf("bad url (%q): want %q, got %q", tt.root, tt.expectRoot, url)
- }
- }
- }
- func TestNewDatasource(t *testing.T) {
- for _, tt := range []struct {
- root string
- expectRoot string
- }{
- {
- root: "",
- expectRoot: "/",
- },
- {
- root: "/",
- expectRoot: "/",
- },
- {
- root: "http://169.254.169.254",
- expectRoot: "http://169.254.169.254/",
- },
- {
- root: "http://169.254.169.254/",
- expectRoot: "http://169.254.169.254/",
- },
- } {
- service := NewDatasource(tt.root, "", "", "", nil)
- if service.Root != tt.expectRoot {
- t.Fatalf("bad root (%q): want %q, got %q", tt.root, tt.expectRoot, service.Root)
- }
- }
- }
- 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"}},
- },
- },
- {
- err: fmt.Errorf("test error"),
- tests: []struct {
- path string
- val []string
- }{
- {"", nil},
- },
- },
- } {
- service := &Service{
- Client: &test.HTTPClient{Resources: s.resources, Err: s.err},
- }
- for _, tt := range s.tests {
- attrs, err := service.FetchAttributes(tt.path)
- 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"},
- },
- },
- {
- err: fmt.Errorf("test error"),
- tests: []struct {
- path string
- val string
- }{
- {"", ""},
- },
- },
- } {
- service := &Service{
- Client: &test.HTTPClient{Resources: s.resources, Err: s.err},
- }
- for _, tt := range s.tests {
- attr, err := service.FetchAttribute(tt.path)
- 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 Error(err error) string {
- if err != nil {
- return err.Error()
- }
- return ""
- }
|