@@ -6,10 +6,13 @@ import (
6
6
"fmt"
7
7
"io/ioutil"
8
8
"net/http"
9
+ "os"
9
10
"strings"
10
11
"testing"
11
12
13
+ "github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
12
14
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
15
+ "github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
13
16
buildapi "github.com/openshift/origin/pkg/build/api"
14
17
testutil "github.com/openshift/origin/test/util"
15
18
)
@@ -18,116 +21,72 @@ func init() {
18
21
testutil .RequireServer ()
19
22
}
20
23
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" )
31
31
client , _ := testutil .GetClusterAdminClient (testutil .KubeConfigPath ())
32
+ kclient , _ := testutil .GetClusterAdminKubeClient (testutil .KubeConfigPath ())
32
33
33
34
repo := testutil .CreateSampleImageRepository (namespace )
35
+
34
36
if repo == nil {
35
37
t .Fatal ("Failed to create ImageRepository" )
36
38
}
37
39
defer testutil .DeleteSampleImageRepository (repo , namespace )
38
40
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 )
41
46
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 )
87
48
}
88
- defer testutil .DeleteSampleImageRepository (repo , namespace )
89
49
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" )
92
51
if err != nil {
93
52
t .Fatalf ("Failed to create watcher: %v" , err )
94
53
}
95
54
defer watcher .Stop ()
96
55
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 )
98
59
if err != nil {
99
- t .Fatalf ("Unexpected error : %v" , err )
60
+ t .Fatalf ("Unable to create Build %s : %v" , dockerBuild . Name , err )
100
61
}
62
+ waitForComplete (newDockerBuild , watcher , t )
101
63
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 )
107
74
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 )
118
78
}
119
79
}
120
80
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.
126
85
func TestSTIEnvironmentBuild (t * testing.T ) {
127
86
namespace := testutil .RandomNamespace ("stienv" )
128
87
fmt .Printf ("Using '%s' namespace\n " , namespace )
129
88
130
- build := testutil .GetBuildFixture ("fixtures/sti -env-build.json" )
89
+ build := testutil .GetBuildFixture ("fixtures/test -env-build.json" )
131
90
client , _ := testutil .GetClusterAdminClient (testutil .KubeConfigPath ())
132
91
133
92
repo := testutil .CreateSampleImageRepository (namespace )
@@ -137,7 +96,7 @@ func TestSTIEnvironmentBuild(t *testing.T) {
137
96
defer testutil .DeleteSampleImageRepository (repo , namespace )
138
97
139
98
// 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" )
141
100
if err != nil {
142
101
t .Fatalf ("Failed to create watcher: %v" , err )
143
102
}
@@ -148,27 +107,9 @@ func TestSTIEnvironmentBuild(t *testing.T) {
148
107
t .Fatalf ("Unexpected error: %v" , err )
149
108
}
150
109
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 )
172
113
}
173
114
}
174
115
@@ -187,17 +128,39 @@ func validateSTIEnvironment(address string) error {
187
128
return nil
188
129
}
189
130
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" )
194
136
if err != nil {
195
137
return err
196
138
}
197
139
defer resp .Body .Close ()
198
140
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 )
201
143
}
202
144
return nil
203
145
}
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
+ }
0 commit comments