Skip to content

Commit

Permalink
Job clients
Browse files Browse the repository at this point in the history
  • Loading branch information
soltysh committed Aug 25, 2015
1 parent 5296980 commit b996942
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/client/unversioned/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
type Interface interface {
PodsNamespacer
PodTemplatesNamespacer
JobsNamespacer
ReplicationControllersNamespacer
DaemonsNamespacer
ServicesNamespacer
Expand All @@ -49,6 +50,10 @@ type Interface interface {
ComponentStatusesInterface
}

func (c *Client) Jobs(namespace string) JobInterface {
return newJobs(c, namespace)
}

func (c *Client) ReplicationControllers(namespace string) ReplicationControllerInterface {
return newReplicationControllers(c, namespace)
}
Expand Down
96 changes: 96 additions & 0 deletions pkg/client/unversioned/jobs.go
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()
}
159 changes: 159 additions & 0 deletions pkg/client/unversioned/jobs_test.go
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)
}
74 changes: 74 additions & 0 deletions pkg/client/unversioned/testclient/fake_jobs.go
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
}
4 changes: 4 additions & 0 deletions pkg/client/unversioned/testclient/testclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (c *Fake) ResourceQuotas(namespace string) client.ResourceQuotaInterface {
return &FakeResourceQuotas{Fake: c, Namespace: namespace}
}

func (c *Fake) Jobs(namespace string) client.JobInterface {
return &FakeJobs{Fake: c, Namespace: namespace}
}

func (c *Fake) ReplicationControllers(namespace string) client.ReplicationControllerInterface {
return &FakeReplicationControllers{Fake: c, Namespace: namespace}
}
Expand Down

0 comments on commit b996942

Please sign in to comment.