fleet/server/contexts/installersize/installersize_test.go
Magnus Jensen da43bf8371
Request body limits (#39080)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves
https://github.com/fleetdm/confidential/issues/13934

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

## Testing

- [x] Added/updated automated tests
- [ ] QA'd all new/changed functionality manually
2026-02-05 10:29:53 -05:00

76 lines
1.9 KiB
Go

package installersize
import (
"context"
"testing"
"github.com/docker/go-units"
"github.com/stretchr/testify/assert"
)
func TestFormatSize(t *testing.T) {
tests := []struct {
name string
bytes int64
expected string
}{
// SI units are shorter
{"1 GB (SI shorter)", 1000000000, "1GB"},
{"500 MB (SI shorter)", 500000000, "500MB"},
{"1.5 GB (SI shorter)", 1500000000, "1.5GB"},
// Binary units are shorter
{"1 GiB (binary shorter)", 1073741824, "1GiB"},
{"512 MiB (binary shorter)", 536870912, "512MiB"},
{"1.5 GiB (binary shorter)", 1610612736, "1.5GiB"},
{"1 TiB (binary shorter)", 1099511627776, "1TiB"},
// Small values
{"1000 bytes", 1000, "1kB"},
{"1024 bytes", 1024, "1KiB"},
// Default max installer size (10 GiB)
{"default max (10 GiB)", MaxSoftwareInstallerSize, "10GiB"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Human(tt.bytes)
assert.Equal(t, tt.expected, result)
})
}
}
func TestDefaultMaxInstallerSize(t *testing.T) {
// Verify the default is 10 GiB
assert.Equal(t, int64(10*units.GiB), MaxSoftwareInstallerSize)
assert.Equal(t, "10GiB", Human(MaxSoftwareInstallerSize))
}
func TestFromContextWithValue(t *testing.T) {
ctx := context.Background()
customSize := int64(5 * units.GiB)
ctx = NewContext(ctx, customSize)
result := FromContext(ctx)
assert.Equal(t, customSize, result)
}
func TestFromContextWithoutValue(t *testing.T) {
ctx := context.Background()
result := FromContext(ctx)
assert.Equal(t, MaxSoftwareInstallerSize, result)
}
func TestNewContextOverwrite(t *testing.T) {
ctx := context.Background()
// Set first value
ctx = NewContext(ctx, int64(5*units.GiB))
assert.Equal(t, int64(5*units.GiB), FromContext(ctx))
// Overwrite with second value
ctx = NewContext(ctx, int64(20*units.GiB))
assert.Equal(t, int64(20*units.GiB), FromContext(ctx))
}