-
Notifications
You must be signed in to change notification settings - Fork 40k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2015 The Kubernetes Authors All rights reserved. | ||
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 unversioned | ||
|
||
import ( | ||
"k8s.io/kubernetes/pkg/expapi" | ||
"k8s.io/kubernetes/pkg/fields" | ||
"k8s.io/kubernetes/pkg/labels" | ||
"k8s.io/kubernetes/pkg/watch" | ||
) | ||
|
||
// JobsNamespacer has methods to work with Job resources in a namespace | ||
type JobsNamespacer interface { | ||
Jobs(namespace string) JobInterface | ||
} | ||
|
||
// JobInterface exposes methods to work on Job resources. | ||
type JobInterface interface { | ||
List(label labels.Selector) (*expapi.JobList, error) | ||
Get(name string) (*expapi.Job, error) | ||
Create(job *expapi.Job) (*expapi.Job, error) | ||
Update(job *expapi.Job) (*expapi.Job, error) | ||
Delete(name string) error | ||
Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) | ||
} | ||
|
||
// jobs implements JobsNamespacer interface | ||
type jobs struct { | ||
r *Client | ||
ns string | ||
} | ||
|
||
// newJobs returns a jobs | ||
func newJobs(c *Client, namespace string) *jobs { | ||
return &jobs{c, namespace} | ||
} | ||
|
||
// List returns a list of jobs that match the label and field selectors. | ||
func (c *jobs) List(label labels.Selector) (result *expapi.JobList, err error) { | ||
result = &expapi.JobList{} | ||
err = c.r.Get().Namespace(c.ns).Resource("jobs").LabelsSelectorParam(label).Do().Into(result) | ||
return | ||
} | ||
|
||
// Get returns information about a particular job. | ||
func (c *jobs) Get(name string) (result *expapi.Job, err error) { | ||
result = &expapi.Job{} | ||
err = c.r.Get().Namespace(c.ns).Resource("jobs").Name(name).Do().Into(result) | ||
return | ||
} | ||
|
||
// Create creates a new job. | ||
func (c *jobs) Create(job *expapi.Job) (result *expapi.Job, err error) { | ||
result = &expapi.Job{} | ||
err = c.r.Post().Namespace(c.ns).Resource("jobs").Body(job).Do().Into(result) | ||
return | ||
} | ||
|
||
// Update updates an existing job. | ||
func (c *jobs) Update(job *expapi.Job) (result *expapi.Job, err error) { | ||
result = &expapi.Job{} | ||
err = c.r.Put().Namespace(c.ns).Resource("jobs").Name(job.Name).Body(job).Do().Into(result) | ||
return | ||
} | ||
|
||
// Delete deletes a job, returns error if one occurs. | ||
func (c *jobs) Delete(name string) (err error) { | ||
err = c.r.Delete().Namespace(c.ns).Resource("jobs").Name(name).Do().Error() | ||
return | ||
} | ||
|
||
// Watch returns a watch.Interface that watches the requested jobs. | ||
func (c *jobs) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) { | ||
return c.r.Get(). | ||
Prefix("watch"). | ||
Namespace(c.ns). | ||
Resource("jobs"). | ||
Param("resourceVersion", resourceVersion). | ||
LabelsSelectorParam(label). | ||
FieldsSelectorParam(field). | ||
Watch() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
Copyright 2015 The Kubernetes Authors All rights reserved. | ||
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 unversioned | ||
|
||
import ( | ||
"testing" | ||
|
||
"k8s.io/kubernetes/pkg/api" | ||
"k8s.io/kubernetes/pkg/api/testapi" | ||
"k8s.io/kubernetes/pkg/expapi" | ||
"k8s.io/kubernetes/pkg/labels" | ||
) | ||
|
||
func getJobResourceName() string { | ||
return "jobs" | ||
} | ||
|
||
func TestListJobs(t *testing.T) { | ||
ns := api.NamespaceAll | ||
c := &testClient{ | ||
Request: testRequest{ | ||
Method: "GET", | ||
Path: testapi.ResourcePath(getJobResourceName(), ns, ""), | ||
}, | ||
Response: Response{StatusCode: 200, | ||
Body: &expapi.JobList{ | ||
Items: []expapi.Job{ | ||
{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Name: "foo", | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
"name": "baz", | ||
}, | ||
}, | ||
Spec: expapi.JobSpec{ | ||
Template: &api.PodTemplateSpec{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
receivedJobList, err := c.Setup().Jobs(ns).List(labels.Everything()) | ||
c.Validate(t, receivedJobList, err) | ||
} | ||
|
||
func TestGetJob(t *testing.T) { | ||
ns := api.NamespaceDefault | ||
c := &testClient{ | ||
Request: testRequest{Method: "GET", Path: testapi.ResourcePath(getJobResourceName(), ns, "foo"), Query: buildQueryValues(nil)}, | ||
Response: Response{ | ||
StatusCode: 200, | ||
Body: &expapi.Job{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Name: "foo", | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
"name": "baz", | ||
}, | ||
}, | ||
Spec: expapi.JobSpec{ | ||
Template: &api.PodTemplateSpec{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
receivedJob, err := c.Setup().Jobs(ns).Get("foo") | ||
c.Validate(t, receivedJob, err) | ||
} | ||
|
||
func TestGetJobWithNoName(t *testing.T) { | ||
ns := api.NamespaceDefault | ||
c := &testClient{Error: true} | ||
receivedJob, err := c.Setup().Jobs(ns).Get("") | ||
if (err != nil) && (err.Error() != nameRequiredError) { | ||
t.Errorf("Expected error: %v, but got %v", nameRequiredError, err) | ||
} | ||
|
||
c.Validate(t, receivedJob, err) | ||
} | ||
|
||
func TestUpdateJob(t *testing.T) { | ||
ns := api.NamespaceDefault | ||
requestJob := &expapi.Job{ | ||
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: "1"}, | ||
} | ||
c := &testClient{ | ||
Request: testRequest{Method: "PUT", Path: testapi.ResourcePath(getJobResourceName(), ns, "foo"), Query: buildQueryValues(nil)}, | ||
Response: Response{ | ||
StatusCode: 200, | ||
Body: &expapi.Job{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Name: "foo", | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
"name": "baz", | ||
}, | ||
}, | ||
Spec: expapi.JobSpec{ | ||
Template: &api.PodTemplateSpec{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
receivedJob, err := c.Setup().Jobs(ns).Update(requestJob) | ||
c.Validate(t, receivedJob, err) | ||
} | ||
|
||
func TestDeleteJob(t *testing.T) { | ||
ns := api.NamespaceDefault | ||
c := &testClient{ | ||
Request: testRequest{Method: "DELETE", Path: testapi.ResourcePath(getJobResourceName(), ns, "foo"), Query: buildQueryValues(nil)}, | ||
Response: Response{StatusCode: 200}, | ||
} | ||
err := c.Setup().Jobs(ns).Delete("foo") | ||
c.Validate(t, nil, err) | ||
} | ||
|
||
func TestCreateJob(t *testing.T) { | ||
ns := api.NamespaceDefault | ||
requestJob := &expapi.Job{ | ||
ObjectMeta: api.ObjectMeta{Name: "foo"}, | ||
} | ||
c := &testClient{ | ||
Request: testRequest{Method: "POST", Path: testapi.ResourcePath(getJobResourceName(), ns, ""), Body: requestJob, Query: buildQueryValues(nil)}, | ||
Response: Response{ | ||
StatusCode: 200, | ||
Body: &expapi.Job{ | ||
ObjectMeta: api.ObjectMeta{ | ||
Name: "foo", | ||
Labels: map[string]string{ | ||
"foo": "bar", | ||
"name": "baz", | ||
}, | ||
}, | ||
Spec: expapi.JobSpec{ | ||
Template: &api.PodTemplateSpec{}, | ||
}, | ||
}, | ||
}, | ||
} | ||
receivedJob, err := c.Setup().Jobs(ns).Create(requestJob) | ||
c.Validate(t, receivedJob, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2015 The Kubernetes Authors All rights reserved. | ||
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 testclient | ||
|
||
import ( | ||
"k8s.io/kubernetes/pkg/expapi" | ||
"k8s.io/kubernetes/pkg/fields" | ||
"k8s.io/kubernetes/pkg/labels" | ||
"k8s.io/kubernetes/pkg/watch" | ||
) | ||
|
||
// FakeJobs implements JobInterface. Meant to be embedded into a struct to get a default | ||
// implementation. This makes faking out just the methods you want to test easier. | ||
type FakeJobs struct { | ||
Fake *Fake | ||
Namespace string | ||
} | ||
|
||
func (c *FakeJobs) Get(name string) (*expapi.Job, error) { | ||
obj, err := c.Fake.Invokes(NewGetAction("get-job", c.Namespace, name), &expapi.Job{}) | ||
if obj == nil { | ||
return nil, err | ||
} | ||
return obj.(*expapi.Job), err | ||
} | ||
|
||
func (c *FakeJobs) List(label labels.Selector) (*expapi.JobList, error) { | ||
obj, err := c.Fake.Invokes(NewListAction("jobs", c.Namespace, label, nil), &expapi.JobList{}) | ||
if obj == nil { | ||
return nil, err | ||
} | ||
|
||
return obj.(*expapi.JobList), err | ||
} | ||
|
||
func (c *FakeJobs) Create(job *expapi.Job) (*expapi.Job, error) { | ||
obj, err := c.Fake.Invokes(NewCreateAction("jobs", c.Namespace, job), job) | ||
if obj == nil { | ||
return nil, err | ||
} | ||
return obj.(*expapi.Job), err | ||
} | ||
|
||
func (c *FakeJobs) Update(job *expapi.Job) (*expapi.Job, error) { | ||
obj, err := c.Fake.Invokes(NewUpdateAction("jobs", c.Namespace, job), job) | ||
if obj == nil { | ||
return nil, err | ||
} | ||
return obj.(*expapi.Job), err | ||
} | ||
|
||
func (c *FakeJobs) Delete(name string) error { | ||
_, err := c.Fake.Invokes(NewDeleteAction("jobs", c.Namespace, name), &expapi.Job{}) | ||
return err | ||
} | ||
|
||
func (c *FakeJobs) Watch(label labels.Selector, field fields.Selector, resourceVersion string) (watch.Interface, error) { | ||
c.Fake.Invokes(NewWatchAction("jobs", c.Namespace, label, field, resourceVersion), nil) | ||
return c.Fake.Watch, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters