Skip to content

Commit c7d6009

Browse files
committed
Test PushSecretName via extended tests
1 parent 11f00ab commit c7d6009

File tree

15 files changed

+246
-223
lines changed

15 files changed

+246
-223
lines changed

hack/test-extended.sh

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ export OPENSHIFT_ON_PANIC=crash
3838

3939
cleanup() {
4040
set +e
41-
server_pids=$(pgrep -P $(cat ${BASETMPDIR}/server.pid))
42-
kill $server_pids $(cat ${BASETMPDIR}/server.pid) ${ETCD_PID}
43-
rm -rf ${ETCD_DIR}
41+
pid=$(cat ${BASETMPDIR}/server.pid 2>/dev/null)
42+
if [ ! -z "$pid" ]; then
43+
server_pids=$(pgrep -P $pid)
44+
kill $server_pids $(cat ${BASETMPDIR}/server.pid) ${ETCD_PID}
45+
fi
46+
rm -rf ${ETCD_DIR-}
4447
echo "[INFO] Cleanup complete"
4548
}
4649

@@ -75,7 +78,7 @@ start_server() {
7578
--signer-key="${CERT_DIR}/ca/key.key" \
7679
--signer-serial="${CERT_DIR}/ca/serial.txt"
7780

78-
echo "[INFO] Starting OpenShift server"
81+
echo "[INFO] Starting OpenShift ..."
7982
sudo env "PATH=${PATH}" openshift start \
8083
--listen="https://0.0.0.0:${OS_MASTER_PORT}" \
8184
--public-master="https://${OS_MASTER_ADDR}" \
@@ -92,16 +95,25 @@ start_server() {
9295

9396
start_docker_registry() {
9497
mkdir -p ${BASETMPDIR}/.registry
95-
echo "[INFO] Creating Router"
98+
echo "[INFO] Creating Router ..."
9699
openshift ex router --create --credentials="${KUBECONFIG}" \
97100
--images='openshift/origin-${component}:latest' &>/dev/null
98101

99-
echo "[INFO] Creating Docker Registry"
102+
echo "[INFO] Creating Registry ..."
100103
openshift ex registry --create --credentials="${KUBECONFIG}" \
101104
--mount-host="${BASETMPDIR}/.registry" \
102105
--images='openshift/origin-${component}:latest' &>/dev/null
103106
}
104107

108+
push_to_registry() {
109+
local image=$1
110+
local registry=$2
111+
echo "[INFO] Caching $image to $registry"
112+
( docker tag $image "${registry}/${image}" && \
113+
docker push "${registry}/${image}" \
114+
) &>/dev/null
115+
}
116+
105117
# Go to the top of the tree.
106118
cd "${OS_ROOT}"
107119

@@ -134,10 +146,22 @@ wait_for_url_timed "http://${REGISTRY_ADDR}" "" $((2*TIME_MIN))
134146
# "409 - Image already exists" during the 'push' when the Build finishes.
135147
# This is because Docker Registry cannot handle parallel pushes.
136148
# See: https://github.com/docker/docker-registry/issues/537
137-
echo "[INFO] Pushing openshift/ruby-20-centos7 image to ${REGISTRY_ADDR}"
138-
docker tag openshift/ruby-20-centos7 ${REGISTRY_ADDR}/openshift/ruby-20-centos7
139-
docker push ${REGISTRY_ADDR}/openshift/ruby-20-centos7 &>/dev/null
149+
push_to_registry "openshift/ruby-20-centos7" $REGISTRY_ADDR
150+
push_to_registry "openshift/origin-custom-docker-builder" $REGISTRY_ADDR
151+
152+
export REGISTRY_ADDR
153+
154+
[ ! -z "${DEBUG-}" ] && set +e
140155

141156
# Run all extended tests cases
142-
echo "[INFO] Starting extended tests"
143-
OS_TEST_PACKAGE="test/extended" OS_TEST_TAGS="extended" OS_TEST_NAMESPACE="extended" ${OS_ROOT}/hack/test-integration.sh $@
157+
while true; do
158+
echo "[INFO] Starting extended tests ..."
159+
time OS_TEST_PACKAGE="test/extended" OS_TEST_TAGS="extended" OS_TEST_NAMESPACE="extended" ${OS_ROOT}/hack/test-integration.sh $@
160+
if [ ! -z "${DEBUG-}" ]; then
161+
read -p "Do you want to re-run the test cases? " yn
162+
case $yn in
163+
[Nn]* ) exit;;
164+
* ) echo "Please answer yes or no.";;
165+
esac
166+
fi
167+
done

test/extended/builds_test.go

Lines changed: 74 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ import (
66
"fmt"
77
"io/ioutil"
88
"net/http"
9+
"os"
910
"strings"
1011
"testing"
1112

13+
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
1214
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
15+
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
1316
buildapi "github.com/openshift/origin/pkg/build/api"
1417
testutil "github.com/openshift/origin/test/util"
1518
)
@@ -18,116 +21,72 @@ func init() {
1821
testutil.RequireServer()
1922
}
2023

21-
// TestSTIContextDirBuild excercises the scenario of having the 'contextDir' set to
22-
// directory where the application sources resides inside the repository.
23-
// The STI strategy is used for this build and this test succeed when the Build
24-
// completes and the resulting image is used for a Pod that replies to HTTP
25-
// request.
26-
func TestSTIContextDirBuild(t *testing.T) {
27-
namespace := testutil.RandomNamespace("contextdir")
28-
fmt.Printf("Using '%s' namespace\n", namespace)
29-
30-
build := testutil.GetBuildFixture("fixtures/contextdir-build.json")
24+
// TestPushSecretName exercises one of the complex Build scenarios, where you
25+
// first build a Docker image using Docker build strategy, which will later by
26+
// consumed by Custom build strategy to verify that the 'PushSecretName' (Docker
27+
// credentials) were successfully transported to the builder. The content of the
28+
// Secret file is verified in the end.
29+
func TestPushSecretName(t *testing.T) {
30+
namespace := testutil.RandomNamespace("secret")
3131
client, _ := testutil.GetClusterAdminClient(testutil.KubeConfigPath())
32+
kclient, _ := testutil.GetClusterAdminKubeClient(testutil.KubeConfigPath())
3233

3334
repo := testutil.CreateSampleImageRepository(namespace)
35+
3436
if repo == nil {
3537
t.Fatal("Failed to create ImageRepository")
3638
}
3739
defer testutil.DeleteSampleImageRepository(repo, namespace)
3840

39-
// TODO: Tweak the selector to match the build name
40-
watcher, err := client.Builds(namespace).Watch(labels.Everything(), labels.Everything(), "0")
41+
// Create Secret with dockercfg
42+
secret := testutil.GetSecretFixture("fixtures/test-secret.json")
43+
// TODO: Why do I need to set namespace here?
44+
secret.Namespace = namespace
45+
_, err := kclient.Secrets(namespace).Create(secret)
4146
if err != nil {
42-
t.Fatalf("Failed to create watcher: %v", err)
43-
}
44-
defer watcher.Stop()
45-
46-
newBuild, err := client.Builds(namespace).Create(build)
47-
if err != nil {
48-
t.Fatalf("Unexpected error: %v", err)
49-
}
50-
51-
for event := range watcher.ResultChan() {
52-
build, ok := event.Object.(*buildapi.Build)
53-
if !ok {
54-
t.Fatalf("cannot convert input to Build")
55-
}
56-
57-
// Iterate over watcher's results and search for
58-
// the build we just started. Also make sure that
59-
// the build is running, complete, or has failed
60-
if build.Name == newBuild.Name {
61-
switch build.Status {
62-
case buildapi.BuildStatusFailed, buildapi.BuildStatusError:
63-
t.Fatalf("Unexpected build status: ", buildapi.BuildStatusFailed)
64-
case buildapi.BuildStatusComplete:
65-
err := testutil.VerifyImage(repo, namespace, validateContextDirImage)
66-
if err != nil {
67-
t.Fatalf("The build image failed validation: %v", err)
68-
}
69-
return
70-
}
71-
}
72-
}
73-
}
74-
75-
// TestDockerStrategyBuild exercises the Docker strategy build. This test succeed when
76-
// the Docker image is successfully built.
77-
func TestDockerStrategyBuild(t *testing.T) {
78-
namespace := testutil.RandomNamespace("docker")
79-
fmt.Printf("Using '%s' namespace\n", namespace)
80-
81-
build := testutil.GetBuildFixture("fixtures/docker-build.json")
82-
client, _ := testutil.GetClusterAdminClient(testutil.KubeConfigPath())
83-
84-
repo := testutil.CreateSampleImageRepository(namespace)
85-
if repo == nil {
86-
t.Fatal("Failed to create ImageRepository")
47+
t.Fatalf("Failed to create Secret: %v", err)
8748
}
88-
defer testutil.DeleteSampleImageRepository(repo, namespace)
8949

90-
// TODO: Tweak the selector to match the build name
91-
watcher, err := client.Builds(namespace).Watch(labels.Everything(), labels.Everything(), "0")
50+
watcher, err := client.Builds(namespace).Watch(labels.Everything(), fields.Everything(), "0")
9251
if err != nil {
9352
t.Fatalf("Failed to create watcher: %v", err)
9453
}
9554
defer watcher.Stop()
9655

97-
newBuild, err := client.Builds(namespace).Create(build)
56+
// First build the builder image (custom build builder)
57+
dockerBuild := testutil.GetBuildFixture("fixtures/test-secret-build.json")
58+
newDockerBuild, err := client.Builds(namespace).Create(dockerBuild)
9859
if err != nil {
99-
t.Fatalf("Unexpected error: %v", err)
60+
t.Fatalf("Unable to create Build %s: %v", dockerBuild.Name, err)
10061
}
62+
waitForComplete(newDockerBuild, watcher, t)
10163

102-
for event := range watcher.ResultChan() {
103-
build, ok := event.Object.(*buildapi.Build)
104-
if !ok {
105-
t.Fatalf("cannot convert input to Build")
106-
}
64+
// Now build the application image using custom build (run the previous image)
65+
// Custom build will copy the dockercfg file into the application image.
66+
customBuild := testutil.GetBuildFixture("fixtures/test-custom-build.json")
67+
imageName := fmt.Sprintf("%s/%s/%s", os.Getenv("REGISTRY_ADDR"), namespace, repo.Name)
68+
customBuild.Parameters.Strategy.CustomStrategy.Image = imageName
69+
newCustomBuild, err := client.Builds(namespace).Create(customBuild)
70+
if err != nil {
71+
t.Fatalf("Unable to create Build %s: %v", dockerBuild.Name, err)
72+
}
73+
waitForComplete(newCustomBuild, watcher, t)
10774

108-
if build.Name == newBuild.Name {
109-
switch build.Status {
110-
case buildapi.BuildStatusFailed, buildapi.BuildStatusError:
111-
t.Fatalf("Unexpected build status: ", buildapi.BuildStatusFailed)
112-
case buildapi.BuildStatusComplete:
113-
// If the Docker build strategy finishes with Complete, then this test
114-
// succeeded
115-
return
116-
}
117-
}
75+
// Verify that the dockercfg file is there
76+
if err := testutil.VerifyImage(repo, "application", namespace, validatePushSecret); err != nil {
77+
t.Fatalf("Image verification failed: %v", err)
11878
}
11979
}
12080

121-
// TestSTIContextDirBuild exercises the scenario of having the '.sti/environment'
122-
// file in the application sources that should set the defined environment
123-
// variables for the resulting application. HTTP request is made to Pod that
124-
// runs the output image and this HTTP request should reply the value of the
125-
// TEST_VAR.
81+
// TestSTIEnvironmentBuild exercises the scenario where you have .sti/environment
82+
// file in your source code repository and you use STI build strategy. In that
83+
// case the STI build should read that file and set all environment variables
84+
// from that file to output image.
12685
func TestSTIEnvironmentBuild(t *testing.T) {
12786
namespace := testutil.RandomNamespace("stienv")
12887
fmt.Printf("Using '%s' namespace\n", namespace)
12988

130-
build := testutil.GetBuildFixture("fixtures/sti-env-build.json")
89+
build := testutil.GetBuildFixture("fixtures/test-env-build.json")
13190
client, _ := testutil.GetClusterAdminClient(testutil.KubeConfigPath())
13291

13392
repo := testutil.CreateSampleImageRepository(namespace)
@@ -137,7 +96,7 @@ func TestSTIEnvironmentBuild(t *testing.T) {
13796
defer testutil.DeleteSampleImageRepository(repo, namespace)
13897

13998
// TODO: Tweak the selector to match the build name
140-
watcher, err := client.Builds(namespace).Watch(labels.Everything(), labels.Everything(), "0")
99+
watcher, err := client.Builds(namespace).Watch(labels.Everything(), fields.Everything(), "0")
141100
if err != nil {
142101
t.Fatalf("Failed to create watcher: %v", err)
143102
}
@@ -148,27 +107,9 @@ func TestSTIEnvironmentBuild(t *testing.T) {
148107
t.Fatalf("Unexpected error: %v", err)
149108
}
150109

151-
for event := range watcher.ResultChan() {
152-
build, ok := event.Object.(*buildapi.Build)
153-
if !ok {
154-
t.Fatalf("cannot convert input to Build")
155-
}
156-
157-
// Iterate over watcher's results and search for
158-
// the build we just started. Also make sure that
159-
// the build is running, complete, or has failed
160-
if build.Name == newBuild.Name {
161-
switch build.Status {
162-
case buildapi.BuildStatusFailed, buildapi.BuildStatusError:
163-
t.Fatalf("Unexpected build status: ", buildapi.BuildStatusFailed)
164-
case buildapi.BuildStatusComplete:
165-
err := testutil.VerifyImage(repo, namespace, validateSTIEnvironment)
166-
if err != nil {
167-
t.Fatalf("The build image failed validation: %v", err)
168-
}
169-
return
170-
}
171-
}
110+
waitForComplete(newBuild, watcher, t)
111+
if err := testutil.VerifyImage(repo, "", namespace, validateSTIEnvironment); err != nil {
112+
t.Fatalf("The build image failed validation: %v", err)
172113
}
173114
}
174115

@@ -187,17 +128,39 @@ func validateSTIEnvironment(address string) error {
187128
return nil
188129
}
189130

190-
// validateContextDirImage verifies that the image with contextDir set can
191-
// properly start and respond to HTTP requests
192-
func validateContextDirImage(address string) error {
193-
resp, err := http.Get("http://" + address)
131+
// validatePushSecret verifies that the content of the sample dockercfg is
132+
// properly returned from the Pod running the image that contains this file.
133+
func validatePushSecret(address string) error {
134+
expected := `{"https://registryhost/v1":{"auth":"secret","email":"[email protected]"}}`
135+
resp, err := http.Get("http://" + address + "/SECRET_FILE")
194136
if err != nil {
195137
return err
196138
}
197139
defer resp.Body.Close()
198140
body, err := ioutil.ReadAll(resp.Body)
199-
if strings.TrimSpace(string(body)) != "success" {
200-
return fmt.Errorf("Expected 'success' got '%v'", body)
141+
if strings.TrimSpace(string(body)) != expected {
142+
return fmt.Errorf("Expected '%s' '%s'", expected, body)
201143
}
202144
return nil
203145
}
146+
147+
// waitForComplete waits for the Build to finish
148+
func waitForComplete(build *buildapi.Build, w watch.Interface, t *testing.T) {
149+
for event := range w.ResultChan() {
150+
eventBuild, ok := event.Object.(*buildapi.Build)
151+
if !ok {
152+
t.Fatalf("Cannot convert input to Build")
153+
}
154+
if build.Name != eventBuild.Name {
155+
continue
156+
}
157+
switch eventBuild.Status {
158+
case buildapi.BuildStatusFailed, buildapi.BuildStatusError:
159+
t.Fatalf("Unexpected status for Build %s: ", eventBuild.Name, buildapi.BuildStatusFailed)
160+
case buildapi.BuildStatusComplete:
161+
return
162+
default:
163+
fmt.Printf("Build %s updated: %v\n", eventBuild.Name, eventBuild.Status)
164+
}
165+
}
166+
}

test/extended/fixtures/context-build-app/Gemfile

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/extended/fixtures/context-build-app/config.ru

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/extended/fixtures/contextdir-build.json

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM openshift/origin-custom-docker-builder
2+
# Override the default build script
3+
ADD build.sh /tmp/build.sh

0 commit comments

Comments
 (0)