github.com/jaylevin/jenkins-library@v1.230.4/integration/integration_github_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  // can be execute with go test -tags=integration ./integration/...
     5  
     6  package main
     7  
     8  import (
     9  	"io/ioutil"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  
    17  	"github.com/SAP/jenkins-library/pkg/command"
    18  	pipergithub "github.com/SAP/jenkins-library/pkg/github"
    19  	"github.com/SAP/jenkins-library/pkg/piperenv"
    20  )
    21  
    22  func TestPiperGithubPublishRelease(t *testing.T) {
    23  	t.Parallel()
    24  	token := os.Getenv("PIPER_INTEGRATION_GITHUB_TOKEN")
    25  	if len(token) == 0 {
    26  		t.Fatal("No GitHub token maintained")
    27  	}
    28  	owner := os.Getenv("PIPER_INTEGRATION_GITHUB_OWNER")
    29  	if len(owner) == 0 {
    30  		owner = "OliverNocon"
    31  	}
    32  	repository := os.Getenv("PIPER_INTEGRATION_GITHUB_REPOSITORY")
    33  	if len(repository) == 0 {
    34  		repository = "piper-integration"
    35  	}
    36  	dir, err := ioutil.TempDir("", "")
    37  	defer os.RemoveAll(dir) // clean up
    38  	assert.NoError(t, err, "Error when creating temp dir")
    39  
    40  	testAsset := filepath.Join(dir, "test.txt")
    41  	err = ioutil.WriteFile(testAsset, []byte("Test"), 0644)
    42  	assert.NoError(t, err, "Error when writing temporary file")
    43  	test2Asset := filepath.Join(dir, "test2.txt")
    44  	err = ioutil.WriteFile(test2Asset, []byte("Test"), 0644)
    45  	assert.NoError(t, err, "Error when writing temporary file")
    46  
    47  	t.Run("test single asset - success", func(t *testing.T) {
    48  		//prepare pipeline environment
    49  		now := time.Now()
    50  		piperenv.SetResourceParameter(filepath.Join(dir, ".pipeline"), "commonPipelineEnvironment", "artifactVersion", now.Format("20060102150405"))
    51  
    52  		cmd := command.Command{}
    53  		cmd.SetDir(dir)
    54  
    55  		piperOptions := []string{
    56  			"githubPublishRelease",
    57  			"--owner",
    58  			owner,
    59  			"--repository",
    60  			repository,
    61  			"--token",
    62  			token,
    63  			"--assetPath",
    64  			testAsset,
    65  			"--noTelemetry",
    66  		}
    67  
    68  		err = cmd.RunExecutable(getPiperExecutable(), piperOptions...)
    69  		assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions)
    70  	})
    71  	t.Run("test multiple assets - success", func(t *testing.T) {
    72  		//prepare pipeline environment
    73  		now := time.Now()
    74  		piperenv.SetResourceParameter(filepath.Join(dir, ".pipeline"), "commonPipelineEnvironment", "artifactVersion", now.Format("20060102150405"))
    75  
    76  		cmd := command.Command{}
    77  		cmd.SetDir(dir)
    78  
    79  		piperOptions := []string{
    80  			"githubPublishRelease",
    81  			"--owner",
    82  			owner,
    83  			"--repository",
    84  			repository,
    85  			"--token",
    86  			token,
    87  			"--assetPathList",
    88  			testAsset,
    89  			"--assetPathList",
    90  			test2Asset,
    91  			"--noTelemetry",
    92  		}
    93  
    94  		err = cmd.RunExecutable(getPiperExecutable(), piperOptions...)
    95  		assert.NoError(t, err, "Calling piper with arguments %v failed.", piperOptions)
    96  	})
    97  }
    98  
    99  func TestGithubFetchCommitStatistics(t *testing.T) {
   100  	t.Parallel()
   101  	// prepare
   102  	token := os.Getenv("PIPER_INTEGRATION_GITHUB_TOKEN")
   103  	if len(token) == 0 {
   104  		t.Fatal("No GitHub token maintained")
   105  	}
   106  
   107  	owner := os.Getenv("PIPER_INTEGRATION_GITHUB_OWNER")
   108  	if len(owner) == 0 {
   109  		owner = "OliverNocon"
   110  	}
   111  	repository := os.Getenv("PIPER_INTEGRATION_GITHUB_REPOSITORY")
   112  	if len(repository) == 0 {
   113  		repository = "piper-integration"
   114  	}
   115  	// test
   116  	result, err := pipergithub.FetchCommitStatistics(&pipergithub.FetchCommitOptions{
   117  		Owner: owner, Repository: repository, APIURL: "https://api.github.com", Token: token, SHA: "3601ed6"})
   118  
   119  	// assert
   120  	assert.NoError(t, err)
   121  	assert.Equal(t, 2, result.Additions)
   122  	assert.Equal(t, 0, result.Deletions)
   123  	assert.Equal(t, 2, result.Total)
   124  	assert.Equal(t, 1, result.Files)
   125  }