Skip to content

Commit

Permalink
Cloud Slack E2e Tests (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
huseyinbabal committed Sep 28, 2023
1 parent 6c85373 commit 1cfd0cd
Show file tree
Hide file tree
Showing 11 changed files with 1,257 additions and 192 deletions.
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ linters-settings:
local-prefixes: github.com/kubeshop/botkube
gocyclo:
# https://github.com/kubeshop/botkube/issues/745
min-complexity: 51
# Merging slack and discord test cases increased cyclo due to if conditions
min-complexity: 55
revive:
rules:
# Disable warns about capitalized and ended with punctuation error messages
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ test: system-check
@go test -v -race ./...

test-integration-slack: system-check
@go test -v -tags=integration -race -count=1 ./test/e2e/... -run "TestSlack"
@go test -timeout=20m -v -tags=integration -race -count=1 ./test/e2e/... -run "TestSlack"

test-integration-discord: system-check
@go test -v -tags=integration -race -count=1 ./test/e2e/... -run "TestDiscord"
@go test -timeout=20m -v -tags=integration -race -count=1 ./test/e2e/... -run "TestDiscord"

test-cli-migration-e2e: system-check
@go test -v -tags=migration -race -count=1 ./test/e2e/...
Expand Down
148 changes: 148 additions & 0 deletions helm/botkube/e2e-cloud-test-values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
analytics:
disable: true

rbac:
create: true
rules:
- apiGroups: [ "*" ]
resources: [ "*" ]
verbs: [ "get", "watch", "list" ] # defaults
staticGroupName: "botkube-plugins-default"

extraObjects:

# Group 'kubectl-first-channel': permissions for kubectl for first channel
## namespace scoped permissions
- apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata: &kubectl-wait
name: kubectl-first-channel-namespaced-perms
labels:
app.kubernetes.io/instance: botkube-e2e-test
rules:
- apiGroups: [ "apps" ]
resources: [ "deployments" ]
verbs: [ "get","watch","list" ]
- apiGroups: [ "" ]
resources: [ "configmaps", "pods" ]
verbs: [ "get", "watch", "list" ]
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
<<: *kubectl-wait
namespace: botkube
roleRef: &kubectl-wait-role
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubectl-first-channel-namespaced-perms
subjects: &kubectl-first-channel-subject
- kind: User
name: kubectl-first-channel
apiGroup: rbac.authorization.k8s.io
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
<<: *kubectl-wait
namespace: default
roleRef: *kubectl-wait-role
subjects: *kubectl-first-channel-subject

### cluster permissions
- apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata: &kubectl-deploy-all-meta
name: kc-first-channel-cluster-perms
labels:
app.kubernetes.io/instance: botkube-e2e-test
rules:
- apiGroups: [ "apps" ]
resources: [ "deployments" ]
verbs: [ "get", "list" ]
- apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata: *kubectl-deploy-all-meta
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kc-first-channel-cluster-perms
subjects: *kubectl-first-channel-subject

# Group 'kc-exec-only'
## exec only for default and botkube namespaces:
- apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata: &kc-exec-only-meta
name: kc-exec-only
labels:
app.kubernetes.io/instance: botkube-e2e-test
rules:
- apiGroups: [ "" ]
resources: [ "pods/exec" ]
verbs: [ "create" ]
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
<<: *kc-exec-only-meta
namespace: botkube
roleRef: &kc-exec-only-role
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kc-exec-only
subjects: &kc-exec-only-subject
- kind: User
name: kc-exec-only
apiGroup: rbac.authorization.k8s.io
- apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
<<: *kc-exec-only-meta
namespace: default
roleRef: *kc-exec-only-role
subjects: *kc-exec-only-subject

# Group 'kc-label-svc-all':
## namespace scoped permissions
- apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata: &kc-label-svc-all-meta
name: kc-label-svc-all
labels:
app.kubernetes.io/instance: botkube-e2e-test
rules:
- apiGroups: [ "" ]
resources: [ "services" ]
verbs: [ "get", "patch" ]
- apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata: *kc-label-svc-all-meta
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kc-label-svc-all
subjects:
- kind: User
name: kc-label-svc-all
apiGroup: rbac.authorization.k8s.io

# Group 'rbac-with-static-mapping':
- apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata: &k8s-cm-watch-meta
name: kc-watch-cm
labels:
app.kubernetes.io/instance: botkube-e2e-test
rules:
- apiGroups: [ "" ]
resources: [ "configmaps" ]
verbs: [ "watch", "list" ]
- apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata: *k8s-cm-watch-meta
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kc-watch-cm
subjects:
- kind: Group
name: kc-watch-cm
apiGroup: rbac.authorization.k8s.io
2 changes: 1 addition & 1 deletion helm/botkube/e2e-test-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ executors:
value: "kc-label-svc-all"

'other-plugins':
botkube/echo@v1.0.1-devel:
botkube/echo@v0.0.0-latest:
enabled: true
config:
changeResponseToUpperCase: true
Expand Down
69 changes: 69 additions & 0 deletions test/botkubex/botkube_cli_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package botkubex

import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/require"
)

type InstallParams struct {
BinaryPath string
HelmRepoDirectory string
ConfigProviderEndpoint string
ConfigProviderIdentifier string
ConfigProviderAPIKey string
ImageRegistry string
ImageRepository string
ImageTag string
PluginRestartPolicyThreshold int
PluginRestartHealthCheckIntervalSeconds int
}

func Install(t *testing.T, params InstallParams) error {
//nolint:gosec // this is not production code
cmd := exec.Command(params.BinaryPath, "install",
"--auto-approve",
"--verbose",
fmt.Sprintf("--repo=%s", params.HelmRepoDirectory),
fmt.Sprintf("--values=%s/botkube/e2e-cloud-test-values.yaml", params.HelmRepoDirectory),
`--version=""`, // installer doesn't call Helm repo`index.yaml` when version is empty, so local Helm chart works as expected.
"--set",
fmt.Sprintf("image.registry=%s", params.ImageRegistry),
"--set",
fmt.Sprintf("image.repository=%s", params.ImageRepository),
"--set",
fmt.Sprintf("image.tag=%s", params.ImageTag),
"--set",
fmt.Sprintf("config.provider.endpoint=%s", params.ConfigProviderEndpoint),
"--set",
fmt.Sprintf("config.provider.identifier=%s", params.ConfigProviderIdentifier),
"--set",
fmt.Sprintf("extraEnv[0].name=%s", "BOTKUBE_PLUGINS_RESTART__POLICY_THRESHOLD"),
"--set-string",
fmt.Sprintf("extraEnv[0].value=%d", params.PluginRestartPolicyThreshold),
"--set",
fmt.Sprintf("extraEnv[1].name=%s", "BOTKUBE_PLUGINS_HEALTH__CHECK__INTERVAL"),
"--set-string",
fmt.Sprintf("extraEnv[1].value=%ds", params.PluginRestartHealthCheckIntervalSeconds),
"--set",
fmt.Sprintf("config.provider.apiKey=%s", params.ConfigProviderAPIKey))
t.Logf("Executing command: %s", cmd.String())
cmd.Env = os.Environ()

o, err := cmd.CombinedOutput()
t.Logf("CLI output:\n%s", string(o))
return err
}

func Uninstall(t *testing.T, binaryPath string) {
//nolint:gosec // this is not production code
cmd := exec.Command(binaryPath, "uninstall", "--release-name", "botkube", "--namespace", "botkube", "--auto-approve")
cmd.Env = os.Environ()

o, err := cmd.CombinedOutput()
t.Logf("CLI output:\n%s", string(o))
require.NoError(t, err)
}
12 changes: 12 additions & 0 deletions test/commplatform/discord_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ func (d *DiscordTester) WaitForLastInteractiveMessagePostedEqualWithCustomRender
})
}

func (d *DiscordTester) SetTimeout(timeout time.Duration) {
d.cfg.MessageWaitTimeout = timeout
}

func (d *DiscordTester) Timeout() time.Duration {
return d.cfg.MessageWaitTimeout
}

func (d *DiscordTester) findUserID(t *testing.T, name string) string {
t.Logf("Getting user %q...", name)
res, err := d.cli.GuildMembersSearch(d.cfg.GuildID, name, 50)
Expand All @@ -457,3 +465,7 @@ func (d *DiscordTester) findUserID(t *testing.T, name string) string {

return ""
}

func (d *DiscordTester) ReplaceBotNamePlaceholder(msg *interactive.CoreMessage, clusterName string) {
msg.ReplaceBotNamePlaceholder(d.BotName())
}
20 changes: 19 additions & 1 deletion test/commplatform/generic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commplatform

import (
"strings"
"testing"
"time"

Expand Down Expand Up @@ -47,6 +48,9 @@ type BotDriver interface {
WaitForInteractiveMessagePostedRecentlyEqual(userID string, channelID string, message interactive.CoreMessage) error
WaitForLastInteractiveMessagePostedEqual(userID string, channelID string, message interactive.CoreMessage) error
WaitForLastInteractiveMessagePostedEqualWithCustomRender(userID, channelID string, renderedMsg string) error
SetTimeout(timeout time.Duration)
Timeout() time.Duration
ReplaceBotNamePlaceholder(msg *interactive.CoreMessage, clusterName string)
}

type MessageAssertion func(content string) (bool, int, string)
Expand All @@ -62,6 +66,20 @@ type ExpAttachmentInput struct {
type DriverType string

const (
SlackBot DriverType = "slack"
SlackBot DriverType = "cloudSlack"
DiscordBot DriverType = "discord"
)

// AssertContains checks if message contains expected message
func AssertContains(expectedMessage string) MessageAssertion {
return func(msg string) (bool, int, string) {
return strings.Contains(msg, expectedMessage), 0, ""
}
}

// AssertEquals checks if message is equal to expected message
func AssertEquals(expectedMessage string) MessageAssertion {
return func(msg string) (bool, int, string) {
return msg == expectedMessage, 0, ""
}
}
Loading

0 comments on commit 1cfd0cd

Please sign in to comment.