Use correct namespace in unit tests that use fake clientset

Fake clientset no longer needs to be prepopulated with records: keeping
them in leads to the name conflict on creates. Also, since fake
clientset now respects namespaces, we need to correctly populate them.
This commit is contained in:
Oleg Shaldybin 2016-06-01 18:47:36 -07:00
parent d445d4082d
commit 3b15d5be19
8 changed files with 47 additions and 19 deletions

View File

@ -1426,8 +1426,25 @@ func newNode(name string) *api.Node {
} }
func newPod(name, host string) *api.Pod { func newPod(name, host string) *api.Pod {
return &api.Pod{ObjectMeta: api.ObjectMeta{Name: name}, Spec: api.PodSpec{NodeName: host}, pod := &api.Pod{
Status: api.PodStatus{Conditions: []api.PodCondition{{Type: api.PodReady, Status: api.ConditionTrue}}}} ObjectMeta: api.ObjectMeta{
Namespace: "default",
Name: name,
},
Spec: api.PodSpec{
NodeName: host,
},
Status: api.PodStatus{
Conditions: []api.PodCondition{
{
Type: api.PodReady,
Status: api.ConditionTrue,
},
},
},
}
return pod
} }
func contains(node *api.Node, nodes []*api.Node) bool { func contains(node *api.Node, nodes []*api.Node) bool {

View File

@ -162,6 +162,10 @@ func TestSyncResourceQuota(t *testing.T) {
func TestSyncResourceQuotaSpecChange(t *testing.T) { func TestSyncResourceQuotaSpecChange(t *testing.T) {
resourceQuota := api.ResourceQuota{ resourceQuota := api.ResourceQuota{
ObjectMeta: api.ObjectMeta{
Namespace: "default",
Name: "rq",
},
Spec: api.ResourceQuotaSpec{ Spec: api.ResourceQuotaSpec{
Hard: api.ResourceList{ Hard: api.ResourceList{
api.ResourceCPU: resource.MustParse("4"), api.ResourceCPU: resource.MustParse("4"),
@ -250,6 +254,10 @@ func TestSyncResourceQuotaSpecChange(t *testing.T) {
func TestSyncResourceQuotaNoChange(t *testing.T) { func TestSyncResourceQuotaNoChange(t *testing.T) {
resourceQuota := api.ResourceQuota{ resourceQuota := api.ResourceQuota{
ObjectMeta: api.ObjectMeta{
Namespace: "default",
Name: "rq",
},
Spec: api.ResourceQuotaSpec{ Spec: api.ResourceQuotaSpec{
Hard: api.ResourceList{ Hard: api.ResourceList{
api.ResourceCPU: resource.MustParse("4"), api.ResourceCPU: resource.MustParse("4"),

View File

@ -223,7 +223,7 @@ func TestTokenCreation(t *testing.T) {
ExpectedActions []core.Action ExpectedActions []core.Action
}{ }{
"new serviceaccount with no secrets": { "new serviceaccount with no secrets": {
ClientObjects: []runtime.Object{serviceAccount(emptySecretReferences()), createdTokenSecret()}, ClientObjects: []runtime.Object{serviceAccount(emptySecretReferences())},
AddedServiceAccount: serviceAccount(emptySecretReferences()), AddedServiceAccount: serviceAccount(emptySecretReferences()),
ExpectedActions: []core.Action{ ExpectedActions: []core.Action{
@ -233,7 +233,7 @@ func TestTokenCreation(t *testing.T) {
}, },
}, },
"new serviceaccount with no secrets encountering create error": { "new serviceaccount with no secrets encountering create error": {
ClientObjects: []runtime.Object{serviceAccount(emptySecretReferences()), createdTokenSecret()}, ClientObjects: []runtime.Object{serviceAccount(emptySecretReferences())},
MaxRetries: 10, MaxRetries: 10,
IsAsync: true, IsAsync: true,
Reactors: []reaction{{ Reactors: []reaction{{
@ -250,7 +250,6 @@ func TestTokenCreation(t *testing.T) {
} }
}, },
}}, }},
AddedServiceAccount: serviceAccount(emptySecretReferences()), AddedServiceAccount: serviceAccount(emptySecretReferences()),
ExpectedActions: []core.Action{ ExpectedActions: []core.Action{
// Attempt 1 // Attempt 1
@ -295,7 +294,7 @@ func TestTokenCreation(t *testing.T) {
}, },
}, },
"new serviceaccount with missing secrets": { "new serviceaccount with missing secrets": {
ClientObjects: []runtime.Object{serviceAccount(missingSecretReferences()), createdTokenSecret()}, ClientObjects: []runtime.Object{serviceAccount(missingSecretReferences())},
AddedServiceAccount: serviceAccount(missingSecretReferences()), AddedServiceAccount: serviceAccount(missingSecretReferences()),
ExpectedActions: []core.Action{ ExpectedActions: []core.Action{
@ -305,7 +304,7 @@ func TestTokenCreation(t *testing.T) {
}, },
}, },
"new serviceaccount with non-token secrets": { "new serviceaccount with non-token secrets": {
ClientObjects: []runtime.Object{serviceAccount(regularSecretReferences()), createdTokenSecret(), opaqueSecret()}, ClientObjects: []runtime.Object{serviceAccount(regularSecretReferences()), opaqueSecret()},
AddedServiceAccount: serviceAccount(regularSecretReferences()), AddedServiceAccount: serviceAccount(regularSecretReferences()),
ExpectedActions: []core.Action{ ExpectedActions: []core.Action{
@ -329,9 +328,8 @@ func TestTokenCreation(t *testing.T) {
core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, api.NamespaceDefault, "default"), core.NewGetAction(unversioned.GroupVersionResource{Resource: "serviceaccounts"}, api.NamespaceDefault, "default"),
}, },
}, },
"updated serviceaccount with no secrets": { "updated serviceaccount with no secrets": {
ClientObjects: []runtime.Object{serviceAccount(emptySecretReferences()), createdTokenSecret()}, ClientObjects: []runtime.Object{serviceAccount(emptySecretReferences())},
UpdatedServiceAccount: serviceAccount(emptySecretReferences()), UpdatedServiceAccount: serviceAccount(emptySecretReferences()),
ExpectedActions: []core.Action{ ExpectedActions: []core.Action{
@ -341,7 +339,7 @@ func TestTokenCreation(t *testing.T) {
}, },
}, },
"updated serviceaccount with missing secrets": { "updated serviceaccount with missing secrets": {
ClientObjects: []runtime.Object{serviceAccount(missingSecretReferences()), createdTokenSecret()}, ClientObjects: []runtime.Object{serviceAccount(missingSecretReferences())},
UpdatedServiceAccount: serviceAccount(missingSecretReferences()), UpdatedServiceAccount: serviceAccount(missingSecretReferences()),
ExpectedActions: []core.Action{ ExpectedActions: []core.Action{
@ -351,7 +349,7 @@ func TestTokenCreation(t *testing.T) {
}, },
}, },
"updated serviceaccount with non-token secrets": { "updated serviceaccount with non-token secrets": {
ClientObjects: []runtime.Object{serviceAccount(regularSecretReferences()), createdTokenSecret(), opaqueSecret()}, ClientObjects: []runtime.Object{serviceAccount(regularSecretReferences()), opaqueSecret()},
UpdatedServiceAccount: serviceAccount(regularSecretReferences()), UpdatedServiceAccount: serviceAccount(regularSecretReferences()),
ExpectedActions: []core.Action{ ExpectedActions: []core.Action{
@ -367,7 +365,7 @@ func TestTokenCreation(t *testing.T) {
ExpectedActions: []core.Action{}, ExpectedActions: []core.Action{},
}, },
"updated serviceaccount with no secrets with resource conflict": { "updated serviceaccount with no secrets with resource conflict": {
ClientObjects: []runtime.Object{updatedServiceAccount(emptySecretReferences()), createdTokenSecret()}, ClientObjects: []runtime.Object{updatedServiceAccount(emptySecretReferences())},
UpdatedServiceAccount: serviceAccount(emptySecretReferences()), UpdatedServiceAccount: serviceAccount(emptySecretReferences()),
ExpectedActions: []core.Action{ ExpectedActions: []core.Action{

View File

@ -578,6 +578,9 @@ func TestDescribeEvents(t *testing.T) {
events := &api.EventList{ events := &api.EventList{
Items: []api.Event{ Items: []api.Event{
{ {
ObjectMeta: api.ObjectMeta{
Namespace: "foo",
},
Source: api.EventSource{Component: "kubelet"}, Source: api.EventSource{Component: "kubelet"},
Message: "Item 1", Message: "Item 1",
FirstTimestamp: unversioned.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)), FirstTimestamp: unversioned.NewTime(time.Date(2014, time.January, 15, 0, 0, 0, 0, time.UTC)),

View File

@ -228,7 +228,7 @@ func TestPlugin(t *testing.T) {
t.Errorf("Can't find the plugin by name") t.Errorf("Can't find the plugin by name")
} }
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: testPodUID}} pod := &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{}) mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil { if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err) t.Errorf("Failed to make a new Mounter: %v", err)
@ -283,7 +283,7 @@ func TestPluginReboot(t *testing.T) {
t.Errorf("Can't find the plugin by name") t.Errorf("Can't find the plugin by name")
} }
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: testPodUID}} pod := &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{}) mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil { if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err) t.Errorf("Failed to make a new Mounter: %v", err)

View File

@ -212,7 +212,8 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) {
ep := &api.Endpoints{ ep := &api.Endpoints{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "ep", Namespace: "nsA",
Name: "ep",
}, },
Subsets: []api.EndpointSubset{{ Subsets: []api.EndpointSubset{{
Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}}, Addresses: []api.EndpointAddress{{IP: "127.0.0.1"}},
@ -228,7 +229,7 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) {
// readOnly bool is supplied by persistent-claim volume source when its mounter creates other volumes // readOnly bool is supplied by persistent-claim volume source when its mounter creates other volumes
spec := volume.NewSpecFromPersistentVolume(pv, true) spec := volume.NewSpecFromPersistentVolume(pv, true)
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}} pod := &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: "nsA", UID: types.UID("poduid")}}
mounter, _ := plug.NewMounter(spec, pod, volume.VolumeOptions{}) mounter, _ := plug.NewMounter(spec, pod, volume.VolumeOptions{})
if !mounter.GetAttributes().ReadOnly { if !mounter.GetAttributes().ReadOnly {

View File

@ -231,7 +231,7 @@ func TestPlugin(t *testing.T) {
t.Errorf("Can't find the plugin by name") t.Errorf("Can't find the plugin by name")
} }
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: testPodUID}} pod := &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{}) mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil { if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err) t.Errorf("Failed to make a new Mounter: %v", err)
@ -304,7 +304,7 @@ func TestPluginReboot(t *testing.T) {
t.Errorf("Can't find the plugin by name") t.Errorf("Can't find the plugin by name")
} }
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: testPodUID}} pod := &api.Pod{ObjectMeta: api.ObjectMeta{Namespace: testNamespace, UID: testPodUID}}
mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{}) mounter, err := plugin.NewMounter(volume.NewSpecFromVolume(volumeSpec), pod, volume.VolumeOptions{})
if err != nil { if err != nil {
t.Errorf("Failed to make a new Mounter: %v", err) t.Errorf("Failed to make a new Mounter: %v", err)

View File

@ -1193,7 +1193,8 @@ func createNamespaceForTest() *kapi.Namespace {
func createSAForTest() *kapi.ServiceAccount { func createSAForTest() *kapi.ServiceAccount {
return &kapi.ServiceAccount{ return &kapi.ServiceAccount{
ObjectMeta: kapi.ObjectMeta{ ObjectMeta: kapi.ObjectMeta{
Name: "default", Namespace: "default",
Name: "default",
}, },
} }
} }