Skip to content

Commit

Permalink
[RFR] Ginkgo test: export applications as tasks to Jira (konveyor#56)
Browse files Browse the repository at this point in the history
* Adding new TC: export applications to Jira

Signed-off-by: Maayan Hadasi <mguetta@redhat.com>

* Using conf variable in utils

Signed-off-by: Maayan Hadasi <mguetta@redhat.com>

---------

Signed-off-by: Maayan Hadasi <mguetta@redhat.com>
  • Loading branch information
mguetta1 committed Jan 31, 2024
1 parent b460e31 commit 15c06d9
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 49 deletions.
8 changes: 8 additions & 0 deletions data/migrationwave_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package data

type ExportApplicationsCase struct {
JiraInstance JiraInstanceTC
NumOfApps int
TicketKind string
TicketParent string
}
53 changes: 10 additions & 43 deletions e2e/jiraintegration/jira_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,33 @@ package jiraintegration

import (
"strconv"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

. "github.com/konveyor/go-konveyor-tests/config"
. "github.com/konveyor/go-konveyor-tests/utils"

"github.com/konveyor/go-konveyor-tests/config"
"github.com/konveyor/go-konveyor-tests/data"
"github.com/konveyor/go-konveyor-tests/hack/uniq"
"github.com/konveyor/go-konveyor-tests/utils"
"github.com/konveyor/tackle2-hub/api"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Jira connection", func() {
var jiraIdentity api.Identity
var jiraInstance api.Tracker
var jiraInstance utils.Jira

AfterEach(func() {
// Resources cleanup
// Delete tracker instance and the associated identity after
if keep, _ := strconv.ParseBool(Config.KEEP); keep {
if keep, _ := strconv.ParseBool(config.Config.KEEP); keep {
return
}
Expect(Tracker.Delete(jiraInstance.ID)).To(Succeed())
Expect(Identity.Delete(jiraIdentity.ID)).To(Succeed())
Expect(utils.Tracker.Delete(jiraInstance.ID)).To(Succeed())
Expect(utils.Identity.Delete(jiraIdentity.ID)).To(Succeed())
})

DescribeTable("",
func(testCase data.JiraInstanceTC) {
By("Create credential")
jiraIdentity = testCase.Identity
uniq.IdentityName(&jiraIdentity)
err := Identity.Create(&jiraIdentity)
Expect(err).NotTo(HaveOccurred())

By("Create Jira instance and associate credential")
jiraInstance = api.Tracker{
URL: testCase.JiraUrl,
Kind: testCase.JiraKind,
Identity: api.Ref{
ID: jiraIdentity.ID,
Name: jiraIdentity.Name,
},
}
uniq.TrackerName(&jiraInstance)
err = Tracker.Create(&jiraInstance)
Expect(err).NotTo(HaveOccurred())

// Wait for connection succeeded
var jira *api.Tracker
var retry, _ = strconv.Atoi(Config.RETRY_NUM)
for i := 0; i < retry; i++ {
jira, err = Tracker.Get(jiraInstance.ID)
if err != nil || jira.Connected {
break
}
time.Sleep(5 * time.Second)
}
Expect(jira.Connected).To(BeTrue())

// Create Jira instance
jiraIdentity, jiraInstance = utils.CreateJiraInstance(testCase)
},
Entry("Jira cloud", data.JiraCloud),
Entry("Jira server with basic auth", data.JiraServer),
Expand Down
81 changes: 81 additions & 0 deletions e2e/migrationwave/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package migrationwave

import (
"strconv"
"time"

"github.com/konveyor/go-konveyor-tests/config"
"github.com/konveyor/go-konveyor-tests/data"
"github.com/konveyor/go-konveyor-tests/hack/uniq"
"github.com/konveyor/go-konveyor-tests/utils"
"github.com/konveyor/tackle2-hub/api"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var (
jiraInstance utils.Jira
jiraIdentity api.Identity
migrationWave api.MigrationWave
issueIds []string
appsToExport []api.Application
)

var _ = Describe("Export applications", func() {
AfterEach(func() {
// Resources cleanup
if keep, _ := strconv.ParseBool(config.Config.KEEP); keep {
return
}
Expect(utils.Tracker.Delete(jiraInstance.ID)).To(Succeed())
Expect(utils.Identity.Delete(jiraIdentity.ID)).To(Succeed())
Expect(utils.MigrationWave.Delete((migrationWave.ID))).To(Succeed()) /* Associated api.Ticket objects are removed as well */
jiraInstance.DeleteJiraIssues(issueIds)
utils.DeleteApplicationsBySlice(appsToExport)
})

DescribeTable("",
func(testCase data.ExportApplicationsCase) {
appsToExport = utils.CreateMultipleApplications(testCase.NumOfApps)
By("Create migration wave")
uniq.MigrationWaveName(&migrationWave)
err := utils.MigrationWave.Create(&migrationWave)
Expect(err).NotTo(HaveOccurred())

By("Manage applications")
for i := 0; i < len(appsToExport); i++ {
migrationWave.Applications = append(migrationWave.Applications, api.Ref{ID: appsToExport[i].ID, Name: appsToExport[i].Name})
}
err = utils.MigrationWave.Update(&migrationWave)
Expect(err).NotTo(HaveOccurred())

By("Create Jira instance")
jiraIdentity, jiraInstance = utils.CreateJiraInstance(testCase.JiraInstance)

By("Check ticket was created")
for i := 0; i < len(appsToExport); i++ {
ticket := api.Ticket{Kind: testCase.TicketKind, Parent: testCase.TicketParent, Application: api.Ref{ID: appsToExport[i].ID},
Tracker: api.Ref{ID: jiraInstance.ID}}
utils.Ticket.Create(&ticket)

// Wait for reference field to be populated
var got *api.Ticket
var retry, _ = strconv.Atoi(config.Config.RETRY_NUM)
for i := 0; i < retry; i++ {
got, err = utils.Ticket.Get(ticket.ID)
if err != nil || got.Reference != "" {
break
}
time.Sleep(5 * time.Second)
}
Expect(got.Reference).NotTo(BeEmpty())
issueIds = append(issueIds, got.Reference)
}

},
Entry("Export applications as a task", data.ExportApplicationsCase{
JiraInstance: data.JiraCloud,
NumOfApps: 3,
TicketKind: "10007", /* Task issuetypeId */
TicketParent: "10001" /* mta_integration projectId */}))
})
1 change: 1 addition & 0 deletions go_konveyor_tests_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

_ "github.com/konveyor/go-konveyor-tests/e2e/jiraintegration"
_ "github.com/konveyor/go-konveyor-tests/e2e/metrics"
_ "github.com/konveyor/go-konveyor-tests/e2e/migrationwave"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand Down
5 changes: 5 additions & 0 deletions hack/uniq/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ func IdentityName(r *api.Identity) {
func TrackerName(r *api.Tracker) {
r.Name = fmt.Sprint(r.Kind, "-", RandString(5))
}

// MigrationWaveName returns random MigrationWave Name
func MigrationWaveName(r *api.MigrationWave) {
r.Name = fmt.Sprint( "migrationwave-", RandString(5))
}
81 changes: 81 additions & 0 deletions utils/jira_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package utils

import (
"encoding/base64"
"net/http"
"strconv"
"time"

"github.com/konveyor/go-konveyor-tests/data"
"github.com/konveyor/go-konveyor-tests/hack/uniq"
"github.com/konveyor/tackle2-hub/api"
"github.com/onsi/gomega"
)

type Jira api.Tracker

func CreateJiraInstance(data data.JiraInstanceTC) (api.Identity, Jira) {
jiraIdentity := data.Identity
uniq.IdentityName(&jiraIdentity)
err := Identity.Create(&jiraIdentity)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

jiraInstance := Jira{
URL: data.JiraUrl,
Kind: data.JiraKind,
Identity: api.Ref{
ID: jiraIdentity.ID,
Name: jiraIdentity.Name,
},
}
uniq.TrackerName((*api.Tracker)(&jiraInstance))
err = Tracker.Create((*api.Tracker)(&jiraInstance))
gomega.Expect(err).NotTo(gomega.HaveOccurred())

// Wait for connection succeeded
var jira *api.Tracker
var retry, _ = strconv.Atoi(conf.RETRY_NUM)
for i := 0; i < retry; i++ {
jira, err = Tracker.Get(jiraInstance.ID)
if err != nil || jira.Connected {
break
}
time.Sleep(5 * time.Second)
}
gomega.Expect(jira.Connected).To(gomega.BeTrue())

return jiraIdentity, jiraInstance
}

func (r *Jira) DeleteJiraIssues(issues []string) {
for i := 0; i < len(issues); i++ {
url := conf.JIRA_CLOUD_URL + "/rest/api/3/issue/" + issues[i]
r.sendJiraRequest(url, "DELETE")
}
}

func (r *Jira) sendJiraRequest(url string, method string) {
// Create a basic authentication string
basicAuth := "Basic " +
base64.StdEncoding.EncodeToString([]byte(conf.JIRA_CLOUD_USERNAME+":"+conf.JIRA_CLOUD_PASSWORD))

// Create a bearer authentication string
bearerAuth := "Bearer " + conf.JIRA_CLOUD_PASSWORD

// Create a request
request, _ := http.NewRequest(method, url, nil)

// Set the authorization header
request.Header.Set("Authorization", basicAuth)
if r.Kind == "on-permise" {
request.Header.Set("Authorization", bearerAuth)
}

// Make the request
client := http.Client{}
response, err := client.Do(request)
gomega.Expect(err).NotTo(gomega.HaveOccurred())

// Ensure that the response body is closed when the function returns
defer response.Body.Close()
}
20 changes: 14 additions & 6 deletions utils/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ package utils

import (
"github.com/jortel/go-utils/logr"
"github.com/konveyor/go-konveyor-tests/config"
"github.com/konveyor/tackle2-hub/binding"
"github.com/konveyor/tackle2-hub/test/api/client"
)

var (
Client *binding.Client
RichClient *binding.RichClient
Application binding.Application
Tracker binding.Tracker
Identity binding.Identity
Log = logr.WithName("test")
conf config.Configuration
Log = logr.WithName("test")
Client *binding.Client
RichClient *binding.RichClient
Application binding.Application
Tracker binding.Tracker
Identity binding.Identity
MigrationWave binding.MigrationWave
Ticket binding.Ticket
)

func init() {
conf = config.Config

// Prepare RichClient and login to Hub API (configured from env variables).
RichClient = client.PrepareRichClient()

Expand All @@ -29,4 +35,6 @@ func init() {
Tracker = RichClient.Tracker

Identity = RichClient.Identity
MigrationWave = RichClient.MigrationWave
Ticket = RichClient.Ticket
}

0 comments on commit 15c06d9

Please sign in to comment.