Skip to content

Commit

Permalink
refactor(integration): use new kind API for integration tests
Browse files Browse the repository at this point in the history
Co-authored-by: Lorenzo Fontana <[email protected]>
Signed-off-by: Leonardo Di Donato <[email protected]>
  • Loading branch information
leodido and fntlnz committed Oct 30, 2020
1 parent 418920d commit 792ff8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion integration/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (k *KubectlTraceSuite) TestRunNode(c *check.C) {
nodes, err := k.kindContext.ListNodes()
nodes, err := k.provider.ListNodes()
c.Assert(err, check.IsNil)
c.Assert(len(nodes), check.Equals, 1)

Expand Down
49 changes: 33 additions & 16 deletions integration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/iovisor/kubectl-trace/pkg/cmd"
"gotest.tools/icmd"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/create"
"sigs.k8s.io/kind/pkg/container/docker"
"sigs.k8s.io/kind/pkg/fs"
)

Expand All @@ -25,6 +23,9 @@ var (
type KubectlTraceSuite struct {
kubeConfigPath string
kindContext *cluster.Context

provider *cluster.Provider
name string
}

func init() {
Expand All @@ -36,39 +37,40 @@ func init() {
}

func (k *KubectlTraceSuite) SetUpSuite(c *check.C) {
clusterName, err := generateClusterName()
var err error
k.name, err = generateClusterName()
c.Assert(err, check.IsNil)
kctx := cluster.NewContext(clusterName)

err = kctx.Create(create.Retain(false), create.WaitForReady(time.Duration(0)))
k.provider = cluster.NewProvider()
// Create the cluster
err := k.provider.Create(
k.name,
cluster.CreateWithRetain(false),
cluster.CreateWithWaitForReady(time.Duration(0)),
)
c.Assert(err, check.IsNil)
k.kindContext = kctx

nodes, err := kctx.ListNodes()

nodes, err := k.provider.ListNodes(k.name)
c.Assert(err, check.IsNil)

// copy the bpftrace into a tar
// Copy the bpftrace into a tar
dir, err := fs.TempDir("", "image-tar")
c.Assert(err, check.IsNil)
defer os.RemoveAll(dir)
imageTarPath := filepath.Join(dir, "image.tar")

err = docker.Save(cmd.ImageNameTag, imageTarPath)
err = save(cmd.ImageNameTag, imageTarPath)
c.Assert(err, check.IsNil)

f, err := os.Open(imageTarPath)
c.Assert(err, check.IsNil)

// copy the bpftrace image to the nodes
// Copy the bpftrace image to the nodes
for _, n := range nodes {
err = n.LoadImageArchive(f)
err = loadImage(imageTarPath, n)
c.Assert(err, check.IsNil)
}
}

func (k *KubectlTraceSuite) TearDownSuite(c *check.C) {
err := k.kindContext.Delete()
err := k.provider.Delete(k.name)
c.Assert(err, check.IsNil)
}

Expand All @@ -88,3 +90,18 @@ func generateClusterName() (string, error) {
}
return strings.ToLower(fmt.Sprintf("%X", buf)), nil
}

// loads an image tarball onto a node
func loadImage(imageTarName string, node nodes.Node) error {
f, err := os.Open(imageTarName)
if err != nil {
return errors.Wrap(err, "failed to open image")
}
defer f.Close()
return nodeutils.LoadImageArchive(node, f)
}

// save saves image to dest, as in `docker save`
func save(image, dest string) error {
return exec.Command("docker", "save", "-o", dest, image).Run()
}

0 comments on commit 792ff8b

Please sign in to comment.