Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
7493ed0f
Commit
7493ed0f
authored
May 26, 2015
by
Saad Ali
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #8786 from pmorie/secret-tests
Add tests for secret volume plugin reboot/idempotency
parents
b7e0cc41
feb0880b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
158 additions
and
24 deletions
+158
-24
fake.go
pkg/util/mount/fake.go
+2
-0
secret_test.go
pkg/volume/secret/secret_test.go
+156
-24
No files found.
pkg/util/mount/fake.go
View file @
7493ed0f
...
@@ -22,6 +22,8 @@ type FakeMounter struct {
...
@@ -22,6 +22,8 @@ type FakeMounter struct {
Log
[]
FakeAction
Log
[]
FakeAction
}
}
var
_
Interface
=
&
FakeMounter
{}
// Values for FakeAction.Action
// Values for FakeAction.Action
const
FakeActionMount
=
"mount"
const
FakeActionMount
=
"mount"
const
FakeActionUnmount
=
"unmount"
const
FakeActionUnmount
=
"unmount"
...
...
pkg/volume/secret/secret_test.go
View file @
7493ed0f
...
@@ -31,20 +31,22 @@ import (
...
@@ -31,20 +31,22 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/mount"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/mount"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume/empty_dir"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume/empty_dir"
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume/util"
)
)
func
newTestHost
(
t
*
testing
.
T
,
client
client
.
Interface
)
volume
.
VolumeHost
{
func
newTestHost
(
t
*
testing
.
T
,
client
client
.
Interface
)
(
string
,
volume
.
VolumeHost
)
{
tempDir
,
err
:=
ioutil
.
TempDir
(
"/tmp"
,
"secret_volume_test."
)
tempDir
,
err
:=
ioutil
.
TempDir
(
"/tmp"
,
"secret_volume_test."
)
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"can't make a temp rootdir: %v"
,
err
)
t
.
Fatalf
(
"can't make a temp rootdir: %v"
,
err
)
}
}
return
volume
.
NewFakeVolumeHost
(
tempDir
,
client
,
empty_dir
.
ProbeVolumePlugins
())
return
tempDir
,
volume
.
NewFakeVolumeHost
(
tempDir
,
client
,
empty_dir
.
ProbeVolumePlugins
())
}
}
func
TestCanSupport
(
t
*
testing
.
T
)
{
func
TestCanSupport
(
t
*
testing
.
T
)
{
pluginMgr
:=
volume
.
VolumePluginMgr
{}
pluginMgr
:=
volume
.
VolumePluginMgr
{}
pluginMgr
.
InitPlugins
(
ProbeVolumePlugins
(),
newTestHost
(
t
,
nil
))
_
,
host
:=
newTestHost
(
t
,
nil
)
pluginMgr
.
InitPlugins
(
ProbeVolumePlugins
(),
host
)
plugin
,
err
:=
pluginMgr
.
FindPluginByName
(
secretPluginName
)
plugin
,
err
:=
pluginMgr
.
FindPluginByName
(
secretPluginName
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -64,33 +66,129 @@ func TestPlugin(t *testing.T) {
...
@@ -64,33 +66,129 @@ func TestPlugin(t *testing.T) {
testVolumeName
=
"test_volume_name"
testVolumeName
=
"test_volume_name"
testNamespace
=
"test_secret_namespace"
testNamespace
=
"test_secret_namespace"
testName
=
"test_secret_name"
testName
=
"test_secret_name"
volumeSpec
=
volumeSpec
(
testVolumeName
,
testName
)
secret
=
secret
(
testNamespace
,
testName
)
client
=
testclient
.
NewSimpleFake
(
&
secret
)
pluginMgr
=
volume
.
VolumePluginMgr
{}
_
,
host
=
newTestHost
(
t
,
client
)
)
)
volumeSpec
:=
&
api
.
Volume
{
pluginMgr
.
InitPlugins
(
ProbeVolumePlugins
(),
host
)
Name
:
testVolumeName
,
VolumeSource
:
api
.
VolumeSource
{
plugin
,
err
:=
pluginMgr
.
FindPluginByName
(
secretPluginName
)
Secret
:
&
api
.
SecretVolumeSource
{
if
err
!=
nil
{
SecretName
:
testName
,
t
.
Errorf
(
"Can't find the plugin by name"
)
},
},
}
}
secret
:=
api
.
Secret
{
pod
:=
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
testPodUID
}}
ObjectMeta
:
api
.
ObjectMeta
{
builder
,
err
:=
plugin
.
NewBuilder
(
volume
.
NewSpecFromVolume
(
volumeSpec
),
pod
,
volume
.
VolumeOptions
{},
&
mount
.
FakeMounter
{})
Namespace
:
testNamespace
,
if
err
!=
nil
{
Name
:
testName
,
t
.
Errorf
(
"Failed to make a new Builder: %v"
,
err
)
},
}
Data
:
map
[
string
][]
byte
{
if
builder
==
nil
{
"data-1"
:
[]
byte
(
"value-1"
),
t
.
Errorf
(
"Got a nil Builder"
)
"data-2"
:
[]
byte
(
"value-2"
),
}
"data-3"
:
[]
byte
(
"value-3"
),
volumePath
:=
builder
.
GetPath
()
if
!
strings
.
HasSuffix
(
volumePath
,
fmt
.
Sprintf
(
"pods/test_pod_uid/volumes/kubernetes.io~secret/test_volume_name"
))
{
t
.
Errorf
(
"Got unexpected path: %s"
,
volumePath
)
}
err
=
builder
.
SetUp
()
if
err
!=
nil
{
t
.
Errorf
(
"Failed to setup volume: %v"
,
err
)
}
if
_
,
err
:=
os
.
Stat
(
volumePath
);
err
!=
nil
{
if
os
.
IsNotExist
(
err
)
{
t
.
Errorf
(
"SetUp() failed, volume path not created: %s"
,
volumePath
)
}
else
{
t
.
Errorf
(
"SetUp() failed: %v"
,
err
)
}
}
doTestSecretDataInVolume
(
volumePath
,
secret
,
t
)
doTestCleanAndTeardown
(
plugin
,
testPodUID
,
testVolumeName
,
volumePath
,
t
)
}
// Test the case where the 'ready' file has been created and the pod volume dir
// is a mountpoint. Mount should not be called.
func
TestPluginIdempotent
(
t
*
testing
.
T
)
{
var
(
testPodUID
=
types
.
UID
(
"test_pod_uid2"
)
testVolumeName
=
"test_volume_name"
testNamespace
=
"test_secret_namespace"
testName
=
"test_secret_name"
volumeSpec
=
volumeSpec
(
testVolumeName
,
testName
)
secret
=
secret
(
testNamespace
,
testName
)
client
=
testclient
.
NewSimpleFake
(
&
secret
)
pluginMgr
=
volume
.
VolumePluginMgr
{}
rootDir
,
host
=
newTestHost
(
t
,
client
)
)
pluginMgr
.
InitPlugins
(
ProbeVolumePlugins
(),
host
)
plugin
,
err
:=
pluginMgr
.
FindPluginByName
(
secretPluginName
)
if
err
!=
nil
{
t
.
Errorf
(
"Can't find the plugin by name"
)
}
podVolumeDir
:=
fmt
.
Sprintf
(
"%v/pods/test_pod_uid2/volumes/kubernetes.io~secret/test_volume_name"
,
rootDir
)
podMetadataDir
:=
fmt
.
Sprintf
(
"%v/pods/test_pod_uid2/plugins/kubernetes.io~secret/test_volume_name"
,
rootDir
)
pod
:=
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
testPodUID
}}
mounter
:=
&
mount
.
FakeMounter
{}
mounter
.
MountPoints
=
[]
mount
.
MountPoint
{
{
Path
:
podVolumeDir
,
},
},
}
}
util
.
SetReady
(
podMetadataDir
)
builder
,
err
:=
plugin
.
NewBuilder
(
volume
.
NewSpecFromVolume
(
volumeSpec
),
pod
,
volume
.
VolumeOptions
{},
mounter
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to make a new Builder: %v"
,
err
)
}
if
builder
==
nil
{
t
.
Errorf
(
"Got a nil Builder"
)
}
volumePath
:=
builder
.
GetPath
()
err
=
builder
.
SetUp
()
if
err
!=
nil
{
t
.
Errorf
(
"Failed to setup volume: %v"
,
err
)
}
client
:=
testclient
.
NewSimpleFake
(
&
secret
)
if
len
(
mounter
.
Log
)
!=
0
{
t
.
Errorf
(
"Unexpected calls made to mounter: %v"
,
mounter
.
Log
)
}
pluginMgr
:=
volume
.
VolumePluginMgr
{}
if
_
,
err
:=
os
.
Stat
(
volumePath
);
err
!=
nil
{
pluginMgr
.
InitPlugins
(
ProbeVolumePlugins
(),
newTestHost
(
t
,
client
))
if
!
os
.
IsNotExist
(
err
)
{
t
.
Errorf
(
"SetUp() failed unexpectedly: %v"
,
err
)
}
}
else
{
t
.
Errorf
(
"volume path should not exist: %v"
,
volumePath
)
}
}
// Test the case where the plugin's ready file exists, but the volume dir is not a
// mountpoint, which is the state the system will be in after reboot. The dir
// should be mounter and the secret data written to it.
func
TestPluginReboot
(
t
*
testing
.
T
)
{
var
(
testPodUID
=
types
.
UID
(
"test_pod_uid3"
)
testVolumeName
=
"test_volume_name"
testNamespace
=
"test_secret_namespace"
testName
=
"test_secret_name"
volumeSpec
=
volumeSpec
(
testVolumeName
,
testName
)
secret
=
secret
(
testNamespace
,
testName
)
client
=
testclient
.
NewSimpleFake
(
&
secret
)
pluginMgr
=
volume
.
VolumePluginMgr
{}
rootDir
,
host
=
newTestHost
(
t
,
client
)
)
pluginMgr
.
InitPlugins
(
ProbeVolumePlugins
(),
host
)
plugin
,
err
:=
pluginMgr
.
FindPluginByName
(
secretPluginName
)
plugin
,
err
:=
pluginMgr
.
FindPluginByName
(
secretPluginName
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -106,8 +204,10 @@ func TestPlugin(t *testing.T) {
...
@@ -106,8 +204,10 @@ func TestPlugin(t *testing.T) {
t
.
Errorf
(
"Got a nil Builder"
)
t
.
Errorf
(
"Got a nil Builder"
)
}
}
podMetadataDir
:=
fmt
.
Sprintf
(
"%v/pods/test_pod_uid3/plugins/kubernetes.io~secret/test_volume_name"
,
rootDir
)
util
.
SetReady
(
podMetadataDir
)
volumePath
:=
builder
.
GetPath
()
volumePath
:=
builder
.
GetPath
()
if
!
strings
.
HasSuffix
(
volumePath
,
fmt
.
Sprintf
(
"pods/test_pod_uid/volumes/kubernetes.io~secret/test_volume_name"
))
{
if
!
strings
.
HasSuffix
(
volumePath
,
fmt
.
Sprintf
(
"pods/test_pod_uid
3
/volumes/kubernetes.io~secret/test_volume_name"
))
{
t
.
Errorf
(
"Got unexpected path: %s"
,
volumePath
)
t
.
Errorf
(
"Got unexpected path: %s"
,
volumePath
)
}
}
...
@@ -123,6 +223,36 @@ func TestPlugin(t *testing.T) {
...
@@ -123,6 +223,36 @@ func TestPlugin(t *testing.T) {
}
}
}
}
doTestSecretDataInVolume
(
volumePath
,
secret
,
t
)
doTestCleanAndTeardown
(
plugin
,
testPodUID
,
testVolumeName
,
volumePath
,
t
)
}
func
volumeSpec
(
volumeName
,
secretName
string
)
*
api
.
Volume
{
return
&
api
.
Volume
{
Name
:
volumeName
,
VolumeSource
:
api
.
VolumeSource
{
Secret
:
&
api
.
SecretVolumeSource
{
SecretName
:
secretName
,
},
},
}
}
func
secret
(
namespace
,
name
string
)
api
.
Secret
{
return
api
.
Secret
{
ObjectMeta
:
api
.
ObjectMeta
{
Namespace
:
namespace
,
Name
:
name
,
},
Data
:
map
[
string
][]
byte
{
"data-1"
:
[]
byte
(
"value-1"
),
"data-2"
:
[]
byte
(
"value-2"
),
"data-3"
:
[]
byte
(
"value-3"
),
},
}
}
func
doTestSecretDataInVolume
(
volumePath
string
,
secret
api
.
Secret
,
t
*
testing
.
T
)
{
for
key
,
value
:=
range
secret
.
Data
{
for
key
,
value
:=
range
secret
.
Data
{
secretDataHostPath
:=
path
.
Join
(
volumePath
,
key
)
secretDataHostPath
:=
path
.
Join
(
volumePath
,
key
)
if
_
,
err
:=
os
.
Stat
(
secretDataHostPath
);
err
!=
nil
{
if
_
,
err
:=
os
.
Stat
(
secretDataHostPath
);
err
!=
nil
{
...
@@ -139,8 +269,10 @@ func TestPlugin(t *testing.T) {
...
@@ -139,8 +269,10 @@ func TestPlugin(t *testing.T) {
}
}
}
}
}
}
}
cleaner
,
err
:=
plugin
.
NewCleaner
(
testVolumeName
,
testPodUID
,
mount
.
New
())
func
doTestCleanAndTeardown
(
plugin
volume
.
VolumePlugin
,
podUID
types
.
UID
,
testVolumeName
,
volumePath
string
,
t
*
testing
.
T
)
{
cleaner
,
err
:=
plugin
.
NewCleaner
(
testVolumeName
,
podUID
,
mount
.
New
())
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Errorf
(
"Failed to make a new Cleaner: %v"
,
err
)
t
.
Errorf
(
"Failed to make a new Cleaner: %v"
,
err
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment