woodpecker/pipeline/frontend/yaml/compiler/option_test.go

175 lines
4.2 KiB
Go
Raw Normal View History

// 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"
"github.com/stretchr/testify/assert"
"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",
),
)
assert.Equal(t, "/pipeline", compiler.workspaceBase)
assert.Equal(t, "src/github.com/octocat/hello-world", compiler.workspacePath)
2019-04-06 13:44:04 +00:00
}
func TestWithEscalated(t *testing.T) {
compiler := New(
WithEscalated(
"docker",
"docker-dev",
),
)
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",
),
)
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",
),
)
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",
),
)
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) {
assert.Equal(t, "someprefix_", New(WithPrefix("someprefix_")).prefix)
2019-04-06 13:44:04 +00:00
}
func TestWithMetadata(t *testing.T) {
metadata := metadata.Metadata{
Repo: metadata.Repo{
Owner: "octacat",
Name: "hello-world",
Private: true,
ForgeURL: "https://github.com/octocat/hello-world",
CloneURL: "https://github.com/octocat/hello-world.git",
2019-04-06 13:44:04 +00:00
},
}
compiler := New(
WithMetadata(metadata),
)
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) {
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",
),
)
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
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(
WithProxy(ProxyOptions{
NoProxy: noProxy,
HTTPProxy: httpProxy,
HTTPSProxy: httpsProxy,
}),
2019-04-06 13:44:04 +00:00
)
for key, value := range testdata {
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",
},
),
)
assert.Equal(t, "development", compiler.env["RACK_ENV"])
assert.Equal(t, "true", compiler.env["SHOW"])
2019-04-06 13:44:04 +00:00
}
func TestWithDefaultCloneImage(t *testing.T) {
compiler := New(
WithDefaultCloneImage("not-an-image"),
)
assert.Equal(t, "not-an-image", compiler.defaultCloneImage)
}