Skip to content

Commit

Permalink
fix: os name that required lowercase
Browse files Browse the repository at this point in the history
this changes the ValidOS() function that now converts the os
to lowercase before check them.

also updated the tests to use the Convey infra.

It may close #9.

Signed-off-by: Sebastian Webber <[email protected]>
  • Loading branch information
sebastianwebber committed Mar 19, 2024
1 parent 9a95cf0 commit 1fe6d98
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 37 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
Expand All @@ -41,13 +42,15 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/smarty/assertions v1.15.0 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/swaggo/files/v2 v2.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw=
Expand Down
18 changes: 14 additions & 4 deletions pkg/rules/os.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
package rules

import (
"strings"

"github.com/pgconfig/api/pkg/category"
"github.com/pgconfig/api/pkg/errors"
"github.com/pgconfig/api/pkg/input"
"github.com/pgconfig/api/pkg/input/bytes"
)

const (
Windows = "windows"
Linux = "linux"
Unix = "unix"
Darwin = "darwin"
)

// ValidOS validates the Operating System
func ValidOS(os string) error {
switch os {
case "windows":
case "linux":
case "unix", "darwin":
switch strings.ToLower(os) {
case Windows:
case Linux:
case Unix, Darwin:
default:
return errors.ErrorInvalidOS
}

return nil
}

Expand Down
81 changes: 48 additions & 33 deletions pkg/rules/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,58 @@ import (
"testing"

"github.com/pgconfig/api/pkg/category"
"github.com/pgconfig/api/pkg/errors"
"github.com/pgconfig/api/pkg/input"
"github.com/pgconfig/api/pkg/input/bytes"

. "github.com/smartystreets/goconvey/convey"
)

func Test_computeOS(t *testing.T) {
_, err := computeOS(&input.Input{OS: "xpto-wrong-os"}, &category.ExportCfg{})

if err == nil {
t.Error("should support only windows, linux and unix")
}

in := fakeInput()
in.OS = "windows"
in.PostgresVersion = 9.6

out, _ := computeOS(in, category.NewExportCfg(*in))

if out.Memory.SharedBuffers > 512*bytes.MB {
t.Error("should limit shared_buffers to 512MB until pg 10 on windows")
}

in = fakeInput()
in.OS = "windows"
in.PostgresVersion = 12.0

out, _ = computeOS(in, category.NewExportCfg(*in))

if out.Storage.EffectiveIOConcurrency > 0 {
t.Error("should limit effective_io_concurrency to 0 on platforms that lack posix_fadvise()")
}

in = fakeInput()
in.TotalRAM = 120 * bytes.GB

out, _ = computeOS(in, category.NewExportCfg(*in))

if out.Memory.SharedBuffers < 25*bytes.GB {
t.Error("should not limit shared_buffers on versions greater or equal than pg 11")
}
Convey("Operating systems", t, func() {
Convey("should return error on non-supported operating systems", func() {
_, err := computeOS(&input.Input{OS: "xpto-wrong-os"}, &category.ExportCfg{})
So(err, ShouldResemble, errors.ErrorInvalidOS)
})

Convey("should ignore case for all operating systems supported", func() {
in := fakeInput()
in.OS = "lINUx"
in.TotalRAM = 120 * bytes.GB

_, err := computeOS(in, category.NewExportCfg(*in))
So(err, ShouldBeNil)
})

Convey("should limit shared_buffers to 512MB until pg 10 on windows", func() {
in := fakeInput()
in.OS = "windows"
in.PostgresVersion = 9.6

out, err := computeOS(in, category.NewExportCfg(*in))
So(err, ShouldBeNil)
So(out.Memory.SharedBuffers, ShouldEqual, 512*bytes.MB)
})

Convey("should limit effective_io_concurrency to 0 on platforms that lack posix_fadvise()", func() {
in := fakeInput()
in.OS = Windows
in.PostgresVersion = 12.0

out, err := computeOS(in, category.NewExportCfg(*in))
So(err, ShouldBeNil)
So(out.Storage.EffectiveIOConcurrency, ShouldEqual, 0)
})

Convey("should not limit shared_buffers on versions greater or equal than pg 11", func() {
in := fakeInput()
in.PostgresVersion = 14.0
in.TotalRAM = 120 * bytes.GB

out, err := computeOS(in, category.NewExportCfg(*in))
So(err, ShouldBeNil)
So(out.Memory.SharedBuffers, ShouldBeGreaterThan, 25*bytes.GB)
})
})
}

0 comments on commit 1fe6d98

Please sign in to comment.