1
0
mirror of https://github.com/aquasecurity/kube-bench.git synced 2024-11-17 21:58:42 +00:00
kube-bench/integration/docker.go
Roberto Rojas 47c5661034
Fixes issue #439: Adds integration testing using KIND (#520)
* Fixes issue #439: Adds integration testing using KIND

* try integration tests

* started using ticker and timeouts

* trying built container image

* adds load image into KIND

* adds comparison

* fixes as per PR review
2019-11-16 09:39:47 -05:00

61 lines
1.5 KiB
Go

package integration
import (
"os"
"path/filepath"
"github.com/pkg/errors"
"sigs.k8s.io/kind/pkg/cluster"
clusternodes "sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/container/docker"
"sigs.k8s.io/kind/pkg/fs"
"sigs.k8s.io/kind/pkg/util/concurrent"
)
func loadImageFromDocker(imageName string, kindCtx *cluster.Context) error {
// Check that the image exists locally and gets its ID, if not return error
_, err := docker.ImageID(imageName)
if err != nil {
return errors.Errorf("Image: %q not present locally", imageName)
}
selectedNodes, err := kindCtx.ListInternalNodes()
if err != nil {
return err
}
// Save the image into a tar
dir, err := fs.TempDir("", "image-tar")
if err != nil {
return errors.Wrap(err, "failed to create tempdir")
}
defer os.RemoveAll(dir)
imageTarPath := filepath.Join(dir, "image.tar")
err = docker.Save(imageName, imageTarPath)
if err != nil {
return err
}
// Load the image on the selected nodes
fns := []func() error{}
for _, selectedNode := range selectedNodes {
selectedNode := selectedNode // capture loop variable
fns = append(fns, func() error {
return loadImage(imageTarPath, &selectedNode)
})
}
return concurrent.UntilError(fns)
}
// loads an image tarball onto a node
func loadImage(imageTarName string, node *clusternodes.Node) error {
f, err := os.Open(imageTarName)
if err != nil {
return errors.Wrap(err, "failed to open image")
}
defer f.Close()
return node.LoadImageArchive(f)
}