Skip to content

Commit

Permalink
Fix @bot describe locks to get triggered
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Oct 4, 2024
1 parent fad976e commit b28b007
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
67 changes: 39 additions & 28 deletions slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ func TestSlackLockUnlock(t *testing.T) {
clientset, err := k.ClientSet()
require.NoError(t, err)

setupConfigMaps(t, clientset, configMaps...)
setupNamespace(t, clientset, "gocat")
ns := getNamespace()
setupNamespace(t, clientset, ns)
setupConfigMaps(t, clientset, ns, configMaps...)

messages := make(chan message, 10)
nextMessage := func() message {
t.Helper()

select {
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for message")
Expand Down Expand Up @@ -229,34 +232,34 @@ func TestSlackLockUnlock(t *testing.T) {
projectList: &projectList,
userList: &userList,
interactorFactory: &interactorFactory,
coordinator: deploy.NewCoordinator("gocat", "deploylocks"),
coordinator: deploy.NewCoordinator(ns, "deploylocks"),
}

require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Channel: "C1234",
Text: "lock myproject1 production for deployment of revision a",
Text: "<@U0LAN0Z89> lock myproject1 production for deployment of revision a",
}))
require.Equal(t, "Locked myproject1 production", nextMessage().Text())

require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Channel: "C1234",
Text: "lock myproject1 production for deployment of revision a",
Text: "<@U0LAN0Z89> lock myproject1 production for deployment of revision a",
}))
require.Equal(t, "deployment is already locked", nextMessage().Text())

require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Channel: "C1234",
Text: "unlock myproject1 production",
Text: "<@U0LAN0Z89> unlock myproject1 production",
}))
require.Equal(t, "Unlocked myproject1 production", nextMessage().Text())

require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Channel: "C1234",
Text: "unlock myproject1 production",
Text: "<@U0LAN0Z89> unlock myproject1 production",
}))
require.Equal(t, "deployment is already unlocked", nextMessage().Text())

Expand All @@ -267,30 +270,30 @@ func TestSlackLockUnlock(t *testing.T) {
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1236",
Channel: "C1234",
Text: "lock myproject1 production for deployment of revision a",
Text: "<@U0LAN0Z89> lock myproject1 production for deployment of revision a",
}))
require.Equal(t, "you are not allowed to lock/unlock projects: slack user id \"\"", nextMessage().Text())

require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1236",
Channel: "C1234",
Text: "unlock myproject1 production",
Text: "<@U0LAN0Z89> unlock myproject1 production",
}))
require.Equal(t, "you are not allowed to lock/unlock projects: slack user id \"\"", nextMessage().Text())

// User 2 is a developer so can lock the project
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "lock myproject1 production for deployment of revision a",
Text: "<@U0LAN0Z89> lock myproject1 production for deployment of revision a",
}))
require.Equal(t, "Locked myproject1 production", nextMessage().Text())

// Describe locks
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "describe locks",
Text: "<@U0LAN0Z89> describe locks",
}))
require.Equal(t, `myproject1
production: Locked (by user2, for deployment of revision a)
Expand All @@ -300,15 +303,15 @@ func TestSlackLockUnlock(t *testing.T) {
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "lock myproject1 staging for deployment of revision b",
Text: "<@U0LAN0Z89> lock myproject1 staging for deployment of revision b",
}))
require.Equal(t, "Locked myproject1 staging", nextMessage().Text())

// Describe locks
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "describe locks",
Text: "<@U0LAN0Z89> describe locks",
}))
require.Equal(t, `myproject1
production: Locked (by user2, for deployment of revision a)
Expand All @@ -319,15 +322,15 @@ func TestSlackLockUnlock(t *testing.T) {
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "lock myproject2 staging for deployment of revision c",
Text: "<@U0LAN0Z89> lock myproject2 staging for deployment of revision c",
}))
require.Equal(t, "Locked myproject2 staging", nextMessage().Text())

// Describe locks
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "describe locks",
Text: "<@U0LAN0Z89> describe locks",
}))
require.Equal(t, `myproject1
production: Locked (by user2, for deployment of revision a)
Expand All @@ -340,23 +343,23 @@ myproject2
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Channel: "C1234",
Text: "unlock myproject1 production",
Text: "<@U0LAN0Z89> unlock myproject1 production",
}))
require.Equal(t, "user user1 is not allowed to unlock this project", nextMessage().Text())

// User 4 is an admin and can unlock the project forcefully
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1237",
Channel: "C1234",
Text: "unlock myproject1 production",
Text: "<@U0LAN0Z89> unlock myproject1 production",
}))
require.Equal(t, "Unlocked myproject1 production", nextMessage().Text())

// Describe locks
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "describe locks",
Text: "<@U0LAN0Z89> describe locks",
}))
require.Equal(t, `myproject1
staging: Locked (by user2, for deployment of revision b)
Expand All @@ -368,15 +371,15 @@ myproject2
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "unlock myproject2 staging",
Text: "<@U0LAN0Z89> unlock myproject2 staging",
}))
require.Equal(t, "Unlocked myproject2 staging", nextMessage().Text())

// Describe locks
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "describe locks",
Text: "<@U0LAN0Z89> describe locks",
}))
require.Equal(t, `myproject1
staging: Locked (by user2, for deployment of revision b)
Expand All @@ -386,31 +389,31 @@ myproject2
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Channel: "C1234",
Text: "deploy myproject1 staging",
Text: "<@U0LAN0Z89> deploy myproject1 staging",
}))
require.Equal(t, "Deployment failed: locked by user2", nextMessage().Text())

// Deployment to myproject1/staging by user2 should succeed because it is locked by user2
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1235",
Channel: "C1234",
Text: "deploy myproject1 staging",
Text: "<@U0LAN0Z89> deploy myproject1 staging",
}))
require.Equal(t, "**\n*staging*\n*master* ブランチ\nをデプロイしますか?", nextMessage().Text())

// Deployment to myproject1/production by user1 should fail because it is not locked
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Channel: "C1234",
Text: "deploy myproject1 production",
Text: "<@U0LAN0Z89> deploy myproject1 production",
}))
require.Equal(t, "**\n*production*\n*master* ブランチ\nをデプロイしますか?", nextMessage().Text())

// Deployment to myproject2/staging by user1 should succeed because it is not locked
require.NoError(t, l.handleMessageEvent(&slackevents.AppMentionEvent{
User: "U1234",
Channel: "C1234",
Text: "deploy myproject2 staging",
Text: "<@U0LAN0Z89> deploy myproject2 staging",
}))
require.Equal(t, "**\n*staging*\n*master* ブランチ\nをデプロイしますか?", nextMessage().Text())
}
Expand Down Expand Up @@ -460,18 +463,26 @@ func setupNamespace(t *testing.T, clientset kubernetes.Interface, name string) {
})
}

func setupConfigMaps(t *testing.T, clientset kubernetes.Interface, configMaps ...corev1.ConfigMap) {
func setupConfigMaps(t *testing.T, clientset kubernetes.Interface, ns string, configMaps ...corev1.ConfigMap) {
for _, cm := range configMaps {
cm := cm
_, err := clientset.CoreV1().ConfigMaps("default").Create(context.Background(), &cm, metav1.CreateOptions{})
_, err := clientset.CoreV1().ConfigMaps(ns).Create(context.Background(), &cm, metav1.CreateOptions{})
if kerrors.IsAlreadyExists(err) {
_, err = clientset.CoreV1().ConfigMaps("default").Update(context.Background(), &cm, metav1.UpdateOptions{})
_, err = clientset.CoreV1().ConfigMaps(ns).Update(context.Background(), &cm, metav1.UpdateOptions{})
require.NoError(t, err)
}
t.Cleanup(func() {
if err := clientset.CoreV1().ConfigMaps("default").Delete(context.Background(), cm.Name, metav1.DeleteOptions{}); err != nil {
if err := clientset.CoreV1().ConfigMaps(ns).Delete(context.Background(), cm.Name, metav1.DeleteOptions{}); err != nil {
t.Logf("failed to delete config map %s: %v", cm.Name, err)
}
})
}
}

func getNamespace() string {
ns := os.Getenv("CONFIG_NAMESPACE")
if ns == "" {
ns = "default"
}
return ns
}
2 changes: 1 addition & 1 deletion slackcmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func parseLockUnlock(text string) (Command, error) {
}

func parseDescribeLocks(text string) (Command, error) {
if text != "describe locks" {
if !strings.Contains(text, "describe locks") {
return nil, patternError("describe locks")
}

Expand Down

0 comments on commit b28b007

Please sign in to comment.