forked from containers/podman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for podman machine init not creating necessary JSON file when is …
…passed. This bug was first described in containers#23544. Signed-off-by: Graceson Aufderheide <[email protected]>
- Loading branch information
Showing
2 changed files
with
145 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package shim | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/containers/podman/v5/pkg/machine/define" | ||
"github.com/containers/podman/v5/pkg/machine/env" | ||
provider2 "github.com/containers/podman/v5/pkg/machine/provider" | ||
"github.com/containers/podman/v5/pkg/machine/vmconfigs" | ||
) | ||
|
||
func fileExists(filename string) bool { | ||
_, err := os.Stat(filename) | ||
return err == nil | ||
} | ||
|
||
func createTempIgnitionFile(t *testing.T) string { | ||
t.Helper() | ||
|
||
mockIgnitionContent := `{"ignition":{"version":"3.4.0"},"passwd":{"users":[{"name":"core"}]}}` | ||
|
||
tmpFile, err := os.CreateTemp("", "test-ignition-*.ign") | ||
if err != nil { | ||
t.Fatalf("Failed to create temporary ignition file: %v", err) | ||
} | ||
|
||
if _, err := tmpFile.WriteString(mockIgnitionContent); err != nil { | ||
t.Fatalf("Failed to write to temporary ignition file: %v", err) | ||
} | ||
|
||
if err := tmpFile.Close(); err != nil { | ||
t.Fatalf("Failed to close temporary ignition file: %v", err) | ||
} | ||
|
||
return tmpFile.Name() | ||
} | ||
|
||
func TestInit(t *testing.T) { | ||
var ( | ||
provider vmconfigs.VMProvider | ||
) | ||
|
||
provider, err := provider2.Get() | ||
if err != nil { | ||
t.Fatalf("Failed to get provider: %v", err) | ||
} | ||
|
||
dirs, err := env.GetMachineDirs(provider.VMType()) | ||
if err != nil { | ||
t.Fatalf("Failed to get dirs: %v", err) | ||
} | ||
|
||
tempIgnitionPath := createTempIgnitionFile(t) | ||
defer func(name string) { | ||
err := os.Remove(name) | ||
if err != nil { | ||
t.Fatalf("Failed to remove temp ignition file: %v", err) | ||
} | ||
}(tempIgnitionPath) | ||
|
||
type args struct { | ||
opts define.InitOptions | ||
mp vmconfigs.VMProvider | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Default machine", | ||
args: args{ | ||
opts: define.InitOptions{ | ||
Name: "test-default-machine", | ||
}, | ||
mp: provider, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "With ignition", | ||
args: args{ | ||
opts: define.InitOptions{ | ||
Name: "test-with-ignition", | ||
IgnitionPath: tempIgnitionPath, | ||
}, | ||
mp: provider, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := Init(tt.args.opts, tt.args.mp); (err != nil) != tt.wantErr { | ||
t.Errorf("Init() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
|
||
// Check for file existence only for valid initialization | ||
if !tt.wantErr { | ||
configDir := dirs.ConfigDir.GetPath() | ||
fileExtensions := []string{".lock", ".json", ".ign"} | ||
|
||
for _, ext := range fileExtensions { | ||
filename := filepath.Join(configDir, fmt.Sprintf("%s%s", tt.args.opts.Name, ext)) | ||
if !fileExists(filename) { | ||
t.Errorf("Expected file %s does not exist", filename) | ||
} else { | ||
t.Logf("File %s exists", filename) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |