From 989078d9cc45624df23a10ce71185a5d2e0cec38 Mon Sep 17 00:00:00 2001 From: Ricardo Zanini Date: Thu, 15 Feb 2024 17:23:17 -0300 Subject: [PATCH 1/2] KIE Tools #2160 - Fix resources generation prefix order Signed-off-by: Ricardo Zanini --- workflowproj/io.go | 4 ++-- workflowproj/workflowproj.go | 17 ++++++++++------- workflowproj/workflowproj_test.go | 11 ++++++++++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/workflowproj/io.go b/workflowproj/io.go index 39b637076..152d37bee 100644 --- a/workflowproj/io.go +++ b/workflowproj/io.go @@ -42,11 +42,11 @@ func ensurePath(path string) error { return nil } -func saveAsKubernetesManifest(object client.Object, savePath, prefix string) error { +func saveAsKubernetesManifest(object client.Object, savePath string, prefix int) error { if reflect.ValueOf(object).IsNil() { return nil } - filename := strings.ToLower(fmt.Sprintf("%s%s_%s%s", + filename := strings.ToLower(fmt.Sprintf("%02d-%s_%s%s", prefix, object.GetObjectKind().GroupVersionKind().Kind, object.GetName(), diff --git a/workflowproj/workflowproj.go b/workflowproj/workflowproj.go index afabd7e5c..30d9db74c 100644 --- a/workflowproj/workflowproj.go +++ b/workflowproj/workflowproj.go @@ -153,18 +153,21 @@ func (w *workflowProjectHandler) SaveAsKubernetesManifests(path string) error { if err := w.parseRawProject(); err != nil { return err } - fileCount := 1 - if err := saveAsKubernetesManifest(w.project.Workflow, path, fmt.Sprintf("%02d-", 1)); err != nil { - return err + fileCount := 0 + if w.project.Properties != nil { + fileCount++ + if err := saveAsKubernetesManifest(w.project.Properties, path, fileCount); err != nil { + return err + } } - for i, r := range w.project.Resources { - fileCount = i + 1 - if err := saveAsKubernetesManifest(r, path, fmt.Sprintf("%02d-", fileCount)); err != nil { + for _, r := range w.project.Resources { + fileCount++ + if err := saveAsKubernetesManifest(r, path, fileCount); err != nil { return err } } fileCount++ - if err := saveAsKubernetesManifest(w.project.Properties, path, fmt.Sprintf("%02d-", fileCount)); err != nil { + if err := saveAsKubernetesManifest(w.project.Workflow, path, fileCount); err != nil { return err } return nil diff --git a/workflowproj/workflowproj_test.go b/workflowproj/workflowproj_test.go index c1207527f..41e7cea46 100644 --- a/workflowproj/workflowproj_test.go +++ b/workflowproj/workflowproj_test.go @@ -140,8 +140,14 @@ func Test_Handler_WorklflowServiceAndPropsAndSpec_SaveAs(t *testing.T) { assert.NoError(t, err) assert.Len(t, files, 4) - for _, f := range files { + sort.Slice(files, func(i, j int) bool { + return files[i].Name() < files[j].Name() + }) + + for fileCount, f := range files { if strings.HasSuffix(f.Name(), yamlFileExt) { + prefix := fmt.Sprintf("%02d-", fileCount+1) + assert.True(t, strings.HasPrefix(f.Name(), prefix)) contents, err := os.ReadFile(path.Join(tmpPath, f.Name())) assert.NoError(t, err) decode := scheme.Codecs.UniversalDeserializer().Decode @@ -170,6 +176,9 @@ func Test_Handler_WorkflowService_SaveAs(t *testing.T) { for _, f := range files { if strings.HasSuffix(f.Name(), yamlFileExt) { + // we have only one file produced in these test cases + prefix := fmt.Sprintf("%02d-", 1) + assert.True(t, strings.HasPrefix(f.Name(), prefix)) contents, err := os.ReadFile(path.Join(tmpPath, f.Name())) assert.NoError(t, err) decode := scheme.Codecs.UniversalDeserializer().Decode From a55de3059e68526f46e43896a8d29ecc18f052d4 Mon Sep 17 00:00:00 2001 From: Walter Medvedeo Date: Fri, 16 Feb 2024 11:36:02 +0100 Subject: [PATCH 2/2] Tests improvements --- workflowproj/workflowproj_test.go | 43 ++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/workflowproj/workflowproj_test.go b/workflowproj/workflowproj_test.go index 41e7cea46..9135e488d 100644 --- a/workflowproj/workflowproj_test.go +++ b/workflowproj/workflowproj_test.go @@ -28,6 +28,8 @@ import ( "strings" "testing" + "k8s.io/apimachinery/pkg/runtime/schema" + "github.com/apache/incubator-kie-kogito-serverless-operator/api/metadata" "github.com/stretchr/testify/assert" "k8s.io/client-go/kubernetes/scheme" @@ -140,25 +142,36 @@ func Test_Handler_WorklflowServiceAndPropsAndSpec_SaveAs(t *testing.T) { assert.NoError(t, err) assert.Len(t, files, 4) - sort.Slice(files, func(i, j int) bool { - return files[i].Name() < files[j].Name() - }) + expectedFiles := []string{ + "01-configmap_service-props.yaml", + "02-configmap_01-service-resources.yaml", + "03-configmap_02-service-resources.yaml", + "04-sonataflow_service.yaml", + } + expectedKinds := []schema.GroupVersionKind{ + {Group: "", Version: "v1", Kind: "ConfigMap"}, + {Group: "", Version: "v1", Kind: "ConfigMap"}, + {Group: "", Version: "v1", Kind: "ConfigMap"}, + {Group: "sonataflow.org", Version: "v1alpha08", Kind: "SonataFlow"}, + } - for fileCount, f := range files { - if strings.HasSuffix(f.Name(), yamlFileExt) { - prefix := fmt.Sprintf("%02d-", fileCount+1) - assert.True(t, strings.HasPrefix(f.Name(), prefix)) - contents, err := os.ReadFile(path.Join(tmpPath, f.Name())) - assert.NoError(t, err) - decode := scheme.Codecs.UniversalDeserializer().Decode - k8sObj, _, err := decode(contents, nil, nil) - assert.NoError(t, err) - assert.NotNil(t, k8sObj) - assert.NotEmpty(t, k8sObj.GetObjectKind().GroupVersionKind().String()) - } + for i := 0; i < len(files); i++ { + assert.Equal(t, files[i].Name(), expectedFiles[i]) + assertIsK8sObject(t, tmpPath, files[i].Name(), expectedKinds[i]) } } +func assertIsK8sObject(t *testing.T, basePath string, fileName string, gvk schema.GroupVersionKind) { + contents, err := os.ReadFile(path.Join(basePath, fileName)) + assert.NoError(t, err) + decode := scheme.Codecs.UniversalDeserializer().Decode + k8sObj, _, err := decode(contents, nil, nil) + assert.NoError(t, err) + assert.NotNil(t, k8sObj) + assert.NotEmpty(t, k8sObj.GetObjectKind().GroupVersionKind().String()) + assert.Equal(t, gvk, k8sObj.GetObjectKind().GroupVersionKind()) +} + func Test_Handler_WorkflowService_SaveAs(t *testing.T) { testRun := func(t *testing.T, handler WorkflowProjectHandler) { proj, err := handler.AsObjects()