forked from firecracker-microvm/firecracker-containerd
-
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.
Merge pull request firecracker-microvm#637 from austinvazquez/integ-t…
…est-ref refactor: integ test common code into internal package
- Loading branch information
Showing
11 changed files
with
234 additions
and
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package integtest | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
|
||
"github.com/containerd/containerd" | ||
"github.com/containerd/containerd/cio" | ||
) | ||
|
||
// CommandResult encapsulates the stdout, stderr, and exit code returned | ||
// from a task. | ||
type CommandResult struct { | ||
Stdout string | ||
Stderr string | ||
ExitCode uint32 | ||
} | ||
|
||
// RunTask is a utility function for running a task and returning the result. | ||
func RunTask(ctx context.Context, c containerd.Container) (*CommandResult, error) { | ||
var stdout bytes.Buffer | ||
var stderr bytes.Buffer | ||
|
||
task, err := c.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, &stdout, &stderr))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
exitCh, err := task.Wait(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = task.Start(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
select { | ||
case exitStatus := <-exitCh: | ||
if err := exitStatus.Error(); err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err := task.Delete(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &CommandResult{ | ||
Stdout: stdout.String(), | ||
Stderr: stderr.String(), | ||
ExitCode: exitStatus.ExitCode(), | ||
}, nil | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
} | ||
} |
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,21 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package integtest | ||
|
||
import "github.com/firecracker-microvm/firecracker-containerd/firecracker-control/client" | ||
|
||
// NewFCControlClient returns a Firecracker control client for the given socket. | ||
func NewFCControlClient(socket string) (*client.Client, error) { | ||
return client.New(socket + ".ttrpc") | ||
} |
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,34 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package integtest | ||
|
||
import ( | ||
"github.com/firecracker-microvm/firecracker-containerd/config" | ||
"github.com/firecracker-microvm/firecracker-containerd/proto" | ||
) | ||
|
||
// WithDefaultNetwork is an option to use the default network configuration | ||
// in the runtime configuration for integration testing | ||
func WithDefaultNetwork() func(c *config.Config) { | ||
return func(c *config.Config) { | ||
c.DefaultNetworkInterfaces = []proto.FirecrackerNetworkInterface{ | ||
{ | ||
CNIConfig: &proto.CNIConfiguration{ | ||
NetworkName: "fcnet", | ||
InterfaceName: "veth0", | ||
}, | ||
}, | ||
} | ||
} | ||
} |
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,36 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package integtest | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
const ( | ||
// FirecrackerRuntime is the Firecracker-containerd runtime | ||
FirecrackerRuntime = "aws.firecracker" | ||
|
||
containerdSockPathEnvVar = "CONTAINERD_SOCKET" | ||
) | ||
|
||
var ( | ||
// ContainerdSockPath is the default Firecracker-containerd socket path | ||
ContainerdSockPath = "/run/firecracker-containerd/containerd.sock" | ||
) | ||
|
||
func init() { | ||
if v := os.Getenv(containerdSockPathEnvVar); v != "" { | ||
ContainerdSockPath = v | ||
} | ||
} |
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
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
Oops, something went wrong.