Skip to content

Commit

Permalink
move sigularity to a container for portability
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
  • Loading branch information
wagoodman committed Aug 2, 2022
1 parent 72e0ece commit 4679c3c
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 35 deletions.
20 changes: 17 additions & 3 deletions .github/workflows/validations.yaml
Expand Up @@ -151,15 +151,29 @@ jobs:
- name: Bootstrap CI environment dependencies
run: make ci-bootstrap

- name: Build key for tar cache
- name: Build key for test-fixture cache
run: make integration-fingerprint

- name: Restore integration test cache
- name: Restore integration test-fixture cache
uses: actions/cache@v2.1.3
with:
path: ${{ github.workspace }}/test/integration/test-fixtures/cache
key: ${{ runner.os }}-integration-test-cache-${{ hashFiles('test/integration/test-fixtures/cache.fingerprint') }}


- name: Build key for tool cache
run: make integration-tools-fingerprint

- name: Restore integration tool cache
id: integration-tool-cache
uses: actions/cache@v2.1.3
with:
path: ${{ github.workspace }}/test/integration/tools/cache
key: ${{ runner.os }}-integration-tools-cache-${{ hashFiles('test/integration/tools/cache.fingerprint') }}

- name: (cache-hit) Load integration tool cache
if: steps.integration-tool-cache.outputs.cache-hit == 'true'
run: make integration-tools-load

- name: Run integration tests
run: make integration

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,6 +8,7 @@
*.DS_Store
coverage.txt
**/test-fixtures/cache/
**/*.fingerprint

# Binaries for programs and plugins
*.exe
Expand Down
20 changes: 18 additions & 2 deletions Makefile
Expand Up @@ -44,7 +44,7 @@ help:
.PHONY: ci-bootstrap
ci-bootstrap: bootstrap
sudo apt install -y bc
curl -sLO https://github.com/sylabs/singularity/releases/download/v3.10.0/singularity-ce_3.10.0-focal_amd64.deb && sudo dpkg -i singularity-ce_3.10.0-focal_amd64.deb
curl -sLO https://github.com/sylabs/singularity/releases/download/v3.10.0/singularity-ce_3.10.0-focal_amd64.deb && sudo apt-get install -y -f singularity-ce_3.10.0-focal_amd64.deb

$(RESULTSDIR):
mkdir -p $(RESULTSDIR)
Expand Down Expand Up @@ -111,8 +111,24 @@ show-benchstat:
integration-fingerprint:
find test/integration/test-fixtures/image-* -type f -exec md5sum {} + | awk '{print $1}' | sort | md5sum | tee test/integration/test-fixtures/cache.fingerprint

.PHONY: integration-tools-fingerprint
integration-tools-fingerprint:
@cd test/integration/tools && make fingerprint

.PHONY: integration-tools
integration-tools:
@cd test/integration/tools && make

.PHONY: integration-tools
integration-tools-load:
@cd test/integration/tools && make load-cache

.PHONY: integration-tools
integration-tools-save:
@cd test/integration/tools && make save-cache

.PHONY: integration
integration: ## Run integration tests
integration: integration-tools ## Run integration tests
$(call title,Running integration tests)
go test -v ./test/integration

Expand Down
38 changes: 18 additions & 20 deletions pkg/imagetest/image_fixtures.go
Expand Up @@ -289,27 +289,25 @@ func getFixtureImageSIFPath(t testing.TB, fixtureName, sifStoreDir, sifFileName
}

func buildSIFFromDocker(t testing.TB, image, path string) error {
singularity, err := exec.LookPath("singularity")
if err != nil {
t.Skipf("singularity not found: %v", err)
}

outfile, err := os.Create(path)
if err != nil {
t.Fatal("unable to create file for SIF image:", err)
}
defer func() {
err := outfile.Close()
if err != nil {
t.Fatalf("unable to close file path=%q : %+v", path, err)
}
}()

cmdArgs := []string{"build", "--disable-cache", "--force", path, "docker-daemon:" + image}
cmd := exec.Command(singularity, cmdArgs...)
cmd.Env = os.Environ()
absHostDir, err := filepath.Abs(filepath.Dir(path))
require.NoError(t, err)

cmd.Stdout = outfile
singularityArgs := []string{"build", "--disable-cache", "--force", "image/" + filepath.Base(path), "docker-daemon:" + image}

allArgs := append([]string{
"run",
"-t",
"--rm",
"-v",
"/var/run/docker.sock:/var/run/docker.sock",
"-v",
absHostDir + ":/image",
"localhost/singularity:dev", // from integration tools (make integration-tools)
"singularity",
}, singularityArgs...)

cmd := exec.Command("docker", allArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd.Run()
Expand Down
13 changes: 3 additions & 10 deletions test/integration/fixture_image_simple_test.go
Expand Up @@ -49,7 +49,6 @@ var simpleImageSingularityLayer = []image.LayerMetadata{

var simpleImageTestCases = []testCase{
{
name: "FromTarball",
source: "docker-archive",
imageMediaType: v1Types.DockerManifestSchema2,
layerMediaType: v1Types.DockerLayer,
Expand All @@ -58,7 +57,6 @@ var simpleImageTestCases = []testCase{
size: 65,
},
{
name: "FromDocker",
source: "docker",
imageMediaType: v1Types.DockerManifestSchema2,
layerMediaType: v1Types.DockerLayer,
Expand All @@ -69,7 +67,6 @@ var simpleImageTestCases = []testCase{
size: 65,
},
{
name: "FromPodman",
source: "podman",
imageMediaType: v1Types.DockerManifestSchema2,
layerMediaType: v1Types.DockerLayer,
Expand All @@ -78,7 +75,6 @@ var simpleImageTestCases = []testCase{
size: 65,
},
{
name: "FromOciTarball",
source: "oci-archive",
imageMediaType: v1Types.OCIManifestSchema1,
layerMediaType: v1Types.OCILayer,
Expand All @@ -87,7 +83,6 @@ var simpleImageTestCases = []testCase{
size: 65,
},
{
name: "FromOciDirectory",
source: "oci-dir",
imageMediaType: v1Types.OCIManifestSchema1,
layerMediaType: v1Types.OCILayer,
Expand All @@ -96,7 +91,6 @@ var simpleImageTestCases = []testCase{
size: 65,
},
{
name: "FromSingularity",
source: "singularity",
imageMediaType: sif.SingularityMediaType,
layerMediaType: image.SingularitySquashFSLayer,
Expand All @@ -108,7 +102,6 @@ var simpleImageTestCases = []testCase{
}

type testCase struct {
name string
source string
imageMediaType v1Types.MediaType
layerMediaType v1Types.MediaType
Expand All @@ -125,7 +118,7 @@ func TestSimpleImage(t *testing.T) {
expectedSet.Remove(int(image.OciRegistrySource))

for _, c := range simpleImageTestCases {
t.Run(c.name, func(t *testing.T) {
t.Run(c.source, func(t *testing.T) {
i := imagetest.GetFixtureImage(t, c.source, "image-simple")

assertImageSimpleMetadata(t, i, c)
Expand Down Expand Up @@ -153,7 +146,7 @@ func BenchmarkSimpleImage_GetImage(b *testing.B) {
}
request := imagetest.PrepareFixtureImage(b, c.source, "image-simple")

b.Run(c.name, func(b *testing.B) {
b.Run(c.source, func(b *testing.B) {
var bi *image.Image
for i := 0; i < b.N; i++ {

Expand Down Expand Up @@ -182,7 +175,7 @@ func BenchmarkSimpleImage_FetchSquashedContents(b *testing.B) {
if len(paths) == 0 {
b.Fatalf("expected paths but found none")
}
b.Run(c.name, func(b *testing.B) {
b.Run(c.source, func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, ref := range paths {
f, err := img.FileCatalog.Get(ref)
Expand Down
1 change: 1 addition & 0 deletions test/integration/tools/.gitignore
@@ -0,0 +1 @@
cache
17 changes: 17 additions & 0 deletions test/integration/tools/Makefile
@@ -0,0 +1,17 @@
.PHONY: all
all:
cd singularity && make

.PHONY: save-cache
save-cache:
cd singularity && make save-cache

.PHONY: load-cache
load-cache:
cd singularity && make load-cache

.PHONY: fingerprint
fingerprint:
@# for all tools, generate
@rm -f cache.fingerprint
@cat */cache.fingerprint | sort | md5sum | tee cache.fingerprint
1 change: 1 addition & 0 deletions test/integration/tools/singularity/.dockerignore
@@ -0,0 +1 @@
Makefile
4 changes: 4 additions & 0 deletions test/integration/tools/singularity/Dockerfile
@@ -0,0 +1,4 @@
FROM ubuntu:20.04
RUN apt-get update && apt-get install curl -y && \
curl -sLO https://github.com/sylabs/singularity/releases/download/v3.10.0/singularity-ce_3.10.0-focal_amd64.deb && \
apt-get install -f ./singularity-ce_3.10.0-focal_amd64.deb -y
28 changes: 28 additions & 0 deletions test/integration/tools/singularity/Makefile
@@ -0,0 +1,28 @@
CACHE_DIR = ../cache
CACHE_FILE_NAME = singularity-image.tar
CACHE_FILE = $(CACHE_DIR)/$(CACHE_FILE_NAME)
TAG = localhost/singularity:dev

.PHONY: all
all:
@docker inspect $(TAG) > /dev/null && echo "$(TAG) already exists" || (make build && make save-cache)

.PHONY: build
build:
docker build -t $(TAG) .

.PHONY: save-cache
save-cache: $(CACHE_DIR)
docker image save $(TAG) -o $(CACHE_FILE)

.PHONY: load-cache
load-cache:
docker image load -i $(CACHE_FILE)

$(CACHE_DIR):
mkdir -p $(CACHE_DIR)

# note: this is used by CI to determine if the tool image should be rebuilt/refreshed
.PHONY: fingerprint
fingerprint:
find Dockerfile -type f -exec md5sum {} + | sort | md5sum | tee cache.fingerprint

0 comments on commit 4679c3c

Please sign in to comment.