mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-01-23 13:51:06 -05:00
converted config file from JSON to YAML. (#132)
converted config file from JSON to YAML
This commit is contained in:
parent
eb9a959353
commit
b8d1cc2b75
@ -6,12 +6,14 @@ package file
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// Option is a bitmask of options for file operations.
|
||||
@ -93,6 +95,30 @@ func (h *Handler) WriteJSON(name string, content any, options Option) error {
|
||||
return h.Write(name, jsonData, options)
|
||||
}
|
||||
|
||||
// ReadYAML reads a YAML file from name and unmarshals it into the content interface.
|
||||
// The interface content must be a pointer to a YAML marchalable object.
|
||||
func (h *Handler) ReadYAML(name string, content any) error {
|
||||
data, err := h.Read(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return yaml.Unmarshal(data, content)
|
||||
}
|
||||
|
||||
// WriteYAML marshals the content interface to YAML and writes it to the path with the given name.
|
||||
func (h *Handler) WriteYAML(name string, content any, options Option) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = errors.New("recovered from panic")
|
||||
}
|
||||
}()
|
||||
data, err := yaml.Marshal(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return h.Write(name, data, options)
|
||||
}
|
||||
|
||||
// Remove deletes the file with the given name.
|
||||
func (h *Handler) Remove(name string) error {
|
||||
return h.fs.Remove(name)
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/spf13/afero"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestReadJSON(t *testing.T) {
|
||||
@ -151,6 +152,148 @@ func TestWriteJSON(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadYAML(t *testing.T) {
|
||||
type testContent struct {
|
||||
First string
|
||||
Second int
|
||||
}
|
||||
someContent := testContent{
|
||||
First: "first",
|
||||
Second: 2,
|
||||
}
|
||||
yamlContent, err := yaml.Marshal(someContent)
|
||||
require.NoError(t, err)
|
||||
|
||||
testCases := map[string]struct {
|
||||
fs afero.Fs
|
||||
setupFs func(fs *afero.Afero) error
|
||||
name string
|
||||
wantContent any
|
||||
wantErr bool
|
||||
}{
|
||||
"successful read": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
name: "test/config.yaml",
|
||||
setupFs: func(fs *afero.Afero) error { return fs.WriteFile("test/config.yaml", yamlContent, 0o755) },
|
||||
wantContent: someContent,
|
||||
},
|
||||
"file not existent": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
name: "test/config.yaml",
|
||||
wantErr: true,
|
||||
},
|
||||
"file not yaml": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
name: "test/config.yaml",
|
||||
setupFs: func(fs *afero.Afero) error { return fs.WriteFile("test/config.yaml", []byte{0x1}, 0o755) },
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
handler := NewHandler(tc.fs)
|
||||
if tc.setupFs != nil {
|
||||
require.NoError(tc.setupFs(handler.fs))
|
||||
}
|
||||
|
||||
resultContent := &testContent{}
|
||||
if tc.wantErr {
|
||||
assert.Error(handler.ReadYAML(tc.name, resultContent))
|
||||
} else {
|
||||
assert.NoError(handler.ReadYAML(tc.name, resultContent))
|
||||
assert.Equal(tc.wantContent, *resultContent)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteYAML(t *testing.T) {
|
||||
type testContent struct {
|
||||
First string
|
||||
Second int
|
||||
}
|
||||
someContent := testContent{
|
||||
First: "first",
|
||||
Second: 2,
|
||||
}
|
||||
notMarshalableContent := struct{ Foo chan int }{Foo: make(chan int)}
|
||||
|
||||
testCases := map[string]struct {
|
||||
fs afero.Fs
|
||||
setupFs func(af afero.Afero) error
|
||||
name string
|
||||
content any
|
||||
options Option
|
||||
wantErr bool
|
||||
}{
|
||||
"successful write": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
name: "test/statefile",
|
||||
content: someContent,
|
||||
},
|
||||
"successful overwrite": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
setupFs: func(af afero.Afero) error { return af.WriteFile("test/statefile", []byte{}, 0o644) },
|
||||
name: "test/statefile",
|
||||
content: someContent,
|
||||
options: OptOverwrite,
|
||||
},
|
||||
"read only fs": {
|
||||
fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
|
||||
name: "test/statefile",
|
||||
content: someContent,
|
||||
wantErr: true,
|
||||
},
|
||||
"file already exists": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
setupFs: func(af afero.Afero) error { return af.WriteFile("test/statefile", []byte{}, 0o644) },
|
||||
name: "test/statefile",
|
||||
content: someContent,
|
||||
wantErr: true,
|
||||
},
|
||||
"marshal error": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
name: "test/statefile",
|
||||
content: notMarshalableContent,
|
||||
wantErr: true,
|
||||
},
|
||||
"mkdirAll works": {
|
||||
fs: afero.NewMemMapFs(),
|
||||
name: "test/statefile",
|
||||
content: someContent,
|
||||
options: OptMkdirAll,
|
||||
},
|
||||
// TODO: add tests for mkdirAll actually creating the necessary folders when https://github.com/spf13/afero/issues/270 is fixed.
|
||||
// Currently, MemMapFs will create files in nonexistent directories due to a bug in afero,
|
||||
// making it impossible to test the actual behavior of the mkdirAll parameter.
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
||||
handler := NewHandler(tc.fs)
|
||||
if tc.setupFs != nil {
|
||||
require.NoError(tc.setupFs(afero.Afero{Fs: tc.fs}))
|
||||
}
|
||||
|
||||
if tc.wantErr {
|
||||
assert.Error(handler.WriteYAML(tc.name, tc.content, tc.options))
|
||||
} else {
|
||||
assert.NoError(handler.WriteYAML(tc.name, tc.content, tc.options))
|
||||
resultContent := &testContent{}
|
||||
assert.NoError(handler.ReadYAML(tc.name, resultContent))
|
||||
assert.Equal(tc.content, *resultContent)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require := require.New(t)
|
||||
|
@ -41,11 +41,11 @@ var (
|
||||
// All fields in this struct and its child structs have pointer types
|
||||
// to ensure the default values of the actual type is not confused with an omitted value.
|
||||
type Config struct {
|
||||
CoordinatorPort *string `json:"coordinatorport,omitempty"`
|
||||
AutoscalingNodeGroupsMin *int `json:"autoscalingnodegroupsmin,omitempty"`
|
||||
AutoscalingNodeGroupsMax *int `json:"autoscalingnodegroupsmax,omitempty"`
|
||||
StateDiskSizeGB *int `json:"statedisksizegb,omitempty"`
|
||||
Provider *ProviderConfig `json:"provider,omitempty"`
|
||||
CoordinatorPort *string `yaml:"coordinatorPort,omitempty"`
|
||||
AutoscalingNodeGroupsMin *int `yaml:"autoscalingNodeGroupsMin,omitempty"`
|
||||
AutoscalingNodeGroupsMax *int `yaml:"autoscalingNodeGroupsMax,omitempty"`
|
||||
StateDiskSizeGB *int `yaml:"StateDisksizeGB,omitempty"`
|
||||
Provider *ProviderConfig `yaml:"provider,omitempty"`
|
||||
}
|
||||
|
||||
// Default returns a struct with the default config.
|
||||
@ -213,7 +213,7 @@ func FromFile(fileHandler file.Handler, name string) (*Config, error) {
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
if err := fileHandler.ReadJSON(name, conf); err != nil {
|
||||
if err := fileHandler.ReadYAML(name, conf); err != nil {
|
||||
return nil, fmt.Errorf("could not load config from file %s: %w", name, err)
|
||||
}
|
||||
return conf, nil
|
||||
@ -221,44 +221,44 @@ func FromFile(fileHandler file.Handler, name string) (*Config, error) {
|
||||
|
||||
// ProviderConfig are cloud-provider specific configuration values used by the CLI.
|
||||
type ProviderConfig struct {
|
||||
EC2 *EC2Config `json:"ec2config,omitempty"`
|
||||
Azure *AzureConfig `json:"azureconfig,omitempty"`
|
||||
GCP *GCPConfig `json:"gcpconfig,omitempty"`
|
||||
QEMU *QEMUConfig `json:"qemuconfig,omitempty"`
|
||||
EC2 *EC2Config `yaml:"ec2Config,omitempty"`
|
||||
Azure *AzureConfig `yaml:"azureConfig,omitempty"`
|
||||
GCP *GCPConfig `yaml:"gcpConfig,omitempty"`
|
||||
QEMU *QEMUConfig `yaml:"qemuConfig,omitempty"`
|
||||
}
|
||||
|
||||
// EC2Config are AWS EC2 specific configuration values used by the CLI.
|
||||
type EC2Config struct {
|
||||
Image *string `json:"image,omitempty"`
|
||||
Tags *[]ec2.Tag `json:"tags,omitempty"`
|
||||
SecurityGroupInput *awsClient.SecurityGroupInput `json:"securitygroupinput,omitempty"`
|
||||
Image *string `yaml:"image,omitempty"`
|
||||
Tags *[]ec2.Tag `yaml:"tags,omitempty"`
|
||||
SecurityGroupInput *awsClient.SecurityGroupInput `yaml:"securityGroupInput,omitempty"`
|
||||
}
|
||||
|
||||
// AzureConfig are Azure specific configuration values used by the CLI.
|
||||
type AzureConfig struct {
|
||||
SubscriptionID *string `json:"subscription,omitempty"` // TODO: This will be user input
|
||||
TenantID *string `json:"tenant,omitempty"` // TODO: This will be user input
|
||||
Location *string `json:"location,omitempty"` // TODO: This will be user input
|
||||
Image *string `json:"image,omitempty"`
|
||||
NetworkSecurityGroupInput *azureClient.NetworkSecurityGroupInput `json:"networksecuritygroupinput,omitempty"`
|
||||
Measurements *map[uint32][]byte `json:"measurements,omitempty"`
|
||||
UserAssignedIdentity *string `json:"userassignedidentity,omitempty"`
|
||||
SubscriptionID *string `yaml:"subscription,omitempty"` // TODO: This will be user input
|
||||
TenantID *string `yaml:"tenant,omitempty"` // TODO: This will be user input
|
||||
Location *string `yaml:"location,omitempty"` // TODO: This will be user input
|
||||
Image *string `yaml:"image,omitempty"`
|
||||
NetworkSecurityGroupInput *azureClient.NetworkSecurityGroupInput `yaml:"networkSecurityGroupInput,omitempty"`
|
||||
Measurements *map[uint32][]byte `yaml:"measurements,omitempty"`
|
||||
UserAssignedIdentity *string `yaml:"userassignedIdentity,omitempty"`
|
||||
}
|
||||
|
||||
// GCPConfig are GCP specific configuration values used by the CLI.
|
||||
type GCPConfig struct {
|
||||
Project *string `json:"project,omitempty"` // TODO: This will be user input
|
||||
Region *string `json:"region,omitempty"` // TODO: This will be user input
|
||||
Zone *string `json:"zone,omitempty"` // TODO: This will be user input
|
||||
Image *string `json:"image,omitempty"`
|
||||
FirewallInput *gcpClient.FirewallInput `json:"firewallinput,omitempty"`
|
||||
VPCsInput *gcpClient.VPCsInput `json:"vpcsinput,omitempty"`
|
||||
ServiceAccountRoles *[]string `json:"serviceaccountroles,omitempty"`
|
||||
Measurements *map[uint32][]byte `json:"measurements,omitempty"`
|
||||
Project *string `yaml:"project,omitempty"` // TODO: This will be user input
|
||||
Region *string `yaml:"region,omitempty"` // TODO: This will be user input
|
||||
Zone *string `yaml:"zone,omitempty"` // TODO: This will be user input
|
||||
Image *string `yaml:"image,omitempty"`
|
||||
FirewallInput *gcpClient.FirewallInput `yaml:"firewallInput,omitempty"`
|
||||
VPCsInput *gcpClient.VPCsInput `yaml:"vpcsInput,omitempty"`
|
||||
ServiceAccountRoles *[]string `yaml:"serviceAccountRoles,omitempty"`
|
||||
Measurements *map[uint32][]byte `yaml:"measurements,omitempty"`
|
||||
}
|
||||
|
||||
type QEMUConfig struct {
|
||||
PCRs *map[uint32][]byte `json:"pcrs,omitempty"`
|
||||
PCRs *map[uint32][]byte `yaml:"pcrs,omitempty"`
|
||||
}
|
||||
|
||||
func pcrPtr(pcrs map[uint32][]byte) *map[uint32][]byte {
|
||||
|
@ -77,7 +77,7 @@ func TestFromFile(t *testing.T) {
|
||||
require := require.New(t)
|
||||
|
||||
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
||||
require.NoError(fileHandler.WriteJSON(configName, tc.from, file.OptNone))
|
||||
require.NoError(fileHandler.WriteYAML(configName, tc.from, file.OptNone))
|
||||
|
||||
result, err := FromFile(fileHandler, tc.configName)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user