test: DRY out MockFilesystem
This commit is contained in:
parent
c093e44049
commit
be62a1df66
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
28
datasource/test/test.go
Normal file
28
datasource/test/test.go
Normal file
@ -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
|
||||
}
|
@ -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(`<?xml version="1.0" encoding="utf-8"?>
|
||||
test.MockFilesystem{"/var/lib/waagent/SharedConfig.xml": `<?xml version="1.0" encoding="utf-8"?>
|
||||
<SharedConfig version="1.0.0.0" goalStateIncarnation="1">
|
||||
<Deployment name="c8f9e4c9c18948e1bebf57c5685da756" guid="{1d10394f-c741-4a1a-a6bb-278f213c5a5e}" incarnation="0" isNonCancellableTopologyChangeEnabled="false">
|
||||
<Service name="core-test-1" guid="{00000000-0000-0000-0000-000000000000}" />
|
||||
@ -89,14 +81,14 @@ func TestFetchMetadata(t *testing.T) {
|
||||
</InputEndpoints>
|
||||
</Instance>
|
||||
</Instances>
|
||||
</SharedConfig>`)},
|
||||
</SharedConfig>`},
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user