Skip to content

Commit

Permalink
ztp: add tests and clean up the code with correct variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelsoccupied committed Mar 14, 2022
1 parent 7e2d93b commit 783b048
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 12 deletions.
27 changes: 15 additions & 12 deletions ztp/siteconfig-generator/siteConfig/siteConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,38 +244,41 @@ type Partitions struct {
Label string `yaml:"-"`
}

type PartitionsUserInput Partitions

func (prt *Partitions) UnmarshalYAML(unmarshal func(interface{}) error) error {
type PartitionsDefault Partitions
var defaults = PartitionsDefault{}
err := unmarshal(&defaults)

if defaults.Start < 25000 {
var userInput = PartitionsUserInput{}

err := unmarshal(&userInput)

if userInput.Start < 25000 {
// 25000 min value, otherwise root partition is too small and the installation will fail
err = fmt.Errorf("start value too small. must be over 25000")
}

if defaults.Size <= 0 {
if userInput.Size <= 0 {
err = fmt.Errorf("choose an appropritate disk size. must be greater than 0")
}

// it's a required field
if defaults.MountPoint == "" {
if userInput.MountPoint == "" {
err = fmt.Errorf("must provide a path for mount_point. e.g /var/path")
}
// run a clean to ensure path is not malformed
defaults.MountPoint = path.Clean(defaults.MountPoint)
userInput.MountPoint = path.Clean(userInput.MountPoint)
// ensure path is absolute
if !(path.IsAbs(defaults.MountPoint)) {
defaults.MountPoint = path.Join("/", defaults.MountPoint)
if !(path.IsAbs(userInput.MountPoint)) {
userInput.MountPoint = path.Join("/", userInput.MountPoint)
}

// generate label from path
defaults.Label = strings.ReplaceAll(defaults.MountPoint[1:], "/", "-")
userInput.Label = strings.ReplaceAll(userInput.MountPoint[1:], "/", "-")

// sensitive and depends on the filesystem.path
defaults.MountFileName = unit.UnitNamePathEscape(defaults.MountPoint) + ".mount"
userInput.MountFileName = unit.UnitNamePathEscape(userInput.MountPoint) + ".mount"

*prt = Partitions(defaults)
*prt = Partitions(userInput)
return err
}

Expand Down
133 changes: 133 additions & 0 deletions ztp/siteconfig-generator/siteConfig/siteConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package siteConfig

import (
"fmt"
"github.com/google/go-cmp/cmp"
"strings"
"testing"

Expand Down Expand Up @@ -370,3 +371,135 @@ spec:
}
}
}

func TestPartitions_UnmarshalYAML(t *testing.T) {
type fields struct {
MountPoint string
Size int
Start int
MountFileName string
Label string
}
type args struct {
unmarshal func(interface{}) error
}

tests := []struct {
name string
fields fields
args args
wantErr bool
expectedResult *Partitions
expectedError string
}{
{
name: "check if the mount file name is generated correctly",
args: args{unmarshal: func(i interface{}) error {
origin := i.(*PartitionsUserInput)
*origin = PartitionsUserInput{
MountPoint: "/var/imageregistry",
Start: 25000,
Size: 100000,
}
return nil
}},
wantErr: false,
expectedResult: &Partitions{
MountPoint: "/var/imageregistry",
Size: 100000,
Start: 25000,
MountFileName: "var-imageregistry.mount",
Label: "var-imageregistry",
},
},
{
name: "expect error when start size is too small",
args: args{unmarshal: func(i interface{}) error {
origin := i.(*PartitionsUserInput)
*origin = PartitionsUserInput{
MountPoint: "/var/imageregistry",
Start: 0,
Size: 100000,
}
return nil
}},
wantErr: true,
expectedError: "start value too small. must be over 25000",
},
{
name: "expect error when the partition size is too small",
args: args{unmarshal: func(i interface{}) error {
origin := i.(*PartitionsUserInput)
*origin = PartitionsUserInput{
MountPoint: "/var/imageregistry",
Start: 25000,
Size: 0,
}
return nil
}},
wantErr: true,
expectedError: "choose an appropritate disk size. must be greater than 0",
},
{
name: "mount file name and labels are correctly generated",
args: args{unmarshal: func(i interface{}) error {
origin := i.(*PartitionsUserInput)
*origin = PartitionsUserInput{
MountPoint: "/my/path/another/dir",
Size: 100000,
Start: 25000,
}
return nil
}},
wantErr: false,
expectedResult: &Partitions{
MountPoint: "/my/path/another/dir",
Size: 100000,
Start: 25000,
Label: "my-path-another-dir",
MountFileName: "my-path-another-dir.mount",
},
},
{
name: "mount point is converted to be absolute if needed",
args: args{unmarshal: func(i interface{}) error {
origin := i.(*PartitionsUserInput)
*origin = PartitionsUserInput{
MountPoint: "path",
Size: 100000,
Start: 25000,
}
return nil
}},
wantErr: false,
expectedResult: &Partitions{
MountPoint: "/path",
Size: 100000,
Start: 25000,
Label: "path",
MountFileName: "path.mount",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
prt := &Partitions{
MountPoint: tt.fields.MountPoint,
Size: tt.fields.Size,
Start: tt.fields.Start,
MountFileName: tt.fields.MountFileName,
Label: tt.fields.Label,
}
err := prt.UnmarshalYAML(tt.args.unmarshal)
if !tt.wantErr {
if !(cmp.Equal(prt, tt.expectedResult)) {
t.Errorf("expected %v, got %v", tt.expectedResult, prt)
}
} else {
if !(cmp.Equal(err, tt.expectedError)) {
assert.EqualErrorf(t, err, tt.expectedError, "expected %v, got %v", err, tt.expectedError)
}
}
})
}
}

0 comments on commit 783b048

Please sign in to comment.