Fix tests

This commit is contained in:
Jakub Al-Khalili 2019-08-23 11:27:24 +02:00
parent f1815e10aa
commit ab1d3fb59b
5 changed files with 39 additions and 22 deletions

View File

@ -14,7 +14,7 @@ import (
var ( var (
errorNotFound = errors.New("404") errorNotFound = errors.New("404")
regex = regexp.MustCompile("(<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>)(?P<secret>[a-z0-9]*)") regex = regexp.MustCompile("(<application-desc main-class=\"hudson.remoting.jnlp.Main\"><argument>)(?P<secret>[a-z0-9]*)")
) )
// Jenkins defines Jenkins API // Jenkins defines Jenkins API

View File

@ -168,6 +168,7 @@ import jenkins.model.GlobalConfiguration
GlobalConfiguration.all().get(GlobalJobDslSecurityConfiguration.class).useScriptSecurity=false GlobalConfiguration.all().get(GlobalJobDslSecurityConfiguration.class).useScriptSecurity=false
GlobalConfiguration.all().get(GlobalJobDslSecurityConfiguration.class).save() GlobalConfiguration.all().get(GlobalJobDslSecurityConfiguration.class).save()
` `
// GetBaseConfigurationConfigMapName returns name of Kubernetes config map used to base configuration // GetBaseConfigurationConfigMapName returns name of Kubernetes config map used to base configuration
func GetBaseConfigurationConfigMapName(jenkins *v1alpha2.Jenkins) string { func GetBaseConfigurationConfigMapName(jenkins *v1alpha2.Jenkins) string {
return fmt.Sprintf("%s-base-configuration-%s", constants.OperatorName, jenkins.ObjectMeta.Name) return fmt.Sprintf("%s-base-configuration-%s", constants.OperatorName, jenkins.ObjectMeta.Name)

View File

@ -231,9 +231,9 @@ func NewJenkinsMasterContainer(jenkins *v1alpha2.Jenkins) corev1.Container {
}, },
}, },
SecurityContext: jenkinsContainer.SecurityContext, SecurityContext: jenkinsContainer.SecurityContext,
Env: envs, Env: envs,
Resources: jenkinsContainer.Resources, Resources: jenkinsContainer.Resources,
VolumeMounts: append(GetJenkinsMasterContainerBaseVolumeMounts(jenkins), jenkinsContainer.VolumeMounts...), VolumeMounts: append(GetJenkinsMasterContainerBaseVolumeMounts(jenkins), jenkinsContainer.VolumeMounts...),
} }
} }

View File

@ -8,21 +8,21 @@ import (
"reflect" "reflect"
"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" "github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
jenkinsclient "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/client"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base/resources" "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base/resources"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants" "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/constants"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/jobs" "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/jobs"
"github.com/jenkinsci/kubernetes-operator/pkg/log" "github.com/jenkinsci/kubernetes-operator/pkg/log"
jenkinsclient "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/client"
"github.com/go-logr/logr" "github.com/go-logr/logr"
stackerr "github.com/pkg/errors" stackerr "github.com/pkg/errors"
apps "k8s.io/api/apps/v1"
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
k8s "sigs.k8s.io/controller-runtime/pkg/client"
apps "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
k8s "sigs.k8s.io/controller-runtime/pkg/client"
) )
const ( const (
@ -47,8 +47,10 @@ const (
// credential type // credential type
JenkinsCredentialTypeLabelName = "jenkins.io/credentials-type" JenkinsCredentialTypeLabelName = "jenkins.io/credentials-type"
// AgentName is the name of seed job // AgentName is the name of seed job agent
AgentName = "seed-job-agent" AgentName = "seed-job-agent"
// AgentNamespace is the namespace of seed job agent
AgentNamespace = "default" AgentNamespace = "default"
) )
@ -105,7 +107,7 @@ func (s *SeedJobs) EnsureSeedJobs(jenkins *v1alpha2.Jenkins) (done bool, err err
err := s.k8sClient.Delete(context.TODO(), &appsv1.Deployment{ err := s.k8sClient.Delete(context.TODO(), &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Namespace: AgentNamespace, Namespace: AgentNamespace,
Name: fmt.Sprintf("%s-deployment", AgentName), Name: fmt.Sprintf("%s-deployment", AgentName),
}, },
}) })
@ -259,6 +261,7 @@ func (s *SeedJobs) isRecreatePodNeeded(jenkins v1alpha2.Jenkins) bool {
return false return false
} }
// CreateAgent deploys Jenkins agent to Kubernetes cluster
func CreateAgent(jenkinsClient jenkinsclient.Jenkins, k8sClient client.Client, jenkinsManifest *v1alpha2.Jenkins, namespace string, agentName string) error { func CreateAgent(jenkinsClient jenkinsclient.Jenkins, k8sClient client.Client, jenkinsManifest *v1alpha2.Jenkins, namespace string, agentName string) error {
var exists bool var exists bool
@ -291,7 +294,7 @@ func CreateAgent(jenkinsClient jenkinsclient.Jenkins, k8sClient client.Client, j
} }
deployment := agentDeployment(jenkinsManifest, namespace, agentName, secret) deployment := agentDeployment(jenkinsManifest, namespace, agentName, secret)
err = k8sClient.List(context.TODO(), &client.ListOptions{}, deployments) err = k8sClient.List(context.TODO(), &client.ListOptions{}, deployments)
if err != nil { if err != nil {
return err return err
} }
@ -317,8 +320,8 @@ func CreateAgent(jenkinsClient jenkinsclient.Jenkins, k8sClient client.Client, j
func agentDeployment(jenkinsManifest *v1alpha2.Jenkins, namespace string, agentName string, secret string) *apps.Deployment { func agentDeployment(jenkinsManifest *v1alpha2.Jenkins, namespace string, agentName string, secret string) *apps.Deployment {
return &apps.Deployment{ return &apps.Deployment{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-deployment", agentName), Name: fmt.Sprintf("%s-deployment", agentName),
Namespace: namespace, Namespace: namespace,
}, },
Spec: apps.DeploymentSpec{ Spec: apps.DeploymentSpec{
Template: corev1.PodTemplateSpec{ Template: corev1.PodTemplateSpec{
@ -336,11 +339,11 @@ func agentDeployment(jenkinsManifest *v1alpha2.Jenkins, namespace string, agentN
jenkinsManifest.Spec.SlaveService.Port), jenkinsManifest.Spec.SlaveService.Port),
}, },
{ {
Name: "JENKINS_SECRET", Name: "JENKINS_SECRET",
Value: secret, Value: secret,
}, },
{ {
Name: "JENKINS_AGENT_NAME", Name: "JENKINS_AGENT_NAME",
Value: agentName, Value: agentName,
}, },
{ {
@ -352,7 +355,7 @@ func agentDeployment(jenkinsManifest *v1alpha2.Jenkins, namespace string, agentN
), ),
}, },
{ {
Name: "JENKINS_AGENT_WORKDIR", Name: "JENKINS_AGENT_WORKDIR",
Value: "/home/jenkins/agent", Value: "/home/jenkins/agent",
}, },
}, },
@ -470,7 +473,7 @@ jobRef.getBuildersList().add(executeDslScripts)
jobRef.setDisplayName(&quot;${params.` + displayNameParameterName + `}&quot;) jobRef.setDisplayName(&quot;${params.` + displayNameParameterName + `}&quot;)
jobRef.setScm(scm) jobRef.setScm(scm)
// TODO don't use master executors // TODO don't use master executors
jobRef.setAssignedLabel(new LabelAtom(&quot;`+AgentName+`&quot;)) jobRef.setAssignedLabel(new LabelAtom(&quot;` + AgentName + `&quot;))
jenkins.getQueue().schedule(jobRef) jenkins.getQueue().schedule(jobRef)
</script> </script>

View File

@ -39,6 +39,19 @@ func TestEnsureSeedJobs(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
buildNumber := int64(1) buildNumber := int64(1)
agentName := "seed-job-agent"
secret := "test-secret"
testNode := &gojenkins.Node{
Raw: &gojenkins.NodeResponse{
DisplayName: agentName,
},
}
jenkinsClient.EXPECT().GetNodeSecret(agentName).Return(secret, nil)
jenkinsClient.EXPECT().GetAllNodes().Return([]*gojenkins.Node{}, nil)
jenkinsClient.EXPECT().CreateNode(agentName, 1, "The jenkins-operator generated agent", "/home/jenkins", agentName).Return(testNode, nil)
jenkinsClient.EXPECT().GetNode(agentName).Return(testNode, nil).AnyTimes()
for reconcileAttempt := 1; reconcileAttempt <= 2; reconcileAttempt++ { for reconcileAttempt := 1; reconcileAttempt <= 2; reconcileAttempt++ {
logger.Info(fmt.Sprintf("Reconcile attempt #%d", reconcileAttempt)) logger.Info(fmt.Sprintf("Reconcile attempt #%d", reconcileAttempt))
@ -235,7 +248,7 @@ func TestCreateAgent(t *testing.T) {
jenkinsCustomRes := jenkinsCustomResource() jenkinsCustomRes := jenkinsCustomResource()
testNode := &gojenkins.Node{ testNode := &gojenkins.Node{
Raw: &gojenkins.NodeResponse{ Raw: &gojenkins.NodeResponse{
DisplayName: agentName, DisplayName: agentName,
}, },
} }
@ -262,7 +275,7 @@ func TestCreateAgent(t *testing.T) {
assert.Equal(t, node.Raw.DisplayName, testNode.Raw.DisplayName) assert.Equal(t, node.Raw.DisplayName, testNode.Raw.DisplayName)
}) })
t.Run("not fail when deployment is available", func(t *testing.T) { t.Run("not fail when deployment is available", func(t *testing.T) {
// given // given
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
@ -285,8 +298,8 @@ func TestCreateAgent(t *testing.T) {
// when // when
err = fakeClient.Create(ctx, &appsv1.Deployment{ err = fakeClient.Create(ctx, &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-deployment", agentName), Name: fmt.Sprintf("%s-deployment", agentName),
Namespace:namespace, Namespace: namespace,
}, },
}) })
@ -296,4 +309,4 @@ func TestCreateAgent(t *testing.T) {
err = CreateAgent(jenkinsClient, fakeClient, jenkinsCustomResource(), namespace, agentName) err = CreateAgent(jenkinsClient, fakeClient, jenkinsCustomResource(), namespace, agentName)
assert.NoError(t, err) assert.NoError(t, err)
}) })
} }