Skip to content
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
36 changes: 22 additions & 14 deletions butane/internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ func isCharDevice(f *os.File) bool {
return stat.Mode()&os.ModeCharDevice != 0
}

func readInput(input string) ([]byte, string, error) {
infile := os.Stdin
filename := "<stdin>"
if input != "" {
f, err := os.Open(input)
if err != nil {
return nil, input, fmt.Errorf("failed to open %s: %w", input, err)
}
defer func() { _ = f.Close() }()
infile = f
filename = input
}

data, err := io.ReadAll(infile)
if err != nil {
return nil, filename, fmt.Errorf("failed to read %s: %w", filename, err)
}
return data, filename, nil
}

func main() {
var (
input string
Expand Down Expand Up @@ -111,21 +131,9 @@ func main() {
os.Exit(0)
}

infile := os.Stdin
filename := "<stdin>"
if input != "" {
var err error
infile, err = os.Open(input)
if err != nil {
fail("failed to open %s: %v\n", input, err)
}
defer func() { _ = infile.Close() }()
filename = input
}

dataIn, err := io.ReadAll(infile)
dataIn, filename, err := readInput(input)
if err != nil {
fail("failed to read %s: %v\n", infile.Name(), err)
fail("%v\n", err)
}

dataOut, r, err := config.TranslateBytes(dataIn, options)
Expand Down
167 changes: 167 additions & 0 deletions butane/internal/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Copyright 2019 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"os"
"path/filepath"
"strings"
"testing"
)

func TestReadInput(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) (input string, cleanup func())
wantData []byte
wantErr bool
wantErrMsg string
}{
{
name: "stdin",
setup: func(t *testing.T) (string, func()) {
content := []byte("hello from stdin")
orig := os.Stdin
tmp, err := os.CreateTemp("", "butane-stdin")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
if _, err := tmp.Write(content); err != nil {
t.Fatalf("failed to write temp file: %v", err)
}
if _, err := tmp.Seek(0, 0); err != nil {
t.Fatalf("failed to seek temp file: %v", err)
}
os.Stdin = tmp
return "", func() {
os.Stdin = orig
if err := tmp.Close(); err != nil {
t.Errorf("failed to close temp file: %v", err)
}
if err := os.Remove(tmp.Name()); err != nil {
t.Errorf("failed to remove temp file: %v", err)
}
}
},
wantData: []byte("hello from stdin"),
wantErr: false,
},
{
name: "empty stdin",
setup: func(t *testing.T) (string, func()) {
orig := os.Stdin
tmp, err := os.CreateTemp("", "butane-stdin-empty")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
if _, err := tmp.Seek(0, 0); err != nil {
t.Fatalf("failed to seek temp file: %v", err)
}
os.Stdin = tmp
return "", func() {
os.Stdin = orig
if err := tmp.Close(); err != nil {
t.Errorf("failed to close temp file: %v", err)
}
if err := os.Remove(tmp.Name()); err != nil {
t.Errorf("failed to remove temp file: %v", err)
}
}
},
wantData: []byte{},
wantErr: false,
},
{
name: "file",
setup: func(t *testing.T) (string, func()) {
dir := t.TempDir()
path := filepath.Join(dir, "input.bu")
content := []byte("variant: fcos")
if err := os.WriteFile(path, content, 0644); err != nil {
t.Fatalf("failed to write file: %v", err)
}
return path, func() {}
},
wantData: []byte("variant: fcos"),
wantErr: false,
},
{
name: "missing file",
setup: func(t *testing.T) (string, func()) {
missing := filepath.Join(t.TempDir(), "does-not-exist")
return missing, func() {}
},
wantData: nil,
wantErr: true,
},
{
name: "stdin read error",
setup: func(t *testing.T) (string, func()) {
orig := os.Stdin
closed, err := os.CreateTemp("", "butane-closed-stdin")
if err != nil {
t.Fatalf("failed to create temp file: %v", err)
}
name := closed.Name()
if err := closed.Close(); err != nil {
t.Fatalf("failed to close temp file: %v", err)
}
os.Stdin = closed
return "", func() {
os.Stdin = orig
if err := os.Remove(name); err != nil {
t.Errorf("failed to remove temp file: %v", err)
}
}
},
wantData: nil,
wantErr: true,
wantErrMsg: "failed to read <stdin>:",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input, cleanup := tt.setup(t)
defer cleanup()

data, filename, err := readInput(input)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
if tt.wantErrMsg != "" {
if !strings.Contains(err.Error(), tt.wantErrMsg) {
t.Errorf("expected error containing %q, got %q", tt.wantErrMsg, err.Error())
}
}
} else if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if string(data) != string(tt.wantData) {
t.Errorf("expected data %q, got %q", tt.wantData, data)
}

wantName := "<stdin>"
if input != "" {
wantName = input
}
if filename != wantName {
t.Errorf("expected filename %q, got %q", wantName, filename)
}
})
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
1 change: 1 addition & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nav_order: 9
- Fix the Butane root partition constraint check to only examine subsequent partitions ([#2304](https://github.com/coreos/ignition/pull/2304))
- Close `/proc/mounts` after checking whether block devices are mounted, preventing file descriptor leaks during disk setup ([#2289](https://github.com/coreos/ignition/pull/2289))
- Drop supplementary groups when dropping privileges to write files as a user ([#2242](https://github.com/coreos/ignition/issues/2242))
- butane: report the friendly input name (`<stdin>` instead of `/dev/stdin`) in stdin read errors

## Ignition 2.27.0 (2026-08-26)

Expand Down
Loading