github.com/singularityware/singularity@v3.1.1+incompatible/pkg/client/library/push_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  	"net/http"
    10  	"testing"
    11  
    12  	"github.com/globalsign/mgo/bson"
    13  	"github.com/sylabs/singularity/internal/pkg/test"
    14  )
    15  
    16  //func postFile(baseURL string, filePath string, imageID string) error {
    17  func Test_postFile(t *testing.T) {
    18  
    19  	tests := []struct {
    20  		description string
    21  		imageRef    string
    22  		testFile    string
    23  		code        int
    24  		reqCallback func(*http.Request, *testing.T)
    25  		expectError bool
    26  	}{
    27  		{
    28  			description: "Container not found response",
    29  			code:        404,
    30  			reqCallback: nil,
    31  			imageRef:    bson.NewObjectId().Hex(),
    32  			testFile:    "test_data/test_sha256",
    33  			expectError: true,
    34  		},
    35  		{
    36  			description: "Unauthorized response",
    37  			code:        401,
    38  			reqCallback: nil,
    39  			imageRef:    bson.NewObjectId().Hex(),
    40  			testFile:    "test_data/test_sha256",
    41  			expectError: true,
    42  		},
    43  		{
    44  			description: "Valid Response",
    45  			code:        200,
    46  			reqCallback: nil,
    47  			imageRef:    bson.NewObjectId().Hex(),
    48  			testFile:    "test_data/test_sha256",
    49  			expectError: false,
    50  		},
    51  	}
    52  
    53  	// Loop over test cases
    54  	for _, tt := range tests {
    55  		t.Run(tt.description, test.WithoutPrivilege(func(t *testing.T) {
    56  
    57  			m := mockService{
    58  				t:        t,
    59  				code:     tt.code,
    60  				httpPath: "/v1/imagefile/" + tt.imageRef,
    61  			}
    62  
    63  			m.Run()
    64  
    65  			err := postFile(m.baseURI, testToken, tt.testFile, tt.imageRef)
    66  
    67  			if err != nil && !tt.expectError {
    68  				t.Errorf("Unexpected error: %v", err)
    69  			}
    70  			if err == nil && tt.expectError {
    71  				t.Errorf("Unexpected success. Expected error.")
    72  			}
    73  
    74  			m.Stop()
    75  
    76  		}))
    77  
    78  	}
    79  }