github.com/stevenmatthewt/agent@v3.5.4+incompatible/bootstrap/integration/artifact_integration_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/buildkite/bintest"
     9  )
    10  
    11  func TestArtifactsUploadAfterCommand(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	tester, err := NewBootstrapTester()
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	defer tester.Close()
    19  
    20  	// Write a file in the command hook
    21  	tester.ExpectGlobalHook("command").Once().AndCallFunc(func(c *bintest.Call) {
    22  		err := ioutil.WriteFile(filepath.Join(c.Dir, "test.txt"), []byte("llamas"), 0700)
    23  		if err != nil {
    24  			t.Fatalf("Write failed with %v", err)
    25  		}
    26  		c.Exit(0)
    27  	})
    28  
    29  	// Mock out the artifact calls
    30  	agent := tester.MustMock(t, "buildkite-agent")
    31  	agent.
    32  		Expect("meta-data", "exists", "buildkite:git:commit").
    33  		AndExitWith(0)
    34  	agent.
    35  		Expect("artifact", "upload", "llamas.txt").
    36  		AndExitWith(0)
    37  
    38  	tester.RunAndCheck(t, "BUILDKITE_ARTIFACT_PATHS=llamas.txt")
    39  }
    40  
    41  func TestArtifactsUploadAfterCommandFails(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	tester, err := NewBootstrapTester()
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	defer tester.Close()
    49  
    50  	tester.MustMock(t, "my-command").Expect().AndCallFunc(func(c *bintest.Call) {
    51  		err := ioutil.WriteFile(filepath.Join(c.Dir, "test.txt"), []byte("llamas"), 0700)
    52  		if err != nil {
    53  			t.Fatalf("Write failed with %v", err)
    54  		}
    55  		c.Exit(1)
    56  	})
    57  
    58  	// Mock out the artifact calls
    59  	agent := tester.MustMock(t, "buildkite-agent")
    60  	agent.
    61  		Expect("meta-data", "exists", "buildkite:git:commit").
    62  		AndExitWith(0)
    63  	agent.
    64  		Expect("artifact", "upload", "llamas.txt").
    65  		AndExitWith(0)
    66  
    67  	err = tester.Run(t, "BUILDKITE_ARTIFACT_PATHS=llamas.txt", "BUILDKITE_COMMAND=my-command")
    68  	if err == nil {
    69  		t.Fatalf("Expected command to fail")
    70  	}
    71  
    72  	tester.CheckMocks(t)
    73  }
    74  
    75  func TestArtifactsUploadAfterCommandHookFails(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	tester, err := NewBootstrapTester()
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	defer tester.Close()
    83  
    84  	// Write a file in the command hook
    85  	tester.ExpectGlobalHook("command").Once().AndCallFunc(func(c *bintest.Call) {
    86  		err := ioutil.WriteFile(filepath.Join(c.Dir, "test.txt"), []byte("llamas"), 0700)
    87  		if err != nil {
    88  			t.Fatalf("Write failed with %v", err)
    89  		}
    90  		c.Exit(1)
    91  	})
    92  
    93  	// Mock out the artifact calls
    94  	agent := tester.MustMock(t, "buildkite-agent")
    95  	agent.
    96  		Expect("meta-data", "exists", "buildkite:git:commit").
    97  		AndExitWith(0)
    98  	agent.
    99  		Expect("artifact", "upload", "llamas.txt").
   100  		AndExitWith(0)
   101  
   102  	if err := tester.Run(t, "BUILDKITE_ARTIFACT_PATHS=llamas.txt"); err == nil {
   103  		t.Fatal("Expected bootstrap to fail")
   104  	}
   105  
   106  	tester.CheckMocks(t)
   107  }