Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add early cleanup for Slack E2E tests #1463

Merged
merged 6 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions .github/actions/cloud-slack-e2e/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ runs:

echo "footer=${FOOTER}" >> $GITHUB_OUTPUT

- name: Slack Notification
uses: rtCamp/action-slack-notify@v2
if: ${{ failure() }}
env:
SLACK_CHANNEL: 'botkube-cloud-ci-alerts'
SLACK_USERNAME: Botkube Cloud CI
SLACK_COLOR: 'red'
SLACK_TITLE: 'Message'
SLACK_MESSAGE: "Cloud Slack ${{ inputs.e2e_type }} E2E tests failed :scream:"
SLACK_ICON_EMOJI: ':this-is-fine-fire:'
SLACK_FOOTER: ${{ steps.footer.outputs.footer }}
SLACK_WEBHOOK: ${{ inputs.slack_alerts_webhook }}
# - name: Slack Notification
# uses: rtCamp/action-slack-notify@v2
# if: ${{ failure() }}
# env:
# SLACK_CHANNEL: 'botkube-cloud-ci-alerts'
# SLACK_USERNAME: Botkube Cloud CI
# SLACK_COLOR: 'red'
# SLACK_TITLE: 'Message'
# SLACK_MESSAGE: "Cloud Slack ${{ inputs.e2e_type }} E2E tests failed :scream:"
# SLACK_ICON_EMOJI: ':this-is-fine-fire:'
# SLACK_FOOTER: ${{ steps.footer.outputs.footer }}
# SLACK_WEBHOOK: ${{ inputs.slack_alerts_webhook }}
53 changes: 53 additions & 0 deletions .github/workflows/ui-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Test UI E2E tests on PR

on:
push:
branches:
- fix-e2e-test

env:
GIT_USER: botkube-dev
HELM_VERSION: v3.9.0
K3D_VERSION: v5.4.6
IMAGE_REGISTRY: "ghcr.io"
IMAGE_REPOSITORY: "kubeshop/botkube"
IMAGE_TAG: v9.99.9-dev # TODO: Use commit hash tag to make the predictable builds for each commit on branch

jobs:
cloud-slack-dev-e2e:
name: Botkube Cloud Slack Dev E2E
runs-on: ubuntu-latest
permissions:
contents: read
packages: read
concurrency:
group: cloud-slack-dev-e2e
cancel-in-progress: false
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Run e2e tests
uses: ./.github/actions/cloud-slack-e2e
with:
access_token: ${{ secrets.E2E_TEST_GH_DEV_ACCOUNT_PAT }}

slack_workspace_name: ${{ secrets.E2E_DEV_SLACK_WORKSPACE_NAME }}
slack_email: ${{ secrets.E2E_DEV_SLACK_EMAIL }}
slack_password: ${{ secrets.E2E_DEV_SLACK_USER_PASSWORD }}
slack_bot_display_name: "BotkubeDev"
slack_tester_bot_token: ${{ secrets.E2E_DEV_SLACK_TESTER_BOT_TOKEN }}
slack_tester_bot_name: "botkubedev"

botkube_cloud_api_base_url: "https://api-dev.botkube.io"
botkube_cloud_email: ${{ secrets.E2E_DEV_BOTKUBE_CLOUD_EMAIL }}
botkube_cloud_password: ${{ secrets.E2E_DEV_BOTKUBE_CLOUD_PASSWORD }}
botkube_cloud_team_organization_id: ${{ secrets.E2E_DEV_BOTKUBE_CLOUD_TEAM_ORGANIZATION_ID }}

slack_alerts_webhook: ${{ secrets.SLACK_CI_ALERTS_WEBHOOK }}

e2e_type: "DEV"




23 changes: 20 additions & 3 deletions test/cloud-slack-dev-e2e/botkube_page_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cloud_slack_dev_e2e

import (
"botkube.io/botube/test/cloud_graphql"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (p *BotkubeCloudPage) HideCookieBanner(t *testing.T) {
p.page.Screenshot()
}

func (p *BotkubeCloudPage) CaptureBearerToken(t *testing.T, browser *rod.Browser) func() {
func (p *BotkubeCloudPage) InterceptBearerToken(t *testing.T, browser *rod.Browser) func() {
t.Logf("Starting hijacking requests to %q to get the bearer token...", p.GQLEndpoint)

router := browser.HijackRequests()
Expand All @@ -80,6 +81,7 @@ func (p *BotkubeCloudPage) CaptureBearerToken(t *testing.T, browser *rod.Browser

require.NotNil(t, ctx.Request)
p.AuthHeaderValue = ctx.Request.Header(authHeaderName)
t.Log("Bearer token intercepted")
ctx.ContinueRequest(&proto.FetchContinueRequest{})
})
go router.Run()
Expand All @@ -90,8 +92,10 @@ func (p *BotkubeCloudPage) CreateNewInstance(t *testing.T, name string) {
t.Log("Create new Botkube Instance")

p.page.MustElement("h6#create-instance").MustClick()
time.Sleep(3 * time.Second)
p.page.Screenshot("after-clicking-create-instance")
p.page.MustElement(`input[name="name"]`).MustSelectAllText().MustInput(name)
p.page.Screenshot()
p.page.Screenshot("after-filling-in-instance-name")

// persist connected deploy info
_, id, _ := strings.Cut(p.page.MustInfo().URL, "add/")
Expand Down Expand Up @@ -193,6 +197,7 @@ func (p *BotkubeCloudPage) FinishWizard(t *testing.T) {
p.page.Screenshot("after-second-next")

t.Log("Submitting changes")
p.page.Mouse.MustMoveTo(0, 0)
time.Sleep(3 * time.Second)
p.page.MustElementR("button", "/^Deploy changes$/i").
MustWaitEnabled().
Expand All @@ -216,10 +221,11 @@ func (p *BotkubeCloudPage) UpdateKubectlNamespace(t *testing.T) {

t.Log("Moving to top left corner of the page")
p.page.Mouse.MustMoveTo(0, 0)
p.page.Screenshot("after-moving-to-top-left")
time.Sleep(3 * time.Second)

t.Log("Submitting changes")
p.page.MustWaitStable()
p.page.Screenshot("before-deploying-plugin-changes")
p.page.MustElementR("button", "/Deploy changes/i").MustClick()
p.page.Screenshot("after-deploying-plugin-changes")
}
Expand Down Expand Up @@ -247,6 +253,17 @@ func (p *BotkubeCloudPage) openKubectlUpdateForm() {
p.page.Screenshot("after-selecting-kubectl-cfg-form")
}

func (p *BotkubeCloudPage) Cleanup(t *testing.T, gqlCli *cloud_graphql.Client) {
t.Log("Cleaning up Botkube instance on test failure...")

if p.ConnectedDeploy == nil {
t.Log("No deployment to delete")
return
}

deleteDeployment(t, gqlCli, p.ConnectedDeploy.ID, "connected")
}

func appendOrgIDQueryParam(t *testing.T, inURL, orgID string) string {
parsedURL, err := url.Parse(inURL)
require.NoError(t, err)
Expand Down
177 changes: 104 additions & 73 deletions test/cloud-slack-dev-e2e/cloud_slack_dev_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type BotkubeCloudConfig struct {
}

func TestCloudSlackE2E(t *testing.T) {
t.Log("Loading configuration...")
t.Log("1. Loading configuration...")
var cfg E2ESlackConfig
err := envconfig.Init(&cfg)
require.NoError(t, err)
Expand Down Expand Up @@ -122,97 +122,74 @@ func TestCloudSlackE2E(t *testing.T) {
botkubeCloudPage := NewBotkubeCloudPage(t, cfg)
slackPage := NewSlackPage(t, cfg)

t.Run("Creating Botkube Instance with newly added Slack Workspace", func(t *testing.T) {
t.Log("Setting up browser...")
t.Log("2. Creating Botkube Instance with newly added Slack Workspace")

launcher := launcher.New().Headless(true)
isHeadless := launcher.Has(flags.Headless)
t.Cleanup(launcher.Cleanup)
t.Log("Setting up browser...")
launcher := launcher.New().Headless(true)
isHeadless := launcher.Has(flags.Headless)
t.Cleanup(launcher.Cleanup)

browser := rod.New().Trace(cfg.DebugMode).ControlURL(launcher.MustLaunch()).MustConnect()
t.Cleanup(func() {
err := browser.Close()
if err != nil {
t.Logf("Failed to close browser: %v", err)
}
})

page := newBrowserPage(t, browser, cfg)
t.Cleanup(func() {
closePage(t, "page", page)
})
browser := rod.New().Trace(cfg.DebugMode).ControlURL(launcher.MustLaunch()).MustConnect()
t.Cleanup(func() {
err := browser.Close()
if err != nil {
t.Logf("Failed to close browser: %v", err)
}
})

botkubeCloudPage.NavigateAndLogin(t, page)
botkubeCloudPage.HideCookieBanner(t)
page := newBrowserPage(t, browser, cfg)
t.Cleanup(func() {
closePage(t, "page", page)
})

stopRouter := botkubeCloudPage.CaptureBearerToken(t, browser)
defer stopRouter()
stopRouter := botkubeCloudPage.InterceptBearerToken(t, browser)
defer stopRouter()

botkubeCloudPage.CreateNewInstance(t, channel.Name())
botkubeCloudPage.InstallAgentInCluster(t, cfg.BotkubeCliBinaryPath)
botkubeCloudPage.OpenSlackAppIntegrationPage(t)
botkubeCloudPage.NavigateAndLogin(t, page)
botkubeCloudPage.HideCookieBanner(t)

slackPage.ConnectWorkspace(t, browser)
botkubeCloudPage.CreateNewInstance(t, channel.Name())

botkubeCloudPage.ReAddSlackPlatformIfShould(t, isHeadless)
botkubeCloudPage.SetupSlackWorkspace(t, channel.Name())
botkubeCloudPage.FinishWizard(t)
botkubeCloudPage.VerifyDeploymentStatus(t, "Connected")
botkubeCloudPage.InstallAgentInCluster(t, cfg.BotkubeCliBinaryPath)
botkubeCloudPage.OpenSlackAppIntegrationPage(t)

botkubeCloudPage.UpdateKubectlNamespace(t)
botkubeCloudPage.VerifyDeploymentStatus(t, "Updating")
botkubeCloudPage.VerifyDeploymentStatus(t, "Connected")
botkubeCloudPage.VerifyUpdatedKubectlNamespace(t)
slackPage.ConnectWorkspace(t, browser)
t.Cleanup(func() {
// disconnect Slack workspace
gqlCli := createGQLCli(t, cfg, botkubeCloudPage)
slackPage.Cleanup(t, gqlCli)
})
t.Cleanup(func() {
// delete Botkube instance
gqlCli := createGQLCli(t, cfg, botkubeCloudPage)
botkubeCloudPage.Cleanup(t, gqlCli)
})

botkubeCloudPage.ReAddSlackPlatformIfShould(t, isHeadless)
botkubeCloudPage.SetupSlackWorkspace(t, channel.Name())
botkubeCloudPage.FinishWizard(t)
botkubeCloudPage.VerifyDeploymentStatus(t, "Connected")

botkubeCloudPage.UpdateKubectlNamespace(t)
botkubeCloudPage.VerifyDeploymentStatus(t, "Updating")
botkubeCloudPage.VerifyDeploymentStatus(t, "Connected")
botkubeCloudPage.VerifyUpdatedKubectlNamespace(t)

t.Run("Run E2E tests with deployment", func(t *testing.T) {
gqlCli := createGQLCli(t, cfg, botkubeCloudPage)

connectedDeploy := botkubeCloudPage.ConnectedDeploy
require.NotNil(t, connectedDeploy, "Previous subtest needs to pass to get connected deployment information")
require.NotEmpty(t, botkubeCloudPage.AuthHeaderValue, "Previous subtest needs to pass to get authorization header value")
// cleanup is done in the upper test function

t.Logf("Using Organization ID %q and Authorization header starting with %q", cfg.BotkubeCloud.TeamOrganizationID,
stringsutil.ShortenString(botkubeCloudPage.AuthHeaderValue, 15))

gqlCli := cloud_graphql.NewClientForAuthAndOrg(botkubeCloudPage.GQLEndpoint, cfg.BotkubeCloud.TeamOrganizationID, botkubeCloudPage.AuthHeaderValue)

t.Logf("Getting connected Slack workspace...")
slackWorkspaces := gqlCli.MustListSlackWorkspacesForOrg(t, cfg.BotkubeCloud.TeamOrganizationID)
require.Len(t, slackWorkspaces, 1)
slackWorkspace := slackWorkspaces[0]
slackWorkspace := findConnectedSlackWorkspace(t, cfg, gqlCli)
require.NotNil(t, slackWorkspace)
t.Cleanup(func() {
if !cfg.Slack.DisconnectWorkspaceAfterTests {
return
}
t.Log("Disconnecting Slack workspace...")
err = retryOperation(func() error {
return gqlCli.DeleteSlackWorkspace(t, cfg.BotkubeCloud.TeamOrganizationID, slackWorkspace.ID)
})
if err != nil {
t.Logf("Failed to disconnect Slack workspace: %s", err.Error())
}
})
// cleanup is done in the upper test function

t.Log("Creating a second deployment to test not connected flow...")
notConnectedDeploy := gqlCli.MustCreateBasicDeploymentWithCloudSlack(t, fmt.Sprintf("%s-2", channel.Name()), slackWorkspace.TeamID, channel.Name())
t.Cleanup(func() {
t.Log("Deleting second deployment...")
err = retryOperation(func() error {
return gqlCli.DeleteDeployment(t, graphql.ID(notConnectedDeploy.ID))
})
if err != nil {
t.Logf("Failed to delete second deployment: %s", err.Error())
}
})

t.Cleanup(func() {
t.Log("Deleting first deployment...")
err = retryOperation(func() error {
return gqlCli.DeleteDeployment(t, graphql.ID(connectedDeploy.ID))
})
if err != nil {
t.Logf("Failed to delete first deployment: %s", err.Error())
}
deleteDeployment(t, gqlCli, notConnectedDeploy.ID, "second (not connected)")
})

t.Log("Waiting for help message...")
Expand Down Expand Up @@ -511,6 +488,60 @@ func createK8sCli(t *testing.T, kubeconfigPath string) *kubernetes.Clientset {
return k8sCli
}

func createGQLCli(t *testing.T, cfg E2ESlackConfig, botkubeCloudPage *BotkubeCloudPage) *cloud_graphql.Client {
require.NotEmpty(t, botkubeCloudPage.AuthHeaderValue, "Authorization header value should be set")

t.Logf("Using Organization ID %q and Authorization header starting with %q", cfg.BotkubeCloud.TeamOrganizationID,
stringsutil.ShortenString(botkubeCloudPage.AuthHeaderValue, 15))
return cloud_graphql.NewClientForAuthAndOrg(botkubeCloudPage.GQLEndpoint, cfg.BotkubeCloud.TeamOrganizationID, botkubeCloudPage.AuthHeaderValue)
}

func findConnectedSlackWorkspace(t *testing.T, cfg E2ESlackConfig, gqlCli *cloud_graphql.Client) *gqlModel.SlackWorkspace {
t.Logf("Finding connected Slack workspace...")
slackWorkspaces := gqlCli.MustListSlackWorkspacesForOrg(t, cfg.BotkubeCloud.TeamOrganizationID)
if len(slackWorkspaces) == 0 {
return nil
}

if len(slackWorkspaces) > 1 {
t.Logf("Found multiple connected Slack workspaces: %v", slackWorkspaces)
return nil
}

slackWorkspace := slackWorkspaces[0]
return slackWorkspace
}

func disconnectConnectedSlackWorkspace(t *testing.T, cfg E2ESlackConfig, gqlCli *cloud_graphql.Client, slackWorkspace *gqlModel.SlackWorkspace) {
if slackWorkspace == nil {
t.Log("Skipping disconnecting Slack workspace as it is nil")
return
}

if !cfg.Slack.DisconnectWorkspaceAfterTests {
t.Log("Skipping disconnecting Slack workspace...")
return
}

t.Log("Disconnecting Slack workspace...")
err := retryOperation(func() error {
return gqlCli.DeleteSlackWorkspace(t, cfg.BotkubeCloud.TeamOrganizationID, slackWorkspace.ID)
})
if err != nil {
t.Logf("Failed to disconnect Slack workspace: %s", err.Error())
}
}

func deleteDeployment(t *testing.T, gqlCli *cloud_graphql.Client, deployID string, label string) {
t.Logf("Deleting %s deployment...", label)
err := retryOperation(func() error {
return gqlCli.DeleteDeployment(t, graphql.ID(deployID))
})
if err != nil {
t.Logf("Failed to delete first deployment: %s", err.Error())
}
}

func retryOperation(fn func() error) error {
return retry.Do(fn,
retry.Attempts(cleanupRetryAttempts),
Expand Down
2 changes: 1 addition & 1 deletion test/cloud-slack-dev-e2e/page_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func closePage(t *testing.T, name string, page *rod.Page) {
t.Helper()
err := page.Close()
if err != nil {
if errors.Is(err, context.Canceled) {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return
}

Expand Down
Loading
Loading