func TestSubstituteImageCustomAllMatch(t *testing.T) { source := mocks.MockSource() strategy := mockCustomStrategyForDockerImage(originalImage) output := mocks.MockOutput() bc := mocks.MockBuildConfig(source, strategy, output) generator := mockBuildGenerator() build, err := generator.generateBuildFromConfig(kapi.NewContext(), bc, nil, nil) if err != nil { t.Fatalf("Unexpected error %v", err) } // Full custom build with a Image and a well defined environment variable image value, // both should be replaced. Additional environment variables should not be touched. build.Spec.Strategy.CustomStrategy.Env = make([]kapi.EnvVar, 2) build.Spec.Strategy.CustomStrategy.Env[0] = kapi.EnvVar{Name: "someImage", Value: originalImage} build.Spec.Strategy.CustomStrategy.Env[1] = kapi.EnvVar{Name: buildapi.CustomBuildStrategyBaseImageKey, Value: originalImage} updateCustomImageEnv(build.Spec.Strategy.CustomStrategy, newImage) if build.Spec.Strategy.CustomStrategy.Env[0].Value != originalImage { t.Errorf("Random env variable %s was improperly substituted in custom strategy", build.Spec.Strategy.CustomStrategy.Env[0].Name) } if build.Spec.Strategy.CustomStrategy.Env[1].Value != newImage { t.Errorf("Image env variable was not properly substituted in custom strategy") } if c := len(build.Spec.Strategy.CustomStrategy.Env); c != 2 { t.Errorf("Expected %d, found %d environment variables", 2, c) } if bc.Spec.Strategy.CustomStrategy.From.Name != originalImage { t.Errorf("Custom BuildConfig Image was updated when Build was modified %s!=%s", bc.Spec.Strategy.CustomStrategy.From.Name, originalImage) } if len(bc.Spec.Strategy.CustomStrategy.Env) != 0 { t.Errorf("Custom BuildConfig Env was updated when Build was modified") } }
func TestSubstituteImageCustomBaseMatchEnvMissing(t *testing.T) { source := mocks.MockSource() strategy := mockCustomStrategyForImageRepository() output := mocks.MockOutput() bc := mocks.MockBuildConfig(source, strategy, output) generator := mockBuildGenerator() build, err := generator.generateBuildFromConfig(kapi.NewContext(), bc, nil, nil) if err != nil { t.Fatalf("Unexpected error %v", err) } // Custom build with a base Image but no image environment variable. // base image should be replaced, new image environment variable should be added, // existing environment variable should be untouched build.Spec.Strategy.CustomStrategy.Env = make([]kapi.EnvVar, 1) build.Spec.Strategy.CustomStrategy.Env[0] = kapi.EnvVar{Name: "someImage", Value: originalImage} updateCustomImageEnv(build.Spec.Strategy.CustomStrategy, newImage) if build.Spec.Strategy.CustomStrategy.Env[0].Value != originalImage { t.Errorf("Random env variable was improperly substituted in custom strategy") } if build.Spec.Strategy.CustomStrategy.Env[1].Name != buildapi.CustomBuildStrategyBaseImageKey || build.Spec.Strategy.CustomStrategy.Env[1].Value != newImage { t.Errorf("Image env variable was not added in custom strategy %s %s |", build.Spec.Strategy.CustomStrategy.Env[1].Name, build.Spec.Strategy.CustomStrategy.Env[1].Value) } if c := len(build.Spec.Strategy.CustomStrategy.Env); c != 2 { t.Errorf("Expected %d, found %d environment variables", 2, c) } }
func TestInstantiateWithLastVersion(t *testing.T) { g := mockBuildGenerator() c := g.Client.(Client) c.GetBuildConfigFunc = func(ctx kapi.Context, name string) (*buildapi.BuildConfig, error) { bc := mocks.MockBuildConfig(mocks.MockSource(), mocks.MockSourceStrategyForImageRepository(), mocks.MockOutput()) bc.Status.LastVersion = 1 return bc, nil } g.Client = c // Version not specified _, err := g.Instantiate(kapi.NewDefaultContext(), &buildapi.BuildRequest{}) if err != nil { t.Errorf("Unexpected error %v", err) } // Version specified and it matches lastVersion := 1 _, err = g.Instantiate(kapi.NewDefaultContext(), &buildapi.BuildRequest{LastVersion: &lastVersion}) if err != nil { t.Errorf("Unexpected error %v", err) } // Version specified, but doesn't match lastVersion = 0 _, err = g.Instantiate(kapi.NewDefaultContext(), &buildapi.BuildRequest{LastVersion: &lastVersion}) if err == nil { t.Errorf("Expected an error and did not get one") } }
func TestSubstituteImageCustomBaseMatchEnvMismatch(t *testing.T) { source := mocks.MockSource() strategy := mockCustomStrategyForImageRepository() output := mocks.MockOutput() bc := mocks.MockBuildConfig(source, strategy, output) generator := mockBuildGenerator() build, err := generator.generateBuildFromConfig(kapi.NewContext(), bc, nil, nil) if err != nil { t.Fatalf("Unexpected error %v", err) } // Full custom build with a Image and a well defined environment variable image value that does not match the new image // Environment variables should not be updated. build.Spec.Strategy.CustomStrategy.Env = make([]kapi.EnvVar, 2) build.Spec.Strategy.CustomStrategy.Env[0] = kapi.EnvVar{Name: "someEnvVar", Value: originalImage} build.Spec.Strategy.CustomStrategy.Env[1] = kapi.EnvVar{Name: buildapi.CustomBuildStrategyBaseImageKey, Value: "dummy"} updateCustomImageEnv(build.Spec.Strategy.CustomStrategy, newImage) if build.Spec.Strategy.CustomStrategy.Env[0].Value != originalImage { t.Errorf("Random env variable %s was improperly substituted in custom strategy", build.Spec.Strategy.CustomStrategy.Env[0].Name) } if build.Spec.Strategy.CustomStrategy.Env[1].Value != newImage { t.Errorf("Image env variable was not substituted in custom strategy") } if c := len(build.Spec.Strategy.CustomStrategy.Env); c != 2 { t.Errorf("Expected %d, found %d environment variables", 2, c) } }
func TestGetNextBuildName(t *testing.T) { bc := mocks.MockBuildConfig(mocks.MockSource(), mocks.MockSourceStrategyForImageRepository(), mocks.MockOutput()) if expected, actual := bc.Name+"-1", getNextBuildName(bc); expected != actual { t.Errorf("Wrong buildName, expected %s, got %s", expected, actual) } if expected, actual := 1, bc.Status.LastVersion; expected != actual { t.Errorf("Wrong version, expected %d, got %d", expected, actual) } }
func TestSubstituteImageCustomAllMismatch(t *testing.T) { source := mocks.MockSource() strategy := mockCustomStrategyForDockerImage(originalImage) output := mocks.MockOutput() bc := mocks.MockBuildConfig(source, strategy, output) generator := mockBuildGenerator() build, err := generator.generateBuildFromConfig(kapi.NewContext(), bc, nil, nil) if err != nil { t.Fatalf("Unexpected error %v", err) } // Full custom build with base image that is not matched // Base image name should be unchanged updateCustomImageEnv(build.Spec.Strategy.CustomStrategy, "dummy") if build.Spec.Strategy.CustomStrategy.From.Name != originalImage { t.Errorf("Base image name was improperly substituted in custom strategy %s %s", build.Spec.Strategy.CustomStrategy.From.Name, originalImage) } }
func TestGenerateBuildFromConfigWithSecrets(t *testing.T) { source := mocks.MockSource() revision := &buildapi.SourceRevision{ Type: buildapi.BuildSourceGit, Git: &buildapi.GitSourceRevision{ Commit: "abcd", }, } dockerCfgTable := map[string]map[string][]byte{ // FIXME: This image pull spec does not return ANY registry, but it should // return the hub. //"docker.io/secret2/image": {".dockercfg": sampleDockerConfigs["hub"]}, "secret1/image": {".dockercfg": mocks.SampleDockerConfigs["hub"]}, "1.1.1.1:5000/secret3/image": {".dockercfg": mocks.SampleDockerConfigs["ipv4"]}, "registry.host/secret4/image": {".dockercfg": mocks.SampleDockerConfigs["host"]}, } for imageName := range dockerCfgTable { // Setup the BuildGenerator strategy := mockDockerStrategyForDockerImage(imageName) output := mockOutputWithImageName(imageName) generator := mockBuildGenerator() bc := mocks.MockBuildConfig(source, strategy, output) build, err := generator.generateBuildFromConfig(kapi.NewContext(), bc, revision) if build.Spec.Output.PushSecret == nil { t.Errorf("Expected PushSecret for image '%s' to be set, got nil", imageName) continue } if build.Spec.Strategy.DockerStrategy.PullSecret == nil { t.Errorf("Expected PullSecret for image '%s' to be set, got nil", imageName) continue } if len(build.Spec.Output.PushSecret.Name) == 0 { t.Errorf("Expected PushSecret for image %s to be set not empty", imageName) } if len(build.Spec.Strategy.DockerStrategy.PullSecret.Name) == 0 { t.Errorf("Expected PullSecret for image %s to be set not empty", imageName) } if err != nil { t.Fatalf("Unexpected error %v", err) } } }
func TestSubstituteImageCustomBaseMatchEnvNil(t *testing.T) { source := mocks.MockSource() strategy := mockCustomStrategyForImageRepository() output := mocks.MockOutput() bc := mocks.MockBuildConfig(source, strategy, output) generator := mockBuildGenerator() build, err := generator.generateBuildFromConfig(kapi.NewContext(), bc, nil, nil) if err != nil { t.Fatalf("Unexpected error %v", err) } // Custom build with a base Image but no environment variables // base image should be replaced, new image environment variable should be added updateCustomImageEnv(build.Spec.Strategy.CustomStrategy, newImage) if build.Spec.Strategy.CustomStrategy.Env[0].Name != buildapi.CustomBuildStrategyBaseImageKey || build.Spec.Strategy.CustomStrategy.Env[0].Value != newImage { t.Errorf("New image name variable was not added to environment list in custom strategy") } if c := len(build.Spec.Strategy.CustomStrategy.Env); c != 1 { t.Errorf("Expected %d, found %d environment variables", 1, c) } }
func TestCreateInstantiate(t *testing.T) { imageStream := mocks.MockImageStream("testImageStream", "registry.com/namespace/imagename", map[string]string{"test": "newImageID123"}) image := mocks.MockImage("testImage@id", "registry.com/namespace/imagename@id") fakeSecrets := []runtime.Object{} for _, s := range mocks.MockBuilderSecrets() { fakeSecrets = append(fakeSecrets, s) } rest := InstantiateREST{&generator.BuildGenerator{ Secrets: testclient.NewSimpleFake(fakeSecrets...), ServiceAccounts: mocks.MockBuilderServiceAccount(mocks.MockBuilderSecrets()), Client: generator.Client{ GetBuildConfigFunc: func(ctx kapi.Context, name string) (*buildapi.BuildConfig, error) { return mocks.MockBuildConfig(mocks.MockSource(), mocks.MockSourceStrategyForImageRepository(), mocks.MockOutput()), nil }, UpdateBuildConfigFunc: func(ctx kapi.Context, buildConfig *buildapi.BuildConfig) error { return nil }, CreateBuildFunc: func(ctx kapi.Context, build *buildapi.Build) error { return nil }, GetBuildFunc: func(ctx kapi.Context, name string) (*buildapi.Build, error) { return &buildapi.Build{}, nil }, GetImageStreamFunc: func(ctx kapi.Context, name string) (*imageapi.ImageStream, error) { return imageStream, nil }, GetImageStreamTagFunc: func(ctx kapi.Context, name string) (*imageapi.ImageStreamTag, error) { return &imageapi.ImageStreamTag{Image: *image}, nil }, GetImageStreamImageFunc: func(ctx kapi.Context, name string) (*imageapi.ImageStreamImage, error) { return &imageapi.ImageStreamImage{Image: *image}, nil }, }}} _, err := rest.Create(kapi.NewDefaultContext(), &buildapi.BuildRequest{ObjectMeta: kapi.ObjectMeta{Name: "name"}}) if err != nil { t.Errorf("Unexpected error %v", err) } }
func TestInstantiateWithLabelsAndAnnotations(t *testing.T) { g := mockBuildGenerator() c := g.Client.(Client) c.GetBuildConfigFunc = func(ctx kapi.Context, name string) (*buildapi.BuildConfig, error) { bc := mocks.MockBuildConfig(mocks.MockSource(), mocks.MockSourceStrategyForImageRepository(), mocks.MockOutput()) bc.Status.LastVersion = 1 return bc, nil } g.Client = c req := &buildapi.BuildRequest{ ObjectMeta: kapi.ObjectMeta{ Annotations: map[string]string{ "a_1": "a_value1", // build number is set as an annotation on the generated build, so we // shouldn't be able to ovewrite it here. buildapi.BuildNumberAnnotation: "bad_annotation", }, Labels: map[string]string{ "l_1": "l_value1", // testbclabel is defined as a label on the mockBuildConfig so we shouldn't // be able to overwrite it here. "testbclabel": "bad_label", }, }, } build, err := g.Instantiate(kapi.NewDefaultContext(), req) if err != nil { t.Errorf("Unexpected error %v", err) } if build.Annotations["a_1"] != "a_value1" || build.Annotations[buildapi.BuildNumberAnnotation] == "bad_annotation" { t.Errorf("Build annotations were merged incorrectly: %v", build.Annotations) } if build.Labels["l_1"] != "l_value1" || build.Labels[buildapi.BuildLabel] == "bad_label" { t.Errorf("Build labels were merged incorrectly: %v", build.Labels) } }
func mockBuildGenerator() *BuildGenerator { fakeSecrets := []runtime.Object{} for _, s := range mocks.MockBuilderSecrets() { fakeSecrets = append(fakeSecrets, s) } return &BuildGenerator{ Secrets: testclient.NewSimpleFake(fakeSecrets...), ServiceAccounts: mocks.MockBuilderServiceAccount(mocks.MockBuilderSecrets()), Client: Client{ GetBuildConfigFunc: func(ctx kapi.Context, name string) (*buildapi.BuildConfig, error) { return mocks.MockBuildConfig(mocks.MockSource(), mocks.MockSourceStrategyForImageRepository(), mocks.MockOutput()), nil }, UpdateBuildConfigFunc: func(ctx kapi.Context, buildConfig *buildapi.BuildConfig) error { return nil }, CreateBuildFunc: func(ctx kapi.Context, build *buildapi.Build) error { return nil }, GetBuildFunc: func(ctx kapi.Context, name string) (*buildapi.Build, error) { return &buildapi.Build{}, nil }, GetImageStreamFunc: func(ctx kapi.Context, name string) (*imageapi.ImageStream, error) { if name != imageRepoName { return &imageapi.ImageStream{}, nil } return &imageapi.ImageStream{ ObjectMeta: kapi.ObjectMeta{ Name: imageRepoName, Namespace: imageRepoNamespace, }, Status: imageapi.ImageStreamStatus{ DockerImageRepository: "repo/namespace/image", Tags: map[string]imageapi.TagEventList{ tagName: { Items: []imageapi.TagEvent{ {DockerImageReference: dockerReference}, }, }, imageapi.DefaultImageTag: { Items: []imageapi.TagEvent{ {DockerImageReference: latestDockerReference}, }, }, }, }, }, nil }, GetImageStreamTagFunc: func(ctx kapi.Context, name string) (*imageapi.ImageStreamTag, error) { return &imageapi.ImageStreamTag{ Image: imageapi.Image{ ObjectMeta: kapi.ObjectMeta{Name: imageRepoName + ":" + newTag}, DockerImageReference: latestDockerReference, }, }, nil }, GetImageStreamImageFunc: func(ctx kapi.Context, name string) (*imageapi.ImageStreamImage, error) { return &imageapi.ImageStreamImage{ Image: imageapi.Image{ ObjectMeta: kapi.ObjectMeta{Name: imageRepoName + ":@id"}, DockerImageReference: latestDockerReference, }, }, nil }, }} }