|
| 1 | +/* |
| 2 | +Copyright (C) 2022-2026 ApeCloud Co., Ltd |
| 3 | +
|
| 4 | +This file is part of KubeBlocks project |
| 5 | +
|
| 6 | +This program is free software: you can redistribute it and/or modify |
| 7 | +it under the terms of the GNU Affero General Public License as published by |
| 8 | +the Free Software Foundation, either version 3 of the License, or |
| 9 | +(at your option) any later version. |
| 10 | +
|
| 11 | +This program is distributed in the hope that it will be useful |
| 12 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +GNU Affero General Public License for more details. |
| 15 | +
|
| 16 | +You should have received a copy of the GNU Affero General Public License |
| 17 | +along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +*/ |
| 19 | + |
| 20 | +package service |
| 21 | + |
| 22 | +import ( |
| 23 | + "encoding/json" |
| 24 | + "strings" |
| 25 | + |
| 26 | + . "github.com/onsi/ginkgo/v2" |
| 27 | + . "github.com/onsi/gomega" |
| 28 | + |
| 29 | + "github.com/apecloud/kubeblocks/pkg/kbagent/proto" |
| 30 | +) |
| 31 | + |
| 32 | +var _ = Describe("marshal task event with size limit", func() { |
| 33 | + It("keeps small task events unchanged", func() { |
| 34 | + event := proto.TaskEvent{ |
| 35 | + Instance: "comp", |
| 36 | + Task: "newReplica", |
| 37 | + Replica: "pod-1", |
| 38 | + Message: "ok", |
| 39 | + } |
| 40 | + msg, err := marshalEventWithSizeLimit(&event, &event.Message, &event.Output) |
| 41 | + Expect(err).Should(BeNil()) |
| 42 | + plain, err := json.Marshal(&event) |
| 43 | + Expect(err).Should(BeNil()) |
| 44 | + Expect(msg).Should(Equal(plain)) |
| 45 | + }) |
| 46 | + |
| 47 | + It("truncates a long task failure message but keeps its head and valid JSON", func() { |
| 48 | + event := proto.TaskEvent{ |
| 49 | + Instance: "comp", |
| 50 | + Task: "newReplica", |
| 51 | + Replica: "pod-1", |
| 52 | + Code: -1, |
| 53 | + Message: "replication setup failed: connection refused\n" + strings.Repeat("verbose replication log line\n", 200), |
| 54 | + } |
| 55 | + msg, err := marshalEventWithSizeLimit(&event, &event.Message, &event.Output) |
| 56 | + Expect(err).Should(BeNil()) |
| 57 | + Expect(len(msg)).Should(BeNumerically("<=", maxEventMessageLength)) |
| 58 | + |
| 59 | + var decoded proto.TaskEvent |
| 60 | + Expect(json.Unmarshal(msg, &decoded)).Should(Succeed()) |
| 61 | + Expect(decoded.Code).Should(Equal(int32(-1))) |
| 62 | + Expect(decoded.Message).Should(HavePrefix("replication setup failed")) |
| 63 | + Expect(decoded.Message).Should(HaveSuffix("...(truncated)")) |
| 64 | + }) |
| 65 | + |
| 66 | + It("shrinks oversized task output while keeping valid JSON", func() { |
| 67 | + event := proto.TaskEvent{ |
| 68 | + Instance: "comp", |
| 69 | + Task: "newReplica", |
| 70 | + Output: []byte(strings.Repeat("y", 8192)), |
| 71 | + } |
| 72 | + msg, err := marshalEventWithSizeLimit(&event, &event.Message, &event.Output) |
| 73 | + Expect(err).Should(BeNil()) |
| 74 | + Expect(len(msg)).Should(BeNumerically("<=", maxEventMessageLength)) |
| 75 | + |
| 76 | + var decoded proto.TaskEvent |
| 77 | + Expect(json.Unmarshal(msg, &decoded)).Should(Succeed()) |
| 78 | + Expect(decoded.Task).Should(Equal("newReplica")) |
| 79 | + }) |
| 80 | +}) |
0 commit comments