github.com/stevenmatthewt/agent@v3.5.4+incompatible/agent/artifact_uploader_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/buildkite/agent/api"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func findArtifact(artifacts []*api.Artifact, search string) *api.Artifact {
    15  	for _, a := range artifacts {
    16  		if filepath.Base(a.Path) == search {
    17  			return a
    18  		}
    19  	}
    20  
    21  	return nil
    22  }
    23  
    24  func TestCollect(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	wd, _ := os.Getwd()
    28  	root := filepath.Join(wd, "..")
    29  	os.Chdir(root)
    30  	defer os.Chdir(wd)
    31  
    32  	volumeName := filepath.VolumeName(root)
    33  	rootWithoutVolume := strings.TrimPrefix(root, volumeName)
    34  
    35  	uploader := ArtifactUploader{
    36  		Paths: fmt.Sprintf("%s;%s",
    37  			filepath.Join("test", "fixtures", "artifacts", "**/*.jpg"),
    38  			filepath.Join(root, "test", "fixtures", "artifacts", "**/*.gif"),
    39  		),
    40  	}
    41  
    42  	artifacts, err := uploader.Collect()
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  
    47  	assert.Equal(t, len(artifacts), 4)
    48  
    49  	var testCases = []struct {
    50  		Name         string
    51  		Path         string
    52  		AbsolutePath string
    53  		GlobPath     string
    54  		FileSize     int
    55  		Sha1Sum      string
    56  	}{
    57  		{
    58  			"Mr Freeze.jpg",
    59  			filepath.Join("test", "fixtures", "artifacts", "Mr Freeze.jpg"),
    60  			filepath.Join(root, "test", "fixtures", "artifacts", "Mr Freeze.jpg"),
    61  			filepath.Join("test", "fixtures", "artifacts", "**", "*.jpg"),
    62  			362371,
    63  			"f5bc7bc9f5f9c3e543dde0eb44876c6f9acbfb6b",
    64  		},
    65  		{
    66  			"Commando.jpg",
    67  			filepath.Join("test", "fixtures", "artifacts", "folder", "Commando.jpg"),
    68  			filepath.Join(root, "test", "fixtures", "artifacts", "folder", "Commando.jpg"),
    69  			filepath.Join("test", "fixtures", "artifacts", "**", "*.jpg"),
    70  			113000,
    71  			"811d7cb0317582e22ebfeb929d601cdabea4b3c0",
    72  		},
    73  		{
    74  			"The Terminator.jpg",
    75  			filepath.Join("test", "fixtures", "artifacts", "this is a folder with a space", "The Terminator.jpg"),
    76  			filepath.Join(root, "test", "fixtures", "artifacts", "this is a folder with a space", "The Terminator.jpg"),
    77  			filepath.Join("test", "fixtures", "artifacts", "**", "*.jpg"),
    78  			47301,
    79  			"ed76566ede9cb6edc975fcadca429665aad8785a",
    80  		},
    81  		{
    82  			"Smile.gif",
    83  			filepath.Join(rootWithoutVolume[1:], "test", "fixtures", "artifacts", "gifs", "Smile.gif"),
    84  			filepath.Join(root, "test", "fixtures", "artifacts", "gifs", "Smile.gif"),
    85  			filepath.Join(root, "test", "fixtures", "artifacts", "**", "*.gif"),
    86  			2038453,
    87  			"bd4caf2e01e59777744ac1d52deafa01c2cb9bfd",
    88  		},
    89  	}
    90  
    91  	for _, tc := range testCases {
    92  		t.Run(tc.Name, func(t *testing.T) {
    93  			a := findArtifact(artifacts, tc.Name)
    94  			if a == nil {
    95  				t.Fatalf("Failed to find artifact %q", tc.Name)
    96  			}
    97  
    98  			assert.Equal(t, tc.Path, a.Path)
    99  			assert.Equal(t, tc.AbsolutePath, a.AbsolutePath)
   100  			assert.Equal(t, tc.GlobPath, a.GlobPath)
   101  			assert.Equal(t, tc.FileSize, int(a.FileSize))
   102  			assert.Equal(t, tc.Sha1Sum, a.Sha1Sum)
   103  		})
   104  	}
   105  }
   106  
   107  func TestCollectThatDoesntMatchAnyFiles(t *testing.T) {
   108  	wd, _ := os.Getwd()
   109  	root := filepath.Join(wd, "..")
   110  	os.Chdir(root)
   111  	defer os.Chdir(wd)
   112  
   113  	uploader := ArtifactUploader{Paths: strings.Join([]string{
   114  		filepath.Join("log", "*"),
   115  		filepath.Join("tmp", "capybara", "**", "*"),
   116  		filepath.Join("mkmf.log"),
   117  		filepath.Join("log", "mkmf.log"),
   118  	}, ";")}
   119  
   120  	artifacts, err := uploader.Collect()
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  
   125  	assert.Equal(t, len(artifacts), 0)
   126  }
   127  
   128  func TestCollectWithSomeGlobsThatDontMatchAnything(t *testing.T) {
   129  	wd, _ := os.Getwd()
   130  	root := filepath.Join(wd, "..")
   131  	os.Chdir(root)
   132  	defer os.Chdir(wd)
   133  
   134  	uploader := ArtifactUploader{Paths: strings.Join([]string{
   135  		filepath.Join("dontmatchanything", "*"),
   136  		filepath.Join("dontmatchanything.zip"),
   137  		filepath.Join("test", "fixtures", "artifacts", "**", "*.jpg"),
   138  	}, ";")}
   139  
   140  	artifacts, err := uploader.Collect()
   141  	if err != nil {
   142  		t.Fatal(err)
   143  	}
   144  
   145  	if len(artifacts) != 3 {
   146  		t.Fatalf("Expected to match 3 artifacts, found %d", len(artifacts))
   147  	}
   148  }