本文整理汇总了Golang中github.com/openshift/origin/pkg/build/admission/testutil.Build函数的典型用法代码示例。如果您正苦于以下问题:Golang Build函数的具体用法?Golang Build怎么用?Golang Build使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Build函数的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestBuildOverrideForcePull
func TestBuildOverrideForcePull(t *testing.T) {
tests := []struct {
name string
build *buildapi.Build
}{
{
name: "build - custom",
build: u.Build().WithCustomStrategy().AsBuild(),
},
{
name: "build - docker",
build: u.Build().WithDockerStrategy().AsBuild(),
},
{
name: "build - source",
build: u.Build().WithSourceStrategy().AsBuild(),
},
}
ops := []admission.Operation{admission.Create, admission.Update}
for _, test := range tests {
for _, op := range ops {
overrides := NewBuildOverrides(&overridesapi.BuildOverridesConfig{ForcePull: true})
pod := u.Pod().WithBuild(t, test.build, "v1")
err := overrides.Admit(pod.ToAttributes())
if err != nil {
t.Errorf("%s: unexpected error: %v", test.name, err)
}
build := pod.GetBuild(t)
strategy := build.Spec.Strategy
switch {
case strategy.CustomStrategy != nil:
if strategy.CustomStrategy.ForcePull == false {
t.Errorf("%s (%s): force pull was false", test.name, op)
}
if pod.Spec.Containers[0].ImagePullPolicy != kapi.PullAlways {
t.Errorf("%s (%s): image pull policy is not PullAlways", test.name, op)
}
if pod.Spec.InitContainers[0].ImagePullPolicy != kapi.PullAlways {
t.Errorf("%s (%s): image pull policy is not PullAlways", test.name, op)
}
case strategy.DockerStrategy != nil:
if strategy.DockerStrategy.ForcePull == false {
t.Errorf("%s (%s): force pull was false", test.name, op)
}
case strategy.SourceStrategy != nil:
if strategy.SourceStrategy.ForcePull == false {
t.Errorf("%s (%s): force pull was false", test.name, op)
}
}
}
}
}
示例2: TestBuildDefaultsAnnotations
func TestBuildDefaultsAnnotations(t *testing.T) {
tests := []struct {
name string
build *buildapi.Build
annotations map[string]string
defaults map[string]string
expected map[string]string
}{
{
name: "build - nil annotations",
build: u.Build().AsBuild(),
annotations: nil,
defaults: map[string]string{"key1": "default1", "key2": "default2"},
expected: map[string]string{"key1": "default1", "key2": "default2"},
},
{
name: "build - full add",
build: u.Build().AsBuild(),
annotations: map[string]string{"key3": "value3"},
defaults: map[string]string{"key1": "default1", "key2": "default2"},
expected: map[string]string{"key1": "default1", "key2": "default2", "key3": "value3"},
},
{
name: "build - partial add",
build: u.Build().AsBuild(),
annotations: map[string]string{"key1": "value1"},
defaults: map[string]string{"key1": "default1", "key2": "default2"},
expected: map[string]string{"key1": "value1", "key2": "default2"},
},
}
for _, test := range tests {
defaults := BuildDefaults{config: &defaultsapi.BuildDefaultsConfig{Annotations: test.defaults}}
pod := u.Pod().WithBuild(t, test.build, "v1")
pod.Annotations = test.annotations
err := defaults.ApplyDefaults((*kapi.Pod)(pod))
if err != nil {
t.Errorf("%s: unexpected error: %v", test.name, err)
}
if len(pod.Annotations) != len(test.expected) {
t.Errorf("%s: incorrect number of annotations, expected %v, got %v", test.name, test.expected, pod.Annotations)
}
for k, v := range pod.Annotations {
if ev, ok := test.expected[k]; !ok || ev != v {
t.Errorf("%s: incorrect annotation value for key %s, expected %s, got %s", test.name, k, ev, v)
}
}
}
}
示例3: TestProxyDefaults
func TestProxyDefaults(t *testing.T) {
defaultsConfig := &defaultsapi.BuildDefaultsConfig{
GitHTTPProxy: "http",
GitHTTPSProxy: "https",
GitNoProxy: "no",
}
admitter := BuildDefaults{defaultsConfig}
pod := u.Pod().WithBuild(t, u.Build().WithDockerStrategy().AsBuild(), "v1")
err := admitter.ApplyDefaults((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
build, _, err := buildadmission.GetBuildFromPod((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if build.Spec.Source.Git.HTTPProxy == nil || len(*build.Spec.Source.Git.HTTPProxy) == 0 || *build.Spec.Source.Git.HTTPProxy != "http" {
t.Errorf("failed to find http proxy in git source")
}
if build.Spec.Source.Git.HTTPSProxy == nil || len(*build.Spec.Source.Git.HTTPSProxy) == 0 || *build.Spec.Source.Git.HTTPSProxy != "https" {
t.Errorf("failed to find http proxy in git source")
}
if build.Spec.Source.Git.NoProxy == nil || len(*build.Spec.Source.Git.NoProxy) == 0 || *build.Spec.Source.Git.NoProxy != "no" {
t.Errorf("failed to find no proxy setting in git source")
}
}
示例4: TestBuildDefaultsNodeSelector
func TestBuildDefaultsNodeSelector(t *testing.T) {
tests := []struct {
name string
build *buildapi.Build
defaults map[string]string
expected map[string]string
}{
{
name: "build - full add",
build: u.Build().AsBuild(),
defaults: map[string]string{"key1": "default1", "key2": "default2"},
expected: map[string]string{"key1": "default1", "key2": "default2"},
},
{
name: "build - ignored",
build: u.Build().WithNodeSelector(map[string]string{"key1": "value1"}).AsBuild(),
defaults: map[string]string{"key1": "default1", "key2": "default2"},
expected: map[string]string{"key1": "value1"},
},
{
name: "build - empty(non-nil) nodeselector",
build: u.Build().WithNodeSelector(map[string]string{}).AsBuild(),
defaults: map[string]string{"key1": "default1"},
expected: map[string]string{},
},
}
for _, test := range tests {
defaults := BuildDefaults{config: &defaultsapi.BuildDefaultsConfig{NodeSelector: test.defaults}}
pod := u.Pod().WithBuild(t, test.build, "v1")
// normally the pod will have the nodeselectors from the build, due to the pod creation logic
// in the build controller flow. fake it out here.
pod.Spec.NodeSelector = test.build.Spec.NodeSelector
err := defaults.ApplyDefaults((*kapi.Pod)(pod))
if err != nil {
t.Errorf("%s: unexpected error: %v", test.name, err)
}
if len(pod.Spec.NodeSelector) != len(test.expected) {
t.Errorf("%s: incorrect number of selectors, expected %v, got %v", test.name, test.expected, pod.Spec.NodeSelector)
}
for k, v := range pod.Spec.NodeSelector {
if ev, ok := test.expected[k]; !ok || ev != v {
t.Errorf("%s: incorrect selector value for key %s, expected %s, got %s", test.name, k, ev, v)
}
}
}
}
示例5: TestIncrementalDefaults
func TestIncrementalDefaults(t *testing.T) {
bool_t := true
defaultsConfig := &defaultsapi.BuildDefaultsConfig{
SourceStrategyDefaults: &defaultsapi.SourceStrategyDefaultsConfig{
Incremental: &bool_t,
},
}
admitter := BuildDefaults{defaultsConfig}
pod := u.Pod().WithBuild(t, u.Build().WithSourceStrategy().AsBuild(), "v1")
err := admitter.ApplyDefaults((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
build, _, err := buildadmission.GetBuildFromPod((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !*build.Spec.Strategy.SourceStrategy.Incremental {
t.Errorf("failed to default incremental to true")
}
build = u.Build().WithSourceStrategy().AsBuild()
bool_f := false
build.Spec.Strategy.SourceStrategy.Incremental = &bool_f
pod = u.Pod().WithBuild(t, build, "v1")
err = admitter.ApplyDefaults((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
build, _, err = buildadmission.GetBuildFromPod((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if *build.Spec.Strategy.SourceStrategy.Incremental {
t.Errorf("should not have overridden incremental to true")
}
}
示例6: TestSetBuildLogLevel
func TestSetBuildLogLevel(t *testing.T) {
build := u.Build().WithSourceStrategy()
pod := u.Pod().WithEnvVar("BUILD", "foo")
SetBuildLogLevel(pod.ToAttributes(), build.AsBuild())
if len(pod.Spec.Containers[0].Args) == 0 {
t.Errorf("Builds pod loglevel was not set")
}
if pod.Spec.Containers[0].Args[0] != "--loglevel=0" {
t.Errorf("Default build pod loglevel was not set to 0")
}
build = u.Build().WithSourceStrategy()
pod = u.Pod().WithEnvVar("BUILD", "foo")
build.Spec.Strategy.SourceStrategy.Env = []kapi.EnvVar{{Name: "BUILD_LOGLEVEL", Value: "7", ValueFrom: nil}}
SetBuildLogLevel(pod.ToAttributes(), build.AsBuild())
if pod.Spec.Containers[0].Args[0] != "--loglevel=7" {
t.Errorf("Build pod loglevel was not transferred from BUILD_LOGLEVEL environment variable: %#v", pod)
}
}
示例7: TestEnvDefaults
func TestEnvDefaults(t *testing.T) {
defaultsConfig := &defaultsapi.BuildDefaultsConfig{
Env: []kapi.EnvVar{
{
Name: "VAR1",
Value: "VALUE1",
},
{
Name: "VAR2",
Value: "VALUE2",
},
},
}
admitter := BuildDefaults{defaultsConfig}
pod := u.Pod().WithBuild(t, u.Build().WithSourceStrategy().AsBuild(), "v1")
err := admitter.ApplyDefaults((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
build, _, err := buildadmission.GetBuildFromPod((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
env := getBuildEnv(build)
var1found, var2found := false, false
for _, ev := range *env {
if ev.Name == "VAR1" {
if ev.Value != "VALUE1" {
t.Errorf("unexpected value %s", ev.Value)
}
var1found = true
}
if ev.Name == "VAR2" {
if ev.Value != "VALUE2" {
t.Errorf("unexpected value %s", ev.Value)
}
var2found = true
}
}
if !var1found {
t.Errorf("VAR1 not found")
}
if !var2found {
t.Errorf("VAR2 not found")
}
}
示例8: TestGetBuild
func TestGetBuild(t *testing.T) {
build := u.Build().WithDockerStrategy()
for _, version := range []string{"v1"} {
pod := u.Pod().WithBuild(t, build.AsBuild(), version)
resultBuild, resultVersion, err := GetBuild(pod.ToAttributes())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if resultVersion.Version != version {
t.Errorf("unexpected version: %s", resultVersion)
}
if !reflect.DeepEqual(build.AsBuild(), resultBuild) {
t.Errorf("%s: did not get expected build: %#v", version, resultBuild)
}
}
}
示例9: TestSetBuild
func TestSetBuild(t *testing.T) {
build := u.Build().WithSourceStrategy()
for _, version := range []string{"v1"} {
pod := u.Pod().WithEnvVar("BUILD", "foo")
groupVersion, err := unversioned.ParseGroupVersion(version)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
err = SetBuild(pod.ToAttributes(), build.AsBuild(), groupVersion)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resultBuild := pod.GetBuild(t)
if !reflect.DeepEqual(build.AsBuild(), resultBuild) {
t.Errorf("%s: did not get expected build: %#v", version, resultBuild)
}
}
}
示例10: TestProxyDefaults
func TestProxyDefaults(t *testing.T) {
defaultsConfig := &defaultsapi.BuildDefaultsConfig{
GitHTTPProxy: "http",
GitHTTPSProxy: "https",
}
admitter := NewBuildDefaults(defaultsConfig)
pod := u.Pod().WithBuild(t, u.Build().WithDockerStrategy().AsBuild(), "v1")
err := admitter.Admit(pod.ToAttributes())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
build, _, err := buildadmission.GetBuild(pod.ToAttributes())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if build.Spec.Source.Git.HTTPProxy == nil || len(*build.Spec.Source.Git.HTTPProxy) == 0 || *build.Spec.Source.Git.HTTPProxy != "http" {
t.Errorf("failed to find http proxy in git source")
}
if build.Spec.Source.Git.HTTPSProxy == nil || len(*build.Spec.Source.Git.HTTPSProxy) == 0 || *build.Spec.Source.Git.HTTPSProxy != "https" {
t.Errorf("failed to find http proxy in git source")
}
}
示例11: TestResourceDefaults
func TestResourceDefaults(t *testing.T) {
tests := map[string]struct {
DefaultResource kapi.ResourceRequirements
BuildResource kapi.ResourceRequirements
ExpectedResource kapi.ResourceRequirements
}{
"BuildDefaults plugin and Build object both defined resource limits and requests": {
DefaultResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"),
},
Requests: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"),
},
},
BuildResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"),
},
Requests: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("40"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("4G"),
},
},
ExpectedResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"),
},
Requests: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("40"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("4G"),
},
},
},
"BuildDefaults plugin defined limits and requests, Build object defined resource requests": {
DefaultResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"),
},
Requests: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"),
},
},
BuildResource: kapi.ResourceRequirements{
Requests: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"),
},
},
ExpectedResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"),
},
Requests: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"),
},
},
},
"BuildDefaults plugin defined limits and requests, Build object defined resource limits": {
DefaultResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"),
},
Requests: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"),
},
},
BuildResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"),
},
},
ExpectedResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("30"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("3G"),
},
Requests: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("2G"),
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("20"),
},
},
},
"BuildDefaults plugin defined nothing, Build object defined resource limits": {
DefaultResource: kapi.ResourceRequirements{},
BuildResource: kapi.ResourceRequirements{
Limits: kapi.ResourceList{
kapi.ResourceName(kapi.ResourceCPU): resource.MustParse("10"),
kapi.ResourceName(kapi.ResourceMemory): resource.MustParse("1G"),
//.........这里部分代码省略.........
示例12: TestLabelDefaults
//.........这里部分代码省略.........
},
{
buildLabels: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
defaultLabels: nil,
expected: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
},
{
buildLabels: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
},
defaultLabels: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "public",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
expected: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
},
{
buildLabels: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
},
defaultLabels: []buildapi.ImageLabel{
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
expected: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
},
}
for i, test := range tests {
defaultsConfig := &defaultsapi.BuildDefaultsConfig{
ImageLabels: test.defaultLabels,
}
admitter := BuildDefaults{defaultsConfig}
pod := u.Pod().WithBuild(t, u.Build().WithImageLabels(test.buildLabels).AsBuild(), "v1")
err := admitter.ApplyDefaults((*kapi.Pod)(pod))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
build := pod.GetBuild(t)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
result := build.Spec.Output.ImageLabels
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("expected[%d]: %v, got: %v", i, test.expected, result)
}
}
}
示例13: TestLabelOverrides
//.........这里部分代码省略.........
Value: "file:///dev/null",
},
},
},
{
buildLabels: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
overrideLabels: nil,
expected: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
},
{
buildLabels: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "public",
},
},
overrideLabels: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
expected: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
},
{
buildLabels: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
},
overrideLabels: []buildapi.ImageLabel{
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
expected: []buildapi.ImageLabel{
{
Name: "distribution-scope",
Value: "private",
},
{
Name: "changelog-url",
Value: "file:///dev/null",
},
},
},
}
for i, test := range tests {
overridesConfig := &overridesapi.BuildOverridesConfig{
ImageLabels: test.overrideLabels,
}
admitter := NewBuildOverrides(overridesConfig)
pod := u.Pod().WithBuild(t, u.Build().WithImageLabels(test.buildLabels).AsBuild(), "v1")
err := admitter.Admit(pod.ToAttributes())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
build := pod.GetBuild(t)
result := build.Spec.Output.ImageLabels
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("expected[%d]: %v, got: %v", i, test.expected, result)
}
}
}