Skip to content

Commit

Permalink
More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 committed Aug 13, 2024
1 parent 5f755aa commit 677e302
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
21 changes: 13 additions & 8 deletions pkg/controller/operatingsystemconfig/ignition/ignition.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,25 @@ func ignitionFromOperatingSystemConfig(osc *extensionsv1alpha1.OperatingSystemCo
mode = &m
}

inline, err := helper.Decode(f.Content.Inline.Encoding, []byte(f.Content.Inline.Data))
if err != nil {
return types.Config{}, fmt.Errorf("unable to decode content from osc: %w", err)
}

ignitionFile := types.File{
Path: f.Path,
Filesystem: "root",
Mode: mode,
Contents: types.FileContents{
Inline: string(inline),
},
// Contents: types.FileContents{
// Inline: string(inline),
// },
Overwrite: ptr.To(true),
}

if f.Content.Inline != nil {
inline, err := helper.Decode(f.Content.Inline.Encoding, []byte(f.Content.Inline.Data))
if err != nil {
return types.Config{}, fmt.Errorf("unable to decode content from osc: %w", err)
}

ignitionFile.Contents.Inline = string(inline)
}

cfg.Storage.Files = append(cfg.Storage.Files, ignitionFile)
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/controller/operatingsystemconfig/ignition/ignition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/flatcar/container-linux-config-transpiler/config/types"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/go-logr/logr"
"github.com/google/go-cmp/cmp"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -130,3 +131,41 @@ func TestIgnitionFromOperatingSystemConfig(t *testing.T) {
})
}
}

func Test_ignition_Transpile(t *testing.T) {
tests := []struct {
name string
osc *extensionsv1alpha1.OperatingSystemConfig
want string
wantErr bool
}{
{
name: "transpiles to ignition format 2.3.0",
osc: &extensionsv1alpha1.OperatingSystemConfig{
Spec: extensionsv1alpha1.OperatingSystemConfigSpec{
Files: []extensionsv1alpha1.File{
{
Path: "/etc/a",
},
},
},
},
want: `{"ignition":{"config":{},"security":{"tls":{}},"timeouts":{},"version":"2.3.0"},"networkd":{},"passwd":{},"storage":{"files":[{"filesystem":"root","overwrite":true,"path":"/etc/a","contents":{"source":"data:,","verification":{}},"mode":420}]},"systemd":{}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tr := &ignition{
log: logr.Discard(),
}
got, err := tr.Transpile(tt.osc)
if (err != nil) != tt.wantErr {
t.Errorf("ignition.Transpile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(string(got), tt.want); diff != "" {
t.Errorf("ignition.Transpile() diff = %s", diff)
}
})
}
}

0 comments on commit 677e302

Please sign in to comment.