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
b223e880
Unverified
Commit
b223e880
authored
Sep 24, 2018
by
k8s-ci-robot
Committed by
GitHub
Sep 24, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #66937 from mortent/CronJobIntegrationTest
Integration test for CronJob
parents
170dcc2e
7e62f734
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
244 additions
and
0 deletions
+244
-0
BUILD
test/integration/BUILD
+1
-0
BUILD
test/integration/cronjob/BUILD
+43
-0
cronjob_test.go
test/integration/cronjob/cronjob_test.go
+174
-0
main_test.go
test/integration/cronjob/main_test.go
+26
-0
No files found.
test/integration/BUILD
View file @
b223e880
...
...
@@ -42,6 +42,7 @@ filegroup(
"//test/integration/client:all-srcs",
"//test/integration/configmap:all-srcs",
"//test/integration/controllermanager:all-srcs",
"//test/integration/cronjob:all-srcs",
"//test/integration/daemonset:all-srcs",
"//test/integration/defaulttolerationseconds:all-srcs",
"//test/integration/deployment:all-srcs",
...
...
test/integration/cronjob/BUILD
0 → 100644
View file @
b223e880
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
)
go_test(
name = "go_default_test",
size = "large",
srcs = [
"cronjob_test.go",
"main_test.go",
],
tags = ["integration"],
deps = [
"//pkg/controller/cronjob:go_default_library",
"//pkg/controller/job:go_default_library",
"//staging/src/k8s.io/api/batch/v1:go_default_library",
"//staging/src/k8s.io/api/batch/v1beta1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/client-go/informers:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
"//staging/src/k8s.io/client-go/kubernetes/typed/batch/v1beta1:go_default_library",
"//staging/src/k8s.io/client-go/rest:go_default_library",
"//test/integration/framework:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
test/integration/cronjob/cronjob_test.go
0 → 100644
View file @
b223e880
/*
Copyright 2018 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
cronjob
import
(
"fmt"
"net/http/httptest"
"testing"
"time"
batchv1
"k8s.io/api/batch/v1"
batchv1beta1
"k8s.io/api/batch/v1beta1"
corev1
"k8s.io/api/core/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
clientset
"k8s.io/client-go/kubernetes"
clientbatchv1beta1
"k8s.io/client-go/kubernetes/typed/batch/v1beta1"
"k8s.io/client-go/rest"
restclient
"k8s.io/client-go/rest"
"k8s.io/kubernetes/pkg/controller/cronjob"
"k8s.io/kubernetes/pkg/controller/job"
"k8s.io/kubernetes/test/integration/framework"
)
func
setup
(
t
*
testing
.
T
)
(
*
httptest
.
Server
,
framework
.
CloseFunc
,
*
cronjob
.
CronJobController
,
*
job
.
JobController
,
informers
.
SharedInformerFactory
,
clientset
.
Interface
,
rest
.
Config
)
{
masterConfig
:=
framework
.
NewIntegrationTestMasterConfig
()
_
,
server
,
closeFn
:=
framework
.
RunAMaster
(
masterConfig
)
config
:=
restclient
.
Config
{
Host
:
server
.
URL
}
clientSet
,
err
:=
clientset
.
NewForConfig
(
&
config
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error creating clientset: %v"
,
err
)
}
resyncPeriod
:=
12
*
time
.
Hour
informerSet
:=
informers
.
NewSharedInformerFactory
(
clientset
.
NewForConfigOrDie
(
restclient
.
AddUserAgent
(
&
config
,
"cronjob-informers"
)),
resyncPeriod
)
cjc
,
err
:=
cronjob
.
NewCronJobController
(
clientSet
)
if
err
!=
nil
{
t
.
Fatalf
(
"Error creating CronJob controller: %v"
,
err
)
}
jc
:=
job
.
NewJobController
(
informerSet
.
Core
()
.
V1
()
.
Pods
(),
informerSet
.
Batch
()
.
V1
()
.
Jobs
(),
clientSet
)
return
server
,
closeFn
,
cjc
,
jc
,
informerSet
,
clientSet
,
config
}
func
newCronJob
(
name
,
namespace
,
schedule
string
)
*
batchv1beta1
.
CronJob
{
zero64
:=
int64
(
0
)
zero32
:=
int32
(
0
)
return
&
batchv1beta1
.
CronJob
{
TypeMeta
:
metav1
.
TypeMeta
{
Kind
:
"CronJob"
,
APIVersion
:
"batch/v1beta1"
,
},
ObjectMeta
:
metav1
.
ObjectMeta
{
Namespace
:
namespace
,
Name
:
name
,
},
Spec
:
batchv1beta1
.
CronJobSpec
{
Schedule
:
schedule
,
SuccessfulJobsHistoryLimit
:
&
zero32
,
JobTemplate
:
batchv1beta1
.
JobTemplateSpec
{
Spec
:
batchv1
.
JobSpec
{
Template
:
corev1
.
PodTemplateSpec
{
Spec
:
corev1
.
PodSpec
{
Containers
:
[]
corev1
.
Container
{{
Name
:
"foo"
,
Image
:
"bar"
}},
TerminationGracePeriodSeconds
:
&
zero64
,
RestartPolicy
:
"Never"
,
},
},
},
},
},
}
}
func
cleanupCronJobs
(
t
*
testing
.
T
,
cjClient
clientbatchv1beta1
.
CronJobInterface
,
name
string
)
{
deletePropagation
:=
metav1
.
DeletePropagationForeground
err
:=
cjClient
.
Delete
(
name
,
&
metav1
.
DeleteOptions
{
PropagationPolicy
:
&
deletePropagation
})
if
err
!=
nil
{
t
.
Errorf
(
"Failed to delete CronJob: %v"
,
err
)
}
}
func
validateJobAndPod
(
t
*
testing
.
T
,
clientSet
kubernetes
.
Interface
,
namespace
string
)
{
if
err
:=
wait
.
PollImmediate
(
1
*
time
.
Second
,
120
*
time
.
Second
,
func
()
(
bool
,
error
)
{
jobs
,
err
:=
clientSet
.
BatchV1
()
.
Jobs
(
namespace
)
.
List
(
metav1
.
ListOptions
{})
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to list jobs: %v"
,
err
)
}
if
len
(
jobs
.
Items
)
==
0
{
return
false
,
nil
}
for
_
,
j
:=
range
jobs
.
Items
{
ownerReferences
:=
j
.
ObjectMeta
.
OwnerReferences
if
refCount
:=
len
(
ownerReferences
);
refCount
!=
1
{
return
false
,
fmt
.
Errorf
(
"job %s has %d OwnerReferences, expected only 1"
,
j
.
Name
,
refCount
)
}
reference
:=
ownerReferences
[
0
]
if
reference
.
Kind
!=
"CronJob"
{
return
false
,
fmt
.
Errorf
(
"job %s has OwnerReference with Kind %s, expected CronJob"
,
j
.
Name
,
reference
.
Kind
)
}
}
pods
,
err
:=
clientSet
.
CoreV1
()
.
Pods
(
namespace
)
.
List
(
metav1
.
ListOptions
{})
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to list pods: %v"
,
err
)
}
if
len
(
pods
.
Items
)
!=
1
{
return
false
,
nil
}
for
_
,
pod
:=
range
pods
.
Items
{
ownerReferences
:=
pod
.
ObjectMeta
.
OwnerReferences
if
refCount
:=
len
(
ownerReferences
);
refCount
!=
1
{
return
false
,
fmt
.
Errorf
(
"pod %s has %d OwnerReferences, expected only 1"
,
pod
.
Name
,
refCount
)
}
reference
:=
ownerReferences
[
0
]
if
reference
.
Kind
!=
"Job"
{
return
false
,
fmt
.
Errorf
(
"pod %s has OwnerReference with Kind %s, expected Job"
,
pod
.
Name
,
reference
.
Kind
)
}
}
return
true
,
nil
});
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
func
TestCronJobLaunchesPodAndCleansUp
(
t
*
testing
.
T
)
{
server
,
closeFn
,
cjc
,
jc
,
informerSet
,
clientSet
,
_
:=
setup
(
t
)
defer
closeFn
()
cronJobName
:=
"foo"
namespaceName
:=
"simple-cronjob-test"
ns
:=
framework
.
CreateTestingNamespace
(
namespaceName
,
server
,
t
)
defer
framework
.
DeleteTestingNamespace
(
ns
,
server
,
t
)
cjClient
:=
clientSet
.
BatchV1beta1
()
.
CronJobs
(
ns
.
Name
)
stopCh
:=
make
(
chan
struct
{})
defer
close
(
stopCh
)
informerSet
.
Start
(
stopCh
)
go
cjc
.
Run
(
stopCh
)
go
jc
.
Run
(
1
,
stopCh
)
_
,
err
:=
cjClient
.
Create
(
newCronJob
(
cronJobName
,
ns
.
Name
,
"* * * * ?"
))
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to create CronJob: %v"
,
err
)
}
defer
cleanupCronJobs
(
t
,
cjClient
,
cronJobName
)
validateJobAndPod
(
t
,
clientSet
,
namespaceName
)
}
test/integration/cronjob/main_test.go
0 → 100644
View file @
b223e880
/*
Copyright 2018 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
cronjob
import
(
"k8s.io/kubernetes/test/integration/framework"
"testing"
)
func
TestMain
(
m
*
testing
.
M
)
{
framework
.
EtcdMain
(
m
.
Run
)
}
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