2023-08-10 09:06:00 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/pipeline/frontend/metadata"
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWithWorkspace(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithWorkspace(
|
|
|
|
"/pipeline",
|
|
|
|
"src/github.com/octocat/hello-world",
|
|
|
|
),
|
|
|
|
)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, "/pipeline", compiler.base)
|
|
|
|
assert.Equal(t, "src/github.com/octocat/hello-world", compiler.path)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithEscalated(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithEscalated(
|
|
|
|
"docker",
|
|
|
|
"docker-dev",
|
|
|
|
),
|
|
|
|
)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, "docker", compiler.escalated[0])
|
|
|
|
assert.Equal(t, "docker-dev", compiler.escalated[1])
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithVolumes(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithVolumes(
|
|
|
|
"/tmp:/tmp",
|
|
|
|
"/foo:/foo",
|
|
|
|
),
|
|
|
|
)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, "/tmp:/tmp", compiler.volumes[0])
|
|
|
|
assert.Equal(t, "/foo:/foo", compiler.volumes[1])
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithNetworks(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithNetworks(
|
|
|
|
"overlay_1",
|
|
|
|
"overlay_bar",
|
|
|
|
),
|
|
|
|
)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, "overlay_1", compiler.networks[0])
|
|
|
|
assert.Equal(t, "overlay_bar", compiler.networks[1])
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithResourceLimit(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithResourceLimit(
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
3,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
"0,2-5",
|
|
|
|
),
|
|
|
|
)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.EqualValues(t, 1, compiler.reslimit.MemSwapLimit)
|
|
|
|
assert.EqualValues(t, 2, compiler.reslimit.MemLimit)
|
|
|
|
assert.EqualValues(t, 3, compiler.reslimit.ShmSize)
|
|
|
|
assert.EqualValues(t, 4, compiler.reslimit.CPUQuota)
|
|
|
|
assert.EqualValues(t, 5, compiler.reslimit.CPUShares)
|
|
|
|
assert.Equal(t, "0,2-5", compiler.reslimit.CPUSet)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithPrefix(t *testing.T) {
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, "someprefix_", New(WithPrefix("someprefix_")).prefix)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithMetadata(t *testing.T) {
|
2023-06-04 22:15:07 +00:00
|
|
|
metadata := metadata.Metadata{
|
|
|
|
Repo: metadata.Repo{
|
|
|
|
Owner: "octacat",
|
|
|
|
Name: "hello-world",
|
2022-11-04 23:35:06 +00:00
|
|
|
Private: true,
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: "https://github.com/octocat/hello-world",
|
2022-11-04 23:35:06 +00:00
|
|
|
CloneURL: "https://github.com/octocat/hello-world.git",
|
2019-04-06 13:44:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
compiler := New(
|
|
|
|
WithMetadata(metadata),
|
|
|
|
)
|
2021-11-25 19:43:31 +00:00
|
|
|
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, metadata, compiler.metadata)
|
|
|
|
assert.Equal(t, metadata.Repo.Name, compiler.env["CI_REPO_NAME"])
|
|
|
|
assert.Equal(t, metadata.Repo.ForgeURL, compiler.env["CI_REPO_URL"])
|
|
|
|
assert.Equal(t, metadata.Repo.CloneURL, compiler.env["CI_REPO_CLONE_URL"])
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithLocal(t *testing.T) {
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.True(t, New(WithLocal(true)).local)
|
|
|
|
assert.False(t, New(WithLocal(false)).local)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithNetrc(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithNetrc(
|
|
|
|
"octocat",
|
|
|
|
"password",
|
|
|
|
"github.com",
|
|
|
|
),
|
|
|
|
)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, "octocat", compiler.cloneEnv["CI_NETRC_USERNAME"])
|
|
|
|
assert.Equal(t, "password", compiler.cloneEnv["CI_NETRC_PASSWORD"])
|
|
|
|
assert.Equal(t, "github.com", compiler.cloneEnv["CI_NETRC_MACHINE"])
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithProxy(t *testing.T) {
|
|
|
|
// alter the default values
|
2023-08-07 19:13:26 +00:00
|
|
|
noProxy := "example.com"
|
|
|
|
httpProxy := "bar.com"
|
|
|
|
httpsProxy := "baz.com"
|
2019-04-06 13:44:04 +00:00
|
|
|
|
|
|
|
testdata := map[string]string{
|
|
|
|
"no_proxy": noProxy,
|
|
|
|
"NO_PROXY": noProxy,
|
|
|
|
"http_proxy": httpProxy,
|
|
|
|
"HTTP_PROXY": httpProxy,
|
|
|
|
"https_proxy": httpsProxy,
|
|
|
|
"HTTPS_PROXY": httpsProxy,
|
|
|
|
}
|
|
|
|
compiler := New(
|
2023-08-07 19:13:26 +00:00
|
|
|
WithProxy(ProxyOptions{
|
|
|
|
NoProxy: noProxy,
|
|
|
|
HTTPProxy: httpProxy,
|
|
|
|
HTTPSProxy: httpsProxy,
|
|
|
|
}),
|
2019-04-06 13:44:04 +00:00
|
|
|
)
|
|
|
|
for key, value := range testdata {
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, value, compiler.env[key])
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithEnviron(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithEnviron(
|
|
|
|
map[string]string{
|
|
|
|
"RACK_ENV": "development",
|
|
|
|
"SHOW": "true",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, "development", compiler.env["RACK_ENV"])
|
|
|
|
assert.Equal(t, "true", compiler.env["SHOW"])
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithVolumeCacher(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithVolumeCacher("/cache"),
|
|
|
|
)
|
|
|
|
cacher, ok := compiler.cacher.(*volumeCacher)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "/cache", cacher.base)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:05:19 +00:00
|
|
|
func TestWithDefaultCloneImage(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithDefaultCloneImage("not-an-image"),
|
|
|
|
)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.Equal(t, "not-an-image", compiler.defaultCloneImage)
|
2022-02-10 16:05:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 13:44:04 +00:00
|
|
|
func TestWithS3Cacher(t *testing.T) {
|
|
|
|
compiler := New(
|
|
|
|
WithS3Cacher("some-access-key", "some-secret-key", "some-region", "some-bucket"),
|
|
|
|
)
|
|
|
|
cacher, ok := compiler.cacher.(*s3Cacher)
|
2024-01-14 18:33:58 +00:00
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, "some-bucket", cacher.bucket)
|
|
|
|
assert.Equal(t, "some-access-key", cacher.access)
|
|
|
|
assert.Equal(t, "some-region", cacher.region)
|
|
|
|
assert.Equal(t, "some-secret-key", cacher.secret)
|
2019-04-06 13:44:04 +00:00
|
|
|
}
|