#54 Improve business logic
This commit is contained in:
		
							parent
							
								
									78b4a2d952
								
							
						
					
					
						commit
						4d2cab5109
					
				|  | @ -47,13 +47,6 @@ func (g *Groovy) EnsureSingle(source, name, hash, groovyScript string) (requeue | ||||||
| 		return false, nil | 		return false, nil | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	for i, ags := range g.jenkins.Status.AppliedGroovyScripts { |  | ||||||
| 		if ags.ConfigurationType == g.configurationType && ags.Name == name && ags.Source == source { |  | ||||||
| 			g.jenkins.Status.AppliedGroovyScripts = append(g.jenkins.Status.AppliedGroovyScripts[:i], |  | ||||||
| 				g.jenkins.Status.AppliedGroovyScripts[i+1:]...) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	logs, err := g.jenkinsClient.ExecuteScript(groovyScript) | 	logs, err := g.jenkinsClient.ExecuteScript(groovyScript) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		if _, ok := err.(*jenkinsclient.GroovyScriptExecutionFailed); ok { | 		if _, ok := err.(*jenkinsclient.GroovyScriptExecutionFailed); ok { | ||||||
|  | @ -62,12 +55,22 @@ func (g *Groovy) EnsureSingle(source, name, hash, groovyScript string) (requeue | ||||||
| 		return true, err | 		return true, err | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	g.jenkins.Status.AppliedGroovyScripts = append(g.jenkins.Status.AppliedGroovyScripts, v1alpha2.AppliedGroovyScript{ | 	var appliedGroovyScripts []v1alpha2.AppliedGroovyScript | ||||||
|  | 
 | ||||||
|  | 	for _, ags := range g.jenkins.Status.AppliedGroovyScripts { | ||||||
|  | 		if ags.ConfigurationType != g.configurationType && ags.Source != source && ags.Name != name { | ||||||
|  | 			appliedGroovyScripts = append(appliedGroovyScripts, ags) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	appliedGroovyScripts = append(appliedGroovyScripts, v1alpha2.AppliedGroovyScript{ | ||||||
| 		ConfigurationType: g.configurationType, | 		ConfigurationType: g.configurationType, | ||||||
| 		Source:            source, | 		Source:            source, | ||||||
| 		Name:              name, | 		Name:              name, | ||||||
| 		Hash:              hash, | 		Hash:              hash, | ||||||
| 	}) | 	}) | ||||||
|  | 
 | ||||||
|  | 	g.jenkins.Status.AppliedGroovyScripts = appliedGroovyScripts | ||||||
|  | 
 | ||||||
| 	return true, g.k8sClient.Update(context.TODO(), g.jenkins) | 	return true, g.k8sClient.Update(context.TODO(), g.jenkins) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -114,6 +114,7 @@ func TestGroovy_EnsureSingle(t *testing.T) { | ||||||
| 		assert.Equal(t, groovyScriptName, jenkins.Status.AppliedGroovyScripts[0].Name) | 		assert.Equal(t, groovyScriptName, jenkins.Status.AppliedGroovyScripts[0].Name) | ||||||
| 	}) | 	}) | ||||||
| 	t.Run("execute script with new version", func(t *testing.T) { | 	t.Run("execute script with new version", func(t *testing.T) { | ||||||
|  | 		anotherHash := "hash1" | ||||||
| 		// given
 | 		// given
 | ||||||
| 		jenkins := &v1alpha2.Jenkins{ | 		jenkins := &v1alpha2.Jenkins{ | ||||||
| 			ObjectMeta: metav1.ObjectMeta{ | 			ObjectMeta: metav1.ObjectMeta{ | ||||||
|  | @ -137,11 +138,11 @@ func TestGroovy_EnsureSingle(t *testing.T) { | ||||||
| 		groovyClient := New(jenkinsClient, fakeClient, log.Log, jenkins, configurationType, emptyCustomization) | 		groovyClient := New(jenkinsClient, fakeClient, log.Log, jenkins, configurationType, emptyCustomization) | ||||||
| 
 | 
 | ||||||
| 		// when
 | 		// when
 | ||||||
| 		_, err = groovyClient.EnsureSingle(source, groovyScriptName, hash, groovyScript) | 		requeue, err := groovyClient.EnsureSingle(source, groovyScriptName, hash, groovyScript) | ||||||
| 
 | 
 | ||||||
| 		// then
 | 		// then
 | ||||||
| 		require.NoError(t, err) | 		require.NoError(t, err) | ||||||
| 
 | 		assert.True(t, requeue) | ||||||
| 		err = fakeClient.Get(ctx, types.NamespacedName{Name: jenkins.Name, Namespace: jenkins.Namespace}, jenkins) | 		err = fakeClient.Get(ctx, types.NamespacedName{Name: jenkins.Name, Namespace: jenkins.Namespace}, jenkins) | ||||||
| 		require.NoError(t, err) | 		require.NoError(t, err) | ||||||
| 		assert.Equal(t, 1, len(jenkins.Status.AppliedGroovyScripts)) | 		assert.Equal(t, 1, len(jenkins.Status.AppliedGroovyScripts)) | ||||||
|  | @ -151,14 +152,15 @@ func TestGroovy_EnsureSingle(t *testing.T) { | ||||||
| 		assert.Equal(t, groovyScriptName, jenkins.Status.AppliedGroovyScripts[0].Name) | 		assert.Equal(t, groovyScriptName, jenkins.Status.AppliedGroovyScripts[0].Name) | ||||||
| 
 | 
 | ||||||
| 		// Update with new hash
 | 		// Update with new hash
 | ||||||
| 		_, err = groovyClient.EnsureSingle(source, groovyScriptName, "hash1", groovyScript) | 		requeue, err = groovyClient.EnsureSingle(source, groovyScriptName, anotherHash, groovyScript) | ||||||
| 		require.NoError(t, err) | 		require.NoError(t, err) | ||||||
|  | 		assert.True(t, requeue) | ||||||
| 
 | 
 | ||||||
| 		err = fakeClient.Get(ctx, types.NamespacedName{Name: jenkins.Name, Namespace: jenkins.Namespace}, jenkins) | 		err = fakeClient.Get(ctx, types.NamespacedName{Name: jenkins.Name, Namespace: jenkins.Namespace}, jenkins) | ||||||
| 		require.NoError(t, err) | 		require.NoError(t, err) | ||||||
| 		assert.Equal(t, 1, len(jenkins.Status.AppliedGroovyScripts)) | 		assert.Equal(t, 1, len(jenkins.Status.AppliedGroovyScripts)) | ||||||
| 		assert.Equal(t, configurationType, jenkins.Status.AppliedGroovyScripts[0].ConfigurationType) | 		assert.Equal(t, configurationType, jenkins.Status.AppliedGroovyScripts[0].ConfigurationType) | ||||||
| 		assert.Equal(t, "hash1", jenkins.Status.AppliedGroovyScripts[0].Hash) | 		assert.Equal(t, anotherHash, jenkins.Status.AppliedGroovyScripts[0].Hash) | ||||||
| 		assert.Equal(t, source, jenkins.Status.AppliedGroovyScripts[0].Source) | 		assert.Equal(t, source, jenkins.Status.AppliedGroovyScripts[0].Source) | ||||||
| 		assert.Equal(t, groovyScriptName, jenkins.Status.AppliedGroovyScripts[0].Name) | 		assert.Equal(t, groovyScriptName, jenkins.Status.AppliedGroovyScripts[0].Name) | ||||||
| 	}) | 	}) | ||||||
|  |  | ||||||
|  | @ -6,18 +6,18 @@ import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | 	"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2" | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/groovy" |  | ||||||
| 	jenkinsclient "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/client" | 	jenkinsclient "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/client" | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base" | 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base" | ||||||
| 	"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/groovy" | ||||||
| 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/plugins" | 	"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/plugins" | ||||||
| 
 | 
 | ||||||
| 	corev1 "k8s.io/api/core/v1" |  | ||||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |  | ||||||
| 	"github.com/bndr/gojenkins" | 	"github.com/bndr/gojenkins" | ||||||
| 	"k8s.io/apimachinery/pkg/api/resource" |  | ||||||
| 	framework "github.com/operator-framework/operator-sdk/pkg/test" | 	framework "github.com/operator-framework/operator-sdk/pkg/test" | ||||||
| 	"github.com/stretchr/testify/assert" | 	"github.com/stretchr/testify/assert" | ||||||
|  | 	corev1 "k8s.io/api/core/v1" | ||||||
|  | 	"k8s.io/apimachinery/pkg/api/resource" | ||||||
|  | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func TestConfiguration(t *testing.T) { | func TestConfiguration(t *testing.T) { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue