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
ee5dbf10
Commit
ee5dbf10
authored
May 12, 2017
by
Maru Newby
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fed: Remove redundant unit tests
The equivalent coverage is provided by integration tests.
parent
578f17c6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
14 additions
and
599 deletions
+14
-599
BUILD
federation/pkg/federation-controller/sync/BUILD
+14
-29
configmap_controller_test.go
...g/federation-controller/sync/configmap_controller_test.go
+0
-190
daemonset_controller_test.go
...g/federation-controller/sync/daemonset_controller_test.go
+0
-168
secret_controller_test.go
.../pkg/federation-controller/sync/secret_controller_test.go
+0
-212
No files found.
federation/pkg/federation-controller/sync/BUILD
View file @
ee5dbf10
...
@@ -39,46 +39,31 @@ go_library(
...
@@ -39,46 +39,31 @@ go_library(
],
],
)
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
go_test(
go_test(
name = "go_default_test",
name = "go_default_test",
srcs = [
srcs = ["controller_test.go"],
"configmap_controller_test.go",
"controller_test.go",
"daemonset_controller_test.go",
"secret_controller_test.go",
],
library = ":go_default_library",
library = ":go_default_library",
tags = ["automanaged"],
tags = ["automanaged"],
deps = [
deps = [
"//federation/apis/federation/v1beta1:go_default_library",
"//federation/apis/federation/v1beta1:go_default_library",
"//federation/client/clientset_generated/federation_clientset/fake:go_default_library",
"//federation/pkg/federatedtypes:go_default_library",
"//federation/pkg/federatedtypes:go_default_library",
"//federation/pkg/federation-controller/util:go_default_library",
"//federation/pkg/federation-controller/util:go_default_library",
"//federation/pkg/federation-controller/util/deletionhelper:go_default_library",
"//federation/pkg/federation-controller/util/test:go_default_library",
"//federation/pkg/federation-controller/util/test:go_default_library",
"//pkg/api/v1:go_default_library",
"//pkg/api/v1:go_default_library",
"//pkg/apis/extensions/v1beta1:go_default_library",
"//pkg/client/clientset_generated/clientset:go_default_library",
"//pkg/client/clientset_generated/clientset/fake:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
"//vendor/github.com/stretchr/testify/require:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/wait:go_default_library",
],
],
)
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
federation/pkg/federation-controller/sync/configmap_controller_test.go
deleted
100644 → 0
View file @
578f17c6
/*
Copyright 2016 The Kubernetes Authors.
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
sync
import
(
"fmt"
"testing"
"time"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
federationapi
"k8s.io/kubernetes/federation/apis/federation/v1beta1"
fakefedclientset
"k8s.io/kubernetes/federation/client/clientset_generated/federation_clientset/fake"
"k8s.io/kubernetes/federation/pkg/federatedtypes"
"k8s.io/kubernetes/federation/pkg/federation-controller/util"
"k8s.io/kubernetes/federation/pkg/federation-controller/util/deletionhelper"
.
"k8s.io/kubernetes/federation/pkg/federation-controller/util/test"
apiv1
"k8s.io/kubernetes/pkg/api/v1"
kubeclientset
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
fakekubeclientset
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
"github.com/golang/glog"
"github.com/stretchr/testify/assert"
)
func
TestConfigMapController
(
t
*
testing
.
T
)
{
configmaps
:=
"configmaps"
clusters
:=
"clusters"
informerStoreErr
:=
"configmap should have appeared in the informer store"
cluster1
:=
NewCluster
(
"cluster1"
,
apiv1
.
ConditionTrue
)
cluster2
:=
NewCluster
(
"cluster2"
,
apiv1
.
ConditionTrue
)
fakeClient
:=
&
fakefedclientset
.
Clientset
{}
RegisterFakeList
(
clusters
,
&
fakeClient
.
Fake
,
&
federationapi
.
ClusterList
{
Items
:
[]
federationapi
.
Cluster
{
*
cluster1
}})
RegisterFakeList
(
configmaps
,
&
fakeClient
.
Fake
,
&
apiv1
.
ConfigMapList
{
Items
:
[]
apiv1
.
ConfigMap
{}})
configmapWatch
:=
RegisterFakeWatch
(
configmaps
,
&
fakeClient
.
Fake
)
configmapUpdateChan
:=
RegisterFakeCopyOnUpdate
(
configmaps
,
&
fakeClient
.
Fake
,
configmapWatch
)
clusterWatch
:=
RegisterFakeWatch
(
clusters
,
&
fakeClient
.
Fake
)
cluster1Client
:=
&
fakekubeclientset
.
Clientset
{}
cluster1Watch
:=
RegisterFakeWatch
(
configmaps
,
&
cluster1Client
.
Fake
)
RegisterFakeList
(
configmaps
,
&
cluster1Client
.
Fake
,
&
apiv1
.
ConfigMapList
{
Items
:
[]
apiv1
.
ConfigMap
{}})
cluster1CreateChan
:=
RegisterFakeCopyOnCreate
(
configmaps
,
&
cluster1Client
.
Fake
,
cluster1Watch
)
cluster1UpdateChan
:=
RegisterFakeCopyOnUpdate
(
configmaps
,
&
cluster1Client
.
Fake
,
cluster1Watch
)
cluster2Client
:=
&
fakekubeclientset
.
Clientset
{}
cluster2Watch
:=
RegisterFakeWatch
(
configmaps
,
&
cluster2Client
.
Fake
)
RegisterFakeList
(
configmaps
,
&
cluster2Client
.
Fake
,
&
apiv1
.
ConfigMapList
{
Items
:
[]
apiv1
.
ConfigMap
{}})
cluster2CreateChan
:=
RegisterFakeCopyOnCreate
(
configmaps
,
&
cluster2Client
.
Fake
,
cluster2Watch
)
configmapController
:=
newFederationSyncController
(
fakeClient
,
federatedtypes
.
NewConfigMapAdapter
(
fakeClient
))
informer
:=
ToFederatedInformerForTestOnly
(
configmapController
.
informer
)
informer
.
SetClientFactory
(
func
(
cluster
*
federationapi
.
Cluster
)
(
kubeclientset
.
Interface
,
error
)
{
switch
cluster
.
Name
{
case
cluster1
.
Name
:
return
cluster1Client
,
nil
case
cluster2
.
Name
:
return
cluster2Client
,
nil
default
:
return
nil
,
fmt
.
Errorf
(
"Unknown cluster"
)
}
})
configmapController
.
minimizeLatency
()
stop
:=
make
(
chan
struct
{})
configmapController
.
Run
(
stop
)
configmap1
:=
&
apiv1
.
ConfigMap
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"test-configmap"
,
Namespace
:
"ns"
,
SelfLink
:
"/api/v1/namespaces/ns/configmaps/test-configmap"
,
},
Data
:
map
[
string
]
string
{
"A"
:
"ala ma kota"
,
"B"
:
"quick brown fox"
,
},
}
// Test add federated configmap.
configmapWatch
.
Add
(
configmap1
)
// There should be 2 updates to add both the finalizers.
updatedConfigMap
:=
GetConfigMapFromChan
(
configmapUpdateChan
)
AssertHasFinalizer
(
t
,
updatedConfigMap
,
deletionhelper
.
FinalizerDeleteFromUnderlyingClusters
)
AssertHasFinalizer
(
t
,
updatedConfigMap
,
metav1
.
FinalizerOrphanDependents
)
// Verify that the configmap is created in underlying cluster1.
createdConfigMap
:=
GetConfigMapFromChan
(
cluster1CreateChan
)
assert
.
NotNil
(
t
,
createdConfigMap
)
assert
.
Equal
(
t
,
configmap1
.
Namespace
,
createdConfigMap
.
Namespace
)
assert
.
Equal
(
t
,
configmap1
.
Name
,
createdConfigMap
.
Name
)
assert
.
True
(
t
,
util
.
ConfigMapEquivalent
(
configmap1
,
createdConfigMap
))
// Wait for the configmap to appear in the informer store
err
:=
WaitForStoreUpdate
(
configmapController
.
informer
.
GetTargetStore
(),
cluster1
.
Name
,
types
.
NamespacedName
{
Namespace
:
configmap1
.
Namespace
,
Name
:
configmap1
.
Name
}
.
String
(),
wait
.
ForeverTestTimeout
)
assert
.
Nil
(
t
,
err
,
informerStoreErr
)
// Test update federated configmap.
configmap1
.
Annotations
=
map
[
string
]
string
{
"A"
:
"B"
,
}
configmapWatch
.
Modify
(
configmap1
)
updatedConfigMap
=
GetConfigMapFromChan
(
cluster1UpdateChan
)
assert
.
NotNil
(
t
,
updatedConfigMap
)
assert
.
Equal
(
t
,
configmap1
.
Name
,
updatedConfigMap
.
Name
)
assert
.
Equal
(
t
,
configmap1
.
Namespace
,
updatedConfigMap
.
Namespace
)
assert
.
True
(
t
,
util
.
ConfigMapEquivalent
(
configmap1
,
updatedConfigMap
))
// Wait for the configmap to appear in the informer store
err
=
WaitForConfigMapStoreUpdate
(
configmapController
.
informer
.
GetTargetStore
(),
cluster1
.
Name
,
types
.
NamespacedName
{
Namespace
:
configmap1
.
Namespace
,
Name
:
configmap1
.
Name
}
.
String
(),
configmap1
,
wait
.
ForeverTestTimeout
)
assert
.
Nil
(
t
,
err
,
informerStoreErr
)
// Test update federated configmap.
configmap1
.
Data
=
map
[
string
]
string
{
"config"
:
"myconfigurationfile"
,
}
configmapWatch
.
Modify
(
configmap1
)
for
{
updatedConfigMap
:=
GetConfigMapFromChan
(
cluster1UpdateChan
)
assert
.
NotNil
(
t
,
updatedConfigMap
)
if
updatedConfigMap
==
nil
{
break
}
assert
.
Equal
(
t
,
configmap1
.
Name
,
updatedConfigMap
.
Name
)
assert
.
Equal
(
t
,
configmap1
.
Namespace
,
updatedConfigMap
.
Namespace
)
if
util
.
ConfigMapEquivalent
(
configmap1
,
updatedConfigMap
)
{
break
}
}
// Test add cluster
clusterWatch
.
Add
(
cluster2
)
createdConfigMap2
:=
GetConfigMapFromChan
(
cluster2CreateChan
)
assert
.
NotNil
(
t
,
createdConfigMap2
)
assert
.
Equal
(
t
,
configmap1
.
Name
,
createdConfigMap2
.
Name
)
assert
.
Equal
(
t
,
configmap1
.
Namespace
,
createdConfigMap2
.
Namespace
)
assert
.
True
(
t
,
util
.
ConfigMapEquivalent
(
configmap1
,
createdConfigMap2
))
close
(
stop
)
}
func
GetConfigMapFromChan
(
c
chan
runtime
.
Object
)
*
apiv1
.
ConfigMap
{
if
configmap
:=
GetObjectFromChan
(
c
);
configmap
==
nil
{
return
nil
}
else
{
return
configmap
.
(
*
apiv1
.
ConfigMap
)
}
}
// Wait till the store is updated with latest configmap.
func
WaitForConfigMapStoreUpdate
(
store
util
.
FederatedReadOnlyStore
,
clusterName
,
key
string
,
desiredConfigMap
*
apiv1
.
ConfigMap
,
timeout
time
.
Duration
)
error
{
retryInterval
:=
200
*
time
.
Millisecond
err
:=
wait
.
PollImmediate
(
retryInterval
,
timeout
,
func
()
(
bool
,
error
)
{
obj
,
found
,
err
:=
store
.
GetByKey
(
clusterName
,
key
)
if
!
found
||
err
!=
nil
{
glog
.
Infof
(
"%s is not in the store"
,
key
)
return
false
,
err
}
equal
:=
util
.
ConfigMapEquivalent
(
obj
.
(
*
apiv1
.
ConfigMap
),
desiredConfigMap
)
if
!
equal
{
glog
.
Infof
(
"wrong content in the store expected:
\n
%v
\n
actual:
\n
%v
\n
"
,
*
desiredConfigMap
,
*
obj
.
(
*
apiv1
.
ConfigMap
))
}
return
equal
,
err
})
return
err
}
federation/pkg/federation-controller/sync/daemonset_controller_test.go
deleted
100644 → 0
View file @
578f17c6
/*
Copyright 2016 The Kubernetes Authors.
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
sync
import
(
"fmt"
"reflect"
"testing"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
federationapi
"k8s.io/kubernetes/federation/apis/federation/v1beta1"
fakefedclientset
"k8s.io/kubernetes/federation/client/clientset_generated/federation_clientset/fake"
"k8s.io/kubernetes/federation/pkg/federatedtypes"
"k8s.io/kubernetes/federation/pkg/federation-controller/util"
"k8s.io/kubernetes/federation/pkg/federation-controller/util/deletionhelper"
.
"k8s.io/kubernetes/federation/pkg/federation-controller/util/test"
apiv1
"k8s.io/kubernetes/pkg/api/v1"
extensionsv1
"k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
kubeclientset
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
fakekubeclientset
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
"github.com/stretchr/testify/assert"
)
func
TestDaemonSetController
(
t
*
testing
.
T
)
{
daemonsets
:=
"daemonsets"
clusters
:=
"clusters"
cluster1
:=
NewCluster
(
"cluster1"
,
apiv1
.
ConditionTrue
)
cluster2
:=
NewCluster
(
"cluster2"
,
apiv1
.
ConditionTrue
)
fakeClient
:=
&
fakefedclientset
.
Clientset
{}
RegisterFakeList
(
clusters
,
&
fakeClient
.
Fake
,
&
federationapi
.
ClusterList
{
Items
:
[]
federationapi
.
Cluster
{
*
cluster1
}})
RegisterFakeList
(
daemonsets
,
&
fakeClient
.
Fake
,
&
extensionsv1
.
DaemonSetList
{
Items
:
[]
extensionsv1
.
DaemonSet
{}})
daemonsetWatch
:=
RegisterFakeWatch
(
daemonsets
,
&
fakeClient
.
Fake
)
daemonsetUpdateChan
:=
RegisterFakeCopyOnUpdate
(
daemonsets
,
&
fakeClient
.
Fake
,
daemonsetWatch
)
clusterWatch
:=
RegisterFakeWatch
(
"clusters"
,
&
fakeClient
.
Fake
)
cluster1Client
:=
&
fakekubeclientset
.
Clientset
{}
cluster1Watch
:=
RegisterFakeWatch
(
daemonsets
,
&
cluster1Client
.
Fake
)
RegisterFakeList
(
daemonsets
,
&
cluster1Client
.
Fake
,
&
extensionsv1
.
DaemonSetList
{
Items
:
[]
extensionsv1
.
DaemonSet
{}})
cluster1CreateChan
:=
RegisterFakeCopyOnCreate
(
daemonsets
,
&
cluster1Client
.
Fake
,
cluster1Watch
)
cluster1UpdateChan
:=
RegisterFakeCopyOnUpdate
(
daemonsets
,
&
cluster1Client
.
Fake
,
cluster1Watch
)
cluster2Client
:=
&
fakekubeclientset
.
Clientset
{}
cluster2Watch
:=
RegisterFakeWatch
(
daemonsets
,
&
cluster2Client
.
Fake
)
RegisterFakeList
(
daemonsets
,
&
cluster2Client
.
Fake
,
&
extensionsv1
.
DaemonSetList
{
Items
:
[]
extensionsv1
.
DaemonSet
{}})
cluster2CreateChan
:=
RegisterFakeCopyOnCreate
(
daemonsets
,
&
cluster2Client
.
Fake
,
cluster2Watch
)
daemonsetController
:=
newFederationSyncController
(
fakeClient
,
federatedtypes
.
NewDaemonSetAdapter
(
fakeClient
))
informer
:=
ToFederatedInformerForTestOnly
(
daemonsetController
.
informer
)
informer
.
SetClientFactory
(
func
(
cluster
*
federationapi
.
Cluster
)
(
kubeclientset
.
Interface
,
error
)
{
switch
cluster
.
Name
{
case
cluster1
.
Name
:
return
cluster1Client
,
nil
case
cluster2
.
Name
:
return
cluster2Client
,
nil
default
:
return
nil
,
fmt
.
Errorf
(
"Unknown cluster"
)
}
})
daemonsetController
.
minimizeLatency
()
stop
:=
make
(
chan
struct
{})
daemonsetController
.
Run
(
stop
)
daemonset1
:=
extensionsv1
.
DaemonSet
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"test-daemonset"
,
Namespace
:
"ns"
,
SelfLink
:
"/api/v1/namespaces/ns/daemonsets/test-daemonset"
,
},
Spec
:
extensionsv1
.
DaemonSetSpec
{
Selector
:
&
metav1
.
LabelSelector
{
MatchLabels
:
map
[
string
]
string
{
"A"
:
"xyz"
,
},
},
},
}
// Test add federated daemonset.
daemonsetWatch
.
Add
(
&
daemonset1
)
// There should be an update to add both the finalizers.
updatedDaemonSet
:=
GetDaemonSetFromChan
(
daemonsetUpdateChan
)
AssertHasFinalizer
(
t
,
updatedDaemonSet
,
deletionhelper
.
FinalizerDeleteFromUnderlyingClusters
)
AssertHasFinalizer
(
t
,
updatedDaemonSet
,
metav1
.
FinalizerOrphanDependents
)
daemonset1
=
*
updatedDaemonSet
createdDaemonSet
:=
GetDaemonSetFromChan
(
cluster1CreateChan
)
assert
.
NotNil
(
t
,
createdDaemonSet
)
assert
.
Equal
(
t
,
daemonset1
.
Namespace
,
createdDaemonSet
.
Namespace
)
assert
.
Equal
(
t
,
daemonset1
.
Name
,
createdDaemonSet
.
Name
)
assert
.
True
(
t
,
daemonsetsEqual
(
daemonset1
,
*
createdDaemonSet
),
fmt
.
Sprintf
(
"expected: %v, actual: %v"
,
daemonset1
,
*
createdDaemonSet
))
daemonsetKey
:=
types
.
NamespacedName
{
Namespace
:
daemonset1
.
Namespace
,
Name
:
daemonset1
.
Name
,
}
.
String
()
// Wait for the daemonset to appear in the informer store
err
:=
WaitForStoreUpdate
(
daemonsetController
.
informer
.
GetTargetStore
(),
cluster1
.
Name
,
daemonsetKey
,
wait
.
ForeverTestTimeout
)
assert
.
Nil
(
t
,
err
,
"daemonset should have appeared in the informer store"
)
// TODO: Re-enable this when we have fixed these flaky tests: https://github.com/kubernetes/kubernetes/issues/36540.
// Test update federated daemonset.
daemonset1
.
Annotations
=
map
[
string
]
string
{
"A"
:
"B"
,
}
daemonsetWatch
.
Modify
(
&
daemonset1
)
updatedDaemonSet
=
GetDaemonSetFromChan
(
cluster1UpdateChan
)
assert
.
NotNil
(
t
,
updatedDaemonSet
)
assert
.
Equal
(
t
,
daemonset1
.
Name
,
updatedDaemonSet
.
Name
)
assert
.
Equal
(
t
,
daemonset1
.
Namespace
,
updatedDaemonSet
.
Namespace
)
assert
.
True
(
t
,
daemonsetsEqual
(
daemonset1
,
*
updatedDaemonSet
),
fmt
.
Sprintf
(
"expected: %v, actual: %v"
,
daemonset1
,
*
updatedDaemonSet
))
// Test update federated daemonset.
daemonset1
.
Spec
.
Template
.
Name
=
"TEST"
daemonsetWatch
.
Modify
(
&
daemonset1
)
err
=
CheckObjectFromChan
(
cluster1UpdateChan
,
MetaAndSpecCheckingFunction
(
&
daemonset1
))
assert
.
NoError
(
t
,
err
)
// Test add cluster
clusterWatch
.
Add
(
cluster2
)
createdDaemonSet2
:=
GetDaemonSetFromChan
(
cluster2CreateChan
)
assert
.
NotNil
(
t
,
createdDaemonSet2
)
assert
.
Equal
(
t
,
daemonset1
.
Name
,
createdDaemonSet2
.
Name
)
assert
.
Equal
(
t
,
daemonset1
.
Namespace
,
createdDaemonSet2
.
Namespace
)
assert
.
True
(
t
,
daemonsetsEqual
(
daemonset1
,
*
createdDaemonSet2
),
fmt
.
Sprintf
(
"expected: %v, actual: %v"
,
daemonset1
,
*
createdDaemonSet2
))
close
(
stop
)
}
func
daemonsetsEqual
(
a
,
b
extensionsv1
.
DaemonSet
)
bool
{
return
util
.
ObjectMetaEquivalent
(
a
.
ObjectMeta
,
b
.
ObjectMeta
)
&&
reflect
.
DeepEqual
(
a
.
Spec
,
b
.
Spec
)
}
func
GetDaemonSetFromChan
(
c
chan
runtime
.
Object
)
*
extensionsv1
.
DaemonSet
{
if
daemonset
:=
GetObjectFromChan
(
c
);
daemonset
==
nil
{
return
nil
}
else
{
return
daemonset
.
(
*
extensionsv1
.
DaemonSet
)
}
}
federation/pkg/federation-controller/sync/secret_controller_test.go
deleted
100644 → 0
View file @
578f17c6
/*
Copyright 2016 The Kubernetes Authors.
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
sync
import
(
"fmt"
"reflect"
"testing"
"time"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
federationapi
"k8s.io/kubernetes/federation/apis/federation/v1beta1"
fakefedclientset
"k8s.io/kubernetes/federation/client/clientset_generated/federation_clientset/fake"
"k8s.io/kubernetes/federation/pkg/federatedtypes"
"k8s.io/kubernetes/federation/pkg/federation-controller/util"
"k8s.io/kubernetes/federation/pkg/federation-controller/util/deletionhelper"
.
"k8s.io/kubernetes/federation/pkg/federation-controller/util/test"
apiv1
"k8s.io/kubernetes/pkg/api/v1"
kubeclientset
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
fakekubeclientset
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake"
"github.com/golang/glog"
"github.com/stretchr/testify/assert"
)
func
TestSecretController
(
t
*
testing
.
T
)
{
clusters
:=
"clusters"
secrets
:=
"secrets"
cluster1
:=
NewCluster
(
"cluster1"
,
apiv1
.
ConditionTrue
)
cluster2
:=
NewCluster
(
"cluster2"
,
apiv1
.
ConditionTrue
)
fakeClient
:=
&
fakefedclientset
.
Clientset
{}
RegisterFakeList
(
clusters
,
&
fakeClient
.
Fake
,
&
federationapi
.
ClusterList
{
Items
:
[]
federationapi
.
Cluster
{
*
cluster1
}})
RegisterFakeList
(
secrets
,
&
fakeClient
.
Fake
,
&
apiv1
.
SecretList
{
Items
:
[]
apiv1
.
Secret
{}})
secretWatch
:=
RegisterFakeWatch
(
secrets
,
&
fakeClient
.
Fake
)
secretUpdateChan
:=
RegisterFakeCopyOnUpdate
(
secrets
,
&
fakeClient
.
Fake
,
secretWatch
)
clusterWatch
:=
RegisterFakeWatch
(
clusters
,
&
fakeClient
.
Fake
)
cluster1Client
:=
&
fakekubeclientset
.
Clientset
{}
cluster1Watch
:=
RegisterFakeWatch
(
secrets
,
&
cluster1Client
.
Fake
)
RegisterFakeList
(
secrets
,
&
cluster1Client
.
Fake
,
&
apiv1
.
SecretList
{
Items
:
[]
apiv1
.
Secret
{}})
cluster1CreateChan
:=
RegisterFakeCopyOnCreate
(
secrets
,
&
cluster1Client
.
Fake
,
cluster1Watch
)
cluster1UpdateChan
:=
RegisterFakeCopyOnUpdate
(
secrets
,
&
cluster1Client
.
Fake
,
cluster1Watch
)
cluster2Client
:=
&
fakekubeclientset
.
Clientset
{}
cluster2Watch
:=
RegisterFakeWatch
(
secrets
,
&
cluster2Client
.
Fake
)
RegisterFakeList
(
secrets
,
&
cluster2Client
.
Fake
,
&
apiv1
.
SecretList
{
Items
:
[]
apiv1
.
Secret
{}})
cluster2CreateChan
:=
RegisterFakeCopyOnCreate
(
secrets
,
&
cluster2Client
.
Fake
,
cluster2Watch
)
secretController
:=
newFederationSyncController
(
fakeClient
,
federatedtypes
.
NewSecretAdapter
(
fakeClient
))
informerClientFactory
:=
func
(
cluster
*
federationapi
.
Cluster
)
(
kubeclientset
.
Interface
,
error
)
{
switch
cluster
.
Name
{
case
cluster1
.
Name
:
return
cluster1Client
,
nil
case
cluster2
.
Name
:
return
cluster2Client
,
nil
default
:
return
nil
,
fmt
.
Errorf
(
"Unknown cluster"
)
}
}
setClientFactory
(
secretController
.
informer
,
informerClientFactory
)
secretController
.
minimizeLatency
()
stop
:=
make
(
chan
struct
{})
secretController
.
Run
(
stop
)
secret1
:=
apiv1
.
Secret
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
"test-secret"
,
Namespace
:
"ns"
,
SelfLink
:
"/api/v1/namespaces/ns/secrets/test-secret"
,
},
Data
:
map
[
string
][]
byte
{
"A"
:
[]
byte
(
"ala ma kota"
),
"B"
:
[]
byte
(
"quick brown fox"
),
},
Type
:
apiv1
.
SecretTypeOpaque
,
}
// Test add federated secret.
secretWatch
.
Add
(
&
secret1
)
// There should be an update to add both the finalizers.
updatedSecret
:=
GetSecretFromChan
(
secretUpdateChan
)
AssertHasFinalizer
(
t
,
updatedSecret
,
deletionhelper
.
FinalizerDeleteFromUnderlyingClusters
)
AssertHasFinalizer
(
t
,
updatedSecret
,
metav1
.
FinalizerOrphanDependents
)
secret1
=
*
updatedSecret
// Verify that the secret is created in underlying cluster1.
createdSecret
:=
GetSecretFromChan
(
cluster1CreateChan
)
assert
.
NotNil
(
t
,
createdSecret
)
assert
.
Equal
(
t
,
secret1
.
Namespace
,
createdSecret
.
Namespace
)
assert
.
Equal
(
t
,
secret1
.
Name
,
createdSecret
.
Name
)
assert
.
True
(
t
,
secretsEqual
(
secret1
,
*
createdSecret
),
fmt
.
Sprintf
(
"expected: %v, actual: %v"
,
secret1
,
*
createdSecret
))
// Wait for the secret to appear in the informer store
err
:=
WaitForStoreUpdate
(
secretController
.
informer
.
GetTargetStore
(),
cluster1
.
Name
,
types
.
NamespacedName
{
Namespace
:
secret1
.
Namespace
,
Name
:
secret1
.
Name
}
.
String
(),
wait
.
ForeverTestTimeout
)
assert
.
Nil
(
t
,
err
,
"secret should have appeared in the informer store"
)
checkAll
:=
func
(
expected
apiv1
.
Secret
)
CheckingFunction
{
return
func
(
obj
runtime
.
Object
)
error
{
glog
.
V
(
4
)
.
Infof
(
"Checking %v"
,
obj
)
s
:=
obj
.
(
*
apiv1
.
Secret
)
if
err
:=
CompareObjectMeta
(
expected
.
ObjectMeta
,
s
.
ObjectMeta
);
err
!=
nil
{
return
err
}
if
!
reflect
.
DeepEqual
(
expected
.
Data
,
s
.
Data
)
{
return
fmt
.
Errorf
(
"Data is different expected:%v actual:%v"
,
expected
.
Data
,
s
.
Data
)
}
if
expected
.
Type
!=
s
.
Type
{
return
fmt
.
Errorf
(
"Type is different expected:%v actual:%v"
,
expected
.
Type
,
s
.
Type
)
}
return
nil
}
}
// Test update federated secret.
secret1
.
Annotations
=
map
[
string
]
string
{
"A"
:
"B"
,
}
secretWatch
.
Modify
(
&
secret1
)
err
=
CheckObjectFromChan
(
cluster1UpdateChan
,
checkAll
(
secret1
))
assert
.
NoError
(
t
,
err
)
// Wait for the secret to be updated in the informer store.
err
=
WaitForSecretStoreUpdate
(
secretController
.
informer
.
GetTargetStore
(),
cluster1
.
Name
,
types
.
NamespacedName
{
Namespace
:
secret1
.
Namespace
,
Name
:
secret1
.
Name
}
.
String
(),
&
secret1
,
wait
.
ForeverTestTimeout
)
assert
.
NoError
(
t
,
err
,
"secret should have been updated in the informer store"
)
// Test update federated secret.
secret1
.
Data
=
map
[
string
][]
byte
{
"config"
:
[]
byte
(
"myconfigurationfile"
),
}
secretWatch
.
Modify
(
&
secret1
)
err
=
CheckObjectFromChan
(
cluster1UpdateChan
,
checkAll
(
secret1
))
assert
.
NoError
(
t
,
err
)
// Test add cluster
clusterWatch
.
Add
(
cluster2
)
createdSecret2
:=
GetSecretFromChan
(
cluster2CreateChan
)
assert
.
NotNil
(
t
,
createdSecret2
)
assert
.
Equal
(
t
,
secret1
.
Name
,
createdSecret2
.
Name
)
assert
.
Equal
(
t
,
secret1
.
Namespace
,
createdSecret2
.
Namespace
)
assert
.
True
(
t
,
secretsEqual
(
secret1
,
*
createdSecret2
),
fmt
.
Sprintf
(
"expected: %v, actual: %v"
,
secret1
,
*
createdSecret2
))
close
(
stop
)
}
func
setClientFactory
(
informer
util
.
FederatedInformer
,
informerClientFactory
func
(
*
federationapi
.
Cluster
)
(
kubeclientset
.
Interface
,
error
))
{
testInformer
:=
ToFederatedInformerForTestOnly
(
informer
)
testInformer
.
SetClientFactory
(
informerClientFactory
)
}
func
secretsEqual
(
a
,
b
apiv1
.
Secret
)
bool
{
// Clear the SelfLink and ObjectMeta.Finalizers since they will be different
// in resource in federation control plane and resource in underlying cluster.
a
.
SelfLink
=
""
b
.
SelfLink
=
""
a
.
ObjectMeta
.
Finalizers
=
[]
string
{}
b
.
ObjectMeta
.
Finalizers
=
[]
string
{}
return
reflect
.
DeepEqual
(
a
,
b
)
}
func
GetSecretFromChan
(
c
chan
runtime
.
Object
)
*
apiv1
.
Secret
{
if
secret
:=
GetObjectFromChan
(
c
);
secret
==
nil
{
return
nil
}
else
{
return
secret
.
(
*
apiv1
.
Secret
)
}
}
// Wait till the store is updated with latest secret.
func
WaitForSecretStoreUpdate
(
store
util
.
FederatedReadOnlyStore
,
clusterName
,
key
string
,
desiredSecret
*
apiv1
.
Secret
,
timeout
time
.
Duration
)
error
{
retryInterval
:=
200
*
time
.
Millisecond
err
:=
wait
.
PollImmediate
(
retryInterval
,
timeout
,
func
()
(
bool
,
error
)
{
obj
,
found
,
err
:=
store
.
GetByKey
(
clusterName
,
key
)
if
!
found
||
err
!=
nil
{
glog
.
Infof
(
"%s is not in the store"
,
key
)
return
false
,
err
}
equal
:=
secretsEqual
(
*
obj
.
(
*
apiv1
.
Secret
),
*
desiredSecret
)
if
!
equal
{
glog
.
Infof
(
"wrong content in the store expected:
\n
%v
\n
actual:
\n
%v
\n
"
,
*
desiredSecret
,
*
obj
.
(
*
apiv1
.
Secret
))
}
return
equal
,
err
})
return
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