internal/osbuild: Add GCP guest-agent conf stage

Add GCP guest-agent conf stage test
This commit is contained in:
fkolwa 2022-08-09 10:14:54 +02:00 committed by Tomáš Hozza
parent d13347e1ca
commit 1fbdb21f6b
2 changed files with 202 additions and 0 deletions

View file

@ -0,0 +1,118 @@
package osbuild
import (
"fmt"
)
type GcpGuestAgentConfigScopeValue string
const (
GcpGuestAgentConfigScopeDistro GcpGuestAgentConfigScopeValue = "distro"
GcpGuestAgentConfigScopeInstance GcpGuestAgentConfigScopeValue = "instance"
)
type GcpGuestAgentConfigAccounts struct {
DeprovisionRemove *bool `json:"deprovision_remove,omitempty"`
Groups []string `json:"groups,omitempty"`
UseraddCmd string `json:"useradd_cmd,omitempty"`
UserdelCmd string `json:"userdel_cmd,omitempty"`
UsermodCmd string `json:"usermod_cmd,omitempty"`
GpasswdAddCmd string `json:"gpasswd_add_cmd,omitempty"`
GpasswdRemoveCmd string `json:"gpasswd_remove_cmd,omitempty"`
GroupaddCmd string `json:"groupadd_cmd,omitempty"`
}
type GcpGuestAgentConfigDaemons struct {
AccountsDaemon *bool `json:"accounts_daemon,omitempty"`
ClockSkewDaemon *bool `json:"clock_skew_daemon,omitempty"`
NetworkDaemon *bool `json:"network_daemon,omitempty"`
}
type GcpGuestAgentConfigInstanceSetup struct {
HostKeyTypes []string `json:"host_key_types,omitempty"`
OptimizeLocalSsd *bool `json:"optimize_local_ssd,omitempty"`
NetworkEnabled *bool `json:"network_enabled,omitempty"`
SetBotoConfig *bool `json:"set_boto_config,omitempty"`
SetHostKeys *bool `json:"set_host_keys,omitempty"`
SetMultiqueue *bool `json:"set_multiqueue,omitempty"`
}
type GcpGuestAgentConfigIpForwarding struct {
EthernetProtoId string `json:"ethernet_proto_id,omitempty"`
IpAliases *bool `json:"ip_aliases,omitempty"`
TargetInstanceIps *bool `json:"target_instance_ips,omitempty"`
}
type GcpGuestAgentConfigMetadataScripts struct {
DefaultShell string `json:"default_shell,omitempty"`
RunDir string `json:"run_dir,omitempty"`
Startup *bool `json:"startup,omitempty"`
Shutdown *bool `json:"shutdown,omitempty"`
}
type GcpGuestAgentConfigNetworkInterfaces struct {
Setup *bool `json:"setup,omitempty"`
IpForwarding *bool `json:"ip_forwarding,omitempty"`
DhcpCommand string `json:"dhcp_command,omitempty"`
}
type GcpGuestAgentConfig struct {
Accounts *GcpGuestAgentConfigAccounts `json:"Accounts,omitempty"`
Daemons *GcpGuestAgentConfigDaemons `json:"Daemons,omitempty"`
InstanceSetup *GcpGuestAgentConfigInstanceSetup `json:"InstanceSetup,omitempty"`
IpForwarding *GcpGuestAgentConfigIpForwarding `json:"IpForwarding,omitempty"`
MetadataScripts *GcpGuestAgentConfigMetadataScripts `json:"MetadataScripts,omitempty"`
NetworkInterfaces *GcpGuestAgentConfigNetworkInterfaces `json:"NetworkInterfaces,omitempty"`
}
type GcpGuestAgentConfigOptions struct {
ConfigScope GcpGuestAgentConfigScopeValue `json:"config_scope,omitempty"`
Config *GcpGuestAgentConfig `json:"config"`
}
func (GcpGuestAgentConfigOptions) isStageOptions() {}
func (o GcpGuestAgentConfigOptions) validate() error {
allowedScopeValues := []GcpGuestAgentConfigScopeValue{
GcpGuestAgentConfigScopeDistro,
GcpGuestAgentConfigScopeInstance,
"", // default empty value when the option is not set
}
valid := false
for _, value := range allowedScopeValues {
if o.ConfigScope == value {
valid = true
break
}
}
if !valid {
return fmt.Errorf(
"scope %q doesn't conform to schema (%s). Must be one of [distro, instance]",
o.ConfigScope,
repoFilenameRegex,
)
}
if o.Config == nil {
return fmt.Errorf("config property is required")
}
if (GcpGuestAgentConfig{}) == *o.Config {
return fmt.Errorf("at least one config section must be defined")
}
return nil
}
func NewGcpGuestAgentConfigStage(options *GcpGuestAgentConfigOptions) *Stage {
if err := options.validate(); err != nil {
panic(err)
}
return &Stage{
Type: "org.osbuild.gcp.guest-agent.conf",
Options: options,
}
}

View file

@ -0,0 +1,84 @@
package osbuild
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewGcpGuestAgentConfigOptionsValidate(t *testing.T) {
tests := []struct {
name string
options GcpGuestAgentConfigOptions
err bool
}{
{
name: "empty-config",
options: GcpGuestAgentConfigOptions{},
err: true,
},
{
name: "empty-config",
options: GcpGuestAgentConfigOptions{
ConfigScope: GcpGuestAgentConfigScopeDistro,
Config: &GcpGuestAgentConfig{},
},
err: true,
},
{
name: "invalid-ConfigScope",
options: GcpGuestAgentConfigOptions{
ConfigScope: "incorrect",
Config: &GcpGuestAgentConfig{
Accounts: &GcpGuestAgentConfigAccounts{
Groups: []string{"group1", "group2"},
},
},
},
err: true,
},
{
name: "valid-data",
options: GcpGuestAgentConfigOptions{
ConfigScope: GcpGuestAgentConfigScopeDistro,
Config: &GcpGuestAgentConfig{
Accounts: &GcpGuestAgentConfigAccounts{
Groups: []string{"group1", "group2"},
},
},
},
err: false,
},
}
for idx, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.err {
assert.Errorf(t, tt.options.validate(), "%q didn't return an error [idx: %d]", tt.name, idx)
assert.Panics(t, func() { NewGcpGuestAgentConfigStage(&tt.options) })
} else {
assert.NoErrorf(t, tt.options.validate(), "%q returned an error [idx: %d]", tt.name, idx)
assert.NotPanics(t, func() { NewGcpGuestAgentConfigStage(&tt.options) })
}
})
}
}
func TestNewGcpGuestAgentConfigStage(t *testing.T) {
expectedStage := &Stage{
Type: "org.osbuild.gcp.guest-agent.conf",
Options: &GcpGuestAgentConfigOptions{
Config: &GcpGuestAgentConfig{
Accounts: &GcpGuestAgentConfigAccounts{
Groups: []string{"group1", "group2"},
},
},
},
}
actualStage := NewGcpGuestAgentConfigStage(&GcpGuestAgentConfigOptions{
Config: &GcpGuestAgentConfig{
Accounts: &GcpGuestAgentConfigAccounts{
Groups: []string{"group1", "group2"},
},
},
})
assert.Equal(t, expectedStage, actualStage)
}