Add uid, gid, and permissions to OverlayFile struct
There is an existing test for this. uid, gid, and permissions seem consistent across test boxes.
This commit is contained in:
committed by
Jonathon Anderson
parent
a99b4eead2
commit
b6c33d9567
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
||||
|
||||
- Added override.conf for nm-wait-online-initrd.service with dracut.
|
||||
- Added userdocs for `wwctl node import` from yaml/csv.
|
||||
- Added uid, gid, and permissions to OverlayFile in REST API. #1925
|
||||
|
||||
### Fixed
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"syscall"
|
||||
|
||||
"github.com/swaggest/usecase"
|
||||
"github.com/swaggest/usecase/status"
|
||||
@@ -68,9 +69,12 @@ func getOverlayByName() usecase.Interactor {
|
||||
}
|
||||
|
||||
type OverlayFile struct {
|
||||
Overlay string `json:"overlay"`
|
||||
Path string `json:"path"`
|
||||
Contents string `json:"contents"`
|
||||
Overlay string `json:"overlay"`
|
||||
Path string `json:"path"`
|
||||
Contents string `json:"contents"`
|
||||
Perms os.FileMode `json:"perms"`
|
||||
Uid uint32 `json:"uid"`
|
||||
Gid uint32 `json:"gid"`
|
||||
rendered bool
|
||||
}
|
||||
|
||||
@@ -83,7 +87,23 @@ func (of *OverlayFile) Exists() bool {
|
||||
}
|
||||
|
||||
func (of *OverlayFile) readContents() (string, error) {
|
||||
f, err := os.ReadFile(of.FullPath())
|
||||
fullPath := of.FullPath()
|
||||
f, err := os.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
wwlog.Warn("os.ReadFile err %w", err)
|
||||
return "", err
|
||||
}
|
||||
// Populate the permissions, uid, and gid.
|
||||
s, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
wwlog.Warn("os.Stat err %w", err)
|
||||
return "", err
|
||||
}
|
||||
fileMode := s.Mode()
|
||||
of.Perms = fileMode & os.ModePerm
|
||||
sys := s.Sys()
|
||||
of.Uid = sys.(*syscall.Stat_t).Uid
|
||||
of.Gid = sys.(*syscall.Stat_t).Gid
|
||||
return string(f), err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -74,7 +75,22 @@ func TestOverlayAPI(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, resp.Body.Close())
|
||||
|
||||
assert.JSONEq(t, `{"overlay":"testoverlay","path":"email.ww","contents":"\n{{ if .Tags.email }}eMail: {{ .Tags.email }}{{else}} noMail{{- end }}\n"}`, string(body))
|
||||
// gid and uid values may vary depending on where this test is run. (local box, github, etc)
|
||||
// Assert the keys exist, but ignore the values.
|
||||
var data map[string]interface{}
|
||||
err = json.Unmarshal([]byte(body), &data)
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, data, "gid")
|
||||
assert.Contains(t, data, "uid")
|
||||
|
||||
// delete gid and uid from the map for comparison
|
||||
delete(data, "gid")
|
||||
delete(data, "uid")
|
||||
|
||||
body2, err := json.Marshal(data)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.JSONEq(t, `{"perms":420, "overlay":"testoverlay","path":"email.ww","contents":"\n{{ if .Tags.email }}eMail: {{ .Tags.email }}{{else}} noMail{{- end }}\n"}`, string(body2))
|
||||
})
|
||||
|
||||
t.Run("create an overlay", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user