Skip to content

Commit

Permalink
fix(framework): assert loadded workload and resource metadata annotat…
Browse files Browse the repository at this point in the history
…ions are coerced

Signed-off-by: Ben Meier <ben.meier@humanitec.com>
  • Loading branch information
astromechza committed May 20, 2024
1 parent 69defc4 commit e2b2acc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
20 changes: 18 additions & 2 deletions framework/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (s *State[StateExtras, WorkloadExtras, ResourceExtras]) WithWorkload(spec *
} else {
out.Workloads = maps.Clone(s.Workloads)
}

spec.Metadata = coerceAnnotationsType(spec.Metadata)

name, ok := spec.Metadata["name"].(string)
if !ok {
return nil, fmt.Errorf("metadata: name: is missing or is not a string")
Expand Down Expand Up @@ -140,15 +143,15 @@ func (s *State[StateExtras, WorkloadExtras, ResourceExtras]) WithPrimedResources
Type: resUid.Type(),
Class: resUid.Class(),
Id: resUid.Id(),
Metadata: res.Metadata,
Metadata: coerceAnnotationsType(res.Metadata),
Params: res.Params,
SourceWorkload: workloadName,
State: map[string]interface{}{},
Outputs: map[string]interface{}{},
}
primedResourceUids[resUid] = true
} else if !primedResourceUids[resUid] {
existing.Metadata = res.Metadata
existing.Metadata = coerceAnnotationsType(res.Metadata)
existing.Params = res.Params
existing.SourceWorkload = workloadName
out.Resources[resUid] = existing
Expand All @@ -175,6 +178,19 @@ func (s *State[StateExtras, WorkloadExtras, ResourceExtras]) WithPrimedResources
return &out, nil
}

// The metadata maps (workload metadata and resource metadata) decode in a weird way in yaml.v3 which causes type
// coercions elsewhere in the software to fail. We have to rewrite the type to be clear.
func coerceAnnotationsType(in map[string]interface{}) map[string]interface{} {
if a, ok := in["annotations"].(score.WorkloadMetadata); ok {
in = maps.Clone(in)
in["annotations"] = map[string]interface{}(a)
} else if a, ok := in["annotations"].(score.ResourceMetadata); ok {
in = maps.Clone(in)
in["annotations"] = map[string]interface{}(a)
}
return in
}

func (s *State[StateExtras, WorkloadExtras, ResourceExtras]) getResourceDependencies(workloadName, resName string) (map[ResourceUid]bool, error) {
outMap := make(map[ResourceUid]bool)
res := s.Workloads[workloadName].Spec.Resources[resName]
Expand Down
13 changes: 11 additions & 2 deletions framework/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func TestWithWorkload(t *testing.T) {
next, err := start.WithWorkload(mustLoadWorkload(t, `
metadata:
name: example
annotations:
acme.org/x: y
containers:
hello-world:
image: hi
Expand All @@ -66,7 +68,12 @@ resources:
assert.Len(t, next.Workloads, 1)
assert.Nil(t, next.Workloads["example"].File, nil)
assert.Equal(t, score.Workload{
Metadata: map[string]interface{}{"name": "example"},
Metadata: map[string]interface{}{
"name": "example",
"annotations": map[string]interface{}{
"acme.org/x": "y",
},
},
Containers: map[string]score.Container{"hello-world": {Image: "hi"}},
Resources: map[string]score.Resource{"foo": {Type: "thing"}},
}, next.Workloads["example"].Spec)
Expand Down Expand Up @@ -156,7 +163,7 @@ resources:
"thing3.apple#dog": {
Guid: "00000000-0000-0000-0000-000000000000",
Type: "thing3", Class: "apple", Id: "dog", State: map[string]interface{}{},
Metadata: map[string]interface{}{"annotations": score.ResourceMetadata{"foo": "bar"}},
Metadata: map[string]interface{}{"annotations": map[string]interface{}{"foo": "bar"}},
Params: map[string]interface{}{"color": "green"},
SourceWorkload: "example",
Outputs: map[string]interface{}{},
Expand All @@ -170,6 +177,8 @@ resources:
Outputs: map[string]interface{}{},
},
}, next.Resources)

assert.NotNil(t, next.Resources["thing3.apple#dog"].Metadata["annotations"].(map[string]interface{}))
})

t.Run("one workload - same resource - same metadata", func(t *testing.T) {
Expand Down

0 comments on commit e2b2acc

Please sign in to comment.