Skip to content

Commit

Permalink
(feat): Add test coverage for layer.ChangePermissionsLayer()
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Jan 3, 2024
1 parent a98c72f commit 0c1a82f
Show file tree
Hide file tree
Showing 2 changed files with 268 additions and 1 deletion.
1 change: 0 additions & 1 deletion internal/layer/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func (fdl *ChangePermissionsLayer) Validate(c *config.Config) error {
if len(cd.MountPoint) == 0 {
continue
}
// When permissions is not provided
if cd.Permissions == 0 {
continue
}
Expand Down
268 changes: 268 additions & 0 deletions internal/layer/permissions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
package layer

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/reecetech/ebs-bootstrap/internal/action"
"github.com/reecetech/ebs-bootstrap/internal/backend"
"github.com/reecetech/ebs-bootstrap/internal/config"
"github.com/reecetech/ebs-bootstrap/internal/model"
"github.com/reecetech/ebs-bootstrap/internal/utils"
)

func TestChangePermissionsLayerModify(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
Files map[string]*model.File
CmpOption cmp.Option
ExpectedOutput []action.Action
ExpectedError error
}{
{
Name: "Change the Permissions of The Mount Point",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
Permissions: model.FilePermissions(0755),
},
},
},
Files: map[string]*model.File{
"/mnt/foo": {
Path: "/mnt/foo",
Type: model.Directory,
Permissions: model.FilePermissions(0644),
},
},
CmpOption: cmp.AllowUnexported(action.ChangePermissionsAction{}),
ExpectedOutput: []action.Action{
action.NewChangePermissionsAction("/mnt/foo", model.FilePermissions(0755), nil).SetMode(model.Force),
},
ExpectedError: nil,
},
{
Name: "Invalid + Mount Point Does Not Exist",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
Permissions: model.FilePermissions(0755),
},
},
},
Files: map[string]*model.File{},
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: nil,
ExpectedError: fmt.Errorf("🔴 /mnt/foo is either not a directory or does not exist"),
},
{
Name: "Invalid + Mount Point Is A Regular File",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
Permissions: model.FilePermissions(0755),
},
},
},
Files: map[string]*model.File{
"/mnt/foo": {
Path: "/mnt/foo",
Type: model.RegularFile,
},
},
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: nil,
ExpectedError: fmt.Errorf("🔴 /mnt/foo is either not a directory or does not exist"),
},
{
Name: "Mount Point Permissions Match Requested Permissions",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
Permissions: model.FilePermissions(0755),
},
},
},
Files: map[string]*model.File{
"/mnt/foo": {
Path: "/mnt/foo",
Type: model.Directory,
Permissions: model.FilePermissions(0755),
},
},
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: []action.Action{},
ExpectedError: nil,
},
{
Name: "Skip + No Mount Point",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {},
},
},
Files: map[string]*model.File{},
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: []action.Action{},
ExpectedError: nil,
},
{
Name: "Skip + No Permissions Declared",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
},
},
},
Files: map[string]*model.File{
"/mnt/foo": {
Path: "/mnt/foo",
Type: model.Directory,
},
},
CmpOption: cmp.AllowUnexported(),
ExpectedOutput: []action.Action{},
ExpectedError: nil,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
lfb := backend.NewMockLinuxFileBackend(subtest.Files)
cpl := NewChangePermissionsLayer(lfb)
actions, err := cpl.Modify(subtest.Config)
utils.CheckError("cpl.Modify()", t, subtest.ExpectedError, err)
utils.CheckOutput("cpl.Modify()", t, subtest.ExpectedOutput, actions, subtest.CmpOption)
})
}
}

func TestChangePermissionsLayerValidate(t *testing.T) {
subtests := []struct {
Name string
Config *config.Config
Files map[string]*model.File
ExpectedError error
}{
{
Name: "Mount Point Permissions Match Requested Permissions",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
Permissions: model.FilePermissions(0755),
},
},
},
Files: map[string]*model.File{
"/mnt/foo": {
Path: "/mnt/foo",
Type: model.Directory,
Permissions: model.FilePermissions(0755),
},
},
ExpectedError: nil,
},
{
Name: "Mount Point Permissions Do Not Match Requested Permissions",
Config: &config.Config{
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
Permissions: model.FilePermissions(0755),
},
},
},
Files: map[string]*model.File{
"/mnt/foo": {
Path: "/mnt/foo",
Type: model.Directory,
Permissions: model.FilePermissions(0644),
},
},
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: Failed permissions validation checks. /mnt/foo Permissions Expected=0755, Actual=0644"),
},
{
Name: "Invalid + Mount Point Is A Regular File",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
Permissions: model.FilePermissions(0755),
},
},
},
Files: map[string]*model.File{
"/mnt/foo": {
Path: "/mnt/foo",
Type: model.RegularFile,
},
},
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: Failed ownership validation checks. /mnt/foo is either not a directory or does not exist"),
},
{
Name: "Invalid + Mount Point Does Not Exist",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
Permissions: model.FilePermissions(0755),
},
},
},
Files: map[string]*model.File{},
ExpectedError: fmt.Errorf("🔴 /dev/xvdf: Failed ownership validation checks. /mnt/foo is either not a directory or does not exist"),
},
{
Name: "Skip + No Mount Point",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {},
},
},
Files: map[string]*model.File{},
ExpectedError: nil,
},
{
Name: "Skip + No Permissions Declared",
Config: &config.Config{
Defaults: config.Options{Mode: model.Force},
Devices: map[string]config.Device{
"/dev/xvdf": {
MountPoint: "/mnt/foo",
},
},
},
Files: map[string]*model.File{
"/mnt/foo": {
Path: "/mnt/foo",
Type: model.Directory,
},
},
ExpectedError: nil,
},
}
for _, subtest := range subtests {
t.Run(subtest.Name, func(t *testing.T) {
lfb := backend.NewMockLinuxFileBackend(subtest.Files)
cpl := NewChangePermissionsLayer(lfb)
err := cpl.Validate(subtest.Config)
utils.CheckError("cpl.Modify()", t, subtest.ExpectedError, err)
})
}
}

0 comments on commit 0c1a82f

Please sign in to comment.