1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/integration/integration_jenkins_test.go
Christopher Fenner 824cd7d768
fix(jenkins): fix job invocation (#2868)
* update mock

* update signarture

* add test case

* use latest gojenkins

* add integration test

* update mock

* add todo

* add job wrapper

* add job mock

* add test cases

* refactor

* cleanup

* update integration test case
2021-06-02 16:45:22 +02:00

50 lines
1.6 KiB
Go

// can be execute with go test -tags=integration ./integration/...
package main
import (
"context"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/SAP/jenkins-library/pkg/jenkins"
)
func TestTriggerJob(t *testing.T) {
t.Skip("no Jenkins instance for testing available yet")
//TODO: check if testcontainers can be used
// init
ctx := context.Background()
// ctx = context.WithValue(ctx, "debug", true)
// os.Setenv("PIPER_INTEGRATION_JENKINS_USER_NAME", "")
// os.Setenv("PIPER_INTEGRATION_JENKINS_TOKEN", "")
// os.Setenv("PIPER_INTEGRATION_JENKINS_HOST", "")
// os.Setenv("PIPER_INTEGRATION_JENKINS_JOB_NAME", "")
host := os.Getenv("PIPER_INTEGRATION_JENKINS_HOST")
user := os.Getenv("PIPER_INTEGRATION_JENKINS_USER_NAME")
token := os.Getenv("PIPER_INTEGRATION_JENKINS_TOKEN")
jobName := os.Getenv("PIPER_INTEGRATION_JENKINS_JOB_NAME")
require.NotEmpty(t, host, "Jenkins host url is missing")
require.NotEmpty(t, user, "Jenkins user name is missing")
require.NotEmpty(t, token, "Jenkins token is missing")
require.NotEmpty(t, jobName, "Jenkins job name is missing")
jenx, err := jenkins.Instance(ctx, http.DefaultClient, host, user, token)
require.NotNil(t, jenx, "could not connect to Jenkins instance")
require.NoError(t, err)
// test
job, getJobErr := jenkins.GetJob(ctx, jenx, jobName)
build, triggerJobErr := jenkins.TriggerJob(ctx, jenx, job, nil)
// asserts
assert.NoError(t, getJobErr)
assert.NoError(t, triggerJobErr)
assert.NotNil(t, build)
assert.True(t, build.IsRunning(ctx))
}