-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Maksim An <[email protected]>
- Loading branch information
Showing
5 changed files
with
208 additions
and
18 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
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,50 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
"sync" | ||
|
||
"github.com/Microsoft/go-winio" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
soutPipe := os.Getenv("CONTAINER_STDOUT") | ||
waitPipe := os.Getenv("CONTAINER_WAIT") | ||
|
||
sout, err := winio.DialPipeContext(ctx, soutPipe) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "couldn't open pipe. %s", err) | ||
os.Exit(1) | ||
} | ||
defer sout.Close() | ||
|
||
wait, err := winio.DialPipeContext(ctx, waitPipe) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "couldn't open pipe. %s", err) | ||
os.Exit(1) | ||
} | ||
|
||
destPath := os.Args[1] | ||
dest, err := os.Create(destPath) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "couldn't open destination file: %s", err) | ||
} | ||
defer dest.Close() | ||
|
||
wait.Close() | ||
|
||
var wg sync.WaitGroup | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
io.Copy(dest, sout) | ||
}() | ||
wg.Wait() | ||
} |
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,138 @@ | ||
// +build functional | ||
|
||
package cri_containerd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" | ||
) | ||
|
||
// This test requires compiling a helper logging binary which can be found | ||
// at test/cri-containerd/helpers/log.go. Copy log.exe to ContainerPlat install | ||
// directory or set "TEST_BINARY_ROOT" environment variable, which this test | ||
// will use to construct logPath for CreateContainerRequest and as the location | ||
// of stdout artifacts created by the binary | ||
func Test_Run_Container_With_Binary_Logger(t *testing.T) { | ||
client := newTestRuntimeClient(t) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
logBinaryRoot := os.Getenv("TEST_BINARY_ROOT") | ||
if logBinaryRoot == "" { | ||
logBinaryRoot = "/ContainerPlat" | ||
} | ||
|
||
binaryPath := fmt.Sprintf("binary://%s/log.exe", logBinaryRoot) | ||
|
||
type config struct { | ||
TestConfig | ||
expectedContent string | ||
} | ||
|
||
tests := []config{ | ||
{ | ||
TestConfig: TestConfig{ | ||
name: "WCOW_Process", | ||
containerName: t.Name() + "-Container-WCOW_Process", | ||
requiredFeatures: []string{featureWCOWProcess}, | ||
runtimeHandler: wcowProcessRuntimeHandler, | ||
sandboxImage: imageWindowsNanoserver, | ||
containerImage: imageWindowsNanoserver, | ||
cmd: []string{"ping", "-t", "127.0.0.1"}, | ||
}, | ||
expectedContent: "Pinging 127.0.0.1 with 32 bytes of data", | ||
}, | ||
{ | ||
TestConfig: TestConfig{ | ||
name: "WCOW_Hypervisor", | ||
containerName: t.Name() + "-Container-WCOW_Hypervisor", | ||
requiredFeatures: []string{featureWCOWHypervisor}, | ||
runtimeHandler: wcowHypervisorRuntimeHandler, | ||
sandboxImage: imageWindowsNanoserver, | ||
containerImage: imageWindowsNanoserver, | ||
cmd: []string{"ping", "-t", "127.0.0.1"}, | ||
}, | ||
expectedContent: "Pinging 127.0.0.1 with 32 bytes of data", | ||
}, | ||
{ | ||
TestConfig: TestConfig{ | ||
name: "LCOW", | ||
containerName: t.Name() + "-Container-LCOW", | ||
requiredFeatures: []string{featureLCOW}, | ||
runtimeHandler: lcowRuntimeHandler, | ||
sandboxImage: imageLcowK8sPause, | ||
containerImage: imageLcowAlpine, | ||
cmd: []string{"ash", "-c", "while true; do echo 'Hello, World!'; sleep 1; done"}, | ||
}, | ||
expectedContent: "Hello, World!", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
requireFeatures(t, test.requiredFeatures...) | ||
|
||
requiredImages := []string{test.sandboxImage, test.containerImage} | ||
if test.runtimeHandler == lcowRuntimeHandler { | ||
pullRequiredLcowImages(t, requiredImages) | ||
} else { | ||
pullRequiredImages(t, requiredImages) | ||
} | ||
|
||
podReq := getRunPodSandboxRequest(t, test.runtimeHandler) | ||
podID := runPodSandbox(t, client, ctx, &podReq) | ||
defer removePodSandbox(t, client, ctx, podID) | ||
|
||
logFileName := fmt.Sprintf("%s/stdout-%s.txt", logBinaryRoot, test.name) | ||
conReq := getCreateContainerRequest(podID, test.containerName, test.containerImage, test.cmd, podReq.Config) | ||
conReq.Config.LogPath = binaryPath + fmt.Sprintf("?%s", logFileName) | ||
|
||
createAndRunContainer(t, client, ctx, conReq) | ||
|
||
_, err := os.Stat(logFileName) | ||
|
||
if os.IsNotExist(err) { | ||
t.Fatalf("log file was not created: %s", logFileName) | ||
} | ||
defer os.Remove(logFileName) | ||
|
||
ok, err := assertFileContent(logFileName, test.expectedContent) | ||
|
||
if err != nil { | ||
t.Fatalf("failed to read log file: %s", err) | ||
} | ||
|
||
if !ok { | ||
t.Fatalf("file content validation failed: %s", test.expectedContent) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func createAndRunContainer(t *testing.T, client runtime.RuntimeServiceClient, ctx context.Context, conReq *runtime.CreateContainerRequest) { | ||
containerID := createContainer(t, client, ctx, conReq) | ||
defer removeContainer(t, client, ctx, containerID) | ||
|
||
startContainer(t, client, ctx, containerID) | ||
defer stopContainer(t, client, ctx, containerID) | ||
|
||
// Let stdio kick in | ||
time.Sleep(time.Second * 1) | ||
} | ||
|
||
func assertFileContent(path string, content string) (bool, error) { | ||
fileContent, err := ioutil.ReadFile(path) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return strings.Contains(string(fileContent), content), nil | ||
} |
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