Skip to content

Commit

Permalink
(feat): Add testing for Config
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Dec 27, 2023
1 parent 1115c64 commit 536a779
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/ebs-bootstrap*
coverage.html
14 changes: 14 additions & 0 deletions build/coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -euo pipefail

# https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "${SCRIPT_DIR}/.."

go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out -o coverage.html
rm -rf coverage.out

# The HTML file can either be opened locally or served through HTTP using a CLI tool like miniserve
# https://github.com/svenstaro/miniserve
# e.g `miniserve coverage.html`
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require gopkg.in/yaml.v2 v2.4.0
require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqy
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
46 changes: 19 additions & 27 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,16 @@ type Flag struct {
}

type Device struct {
Fs model.FileSystem `yaml:"fs"`
MountPoint string `yaml:"mountPoint"`
MountOptions model.MountOptions `yaml:"mountOptions"`
User string `yaml:"user"`
Group string `yaml:"group"`
Label string `yaml:"label"`
Permissions model.FilePermissions `yaml:"permissions"`
Mode model.Mode `yaml:"mode"`
Remount bool `yaml:"remount"`
ResizeFs bool `yaml:"resizeFs"`
ResizeThreshold float64 `yaml:"resizeThreshold"`
Fs model.FileSystem `yaml:"fs"`
MountPoint string `yaml:"mountPoint"`
User string `yaml:"user"`
Group string `yaml:"group"`
Label string `yaml:"label"`
Permissions model.FilePermissions `yaml:"permissions"`
Options `yaml:",inline"`
}

type Defaults struct {
Mode model.Mode `yaml:"mode"`
Remount bool `yaml:"remount"`
MountOptions model.MountOptions `yaml:"mountOptions"`
ResizeFs bool `yaml:"resizeFs"`
ResizeThreshold float64 `yaml:"resizeThreshold"`
}

type Overrides struct {
type Options struct {
Mode model.Mode `yaml:"mode"`
Remount bool `yaml:"remount"`
MountOptions model.MountOptions `yaml:"mountOptions"`
Expand All @@ -57,9 +45,9 @@ type Overrides struct {
// We don't export "overrides" as this is an attribute that is used
// internally to store the state of flag overrides
type Config struct {
Defaults Defaults `yaml:"defaults"`
Defaults Options `yaml:"defaults"`
Devices map[string]Device `yaml:"devices"`
overrides Overrides
overrides Options
}

func New(args []string) (*Config, error) {
Expand All @@ -70,23 +58,27 @@ func New(args []string) (*Config, error) {
return nil, fmt.Errorf("🔴 Failed to parse provided flags")
}

// Create config structure
c := (&Config{}).setOverrides(f)

// Load config file into memory
file, err := os.ReadFile(f.Config)
if err != nil {
return nil, err
if os.IsNotExist(err) {
return nil, fmt.Errorf("🔴 %s: File not found", f.Config)
}
return nil, fmt.Errorf("🔴 %s: %v", f.Config, err)
}

// Create config structure
c := &Config{}

// Unmarshal YAML file from memory into struct
err = yaml.UnmarshalStrict(file, c)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return nil, fmt.Errorf("🔴 %s: Failed to ingest malformed config", f.Config)
}

return c, nil
// Inject flag overrides into config
return c.setOverrides(f), nil
}

func parseFlags(program string, args []string) (*Flag, error) {
Expand Down
Loading

0 comments on commit 536a779

Please sign in to comment.