Skip to content

Commit

Permalink
refactor(test): seedjob tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
ansh-devs committed Jul 25, 2024
1 parent 8da241b commit 559d145
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.22.5

require (
github.com/bndr/gojenkins v1.1.0
github.com/docker/distribution v2.8.3+incompatible
github.com/distribution/reference v0.6.0
github.com/emersion/go-smtp v0.21.3
github.com/go-logr/logr v1.4.2
github.com/go-logr/zapr v1.3.0
Expand Down Expand Up @@ -36,7 +36,6 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBirtxJnzDrHLEKxTAYk=
github.com/docker/distribution v2.8.3+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 h1:hH4PQfOndHDlpzYfLAAfl63E8Le6F2+EL/cdhlkyRJY=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
Expand Down
1 change: 0 additions & 1 deletion pkg/client/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func Test_ExecuteScript(t *testing.T) {
Client: client,
BasicAuth: &gojenkins.BasicAuth{Username: "unused", Password: "unused"},
}

logs, err := jenkinsClient.executeScript(script, verifier)
assert.NoError(t, err, logs)
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/configuration/user/seedjobs/seedjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (s *seedJobs) EnsureSeedJobs(jenkins *v1alpha2.Jenkins) (done bool, err err
})

if err != nil && !apierrors.IsNotFound(err) {

return false, stackerr.WithStack(err)
}
}
Expand All @@ -236,6 +237,7 @@ func (s *seedJobs) EnsureSeedJobs(jenkins *v1alpha2.Jenkins) (done bool, err err

seedJobIDs := s.getAllSeedJobIDs(*jenkins)
if !reflect.DeepEqual(seedJobIDs, jenkins.Status.CreatedSeedJobs) {
// @ansh-devs fixed : calls to Update and Patch will not alter its status.
jenkins.Status.CreatedSeedJobs = seedJobIDs
return false, stackerr.WithStack(s.Client.Status().Update(context.TODO(), jenkins))
}
Expand Down Expand Up @@ -303,6 +305,7 @@ func (s *seedJobs) createJobs(jenkins *v1alpha2.Jenkins) (requeue bool, err erro
// Operator will able to watch any changes made to them
func (s *seedJobs) ensureLabelsForSecrets(jenkins v1alpha2.Jenkins) error {
for _, seedJob := range jenkins.Spec.SeedJobs {

if seedJob.JenkinsCredentialType == v1alpha2.BasicSSHCredentialType || seedJob.JenkinsCredentialType == v1alpha2.UsernamePasswordCredentialType {
requiredLabels := resources.BuildLabelsForWatchedResources(jenkins)
requiredLabels[JenkinsCredentialTypeLabelName] = string(seedJob.JenkinsCredentialType)
Expand Down
17 changes: 11 additions & 6 deletions pkg/configuration/user/seedjobs/seedjobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ func TestEnsureSeedJobs(t *testing.T) {
jenkins.Spec.SeedJobs = []v1alpha2.SeedJob{}

jenkinsClient := jenkinsclient.NewMockJenkins(ctrl)
fakeClient := fake.NewClientBuilder().Build()
// DONE: @ansh-devs fixed `jenkinses.jenkins` not found
fakeClient := fake.NewClientBuilder().
WithRuntimeObjects(jenkins).
WithStatusSubresource(jenkins).
Build()
err := v1alpha2.SchemeBuilder.AddToScheme(scheme.Scheme)
assert.NoError(t, err)

Expand All @@ -139,8 +143,8 @@ func TestEnsureSeedJobs(t *testing.T) {
Jenkins: jenkins,
}

jenkinsClient.EXPECT().GetNode(context.TODO(), AgentName).AnyTimes()
jenkinsClient.EXPECT().CreateNode(context.TODO(), AgentName, 1, "The jenkins-operator generated agent", "/home/jenkins", AgentName).AnyTimes()
jenkinsClient.EXPECT().GetNode(ctx, AgentName).AnyTimes()
jenkinsClient.EXPECT().CreateNode(ctx, AgentName, 1, "The jenkins-operator generated agent", "/home/jenkins", AgentName).AnyTimes()
jenkinsClient.EXPECT().GetNodeSecret(AgentName).Return(agentSecret, nil).AnyTimes()

seedJobsClient := New(jenkinsClient, config)
Expand All @@ -155,13 +159,14 @@ func TestEnsureSeedJobs(t *testing.T) {

// when
_, err = seedJobsClient.EnsureSeedJobs(jenkins)
// TODO @ansh-devs currently going on
// then
assert.NoError(t, err)

var deployment appsv1.Deployment
err = fakeClient.Get(ctx, types.NamespacedName{Namespace: jenkins.Namespace, Name: agentDeploymentName(*jenkins, AgentName)}, &deployment)

err = fakeClient.Get(ctx, types.NamespacedName{
Namespace: jenkins.Namespace,
Name: agentDeploymentName(*jenkins, AgentName),
}, &deployment)
assert.True(t, errors.IsNotFound(err), "Agent deployment hasn't been deleted")
})
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/groovy/groovy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (g *Groovy) EnsureSingle(source, name, hash, groovyScript string) (requeue

g.jenkins.Status.AppliedGroovyScripts = appliedGroovyScripts

return true, g.k8sClient.Status().Update(context.TODO(), g.jenkins)
return true, g.k8sClient.Update(context.TODO(), g.jenkins)
}

// WaitForSecretSynchronization runs groovy script which waits to synchronize secrets in pod by k8s
Expand Down Expand Up @@ -114,7 +114,10 @@ func (g *Groovy) WaitForSecretSynchronization(secretsPath string) (requeue bool,
func (g *Groovy) Ensure(filter func(name string) bool, updateGroovyScript func(groovyScript string) string) (requeue bool, err error) {
secret := &corev1.Secret{}
if len(g.customization.Secret.Name) > 0 {
err := g.k8sClient.Get(context.TODO(), types.NamespacedName{Name: g.customization.Secret.Name, Namespace: g.jenkins.ObjectMeta.Namespace}, secret)
err := g.k8sClient.Get(context.TODO(), types.NamespacedName{
Name: g.customization.Secret.Name,
Namespace: g.jenkins.ObjectMeta.Namespace,
}, secret)
if err != nil {
return true, err
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/notifications/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func (s SMTP) generateMessage(e event.Event) *gomail.Message {
message.SetHeader("To", s.config.SMTP.To)
message.SetHeader("Subject", mailSubject)
message.SetBody("text/html", htmlMessage)

return message
}

Expand All @@ -100,7 +99,6 @@ func (s SMTP) Send(e event.Event) error {
if err != nil {
return err
}

usernameSecretValue := string(usernameSecret.Data[usernameSelector.Key])
if usernameSecretValue == "" {
return errors.Errorf("SMTP username is empty in secret '%s/%s[%s]", e.Jenkins.Namespace, usernameSelector.Name, usernameSelector.Key)
Expand All @@ -110,15 +108,19 @@ func (s SMTP) Send(e event.Event) error {
if passwordSecretValue == "" {
return errors.Errorf("SMTP password is empty in secret '%s/%s[%s]", e.Jenkins.Namespace, passwordSelector.Name, passwordSelector.Key)
}

mailer := gomail.NewDialer(s.config.SMTP.Server, s.config.SMTP.Port, usernameSecretValue, passwordSecretValue)
mailer := gomail.NewDialer(
s.config.SMTP.Server,
s.config.SMTP.Port,
usernameSecretValue,
passwordSecretValue,
)
mailer.TLSConfig = &tls.Config{InsecureSkipVerify: s.config.SMTP.TLSInsecureSkipVerify}

message := s.generateMessage(e)

if err := mailer.DialAndSend(message); err != nil {
return err
}

return nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/notifications/smtp/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ func (s testSession) Data(r io.Reader) error {
if err != nil {
return err
}

content := contentRegex.FindAllStringSubmatch(string(b), -1)
headers := headersRegex.FindAllStringSubmatch(string(b), -1)
fmt.Println(content)
if len(content[0]) > 0 {

Check failure on line 107 in pkg/notifications/smtp/smtp_test.go

View workflow job for this annotation

GitHub Actions / E2E Verify code before tests

SA9003: empty branch (staticcheck)

Check failure on line 107 in pkg/notifications/smtp/smtp_test.go

View workflow job for this annotation

GitHub Actions / HELM Verify code before tests

SA9003: empty branch (staticcheck)

}
if s.event.Jenkins.Name == content[0][1] {
return fmt.Errorf("jenkins CR not identical: %s, expected: %s", content[0][1], s.event.Jenkins.Name)
} else if string(s.event.Phase) == content[1][1] {
Expand Down Expand Up @@ -172,7 +174,6 @@ func TestSMTP_Send(t *testing.T) {
}}

ts := &testServer{event: e}

// Create fake SMTP server
// be := *new(smtp.Backend)
s := smtp.NewServer(ts)
Expand Down Expand Up @@ -200,7 +201,6 @@ func TestSMTP_Send(t *testing.T) {

err := fakeClient.Create(context.TODO(), secret)
assert.NoError(t, err)

l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", testSMTPPort))
assert.NoError(t, err)

Expand Down

0 comments on commit 559d145

Please sign in to comment.