Skip to content

Commit

Permalink
Merge pull request #5 from aufi/analysis-custom-rules
Browse files Browse the repository at this point in the history
Add hazelcast ruleset to petclinic analysis
  • Loading branch information
aufi committed Jun 27, 2023
2 parents e58177c + 86f6579 commit 770d2be
Show file tree
Hide file tree
Showing 11 changed files with 882 additions and 105 deletions.
147 changes: 89 additions & 58 deletions analysis/analysis_test.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,119 @@
package analysis

import (
"encoding/json"
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/konveyor/go-konveyor-tests/utils/uniq"
"github.com/konveyor/go-konveyor-tests/hack/addon"
"github.com/konveyor/go-konveyor-tests/hack/uniq"
"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/test/assert"
)

//
// Test application analysis
// "Basic" means that there no other dependencies than the application itself (no need prepare credentials, proxy, etc)
func TestApplicationAnalysis(t *testing.T) {
for _, analyzerAddon := range Addons {
for _, testcase := range TestCases {
t.Run(fmt.Sprintf("%s_%s", testcase.Name, analyzerAddon), func(t *testing.T) {
// Prepare parallel execution if env variable PARALLEL is set.
tc := testcase
_, parallel := os.LookupEnv("PARALLEL")
if parallel {
t.Parallel()
}

// Test using "richclient" methods (preffered way).
for _, testcase := range TestCases {

t.Run(testcase.Name, func(t *testing.T) {
// Prepare parallel execution if env variable PARALLEL is set.
tc := testcase
_, parallel := os.LookupEnv("PARALLEL")
if parallel {
t.Parallel()
}
// Create the application.
uniq.ApplicationName(&tc.Application)
assert.Should(t, RichClient.Application.Create(&tc.Application))

// Create the application.
uniq.ApplicationName(&tc.Application)
assert.Should(t, RichClient.Application.Create(&tc.Application))
// Prepare custom rules.
for i := range tc.CustomRules {
r := &tc.CustomRules[i]
uniq.RuleSetName(r)
// ruleFiles := []api.File{}
rules := []api.Rule{}
for _, rule := range r.Rules {
ruleFile, err := RichClient.File.Put(rule.File.Name)
assert.Should(t, err)
rules = append(rules, api.Rule{
File: &api.Ref{
ID: ruleFile.ID,
},
})
// ruleFiles = append(ruleFiles, *ruleFile)
}
r.Rules = rules
assert.Should(t, RichClient.RuleSet.Create(r))
}

// Prepare and submit the analyze task.
json.Unmarshal([]byte(tc.TaskData), &tc.Task.Data)
tc.Task.Application = &api.Ref{ID: tc.Application.ID}
assert.Should(t, RichClient.Task.Create(&tc.Task))
// Prepare and submit the analyze task.
tc.Task.Addon = analyzerAddon
tc.Task.Application = &api.Ref{ID: tc.Application.ID}
taskData := tc.Task.Data.(addon.Data)
for _, r := range tc.CustomRules {
taskData.Rules.RuleSets = append(taskData.Rules.RuleSets, api.Ref{ID: r.ID, Name: r.Name})
}
tc.Task.Data = taskData
assert.Should(t, RichClient.Task.Create(&tc.Task))

// Wait until task finishes
var task *api.Task
var err error
for i := 0; i < Retry; i++ {
task, err = RichClient.Task.Get(tc.Task.ID)
if err != nil || task.State == "Succeeded" || task.State == "Failed" {
break
// Wait until task finishes
var task *api.Task
var err error
for i := 0; i < Retry; i++ {
task, err = RichClient.Task.Get(tc.Task.ID)
if err != nil || task.State == "Succeeded" || task.State == "Failed" {
break
}
time.Sleep(Wait)
}
time.Sleep(Wait)
}

if task.State != "Succeeded" {
t.Errorf("Analyze Task failed. Details: %+v", task)
}
if task.State != "Succeeded" {
t.Errorf("Analyze Task failed. Details: %+v", task)
}

// Check the report content.
for path, expectedElems := range tc.ReportContent {
content := getReportText(t, &tc, path)
// Check its content.
for _, expectedContent := range expectedElems {
if !strings.Contains(content, expectedContent) {
t.Errorf("Error report contect check for %s. Cannot find %s in %s", path, expectedContent, content)
// Check the report content.
for path, expectedElems := range tc.ReportContent {
content := getReportText(t, &tc, path)
// Check its content.
for _, expectedContent := range expectedElems {
if !strings.Contains(content, expectedContent) {
t.Errorf("Error report contect check for %s. Cannot find %s in %s", path, expectedContent, content)
}
}
}
}

// Check analysis-created Tags.
gotApp, _ := RichClient.Application.Get(tc.Application.ID)
found, gotAnalysisTags := 0, 0
for _, t := range gotApp.Tags {
if t.Source == "Analysis" {
gotAnalysisTags = gotAnalysisTags + 1
for _, expectedTag := range tc.AnalysisTags {
if expectedTag.Name == t.Name {
found = found + 1
break
// Check analysis-created Tags.
gotApp, _ := RichClient.Application.Get(tc.Application.ID)
found, gotAnalysisTags := 0, 0
for _, t := range gotApp.Tags {
if t.Source == "Analysis" {
gotAnalysisTags = gotAnalysisTags + 1
for _, expectedTag := range tc.AnalysisTags {
if expectedTag.Name == t.Name {
found = found + 1
break
}
}
}
}
}
if found != len(tc.AnalysisTags) || found < gotAnalysisTags {
t.Errorf("Analysis Tags don't match. Got:\n %v\nexpected:\n %v\n", gotApp.Tags, tc.AnalysisTags)
}
if found != len(tc.AnalysisTags) || found < gotAnalysisTags {
t.Errorf("Analysis Tags don't match. Got:\n %v\nexpected:\n %v\n", gotApp.Tags, tc.AnalysisTags)
}

// Cleanup.
assert.Must(t, RichClient.Application.Delete(tc.Application.ID))
})
// Cleanup Application.
assert.Must(t, RichClient.Application.Delete(tc.Application.ID))

// Cleanup custom rules and their files.
for _, r := range tc.CustomRules {
assert.Should(t, RichClient.RuleSet.Delete(r.ID))
for _, rl := range r.Rules {
assert.Should(t, RichClient.File.Delete(rl.File.ID))
}
}
})
}
}
}
68 changes: 68 additions & 0 deletions analysis/data/hz.windup.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0"?>
<ruleset id="HZRules"
xmlns="http://windup.jboss.org/schema/jboss-ruleset"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://windup.jboss.org/schema/jboss-ruleset http://windup.jboss.org/schema/jboss-ruleset/windup-jboss-ruleset.xsd">

<metadata>
<description>
This ruleset detects embedded hazelcast, which may be problematic
when migrating an application to a cloud environment.
</description>
<dependencies>
<addon id="org.jboss.windup.rules,windup-rules-javaee,3.0.0.Final" />
<addon id="org.jboss.windup.rules,windup-rules-java,3.0.0.Final" />
</dependencies>

<targetTechnology id="cloud-readiness" />

<tag>Hazelcast</tag>
</metadata>
<rules>
<rule id="hazelcast-cloud-readiness-hz001">
<when>
<or>
<javaclass
references="com.hazelcast.config.JoinConfig.getMulticastConfig({*})">
<location>METHOD_CALL</location>
</javaclass>
<javaclass
references="com.hazelcast.config.JoinConfig.getTcpIpConfig({*})">
<location>METHOD_CALL</location>
</javaclass>
</or>
</when>
<perform>
<hint title="Embedded Hazelcast"
category-id="cloud-mandatory" effort="3">
<message> Consider using Kubernetes specific configuration.
<![CDATA[
// Example using Kubernetes specific configuration
JoinConfig joinConfig = config.getNetworkConfig().getJoin();
config.getKubernetesConfig().setEnabled(true)
.setProperty("namespace", "namespace")
.setProperty("service-name", "hazelcast-service");
]]>
</message>
</hint>
</perform>
</rule>

<rule id="hazelcast-cloud-readiness-hz002">
<when>
<project>
<artifact groupId="com.hazelcast" artifactId="hazelcast"
fromVersion="2.0.0" toVersion="4.2.7" />
</project>
</when>
<perform>
<hint title="Embedded Hazelcast dependencies"
category-id="cloud-mandatory" effort="1">
<message>The project uses hazelcast with the version between 2.0.0 and less than 5.0.0. Please use hazelcast 5.0 or above. </message>T
</hint>

</perform>
</rule>
</rules>
</ruleset>
8 changes: 6 additions & 2 deletions analysis/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ func init() {
RichClient = client.PrepareRichClient()

// Access REST client directly (some test API call need it)
Client = RichClient.Client()
Client = RichClient.Client
}

// Test cases for Application Analysis.
type TC struct {
Name string
Application api.Application
// Application and other test data declaration.
Application api.Application // Required.
CustomRules []api.RuleSet
// Analysis parameters.
Task api.Task
TaskData string
// After-analysis assertions.
ReportContent map[string][]string
AnalysisTags []api.Tag
}
Expand Down
Loading

0 comments on commit 770d2be

Please sign in to comment.