Skip to content

Commit

Permalink
Merge pull request #6540 from gabriel-samfira/enable-tests-on-windows
Browse files Browse the repository at this point in the history
Enable TestContainerPTY and TestContainerUsername
  • Loading branch information
estesp committed Feb 10, 2022
2 parents e2c5f8f + 4f0d5f0 commit a7f43c8
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 139 deletions.
139 changes: 0 additions & 139 deletions integration/client/container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,76 +445,6 @@ func getRuntimeVersion() string {
}
}

func TestContainerPTY(t *testing.T) {
t.Parallel()

client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()

var (
image Image
ctx, cancel = testContext(t)
id = t.Name()
)
defer cancel()

image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Fatal(err)
}

container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithNewSpec(oci.WithImageConfig(image), oci.WithTTY, withProcessArgs("echo", "hello")))
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx, WithSnapshotCleanup)

direct, err := newDirectIO(ctx, true)
if err != nil {
t.Fatal(err)
}
defer direct.Delete()
var (
wg sync.WaitGroup
buf = bytes.NewBuffer(nil)
)
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(buf, direct.Stdout)
}()

task, err := container.NewTask(ctx, direct.IOCreate)
if err != nil {
t.Fatal(err)
}
defer task.Delete(ctx)

status, err := task.Wait(ctx)
if err != nil {
t.Error(err)
}

if err := task.Start(ctx); err != nil {
t.Fatal(err)
}

<-status
wg.Wait()

if err := direct.Close(); err != nil {
t.Error(err)
}

out := buf.String()
if !strings.ContainsAny(fmt.Sprintf("%#q", out), `\x00`) {
t.Fatal(`expected \x00 in output`)
}
}

func TestContainerAttach(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -620,75 +550,6 @@ func TestContainerAttach(t *testing.T) {
}
}

func TestContainerUsername(t *testing.T) {
t.Parallel()

client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()

var (
image Image
ctx, cancel = testContext(t)
id = t.Name()
)
defer cancel()

image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Fatal(err)
}
direct, err := newDirectIO(ctx, false)
if err != nil {
t.Fatal(err)
}
defer direct.Delete()
var (
wg sync.WaitGroup
buf = bytes.NewBuffer(nil)
)
wg.Add(1)
go func() {
defer wg.Done()
io.Copy(buf, direct.Stdout)
}()

// the www-data user in the busybox image has a uid of 33
container, err := client.NewContainer(ctx, id,
WithNewSnapshot(id, image),
WithNewSpec(oci.WithImageConfig(image), oci.WithUsername("www-data"), oci.WithProcessArgs("id", "-u")),
)
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx, WithSnapshotCleanup)

task, err := container.NewTask(ctx, direct.IOCreate)
if err != nil {
t.Fatal(err)
}
defer task.Delete(ctx)

statusC, err := task.Wait(ctx)
if err != nil {
t.Fatal(err)
}

if err := task.Start(ctx); err != nil {
t.Fatal(err)
}
<-statusC

wg.Wait()

output := strings.TrimSuffix(buf.String(), "\n")
if output != "33" {
t.Errorf("expected www-data uid to be 33 but received %q", output)
}
}

func TestContainerUser(t *testing.T) {
t.Parallel()
t.Run("UserNameAndGroupName", func(t *testing.T) { testContainerUser(t, "www-data:www-data", "33:33") })
Expand Down
124 changes: 124 additions & 0 deletions integration/client/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2418,3 +2418,127 @@ func TestTaskSpec(t *testing.T) {
}
<-statusC
}

func TestContainerUsername(t *testing.T) {
t.Parallel()

client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()

var (
image Image
ctx, cancel = testContext(t)
id = t.Name()
)
defer cancel()

image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Fatal(err)
}

username := "www-data"
command := []string{
"id", "-u",
}
expectedOutput := "33"
if runtime.GOOS == "windows" {
username = "ContainerUser"
command = []string{
"echo", `%USERNAME%`,
}
expectedOutput = "ContainerUser"
}

// the www-data user in the busybox image has a uid of 33
container, err := client.NewContainer(ctx, id,
WithNewSnapshot(id, image),
WithNewSpec(oci.WithImageConfig(image), oci.WithUsername(username), withProcessArgs(command...)),
)
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx, WithSnapshotCleanup)

buf := bytes.NewBuffer(nil)
task, err := container.NewTask(ctx, cio.NewCreator(withByteBuffers(buf)))
if err != nil {
t.Fatal(err)
}

statusC, err := task.Wait(ctx)
if err != nil {
t.Fatal(err)
}

if err := task.Start(ctx); err != nil {
t.Fatal(err)
}
<-statusC
if _, err := task.Delete(ctx); err != nil {
t.Fatal(err)
}

output := strings.TrimSuffix(buf.String(), newLine)
if output != expectedOutput {
t.Errorf("expected %s uid to be %s but received %q", username, expectedOutput, buf.String())
}
}

func TestContainerPTY(t *testing.T) {
t.Parallel()

client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()

var (
image Image
ctx, cancel = testContext(t)
id = t.Name()
)
defer cancel()

image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Fatal(err)
}

container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithNewSpec(oci.WithImageConfig(image), oci.WithTTY, withProcessArgs("echo", "hello")))
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx, WithSnapshotCleanup)

buf := bytes.NewBuffer(nil)
task, err := container.NewTask(ctx, cio.NewCreator(withByteBuffers(buf), withProcessTTY()))
if err != nil {
t.Fatal(err)
}
defer task.Delete(ctx)

statusC, err := task.Wait(ctx)
if err != nil {
t.Error(err)
}

if err := task.Start(ctx); err != nil {
t.Fatal(err)
}

<-statusC

if _, err := task.Delete(ctx); err != nil {
t.Fatal(err)
}

out := buf.String()
if !strings.ContainsAny(fmt.Sprintf("%#q", out), `\x00`) {
t.Fatal(`expected \x00 in output`)
}
}

0 comments on commit a7f43c8

Please sign in to comment.