Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock base image distro #66

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion policy/policy_handler/mocks/base_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ type BaseImageQueryResult struct {
FromTag *string `edn:"docker.image/from-tag"`
}

type SubscriptionDistro struct {
Name string `edn:"os.distro/name"`
Version string `edn:"os.distro/version"`
}

type SubscriptionImage struct {
Digest string `edn:"docker.image/digest"`
Digest string `edn:"docker.image/digest"`
Distro *SubscriptionDistro `edn:"docker.image/distro"`
}

type SubscriptionRepository struct {
Expand All @@ -30,6 +36,7 @@ func MockBaseImage(sb *types.SBOM) BaseImageQueryResult {
return BaseImageQueryResult{
FromReference: &SubscriptionImage{
Digest: sb.Source.Provenance.BaseImage.Digest,
Distro: convertDistro(sb.Source.Image.Distro),
},
FromRepo: parseFromReference(sb.Source.Provenance.BaseImage.Name),
FromTag: &sb.Source.Provenance.BaseImage.Tag,
Expand Down Expand Up @@ -64,3 +71,14 @@ func parseFromReference(ref string) *SubscriptionRepository {
}
}
}

func convertDistro(sbDistro types.Distro) *SubscriptionDistro {
if sbDistro.OsName != "" && sbDistro.OsVersion != "" {
return &SubscriptionDistro{
Name: sbDistro.OsName,
Version: sbDistro.OsVersion,
}
}

return nil
}
35 changes: 35 additions & 0 deletions policy/policy_handler/mocks/base_image_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mocks

import (
"github.com/atomist-skills/go-skill/policy/types"
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
Expand Down Expand Up @@ -52,3 +54,36 @@ func Test_parseFromReference(t *testing.T) {
})
}
}

func Test_convertDistro(t *testing.T) {
testCases := []struct {
name string
arg types.Distro
expected *SubscriptionDistro
}{
{
name: "Returns nil when distro information is not present",
arg: types.Distro{},
expected: nil,
},
{
name: "Correctly converts distro to subscription form when available",
arg: types.Distro{
OsName: "debian",
OsVersion: "10",
OsDistro: "buster",
},
expected: &SubscriptionDistro{
Name: "debian",
Version: "10",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := convertDistro(tc.arg)
assert.Equal(t, tc.expected, actual, "convertDistro() = %+v, want %+v", actual, tc.expected)
})
}
}
Loading