Skip to content

Commit 63d67a9

Browse files
hmndmikesellitto
andcommitted
add test for #503
Co-authored-by: mikesellitto <mikesellitto@users.noreply.github.com>
1 parent c2e41ce commit 63d67a9

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

pkg/controllers/secrets_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"os"
21+
"path/filepath"
2022
"strings"
2123
"testing"
24+
"time"
2225

26+
"github.com/DopplerHQ/cli/pkg/utils"
2327
"github.com/stretchr/testify/assert"
2428
)
2529

@@ -106,3 +110,59 @@ func TestSecretsToBytes(t *testing.T) {
106110
t.Errorf("Unable to convert secrets to byte array in %s format", format)
107111
}
108112
}
113+
114+
func TestMountSecrets(t *testing.T) {
115+
if !utils.SupportsNamedPipes {
116+
t.Skip("Named pipes not supported on this platform")
117+
}
118+
119+
secrets := []byte(`{"SECRET_KEY":"secret_value"}`)
120+
mountPath := filepath.Join(t.TempDir(), "secrets_mount")
121+
122+
path, cleanup, err := MountSecrets(secrets, mountPath, 1)
123+
if !err.IsNil() {
124+
t.Fatalf("MountSecrets failed: %v", err.Err)
125+
}
126+
defer cleanup()
127+
128+
time.Sleep(50 * time.Millisecond)
129+
130+
content, readErr := os.ReadFile(path)
131+
assert.NoError(t, readErr)
132+
assert.Equal(t, string(secrets), string(content))
133+
}
134+
135+
func TestMountSecretsBrokenPipe(t *testing.T) {
136+
if !utils.SupportsNamedPipes {
137+
t.Skip("Named pipes not supported on this platform")
138+
}
139+
140+
// large data to exceed pipe buffer and trigger EPIPE when reader closes early
141+
secrets := make([]byte, 256*1024)
142+
for i := range secrets {
143+
secrets[i] = byte('A' + (i % 26))
144+
}
145+
mountPath := filepath.Join(t.TempDir(), "secrets_mount_epipe")
146+
147+
path, cleanup, err := MountSecrets(secrets, mountPath, 11)
148+
if !err.IsNil() {
149+
t.Fatalf("MountSecrets failed: %v", err.Err)
150+
}
151+
defer cleanup()
152+
153+
time.Sleep(50 * time.Millisecond)
154+
155+
for i := 0; i < 10; i++ {
156+
f, openErr := os.OpenFile(path, os.O_RDONLY, 0)
157+
if openErr != nil {
158+
continue
159+
}
160+
f.Read(make([]byte, 1))
161+
f.Close()
162+
time.Sleep(5 * time.Millisecond)
163+
}
164+
165+
content, readErr := os.ReadFile(path)
166+
assert.NoError(t, readErr, "mount should survive broken pipe")
167+
assert.Equal(t, secrets, content)
168+
}

0 commit comments

Comments
 (0)