|
| 1 | +package action |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "math/rand" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "helm.sh/helm/v3/pkg/release" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1" |
| 15 | + |
| 16 | + "github.com/operator-framework/helm-operator-plugins/pkg/storage" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // maxCompressedReleaseSize is the maximum total size in bytes of a |
| 21 | + // gzip-compressed Helm release that the chunked storage driver can store |
| 22 | + // (ChunkSize * MaxWriteChunks). It is tested for exact equality with the |
| 23 | + // config so that any config increase forces this const to be raised too. |
| 24 | + // This value must never decrease: lowering it would cause previously-storable |
| 25 | + // releases to fail. |
| 26 | + maxCompressedReleaseSize = (1024 - 8) * 1024 * 11 // 1016 KB * 11 chunks = 11,176 KB |
| 27 | + |
| 28 | + // minMaxWriteChunks is tested for exact equality with MaxWriteChunks so |
| 29 | + // that any config increase forces this const to be raised too. This value |
| 30 | + // must never decrease: lowering it would cause previously-storable releases |
| 31 | + // to fail. |
| 32 | + minMaxWriteChunks = 11 |
| 33 | + |
| 34 | + // minMaxReadChunks is tested for exact equality with MaxReadChunks so |
| 35 | + // that any config increase forces this const to be raised too. This value |
| 36 | + // must never decrease: lowering it would make previously-stored releases |
| 37 | + // unreadable. |
| 38 | + minMaxReadChunks = 11 |
| 39 | +) |
| 40 | + |
| 41 | +func TestChunkedSecretsConfigTotalCapacity(t *testing.T) { |
| 42 | + assert.Equal(t, maxCompressedReleaseSize, chunkedSecretsConfig.ChunkSize*chunkedSecretsConfig.MaxWriteChunks, |
| 43 | + "ChunkSize * MaxWriteChunks must equal maxCompressedReleaseSize") |
| 44 | +} |
| 45 | + |
| 46 | +func TestChunkedSecretsConfigMaxWriteChunks(t *testing.T) { |
| 47 | + assert.Equal(t, minMaxWriteChunks, chunkedSecretsConfig.MaxWriteChunks, |
| 48 | + "MaxWriteChunks changed — update minMaxWriteChunks to match (it must never decrease)") |
| 49 | +} |
| 50 | + |
| 51 | +func TestChunkedSecretsConfigMaxReadChunks(t *testing.T) { |
| 52 | + assert.Equal(t, minMaxReadChunks, chunkedSecretsConfig.MaxReadChunks, |
| 53 | + "MaxReadChunks changed — update minMaxReadChunks to match (it must never decrease)") |
| 54 | + assert.GreaterOrEqual(t, chunkedSecretsConfig.MaxReadChunks, chunkedSecretsConfig.MaxWriteChunks, |
| 55 | + "MaxReadChunks must be >= MaxWriteChunks so any written release can be read back") |
| 56 | +} |
| 57 | + |
| 58 | +func TestChunkedSecretsMaxCapacityRelease(t *testing.T) { |
| 59 | + // Regression test: stores a release through the real chunked storage driver |
| 60 | + // that fills all MaxWriteChunks chunks, reads it back, and verifies the |
| 61 | + // first MaxWriteChunks-1 chunks are exactly ChunkSize bytes. This proves |
| 62 | + // the configured ChunkSize fits within the Kubernetes 1MB Secret data limit |
| 63 | + // end-to-end against a real API server. |
| 64 | + |
| 65 | + chunkSize := chunkedSecretsConfig.ChunkSize |
| 66 | + maxChunks := chunkedSecretsConfig.MaxWriteChunks |
| 67 | + |
| 68 | + // Use a large incompressible payload to guarantee all chunks are used. |
| 69 | + // Raw []byte in Config is base64-encoded by json.Marshal, giving full |
| 70 | + // 8-bit entropy. The base64 expansion (~33%) and gzip compression roughly |
| 71 | + // cancel out at a ~1.004 ratio, so ChunkSize*10 raw bytes compresses to |
| 72 | + // just over 10 chunks, requiring all 11. |
| 73 | + rel := &release.Release{ |
| 74 | + Name: "max-capacity-test", |
| 75 | + Version: 1, |
| 76 | + Config: map[string]any{"payload": deterministicPayload(chunkSize * (maxChunks - 1))}, |
| 77 | + Info: &release.Info{Status: release.StatusDeployed}, |
| 78 | + } |
| 79 | + |
| 80 | + secretsClient := clientcorev1.NewForConfigOrDie(cfg).Secrets("default") |
| 81 | + drv := storage.NewChunkedSecrets(secretsClient, "test-owner", chunkedSecretsConfig) |
| 82 | + |
| 83 | + key := fmt.Sprintf("sh.helm.release.v1.%s.v%d", rel.Name, rel.Version) |
| 84 | + require.NoError(t, drv.Create(key, rel)) |
| 85 | + |
| 86 | + // Verify round-trip |
| 87 | + actual, err := drv.Get(key) |
| 88 | + require.NoError(t, err) |
| 89 | + assert.Equal(t, rel.Name, actual.Name) |
| 90 | + assert.Equal(t, rel.Version, actual.Version) |
| 91 | + |
| 92 | + // Collect secrets and verify chunk sizes using the extraChunks field |
| 93 | + // from the index Secret to determine ordering. |
| 94 | + allSecrets, err := secretsClient.List(context.Background(), metav1.ListOptions{}) |
| 95 | + require.NoError(t, err) |
| 96 | + |
| 97 | + secretsByName := map[string][]byte{} |
| 98 | + var indexChunkData []byte |
| 99 | + var extraChunkNames []string |
| 100 | + for _, s := range allSecrets.Items { |
| 101 | + switch s.Type { |
| 102 | + case storage.SecretTypeChunkedIndex: |
| 103 | + indexChunkData = s.Data["chunk"] |
| 104 | + require.NoError(t, json.Unmarshal(s.Data["extraChunks"], &extraChunkNames)) |
| 105 | + case storage.SecretTypeChunkedChunk: |
| 106 | + secretsByName[s.Name] = s.Data["chunk"] |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + require.NotNil(t, indexChunkData, "index Secret must exist") |
| 111 | + require.Len(t, extraChunkNames, maxChunks-1, |
| 112 | + "release must use all %d chunks", maxChunks) |
| 113 | + |
| 114 | + // The first 10 chunks (index + 9 extras) must be exactly ChunkSize. |
| 115 | + // The last chunk may be smaller since it holds the remainder. |
| 116 | + assert.Equalf(t, chunkSize, len(indexChunkData), |
| 117 | + "chunk 1/%d (index) must be exactly ChunkSize", maxChunks) |
| 118 | + for i, name := range extraChunkNames[:maxChunks-2] { |
| 119 | + chunkData, ok := secretsByName[name] |
| 120 | + require.True(t, ok, "chunk Secret %q not found", name) |
| 121 | + assert.Equalf(t, chunkSize, len(chunkData), |
| 122 | + "chunk %d/%d must be exactly ChunkSize", i+2, maxChunks) |
| 123 | + } |
| 124 | + |
| 125 | + // The last chunk just needs to be non-empty. |
| 126 | + lastName := extraChunkNames[maxChunks-2] |
| 127 | + lastChunk, ok := secretsByName[lastName] |
| 128 | + require.True(t, ok, "last chunk Secret %q not found", lastName) |
| 129 | + assert.NotEmpty(t, lastChunk, "last chunk must be non-empty") |
| 130 | + |
| 131 | + _, err = drv.Delete(key) |
| 132 | + require.NoError(t, err) |
| 133 | +} |
| 134 | + |
| 135 | +func deterministicPayload(n int) []byte { |
| 136 | + r := rand.New(rand.NewSource(42)) |
| 137 | + b := make([]byte, n) |
| 138 | + // rand.Read always returns len(b), nil |
| 139 | + r.Read(b) |
| 140 | + return b |
| 141 | +} |
0 commit comments