Skip to content

Commit 1e01dcc

Browse files
authored
GCS tests and features/bugfixes to support them (#1360)
Exposed data and functionality for testing GCS: * `internal\guest\runtime\hcsv2.Container.InitProcess()` * `internal\guest\runtime\hcsv2.GetOrAddNetworkNamespace()` * `internal\guest\runtime\hcsv2.RemoveNetworkNamespace()` * `internal\guest\runtime\hcsv2.Host.SecurityPolicyEnforcer()` * `internal\guest\runtime\hcsv2.Host.Transport()` Fixed bug where `host.RemoveContainer` did not remove the network namespace for standalone and pod containers. Updated go-winio version to include bugfixes for closing hvsockets, specifically to close a socket for writing (needed by internal\cmd to signal that the stdin stream has finished). Added doc.go files to guest packages to prevent linter/compiler errors under windows. The tests themselves are broken out here: #1352 #1351 Signed-off-by: Hamza El-Saawy <[email protected]>
1 parent 2b176ab commit 1e01dcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1204
-852
lines changed

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ service/pkg/
2424
*.img
2525
*.vhd
2626
*.tar.gz
27+
*.tar
2728

2829
# Make stuff
2930
.rootfs-done
@@ -36,5 +37,9 @@ rootfs-conv/*
3637
deps/*
3738
out/*
3839

40+
# test results
41+
test/results
42+
43+
# go workspace files
3944
go.work
40-
go.work.sum
45+
go.work.sum

functional_tests.ps1

Lines changed: 0 additions & 12 deletions
This file was deleted.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.17
44

55
require (
66
github.com/BurntSushi/toml v0.3.1
7-
github.com/Microsoft/go-winio v0.4.17
7+
github.com/Microsoft/go-winio v0.5.2
88
github.com/cenkalti/backoff/v4 v4.1.1
99
github.com/containerd/cgroups v1.0.1
1010
github.com/containerd/console v1.0.2

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ github.com/Microsoft/go-winio v0.4.16-0.20201130162521-d1ffc52c7331/go.mod h1:XB
4444
github.com/Microsoft/go-winio v0.4.16/go.mod h1:XB6nPKklQyQ7GC9LdcBEcBl8PF76WugXOPRXwdLnMv0=
4545
github.com/Microsoft/go-winio v0.4.17-0.20210211115548-6eac466e5fa3/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
4646
github.com/Microsoft/go-winio v0.4.17-0.20210324224401-5516f17a5958/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
47-
github.com/Microsoft/go-winio v0.4.17 h1:iT12IBVClFevaf8PuVyi3UmZOVh4OqnaLxDTW2O6j3w=
4847
github.com/Microsoft/go-winio v0.4.17/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
48+
github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA=
49+
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
4950
github.com/Microsoft/hcsshim v0.8.6/go.mod h1:Op3hHsoHPAvb6lceZHDtd9OkTew38wNoXnJs8iY7rUg=
5051
github.com/Microsoft/hcsshim v0.8.7-0.20190325164909-8abdbb8205e4/go.mod h1:Op3hHsoHPAvb6lceZHDtd9OkTew38wNoXnJs8iY7rUg=
5152
github.com/Microsoft/hcsshim v0.8.7/go.mod h1:OHd7sQqRFrYd3RmSgbgji+ctCwkbq2wbEYNSzOYtcBQ=

internal/guest/linux/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Package linux contains definitions required for making a linux ioctl.
2+
package linux

internal/guest/linux/ioctl.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//go:build linux
22
// +build linux
33

4-
// Package linux contains definitions required for making a linux ioctl.
54
package linux
65

76
import (

internal/guest/policy/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package policy

internal/guest/runtime/hcsv2/container.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,19 @@ func (c *Container) ExecProcess(ctx context.Context, process *oci.Process, conSe
123123
return pid, nil
124124
}
125125

126+
// InitProcess returns the container's init process
127+
func (c *Container) InitProcess() Process {
128+
return c.initProcess
129+
}
130+
126131
// GetProcess returns the Process with the matching 'pid'. If the 'pid' does
127132
// not exit returns error.
128133
func (c *Container) GetProcess(pid uint32) (Process, error) {
129134
//todo: thread a context to this function call
130135
logrus.WithFields(logrus.Fields{
131136
logfields.ContainerID: c.id,
132137
logfields.ProcessID: pid,
133-
}).Info("opengcs::Container::GetProcesss")
138+
}).Info("opengcs::Container::GetProcess")
134139
if c.initProcess.pid == pid {
135140
return c.initProcess, nil
136141
}
@@ -245,3 +250,7 @@ func (c *Container) getStatus() containerStatus {
245250
func (c *Container) setStatus(st containerStatus) {
246251
atomic.StoreUint32((*uint32)(&c.status), uint32(st))
247252
}
253+
254+
func (c *Container) ID() string {
255+
return c.id
256+
}

internal/guest/runtime/hcsv2/network.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func getNetworkNamespace(id string) (*namespace, error) {
5151
return ns, nil
5252
}
5353

54-
// getOrAddNetworkNamespace returns the namespace found by `id` or creates a new
54+
// GetOrAddNetworkNamespace returns the namespace found by `id` or creates a new
5555
// one and assigns `id.
56-
func getOrAddNetworkNamespace(id string) *namespace {
56+
func GetOrAddNetworkNamespace(id string) *namespace {
5757
id = strings.ToLower(id)
5858

5959
namespaceSync.Lock()
@@ -69,8 +69,8 @@ func getOrAddNetworkNamespace(id string) *namespace {
6969
return ns
7070
}
7171

72-
// removeNetworkNamespace removes the in-memory `namespace` found by `id`.
73-
func removeNetworkNamespace(ctx context.Context, id string) (err error) {
72+
// RemoveNetworkNamespace removes the in-memory `namespace` found by `id`.
73+
func RemoveNetworkNamespace(ctx context.Context, id string) (err error) {
7474
_, span := trace.StartSpan(ctx, "hcsv2::removeNetworkNamespace")
7575
defer span.End()
7676
defer func() { oc.SetSpanStatus(span, err) }()
@@ -123,7 +123,7 @@ func (n *namespace) AssignContainerPid(ctx context.Context, pid int) (err error)
123123
defer n.m.Unlock()
124124

125125
if n.pid != 0 {
126-
return errors.Errorf("previously assigned container pid: %d", n.pid)
126+
return errors.Errorf("previously assigned container pid %d to network namespace %q", n.pid, n.id)
127127
}
128128

129129
n.pid = pid

internal/guest/runtime/hcsv2/network_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func Test_getNetworkNamespace_NotExist(t *testing.T) {
1414
defer func() {
15-
err := removeNetworkNamespace(context.Background(), t.Name())
15+
err := RemoveNetworkNamespace(context.Background(), t.Name())
1616
if err != nil {
1717
t.Errorf("failed to remove ns with error: %v", err)
1818
}
@@ -29,13 +29,13 @@ func Test_getNetworkNamespace_NotExist(t *testing.T) {
2929

3030
func Test_getNetworkNamespace_PreviousExist(t *testing.T) {
3131
defer func() {
32-
err := removeNetworkNamespace(context.Background(), t.Name())
32+
err := RemoveNetworkNamespace(context.Background(), t.Name())
3333
if err != nil {
3434
t.Errorf("failed to remove ns with error: %v", err)
3535
}
3636
}()
3737

38-
ns1 := getOrAddNetworkNamespace(t.Name())
38+
ns1 := GetOrAddNetworkNamespace(t.Name())
3939
if ns1 == nil {
4040
t.Fatal("namespace ns1 should not be nil")
4141
}
@@ -50,43 +50,43 @@ func Test_getNetworkNamespace_PreviousExist(t *testing.T) {
5050

5151
func Test_getOrAddNetworkNamespace_NotExist(t *testing.T) {
5252
defer func() {
53-
err := removeNetworkNamespace(context.Background(), t.Name())
53+
err := RemoveNetworkNamespace(context.Background(), t.Name())
5454
if err != nil {
5555
t.Errorf("failed to remove ns with error: %v", err)
5656
}
5757
}()
5858

59-
ns := getOrAddNetworkNamespace(t.Name())
59+
ns := GetOrAddNetworkNamespace(t.Name())
6060
if ns == nil {
6161
t.Fatalf("namespace should not be nil")
6262
}
6363
}
6464

6565
func Test_getOrAddNetworkNamespace_PreviousExist(t *testing.T) {
6666
defer func() {
67-
err := removeNetworkNamespace(context.Background(), t.Name())
67+
err := RemoveNetworkNamespace(context.Background(), t.Name())
6868
if err != nil {
6969
t.Errorf("failed to remove ns with error: %v", err)
7070
}
7171
}()
7272

73-
ns1 := getOrAddNetworkNamespace(t.Name())
74-
ns2 := getOrAddNetworkNamespace(t.Name())
73+
ns1 := GetOrAddNetworkNamespace(t.Name())
74+
ns2 := GetOrAddNetworkNamespace(t.Name())
7575
if ns1 != ns2 {
7676
t.Fatalf("ns1 %+v != ns2 %+v", ns1, ns2)
7777
}
7878
}
7979

8080
func Test_removeNetworkNamespace_NotExist(t *testing.T) {
81-
err := removeNetworkNamespace(context.Background(), t.Name())
81+
err := RemoveNetworkNamespace(context.Background(), t.Name())
8282
if err != nil {
8383
t.Fatalf("failed to remove non-existing ns with error: %v", err)
8484
}
8585
}
8686

8787
func Test_removeNetworkNamespace_HasAdapters(t *testing.T) {
8888
defer func() {
89-
err := removeNetworkNamespace(context.Background(), t.Name())
89+
err := RemoveNetworkNamespace(context.Background(), t.Name())
9090
if err != nil {
9191
t.Errorf("failed to remove ns with error: %v", err)
9292
}
@@ -96,7 +96,7 @@ func Test_removeNetworkNamespace_HasAdapters(t *testing.T) {
9696
networkInstanceIDToName = nsOld
9797
}()
9898

99-
ns := getOrAddNetworkNamespace(t.Name())
99+
ns := GetOrAddNetworkNamespace(t.Name())
100100

101101
networkInstanceIDToName = func(ctx context.Context, id string, _ bool) (string, error) {
102102
return "/dev/sdz", nil
@@ -105,15 +105,15 @@ func Test_removeNetworkNamespace_HasAdapters(t *testing.T) {
105105
if err != nil {
106106
t.Fatalf("failed to add adapter: %v", err)
107107
}
108-
err = removeNetworkNamespace(context.Background(), t.Name())
108+
err = RemoveNetworkNamespace(context.Background(), t.Name())
109109
if err == nil {
110110
t.Fatal("should have failed to delete namespace with adapters")
111111
}
112112
err = ns.RemoveAdapter(context.Background(), "test")
113113
if err != nil {
114114
t.Fatalf("failed to remove adapter: %v", err)
115115
}
116-
err = removeNetworkNamespace(context.Background(), t.Name())
116+
err = RemoveNetworkNamespace(context.Background(), t.Name())
117117
if err != nil {
118118
t.Fatalf("should not have failed to delete empty namepace got: %v", err)
119119
}

internal/guest/runtime/hcsv2/process.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ type containerProcess struct {
6767
writersCalled bool
6868
}
6969

70+
var _ Process = &containerProcess{}
71+
7072
// newProcess returns a containerProcess struct that has been initialized with
7173
// an outstanding wait for process exit, and post exit an outstanding wait for
7274
// process cleanup to release all resources once at least 1 waiter has
@@ -262,6 +264,8 @@ type externalProcess struct {
262264
remove func(pid int)
263265
}
264266

267+
var _ Process = &externalProcess{}
268+
265269
func (ep *externalProcess) Kill(ctx context.Context, signal syscall.Signal) error {
266270
if err := syscall.Kill(int(ep.cmd.Process.Pid), signal); err != nil {
267271
if err == syscall.ESRCH {

internal/guest/runtime/hcsv2/standalone_container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func setupStandaloneContainerSpec(ctx context.Context, id string, spec *oci.Spec
103103

104104
// Write resolv.conf
105105
if !specInternal.MountPresent("/etc/resolv.conf", spec.Mounts) {
106-
ns := getOrAddNetworkNamespace(getNetworkNamespaceID(spec))
106+
ns := GetOrAddNetworkNamespace(getNetworkNamespaceID(spec))
107107
var searches, servers []string
108108
for _, n := range ns.Adapters() {
109109
if len(n.DNSSuffix) > 0 {

internal/guest/runtime/hcsv2/uvm.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,29 @@ func (h *Host) SetSecurityPolicy(base64Policy string) error {
115115
return nil
116116
}
117117

118+
func (h *Host) SecurityPolicyEnforcer() securitypolicy.SecurityPolicyEnforcer {
119+
return h.securityPolicyEnforcer
120+
}
121+
122+
func (h *Host) Transport() transport.Transport {
123+
return h.vsock
124+
}
125+
118126
func (h *Host) RemoveContainer(id string) {
119127
h.containersMutex.Lock()
120128
defer h.containersMutex.Unlock()
121129

130+
c, ok := h.containers[id]
131+
if !ok {
132+
return
133+
}
134+
135+
// delete the network namespace for standalone and sandbox containers
136+
criType, isCRI := c.spec.Annotations[annotations.KubernetesContainerType]
137+
if !isCRI || criType == "sandbox" {
138+
RemoveNetworkNamespace(context.Background(), id)
139+
}
140+
122141
delete(h.containers, id)
123142
}
124143

@@ -580,15 +599,15 @@ func modifyCombinedLayers(ctx context.Context, rt guestrequest.RequestType, cl *
580599
func modifyNetwork(ctx context.Context, rt guestrequest.RequestType, na *guestresource.LCOWNetworkAdapter) (err error) {
581600
switch rt {
582601
case guestrequest.RequestTypeAdd:
583-
ns := getOrAddNetworkNamespace(na.NamespaceID)
602+
ns := GetOrAddNetworkNamespace(na.NamespaceID)
584603
if err := ns.AddAdapter(ctx, na); err != nil {
585604
return err
586605
}
587606
// This code doesnt know if the namespace was already added to the
588607
// container or not so it must always call `Sync`.
589608
return ns.Sync(ctx)
590609
case guestrequest.RequestTypeRemove:
591-
ns := getOrAddNetworkNamespace(na.ID)
610+
ns := GetOrAddNetworkNamespace(na.ID)
592611
if err := ns.RemoveAdapter(ctx, na.ID); err != nil {
593612
return err
594613
}

0 commit comments

Comments
 (0)