diff --git a/datasource/configdrive/configdrive_test.go b/datasource/configdrive/configdrive_test.go index 5d72a19..fa42209 100644 --- a/datasource/configdrive/configdrive_test.go +++ b/datasource/configdrive/configdrive_test.go @@ -15,83 +15,75 @@ package configdrive import ( - "os" "testing" + + "github.com/coreos/coreos-cloudinit/datasource/test" ) -type mockFilesystem []string - -func (m mockFilesystem) readFile(filename string) ([]byte, error) { - for _, file := range m { - if file == filename { - return []byte(filename), nil - } - } - return nil, os.ErrNotExist -} - func TestFetchMetadata(t *testing.T) { for _, tt := range []struct { - root string - filename string - files mockFilesystem + root string + files test.MockFilesystem + + metadata string }{ { "/", + test.MockFilesystem{}, "", - mockFilesystem{}, }, { "/", - "/openstack/latest/meta_data.json", - mockFilesystem([]string{"/openstack/latest/meta_data.json"}), + test.MockFilesystem{"/openstack/latest/meta_data.json": "metadata"}, + "metadata", }, { "/media/configdrive", - "/media/configdrive/openstack/latest/meta_data.json", - mockFilesystem([]string{"/media/configdrive/openstack/latest/meta_data.json"}), + test.MockFilesystem{"/media/configdrive/openstack/latest/meta_data.json": "metadata"}, + "metadata", }, } { - cd := configDrive{tt.root, tt.files.readFile} - filename, err := cd.FetchMetadata() + cd := configDrive{tt.root, tt.files.ReadFile} + metadata, err := cd.FetchMetadata() if err != nil { t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err) } - if string(filename) != tt.filename { - t.Fatalf("bad path for %q: want %q, got %q", tt, tt.filename, filename) + if string(metadata) != tt.metadata { + t.Fatalf("bad path for %q: want %q, got %q", tt, tt.metadata, metadata) } } } func TestFetchUserdata(t *testing.T) { for _, tt := range []struct { - root string - filename string - files mockFilesystem + root string + files test.MockFilesystem + + userdata string }{ { "/", + test.MockFilesystem{}, "", - mockFilesystem{}, }, { "/", - "/openstack/latest/user_data", - mockFilesystem([]string{"/openstack/latest/user_data"}), + test.MockFilesystem{"/openstack/latest/user_data": "userdata"}, + "userdata", }, { "/media/configdrive", - "/media/configdrive/openstack/latest/user_data", - mockFilesystem([]string{"/media/configdrive/openstack/latest/user_data"}), + test.MockFilesystem{"/media/configdrive/openstack/latest/user_data": "userdata"}, + "userdata", }, } { - cd := configDrive{tt.root, tt.files.readFile} - filename, err := cd.FetchUserdata() + cd := configDrive{tt.root, tt.files.ReadFile} + userdata, err := cd.FetchUserdata() if err != nil { t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err) } - if string(filename) != tt.filename { - t.Fatalf("bad path for %q: want %q, got %q", tt, tt.filename, filename) + if string(userdata) != tt.userdata { + t.Fatalf("bad path for %q: want %q, got %q", tt, tt.userdata, userdata) } } } diff --git a/datasource/test/test.go b/datasource/test/test.go new file mode 100644 index 0000000..84696e2 --- /dev/null +++ b/datasource/test/test.go @@ -0,0 +1,28 @@ +// 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 test + +import ( + "os" +) + +type MockFilesystem map[string]string + +func (m MockFilesystem) ReadFile(filename string) ([]byte, error) { + if contents, ok := m[filename]; ok { + return []byte(contents), nil + } + return nil, os.ErrNotExist +} diff --git a/datasource/waagent/waagent_test.go b/datasource/waagent/waagent_test.go index 464d551..326c6a0 100644 --- a/datasource/waagent/waagent_test.go +++ b/datasource/waagent/waagent_test.go @@ -16,44 +16,36 @@ package waagent import ( "encoding/json" - "os" "reflect" "testing" + + "github.com/coreos/coreos-cloudinit/datasource/test" ) -type mockFilesystem map[string][]byte - -func (m mockFilesystem) readFile(filename string) ([]byte, error) { - if contents := m[filename]; contents != nil { - return contents, nil - } - return nil, os.ErrNotExist -} - func TestFetchMetadata(t *testing.T) { for _, tt := range []struct { root string - files mockFilesystem + files test.MockFilesystem metadata map[string]string }{ { "/", - mockFilesystem{}, + test.MockFilesystem{}, nil, }, { "/", - mockFilesystem{"/SharedConfig.xml": []byte("")}, + test.MockFilesystem{"/SharedConfig.xml": ""}, nil, }, { "/var/lib/waagent", - mockFilesystem{"/var/lib/waagent/SharedConfig.xml": []byte("")}, + test.MockFilesystem{"/var/lib/waagent/SharedConfig.xml": ""}, nil, }, { "/var/lib/waagent", - mockFilesystem{"/var/lib/waagent/SharedConfig.xml": []byte(` + test.MockFilesystem{"/var/lib/waagent/SharedConfig.xml": ` @@ -89,14 +81,14 @@ func TestFetchMetadata(t *testing.T) { -`)}, +`}, map[string]string{ "local-ipv4": "100.73.202.64", "public-ipv4": "191.239.39.77", }, }, } { - a := waagent{tt.root, tt.files.readFile} + a := waagent{tt.root, tt.files.ReadFile} metadataBytes, err := a.FetchMetadata() if err != nil { t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err) @@ -116,22 +108,22 @@ func TestFetchMetadata(t *testing.T) { func TestFetchUserdata(t *testing.T) { for _, tt := range []struct { root string - files mockFilesystem + files test.MockFilesystem }{ { "/", - mockFilesystem{}, + test.MockFilesystem{}, }, { "/", - mockFilesystem{"/CustomData": []byte{}}, + test.MockFilesystem{"/CustomData": ""}, }, { "/var/lib/waagent/", - mockFilesystem{"/var/lib/waagent/CustomData": []byte{}}, + test.MockFilesystem{"/var/lib/waagent/CustomData": ""}, }, } { - a := waagent{tt.root, tt.files.readFile} + a := waagent{tt.root, tt.files.ReadFile} _, err := a.FetchUserdata() if err != nil { t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err)