Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Sep 28, 2022
1 parent 38348f0 commit 4acfd12
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 115 deletions.
2 changes: 1 addition & 1 deletion cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/util/wait"
"log"
"net/http"
"os"
Expand All @@ -17,6 +16,7 @@ import (
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/discovery"
cacheddiscovery "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/dynamic"
Expand Down
223 changes: 115 additions & 108 deletions helm/botkube/README.md

Large diffs are not rendered by default.

66 changes: 63 additions & 3 deletions internal/lifecycle/server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,67 @@
package lifecycle

import "testing"
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

func TestNewReloadHandler(t *testing.T) {
t.Fail()
logtest "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

"github.com/kubeshop/botkube/pkg/config"
)

func TestNewReloadHandler_HappyPath(t *testing.T) {
// given
clusterName := "foo"

expectedMsg := fmt.Sprintf(":arrows_counterclockwise: Configuration reload requested for cluster '%s'. Hold on a sec...", clusterName)
expectedResponse := `Deployment "namespace/name" restarted successfully.`

expectedStatusCode := http.StatusOK
deployCfg := config.K8sResourceRef{
Name: "name",
Namespace: "namespace",
}
inputDeploy := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: deployCfg.Name,
Namespace: deployCfg.Namespace,
},
}
sendMsgFn := SendMessageFn(func(msg string) error {
assert.Equal(t, expectedMsg, msg)
return nil
})
logger, _ := logtest.NewNullLogger()
k8sCli := fake.NewSimpleClientset(inputDeploy)

req := httptest.NewRequest(http.MethodPost, "/reload", nil)
writer := httptest.NewRecorder()
handler := newReloadHandler(logger, k8sCli, deployCfg, clusterName, sendMsgFn)

// when
handler(writer, req)

res := writer.Result()
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
require.NoError(t, err)

// then
assert.Equal(t, expectedStatusCode, res.StatusCode)
assert.Equal(t, expectedResponse, string(data))

actualDeploy, err := k8sCli.AppsV1().Deployments(deployCfg.Namespace).Get(context.Background(), deployCfg.Name, metav1.GetOptions{})
require.NoError(t, err)

_, exists := actualDeploy.Spec.Template.Annotations["kubectl.kubernetes.io/restartedAt"]
assert.True(t, exists)
}
9 changes: 8 additions & 1 deletion pkg/config/testdata/TestLoadConfigSuccess/config.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ analytics:
disable: true
settings:
clusterName: cluster-name-from-env
configWatcher: true
upgradeNotifier: true
systemConfigMap:
name: botkube-system
Expand All @@ -404,8 +403,16 @@ settings:
configMap:
name: runtime-config
metricsPort: "1313"
lifecycleServer:
enabled: false
port: 0
deployment: {}
log:
level: error
disableColors: false
informersResyncPeriod: 30m0s
kubeconfig: kubeconfig-from-env
configWatcher:
enabled: false
initialSyncTimeout: 0s
tmpDir: ""
22 changes: 21 additions & 1 deletion pkg/execute/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,64 +43,84 @@ func TestSourceBindingsHappyPath(t *testing.T) {
DisplayName: "BAZ",
},
},
ConfigWatcher: config.CfgWatcher{
Enabled: true,
},
}
cfgWithCfgWatcherDisabled := config.Config{Sources: cfg.Sources}

tests := []struct {
name string
command string
config config.Config

message string
sourceBindings []string
}{
{
name: "Should resolve quoted list which is separated by comma",
command: `edit SourceBindings "bar,xyz"`,
config: cfg,

message: ":white_check_mark: Joe adjusted the BotKube notifications settings to BAR and XYZ messages. Expect BotKube reload soon...",
sourceBindings: []string{"bar", "xyz"},
},
{
name: "Should resolve quoted and code items separated by comma",
command: "edit sourcebindings “`bar`,xyz ”",
config: cfg,

message: ":white_check_mark: Joe adjusted the BotKube notifications settings to BAR and XYZ messages. Expect BotKube reload soon...",
sourceBindings: []string{"bar", "xyz"},
},
{
name: "Should resolve list which is separated by comma and ends with whitespace",
command: `edit sourceBindings bar,xyz `,
config: cfg,

message: ":white_check_mark: Joe adjusted the BotKube notifications settings to BAR and XYZ messages. Expect BotKube reload soon...",
sourceBindings: []string{"bar", "xyz"},
},
{
name: "Should resolve list which is separated by comma but has a lot of whitespaces",
command: `edit sourcebindings bar, xyz, `,
config: cfg,

message: ":white_check_mark: Joe adjusted the BotKube notifications settings to BAR and XYZ messages. Expect BotKube reload soon...",
sourceBindings: []string{"bar", "xyz"},
},
{
name: "Should resolve list which is separated by comma, has a lot of whitespaces and some items are quoted",
command: `edit SourceBindings bar xyz, "baz"`,
config: cfg,

message: ":white_check_mark: Joe adjusted the BotKube notifications settings to BAR, XYZ, and BAZ messages. Expect BotKube reload soon...",
sourceBindings: []string{"bar", "xyz", "baz"},
},
{
name: "Should resolve list with unicode quotes",
command: `edit SourceBindings “foo,bar”`,
config: cfg,

message: ":white_check_mark: Joe adjusted the BotKube notifications settings to FOO and BAR messages. Expect BotKube reload soon...",
sourceBindings: []string{"foo", "bar"},
},
{
name: "Should resolve list which has mixed formatting for different items, all at once",
command: `edit SourceBindings foo baz "bar,xyz" "fiz"`,
config: cfg,

message: ":white_check_mark: Joe adjusted the BotKube notifications settings to FOO, BAZ, BAR, XYZ, and FIZ messages. Expect BotKube reload soon...",
sourceBindings: []string{"foo", "baz", "bar", "xyz", "fiz"},
},
{
name: "Should mention manual app restart",
command: `edit SourceBindings "bar,xyz"`,
config: cfgWithCfgWatcherDisabled,

message: ":white_check_mark: Joe adjusted the BotKube notifications settings to BAR and XYZ messages.\nAs the Config Watcher is disabled, you need to restart BotKube manually to apply the changes.",
sourceBindings: []string{"bar", "xyz"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -109,7 +129,7 @@ func TestSourceBindingsHappyPath(t *testing.T) {

fakeStorage := &fakeBindingsStorage{}
args := strings.Fields(strings.TrimSpace(tc.command))
executor := NewEditExecutor(log, &fakeAnalyticsReporter{}, fakeStorage, cfg)
executor := NewEditExecutor(log, &fakeAnalyticsReporter{}, fakeStorage, tc.config)

expMessage := interactive.Message{
Base: interactive.Base{
Expand Down
9 changes: 8 additions & 1 deletion pkg/execute/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func TestNotifierExecutor_Do_Success(t *testing.T) {
disable: false
settings:
clusterName: foo
configWatcher: false
upgradeNotifier: false
systemConfigMap: {}
persistentConfig:
Expand All @@ -105,11 +104,19 @@ func TestNotifierExecutor_Do_Success(t *testing.T) {
fileName: ""
configMap: {}
metricsPort: ""
lifecycleServer:
enabled: false
port: 0
deployment: {}
log:
level: ""
disableColors: false
informersResyncPeriod: 0s
kubeconfig: ""
configWatcher:
enabled: false
initialSyncTimeout: 0s
tmpDir: ""
`),
ExpectedStatusAfter: `Notifications from cluster 'cluster-name' are disabled here.`,
},
Expand Down

0 comments on commit 4acfd12

Please sign in to comment.