Skip to content

Commit f086b06

Browse files
author
Zhou Hao
authored
Merge pull request #667 from jterry75/add_windows_generate
Add generate.New support for Windows.
2 parents 0ace550 + c291c2a commit f086b06

File tree

4 files changed

+83
-45
lines changed

4 files changed

+83
-45
lines changed

Diff for: generate/generate.go

+27-13
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,43 @@ type ExportOptions struct {
3939
// New creates a configuration Generator with the default
4040
// configuration for the target operating system.
4141
func New(os string) (generator Generator, err error) {
42-
if os != "linux" && os != "solaris" {
42+
if os != "linux" && os != "solaris" && os != "windows" {
4343
return generator, fmt.Errorf("no defaults configured for %s", os)
4444
}
4545

4646
config := rspec.Spec{
47-
Version: rspec.Version,
48-
Root: &rspec.Root{
47+
Version: rspec.Version,
48+
Hostname: "mrsdalloway",
49+
}
50+
51+
if os == "windows" {
52+
config.Process = &rspec.Process{
53+
Args: []string{
54+
"cmd",
55+
},
56+
Cwd: `C:\`,
57+
ConsoleSize: &rspec.Box{
58+
Width: 80,
59+
Height: 20,
60+
},
61+
}
62+
config.Windows = &rspec.Windows{
63+
IgnoreFlushesDuringBoot: true,
64+
Network: &rspec.WindowsNetwork{
65+
AllowUnqualifiedDNSQuery: true,
66+
},
67+
}
68+
} else {
69+
config.Root = &rspec.Root{
4970
Path: "rootfs",
5071
Readonly: false,
51-
},
52-
Process: &rspec.Process{
72+
}
73+
config.Process = &rspec.Process{
5374
Terminal: false,
5475
Args: []string{
5576
"sh",
5677
},
57-
},
58-
Hostname: "mrsdalloway",
78+
}
5979
}
6080

6181
if os == "linux" || os == "solaris" {
@@ -1537,12 +1557,6 @@ func (g *Generator) SetWindowsNetwork(network rspec.WindowsNetwork) {
15371557
g.Config.Windows.Network = &network
15381558
}
15391559

1540-
// SetWindowsNetworkNamespace sets g.Config.Windows.Network.NetworkNamespace
1541-
func (g *Generator) SetWindowsNetworkNamespace(path string) {
1542-
g.initConfigWindows()
1543-
g.Config.Windows.Network.NetworkNamespace = path
1544-
}
1545-
15461560
// SetWindowsResourcesCPU sets g.Config.Windows.Resources.CPU.
15471561
func (g *Generator) SetWindowsResourcesCPU(cpu rspec.WindowsCPUResources) {
15481562
g.initConfigWindowsResources()

Diff for: generate/generate_test.go

+54-30
Original file line numberDiff line numberDiff line change
@@ -18,42 +18,66 @@ import (
1818
// and needs to be fixed immediately (as it will break downstreams that depend
1919
// on us for a "sane default" and do compliance testing -- such as umoci).
2020
func TestGenerateValid(t *testing.T) {
21-
bundle, err := ioutil.TempDir("", "TestGenerateValid_bundle")
22-
if err != nil {
23-
t.Fatal(err)
21+
plat := "linux"
22+
if runtime.GOOS == "windows" {
23+
plat = "windows"
2424
}
25-
defer os.RemoveAll(bundle)
2625

27-
// Create our toy bundle.
28-
rootfsPath := filepath.Join(bundle, "rootfs")
29-
if err := os.Mkdir(rootfsPath, 0755); err != nil {
30-
t.Fatal(err)
31-
}
32-
configPath := filepath.Join(bundle, "config.json")
33-
g, err := generate.New("linux")
34-
if err != nil {
35-
t.Fatal(err)
36-
}
37-
if err := (&g).SaveToFile(configPath, generate.ExportOptions{Seccomp: false}); err != nil {
38-
t.Fatal(err)
39-
}
26+
isolations := []string{"process", "hyperv"}
27+
for _, isolation := range isolations {
28+
if plat == "linux" && isolation == "hyperv" {
29+
// Combination doesn't make sense.
30+
continue
31+
}
4032

41-
// Validate the bundle.
42-
v, err := validate.NewValidatorFromPath(bundle, true, runtime.GOOS)
43-
if err != nil {
44-
t.Errorf("unexpected NewValidatorFromPath error: %+v", err)
45-
}
46-
if err := v.CheckAll(); err != nil {
47-
levelErrors, err := specerror.SplitLevel(err, rfc2119.Must)
33+
bundle, err := ioutil.TempDir("", "TestGenerateValid_bundle")
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
defer os.RemoveAll(bundle)
38+
39+
// Create our toy bundle.
40+
rootfsPath := filepath.Join(bundle, "rootfs")
41+
if err := os.Mkdir(rootfsPath, 0755); err != nil {
42+
t.Fatal(err)
43+
}
44+
configPath := filepath.Join(bundle, "config.json")
45+
g, err := generate.New(plat)
4846
if err != nil {
49-
t.Errorf("unexpected non-multierror: %+v", err)
50-
return
47+
t.Fatal(err)
48+
}
49+
if runtime.GOOS == "windows" {
50+
g.AddWindowsLayerFolders("C:\\fakelayer")
51+
g.AddWindowsLayerFolders("C:\\fakescratch")
52+
if isolation == "process" {
53+
// Add the Rootfs section (note: fake volume guid)
54+
g.SetRootPath("\\\\?\\Volume{ec84d99e-3f02-11e7-ac6c-00155d7682cf}\\")
55+
} else {
56+
// Add the Hyper-V section
57+
g.SetWindowsHypervUntilityVMPath("")
58+
}
59+
}
60+
if err := (&g).SaveToFile(configPath, generate.ExportOptions{Seccomp: false}); err != nil {
61+
t.Fatal(err)
5162
}
52-
for _, e := range levelErrors.Warnings {
53-
t.Logf("unexpected warning: %v", e)
63+
64+
// Validate the bundle.
65+
v, err := validate.NewValidatorFromPath(bundle, true, runtime.GOOS)
66+
if err != nil {
67+
t.Errorf("unexpected NewValidatorFromPath error: %+v", err)
5468
}
55-
if err := levelErrors.Error; err != nil {
56-
t.Errorf("unexpected MUST error(s): %+v", err)
69+
if err := v.CheckAll(); err != nil {
70+
levelErrors, err := specerror.SplitLevel(err, rfc2119.Must)
71+
if err != nil {
72+
t.Errorf("unexpected non-multierror: %+v", err)
73+
return
74+
}
75+
for _, e := range levelErrors.Warnings {
76+
t.Logf("unexpected warning: %v", e)
77+
}
78+
if err := levelErrors.Error; err != nil {
79+
t.Errorf("unexpected MUST error(s): %+v", err)
80+
}
5781
}
5882
}
5983
}

Diff for: validate/validate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestJSONSchema(t *testing.T) {
4343
}{
4444
{
4545
config: &rspec.Spec{},
46-
error: "1 error occurred:\n\n* Version string empty\nRefer to: https://github.com/opencontainers/runtime-spec/blob/v1.0.1-dev/config.md#specification-version",
46+
error: "1 error occurred:\n\n* Version string empty\nRefer to: https://github.com/opencontainers/runtime-spec/blob/v1.0.1/config.md#specification-version",
4747
},
4848
{
4949
config: &rspec.Spec{

Diff for: vendor/github.com/opencontainers/runtime-spec/specs-go/version.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)