go-plugin/metadata_test.go
2023-01-08 04:53:03 +02:00

118 lines
4.4 KiB
Go

// Copyright 2023 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plugin
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func testMetadata() map[string]string {
return map[string]string{
"CI": "woodpecker",
// Repository
"CI_REPO": "woodpecker-ci/woodpecker",
"CI_REPO_NAME": "woodpecker",
"CI_REPO_OWNER": "woodpecker-ci",
"CI_REPO_SCM": "git",
"CI_REPO_LINK": "https://github.com/woodpecker-ci/woodpecker",
"CI_REPO_CLONE_URL": "https://github.com/woodpecker-ci/woodpecker.git",
"CI_REPO_DEFAULT_BRANCH": "main",
"CI_REPO_PRIVATE": "false",
// Commit
"CI_COMMIT_SHA": "a1b2c3d4",
"CI_COMMIT_REF": "refs/heads/main",
"CI_COMMIT_REFSPEC": "refs/heads/main",
"CI_COMMIT_BRANCH": "main",
"CI_COMMIT_MESSAGE": "test commit",
"CI_COMMIT_AUTHOR": "John Doe",
"CI_COMMIT_AUTHOR_EMAIL": "john@example.com",
"CI_COMMIT_AUTHOR_AVATAR": "https://avatars.githubusercontent.com/u/1234567?v=4",
"CI_COMMIT_LINK": "https://github.com/woodpecker-ci/woodpecker/commit/a1b2c3d4",
// Build
"CI_PIPELINE_NUMBER": "1",
"CI_PIPELINE_EVENT": "push",
"CI_PIPELINE_LINK": "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1",
"CI_PIPELINE_STATUS": "running",
"CI_PIPELINE_CREATED": "1611234567",
"CI_PIPELINE_STARTED": "1611234567",
// Step
"CI_STEP_NUMBER": "1",
"CI_STEP_NAME": "test",
"CI_STEP_STATUS": "running",
"CI_STEP_STARTED": "1611234567",
// System
"CI_SYSTEM_NAME": "woodpecker",
"CI_SYSTEM_LINK": "https://ci.woodpecker-ci.org",
"CI_SYSTEM_VERSION": "1.0.0",
"CI_SYSTEM_HOST": "woodpecker-ci.org",
}
}
func TestMetadata(t *testing.T) {
for k, v := range testMetadata() {
os.Setenv(k, v)
defer os.Unsetenv(k)
}
plugin := New(Options{
Execute: func(ctx context.Context) error {
return nil
},
})
err := plugin.app.Run([]string{"test"})
require.NoError(t, err)
// Repository
assert.Equal(t, "woodpecker", plugin.Metadata.Repository.Name)
assert.Equal(t, "woodpecker-ci", plugin.Metadata.Repository.Owner)
assert.Equal(t, "https://github.com/woodpecker-ci/woodpecker", plugin.Metadata.Repository.Link)
assert.Equal(t, "https://github.com/woodpecker-ci/woodpecker.git", plugin.Metadata.Repository.CloneURL)
assert.Equal(t, "main", plugin.Metadata.Repository.Branch)
assert.False(t, plugin.Metadata.Repository.Private)
// Commit
assert.Equal(t, "a1b2c3d4", plugin.Metadata.Curr.Sha)
assert.Equal(t, "refs/heads/main", plugin.Metadata.Curr.Ref)
assert.Equal(t, "refs/heads/main", plugin.Metadata.Curr.Refspec)
assert.Equal(t, "main", plugin.Metadata.Curr.Branch)
assert.Equal(t, "test commit", plugin.Metadata.Curr.Message)
assert.Equal(t, "John Doe", plugin.Metadata.Curr.Author.Name)
assert.Equal(t, "john@example.com", plugin.Metadata.Curr.Author.Email)
assert.Equal(t, "https://avatars.githubusercontent.com/u/1234567?v=4", plugin.Metadata.Curr.Author.Avatar)
// Pipeline
assert.Equal(t, int64(1), plugin.Metadata.Pipeline.Number)
assert.Equal(t, "push", plugin.Metadata.Pipeline.Event)
assert.Equal(t, "https://ci.woodpecker-ci.org/woodpecker-ci/woodpecker/1", plugin.Metadata.Pipeline.Link)
assert.Equal(t, "running", plugin.Metadata.Pipeline.Status)
assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Created)
assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Pipeline.Started)
// Step
assert.Equal(t, 1, plugin.Metadata.Step.Number)
assert.Equal(t, time.Unix(1611234567, 0), plugin.Metadata.Step.Started)
// System
assert.Equal(t, "woodpecker", plugin.Metadata.System.Name)
assert.Equal(t, "https://ci.woodpecker-ci.org", plugin.Metadata.System.Link)
assert.Equal(t, "1.0.0", plugin.Metadata.System.Version)
assert.Equal(t, "woodpecker-ci.org", plugin.Metadata.System.Host)
}