github.com/apptainer/singularity@v3.1.1+incompatible/pkg/client/shub/util_test.go (about)

     1  // Copyright (c) 2018, 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 client
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/sylabs/singularity/internal/pkg/test"
    14  	useragent "github.com/sylabs/singularity/pkg/util/user-agent"
    15  )
    16  
    17  var (
    18  	validShubURIs = []string{
    19  		`shub://username/container`,
    20  		`shub://username/container:tag`,
    21  		`shub://username/container@00000000000000000000000000000000`,
    22  		`shub://registry/username/container`,
    23  		`shub://registry/with/levels/username/container`,
    24  		`shub://registry/user-name/container-with-dash`,
    25  		`shub://registry/username/container.with.period`,
    26  		`shub://username/container:tag-with-dash`,
    27  		`shub://username/container:tag_wtih_underscore`,
    28  		`shub://username/container:tag.with.period`,
    29  		`shub://myprivateregistry.sylabs.io/sylabs/container:latest`,
    30  	}
    31  )
    32  
    33  func TestMain(m *testing.M) {
    34  	useragent.InitValue("singularity", "3.0.0-alpha.1-303-gaed8d30-dirty")
    35  
    36  	os.Exit(m.Run())
    37  }
    38  
    39  // TestShubParser checks if the Shub ref parser is working as expected
    40  func TestIsShubPullRef(t *testing.T) {
    41  	test.DropPrivilege(t)
    42  	defer test.ResetPrivilege(t)
    43  
    44  	invalidShubURIs := []string{
    45  		`shub://username/`,
    46  		`shub://username/container:`,
    47  		`shub://username/container@`,
    48  		`shub://username/container@0000000000000000000000000000000`,
    49  		`shub://username/container@000000000000000000000000000000000`,
    50  		`shub://username/container@abcdefghijklmnopqrstuvwxyz123456`,
    51  		`shub://registry/user.name/container`,
    52  		`shub://username.with.period/container:tag`,
    53  		`shub://-username/container:`,
    54  		`shub://username-/container:`,
    55  		`shub://-registry/username/container:`,
    56  		`shub://registry-/username/container:`,
    57  	}
    58  
    59  	for _, uri := range validShubURIs {
    60  		t.Run(fmt.Sprintf("Valid URI: %v", uri),
    61  			func(t *testing.T) {
    62  				ok := isShubPullRef(uri)
    63  				if !ok {
    64  					t.Fatalf("failed to parse valid URI: %v", uri)
    65  				}
    66  			})
    67  	}
    68  
    69  	for _, uri := range invalidShubURIs {
    70  		t.Run(fmt.Sprintf("Invalid URI: %v", uri),
    71  			func(t *testing.T) {
    72  				ok := isShubPullRef(uri)
    73  				if ok {
    74  					t.Fatalf("failed to parse valid URI: %v", uri)
    75  				}
    76  			})
    77  	}
    78  }
    79  
    80  func TestShubParser(t *testing.T) {
    81  	for _, uri := range validShubURIs {
    82  		t.Run(fmt.Sprintf("Valid URI: %v", uri),
    83  			func(t *testing.T) {
    84  				sURI, err := shubParseReference(uri)
    85  				if err != nil {
    86  					t.Fatalf("failed to parse valid URI: %v", uri)
    87  				}
    88  				fmt.Println(sURI.String())
    89  			})
    90  	}
    91  }