Skip to content

Commit

Permalink
osbuild: add new osbuild.NewOSTreeDeploymentMountDefault()
Browse files Browse the repository at this point in the history
Add the "Default" and "Source" options to OSTreeMountDeployment.
See
- "default" osbuild/osbuild#1553
- "source"  osbuild/osbuild#1535
  • Loading branch information
mvo5 authored and ondrejbudai committed Apr 7, 2024
1 parent 63f777a commit 3039ddb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
30 changes: 28 additions & 2 deletions pkg/osbuild/ostree_deployment_mount.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package osbuild

import "github.com/osbuild/images/internal/common"

type OSTreeMountSource string

const (
OSTreeMountSourceTree OSTreeMountSource = "tree"
OSTreeMountSourceMount OSTreeMountSource = "mount"
)

type OSTreeMountOptions struct {
Source OSTreeMountSource `json:"source,omitempty"`
Deployment OSTreeMountDeployment `json:"deployment"`
}

func (OSTreeMountOptions) isMountOptions() {}

type OSTreeMountDeployment struct {
// Name of the stateroot to be used in the deployment
OSName string `json:"osname"`
OSName string `json:"osname,omitempty"`

// OStree ref to create and use for deployment
Ref string `json:"ref"`
Ref string `json:"ref,omitempty"`

// The deployment serial (usually '0')
Serial *int `json:"serial,omitempty"`

// When set the OSName/Ref/Serial is detected automatically
Default *bool `json:"default,omitempty"`
}

func NewOSTreeDeploymentMount(name, osName, ref string, serial int) *Mount {
Expand All @@ -30,3 +43,16 @@ func NewOSTreeDeploymentMount(name, osName, ref string, serial int) *Mount {
},
}
}

func NewOSTreeDeploymentMountDefault(name string, source OSTreeMountSource) *Mount {
return &Mount{
Type: "org.osbuild.ostree.deployment",
Name: name,
Options: &OSTreeMountOptions{
Source: source,
Deployment: OSTreeMountDeployment{
Default: common.ToPtr(true),
},
},
}
}
46 changes: 46 additions & 0 deletions pkg/osbuild/ostree_deployment_mount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package osbuild_test

import (
"encoding/json"
"testing"

"github.com/osbuild/images/pkg/osbuild"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestOSTreeMountDeploymentDefaultSerialized(t *testing.T) {
mntStage := osbuild.NewOSTreeDeploymentMountDefault("some-name", osbuild.OSTreeMountSourceMount)
json, err := json.MarshalIndent(mntStage, "", " ")
require.Nil(t, err)
assert.Equal(t, string(json), `
{
"name": "some-name",
"type": "org.osbuild.ostree.deployment",
"options": {
"source": "mount",
"deployment": {
"default": true
}
}
}`[1:])
}

func TestOSTreeMountDeploymentSerialized(t *testing.T) {
mntStage := osbuild.NewOSTreeDeploymentMount("some-name", "some-osname", "some-ref", 0)
json, err := json.MarshalIndent(mntStage, "", " ")
require.Nil(t, err)
assert.Equal(t, string(json), `
{
"name": "some-name",
"type": "org.osbuild.ostree.deployment",
"options": {
"deployment": {
"osname": "some-osname",
"ref": "some-ref",
"serial": 0
}
}
}`[1:])
}

0 comments on commit 3039ddb

Please sign in to comment.