notification.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2021 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package clients
  14. import (
  15. "strings"
  16. "time"
  17. "github.com/rook/rook/tests/framework/installer"
  18. "github.com/rook/rook/tests/framework/utils"
  19. )
  20. // TopicOperation is a wrapper for rook notification operations
  21. type NotificationOperation struct {
  22. k8sh *utils.K8sHelper
  23. manifests installer.CephManifests
  24. }
  25. // CreateTopicOperation creates a new topic client
  26. func CreateNotificationOperation(k8sh *utils.K8sHelper, manifests installer.CephManifests) *NotificationOperation {
  27. return &NotificationOperation{k8sh, manifests}
  28. }
  29. func (n *NotificationOperation) CreateNotification(notificationName string, topicName string) error {
  30. return n.k8sh.ResourceOperation("create", n.manifests.GetBucketNotification(notificationName, topicName))
  31. }
  32. func (n *NotificationOperation) DeleteNotification(notificationName string, topicName string) error {
  33. return n.k8sh.ResourceOperation("delete", n.manifests.GetBucketNotification(notificationName, topicName))
  34. }
  35. func (n *NotificationOperation) UpdateNotification(notificationName string, topicName string) error {
  36. return n.k8sh.ResourceOperation("apply", n.manifests.GetBucketNotification(notificationName, topicName))
  37. }
  38. // CheckNotification if notification was set
  39. func (t *NotificationOperation) CheckNotificationCR(notificationName string) bool {
  40. // TODO: return result based on reconcile status of the CR
  41. const resourceName = "cephbucketnotification"
  42. _, err := t.k8sh.GetResource(resourceName, notificationName)
  43. if err != nil {
  44. logger.Infof("%q %q does not exist", resourceName, notificationName)
  45. return false
  46. }
  47. return true
  48. }
  49. func (t *NotificationOperation) CheckNotificationFromHTTPEndPoint(appLabel, eventName, fileName string) (bool, error) {
  50. // wait for the notification to reach http-server
  51. time.Sleep(5 * time.Second)
  52. selectorName := "--selector=" + appLabel
  53. l, err := t.k8sh.Kubectl("logs", selectorName, "--tail", "5")
  54. if err != nil {
  55. return false, err
  56. }
  57. if strings.Contains(l, eventName) && strings.Contains(l, fileName) {
  58. return true, nil
  59. }
  60. return false, nil
  61. }