test: DRY out MockFilesystem
This commit is contained in:
parent
c093e44049
commit
be62a1df66
@ -15,50 +15,41 @@
|
|||||||
package configdrive
|
package configdrive
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"testing"
|
"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) {
|
func TestFetchMetadata(t *testing.T) {
|
||||||
for _, tt := range []struct {
|
for _, tt := range []struct {
|
||||||
root string
|
root string
|
||||||
filename string
|
files test.MockFilesystem
|
||||||
files mockFilesystem
|
|
||||||
|
metadata string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"/",
|
"/",
|
||||||
|
test.MockFilesystem{},
|
||||||
"",
|
"",
|
||||||
mockFilesystem{},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/",
|
"/",
|
||||||
"/openstack/latest/meta_data.json",
|
test.MockFilesystem{"/openstack/latest/meta_data.json": "metadata"},
|
||||||
mockFilesystem([]string{"/openstack/latest/meta_data.json"}),
|
"metadata",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/media/configdrive",
|
"/media/configdrive",
|
||||||
"/media/configdrive/openstack/latest/meta_data.json",
|
test.MockFilesystem{"/media/configdrive/openstack/latest/meta_data.json": "metadata"},
|
||||||
mockFilesystem([]string{"/media/configdrive/openstack/latest/meta_data.json"}),
|
"metadata",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
cd := configDrive{tt.root, tt.files.readFile}
|
cd := configDrive{tt.root, tt.files.ReadFile}
|
||||||
filename, err := cd.FetchMetadata()
|
metadata, err := cd.FetchMetadata()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err)
|
t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err)
|
||||||
}
|
}
|
||||||
if string(filename) != tt.filename {
|
if string(metadata) != tt.metadata {
|
||||||
t.Fatalf("bad path for %q: want %q, got %q", tt, tt.filename, filename)
|
t.Fatalf("bad path for %q: want %q, got %q", tt, tt.metadata, metadata)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,32 +57,33 @@ func TestFetchMetadata(t *testing.T) {
|
|||||||
func TestFetchUserdata(t *testing.T) {
|
func TestFetchUserdata(t *testing.T) {
|
||||||
for _, tt := range []struct {
|
for _, tt := range []struct {
|
||||||
root string
|
root string
|
||||||
filename string
|
files test.MockFilesystem
|
||||||
files mockFilesystem
|
|
||||||
|
userdata string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"/",
|
"/",
|
||||||
|
test.MockFilesystem{},
|
||||||
"",
|
"",
|
||||||
mockFilesystem{},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/",
|
"/",
|
||||||
"/openstack/latest/user_data",
|
test.MockFilesystem{"/openstack/latest/user_data": "userdata"},
|
||||||
mockFilesystem([]string{"/openstack/latest/user_data"}),
|
"userdata",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/media/configdrive",
|
"/media/configdrive",
|
||||||
"/media/configdrive/openstack/latest/user_data",
|
test.MockFilesystem{"/media/configdrive/openstack/latest/user_data": "userdata"},
|
||||||
mockFilesystem([]string{"/media/configdrive/openstack/latest/user_data"}),
|
"userdata",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
cd := configDrive{tt.root, tt.files.readFile}
|
cd := configDrive{tt.root, tt.files.ReadFile}
|
||||||
filename, err := cd.FetchUserdata()
|
userdata, err := cd.FetchUserdata()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err)
|
t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err)
|
||||||
}
|
}
|
||||||
if string(filename) != tt.filename {
|
if string(userdata) != tt.userdata {
|
||||||
t.Fatalf("bad path for %q: want %q, got %q", tt, tt.filename, filename)
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"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) {
|
func TestFetchMetadata(t *testing.T) {
|
||||||
for _, tt := range []struct {
|
for _, tt := range []struct {
|
||||||
root string
|
root string
|
||||||
files mockFilesystem
|
files test.MockFilesystem
|
||||||
metadata map[string]string
|
metadata map[string]string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"/",
|
"/",
|
||||||
mockFilesystem{},
|
test.MockFilesystem{},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/",
|
"/",
|
||||||
mockFilesystem{"/SharedConfig.xml": []byte("")},
|
test.MockFilesystem{"/SharedConfig.xml": ""},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/var/lib/waagent",
|
"/var/lib/waagent",
|
||||||
mockFilesystem{"/var/lib/waagent/SharedConfig.xml": []byte("")},
|
test.MockFilesystem{"/var/lib/waagent/SharedConfig.xml": ""},
|
||||||
nil,
|
nil,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/var/lib/waagent",
|
"/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">
|
<SharedConfig version="1.0.0.0" goalStateIncarnation="1">
|
||||||
<Deployment name="c8f9e4c9c18948e1bebf57c5685da756" guid="{1d10394f-c741-4a1a-a6bb-278f213c5a5e}" incarnation="0" isNonCancellableTopologyChangeEnabled="false">
|
<Deployment name="c8f9e4c9c18948e1bebf57c5685da756" guid="{1d10394f-c741-4a1a-a6bb-278f213c5a5e}" incarnation="0" isNonCancellableTopologyChangeEnabled="false">
|
||||||
<Service name="core-test-1" guid="{00000000-0000-0000-0000-000000000000}" />
|
<Service name="core-test-1" guid="{00000000-0000-0000-0000-000000000000}" />
|
||||||
@ -89,14 +81,14 @@ func TestFetchMetadata(t *testing.T) {
|
|||||||
</InputEndpoints>
|
</InputEndpoints>
|
||||||
</Instance>
|
</Instance>
|
||||||
</Instances>
|
</Instances>
|
||||||
</SharedConfig>`)},
|
</SharedConfig>`},
|
||||||
map[string]string{
|
map[string]string{
|
||||||
"local-ipv4": "100.73.202.64",
|
"local-ipv4": "100.73.202.64",
|
||||||
"public-ipv4": "191.239.39.77",
|
"public-ipv4": "191.239.39.77",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
a := waagent{tt.root, tt.files.readFile}
|
a := waagent{tt.root, tt.files.ReadFile}
|
||||||
metadataBytes, err := a.FetchMetadata()
|
metadataBytes, err := a.FetchMetadata()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err)
|
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) {
|
func TestFetchUserdata(t *testing.T) {
|
||||||
for _, tt := range []struct {
|
for _, tt := range []struct {
|
||||||
root string
|
root string
|
||||||
files mockFilesystem
|
files test.MockFilesystem
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"/",
|
"/",
|
||||||
mockFilesystem{},
|
test.MockFilesystem{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/",
|
"/",
|
||||||
mockFilesystem{"/CustomData": []byte{}},
|
test.MockFilesystem{"/CustomData": ""},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/var/lib/waagent/",
|
"/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()
|
_, err := a.FetchUserdata()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err)
|
t.Fatalf("bad error for %q: want %v, got %q", tt, nil, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user