github.com/apptainer/singularity@v3.1.1+incompatible/cmd/singularity/pull_test.go (about) 1 // Copyright (c) 2018-2019, Sylabs Inc. All rights reserved. 2 // This software is licensed under a 3-clause BSD license. Please consult the 3 // LICENSE.md file distributed with the sources of this project regarding your 4 // rights to use or distribute this software. 5 6 package main 7 8 import ( 9 "os" 10 "os/exec" 11 "testing" 12 13 "github.com/sylabs/singularity/internal/pkg/test" 14 ) 15 16 func imagePull(library string, imagePath string, sourceSpec string, force bool) ([]byte, error) { 17 var argv []string 18 argv = append(argv, "pull") 19 if force { 20 argv = append(argv, "--force") 21 } 22 if library != "" { 23 argv = append(argv, "--library", library) 24 } 25 if imagePath != "" { 26 argv = append(argv, imagePath) 27 } 28 argv = append(argv, sourceSpec) 29 30 return exec.Command(cmdPath, argv...).CombinedOutput() 31 } 32 33 func TestPull(t *testing.T) { 34 test.DropPrivilege(t) 35 36 imagePath := "./test_pull.sif" 37 38 tests := []struct { 39 name string 40 sourceSpec string 41 force bool 42 library string 43 imagePath string 44 success bool 45 }{ 46 {"Pull_Library", "library://alpine:3.7", false, "", imagePath, true}, // https://cloud.sylabs.io/library 47 {"Force", "library://alpine:3.7", true, "", imagePath, true}, 48 {"Pull_Docker", "docker://alpine:3.7", true, "", imagePath, true}, // https://hub.docker.com/ 49 {"Pull_Shub", "shub://GodloveD/busybox", true, "", imagePath, true}, // https://singularity-hub.org/ 50 {"PullWithHash", "library://sylabs/tests/signed:sha256.5c439fd262095766693dae95fb81334c3a02a7f0e4dc6291e0648ed4ddc61c6c", true, "", imagePath, true}, 51 {"PullWithoutTransportProtocol", "alpine:3.7", true, "", imagePath, true}, 52 } 53 defer os.Remove(imagePath) 54 for _, tt := range tests { 55 t.Run(tt.name, test.WithoutPrivilege(func(t *testing.T) { 56 if b, err := imagePull(tt.library, tt.imagePath, tt.sourceSpec, tt.force); err != nil { 57 t.Log(string(b)) 58 t.Fatalf("unexpected failure: %v", err) 59 } 60 imageVerify(t, tt.imagePath, false) 61 })) 62 } 63 } 64 65 func TestPullNonExistent(t *testing.T) { 66 test.DropPrivilege(t) 67 68 if b, err := imagePull("", "", "library://this_should_not/exist", false); err == nil { 69 t.Log(string(b)) 70 t.Fatalf("unexpected success") 71 } 72 }