Skip to content

Commit

Permalink
GetConfig for incomplete config and tests (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangra authored Aug 13, 2024
1 parent 7de2582 commit 6a7123d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 9 deletions.
31 changes: 22 additions & 9 deletions machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,25 @@ func (m *Machine) ImageRefWithVersion() string {
return ref
}

// GetConfig returns `IncompleteConfig` if `Config` is unset which happens when
// `HostStatus` isn't "ok"
func (m *Machine) GetConfig() *MachineConfig {
if m.Config != nil {
return m.Config
}
return m.IncompleteConfig
}

func (m *Machine) GetMetadataByKey(key string) string {
c := m.GetConfig()
if c == nil || c.Metadata == nil {
return ""
}
return c.Metadata[key]
}

func (m *Machine) IsAppsV2() bool {
return m.Config != nil && m.Config.Metadata[MachineConfigMetadataKeyFlyPlatformVersion] == MachineFlyPlatformVersion2
return m.GetMetadataByKey(MachineConfigMetadataKeyFlyPlatformVersion) == MachineFlyPlatformVersion2
}

func (m *Machine) IsFlyAppsPlatform() bool {
Expand All @@ -113,14 +130,11 @@ func (m *Machine) IsActive() bool {
}

func (m *Machine) ProcessGroup() string {
if m.Config == nil {
return ""
}
return m.Config.ProcessGroup()
return m.GetConfig().ProcessGroup()
}

func (m *Machine) HasProcessGroup(desired string) bool {
return m.Config != nil && m.ProcessGroup() == desired
return m.ProcessGroup() == desired
}

func (m *Machine) ImageVersion() string {
Expand Down Expand Up @@ -241,7 +255,7 @@ func (m *Machine) MostRecentStartTimeAfterLaunch() (time.Time, error) {
}

func (m *Machine) IsReleaseCommandMachine() bool {
return m.HasProcessGroup(MachineProcessGroupFlyAppReleaseCommand) || m.Config.Metadata["process_group"] == "release_command"
return m.HasProcessGroup(MachineProcessGroupFlyAppReleaseCommand) || m.GetMetadataByKey("process_group") == "release_command"
}

type MachineImageRef struct {
Expand Down Expand Up @@ -668,8 +682,7 @@ func (c *MachineConfig) ProcessGroup() string {
// - machines with only 'process_group'
// - machines with both 'process_group' and 'fly_process_group'
// - machines with only 'fly_process_group'

if c.Metadata == nil {
if c == nil || c.Metadata == nil {
return ""
}

Expand Down
61 changes: 61 additions & 0 deletions machine_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ func TestGetProcessGroup(t *testing.T) {
},
},
},
{
name: "machine with incomplete config and 'fly_process_group'",
expected: "web",
machine: &Machine{
IncompleteConfig: &MachineConfig{
Metadata: map[string]string{
"fly_process_group": "web",
},
},
},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -299,3 +310,53 @@ func TestMachineAutostopMarshalJSON(t *testing.T) {
}
}
}

func TestIsAppV2(t *testing.T) {
type testcase struct {
name string
machine *Machine
expected bool
}

cases := []testcase{
{
name: "machine with 'fly_platform_version=v2'",
expected: true,
machine: &Machine{
Config: &MachineConfig{
Metadata: map[string]string{"fly_platform_version": "v2"},
},
},
},
{
name: "machine with non v2 'fly_platform_version'",
expected: false,
machine: &Machine{
Config: &MachineConfig{
Metadata: map[string]string{"fly_platform_version": "v1"},
},
},
},
{
name: "machine without config",
expected: false,
machine: &Machine{},
},
{
name: "machine with 'fly_platform_version=v2' in incomplete config",
expected: true,
machine: &Machine{
IncompleteConfig: &MachineConfig{
Metadata: map[string]string{"fly_platform_version": "v2"},
},
},
},
}

for _, tc := range cases {
result := tc.machine.IsAppsV2()
if result != tc.expected {
t.Errorf("%s, got '%v', want '%v'", tc.name, result, tc.expected)
}
}
}

0 comments on commit 6a7123d

Please sign in to comment.