Skip to content

Commit 2190adc

Browse files
committed
add integration test
- also add Profile to skaffold build test fixture
1 parent acceea6 commit 2190adc

19 files changed

+1084
-0
lines changed

integration/skaffold/helper.go

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type RunBuilder struct {
4141
dir string
4242
ns string
4343
repo string
44+
profiles []string
4445
args []string
4546
env []string
4647
stdin []byte
@@ -150,6 +151,12 @@ func (b *RunBuilder) WithEnv(env []string) *RunBuilder {
150151
return b
151152
}
152153

154+
// WithProfiles sets profiles.
155+
func (b *RunBuilder) WithProfiles(profiles []string) *RunBuilder {
156+
b.profiles = profiles
157+
return b
158+
}
159+
153160
// RunBackground runs the skaffold command in the background.
154161
// Returns a teardown function that stops skaffold.
155162
func (b *RunBuilder) RunBackground(t *testing.T) context.CancelFunc {
@@ -262,6 +269,9 @@ func (b *RunBuilder) cmd(ctx context.Context) *exec.Cmd {
262269
if b.repo != "" && command.Flags().Lookup("default-repo") != nil {
263270
args = append(args, "--default-repo", b.repo)
264271
}
272+
if b.profiles != nil && len(b.profiles) > 0 && command.Flags().Lookup("profile") != nil {
273+
args = append(args, "--profile", strings.Join(b.profiles, ","))
274+
}
265275
args = append(args, b.args...)
266276

267277
cmd := exec.CommandContext(ctx, "skaffold", args...)

integration/sync_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"os/exec"
24+
"strings"
2425
"testing"
2526
"time"
2627

@@ -81,6 +82,76 @@ func TestDevSync(t *testing.T) {
8182
}
8283
}
8384

85+
func TestDevAutoSync(t *testing.T) {
86+
if testing.Short() || RunOnGCP() {
87+
t.Skip("skipping kind integration test")
88+
}
89+
90+
dir := "testdata/jib-sync/"
91+
92+
tests := []struct {
93+
description string
94+
profiles []string
95+
uniqueStr string
96+
}{
97+
{
98+
description: "jib maven auto sync",
99+
profiles: []string{"maven"},
100+
uniqueStr: "maven-maven",
101+
},
102+
{
103+
description: "jib gradle auto sync",
104+
profiles: []string{"gradle"},
105+
uniqueStr: "gradle-gradle",
106+
},
107+
}
108+
for _, test := range tests {
109+
t.Run(test.description, func(t *testing.T) {
110+
// Run skaffold build first to fail quickly on a build failure
111+
skaffold.Build().WithRepo("gcr.io/appu-learn").WithProfiles(test.profiles).InDir(dir).RunOrFail(t)
112+
113+
ns, client, deleteNs := SetupNamespace(t)
114+
defer deleteNs()
115+
116+
stop := skaffold.Dev("--trigger", "notify").WithRepo("gcr.io/appu-learn").WithProfiles(test.profiles).InDir(dir).InNs(ns.Name).RunBackground(t)
117+
defer stop()
118+
119+
client.WaitForPodsReady("test-file-sync")
120+
121+
// direct file sync
122+
directFile := dir + "src/main/jib/foo"
123+
if err := ioutil.WriteFile(directFile, []byte("foo"), 0644); err != nil {
124+
t.Fatalf("Failed to write local file to sync %s", directFile)
125+
}
126+
defer func() { os.Truncate(directFile, 0) }()
127+
128+
err := wait.PollImmediate(time.Millisecond*500, 1*time.Minute, func() (bool, error) {
129+
out, _ := exec.Command("kubectl", "exec", "test-file-sync", "-n", ns.Name, "--", "cat", "foo").Output()
130+
return string(out) == "foo", nil
131+
})
132+
failNowIfError(t, err)
133+
134+
// compile and sync
135+
generatedFileSrc := dir + "src/main/java/hello/HelloController.java"
136+
if oldContents, err := ioutil.ReadFile(generatedFileSrc); err != nil {
137+
t.Fatalf("Failed to read file %s", generatedFileSrc)
138+
} else {
139+
newContents := strings.Replace(string(oldContents),"text-to-replace",test.uniqueStr, 1)
140+
if err := ioutil.WriteFile(generatedFileSrc, []byte(newContents), 0644); err != nil {
141+
t.Fatalf("Failed to write new contents to file %s", generatedFileSrc)
142+
}
143+
defer func() { ioutil.WriteFile(generatedFileSrc, oldContents, 0644)}()
144+
}
145+
err = wait.PollImmediate(time.Millisecond*500, 1*time.Minute, func() (bool, error) {
146+
// distroless debug only has wget, not curl
147+
out, _ := exec.Command("kubectl", "exec", "test-file-sync", "-n", ns.Name, "--", "wget", "localhost:8080/", "-q", "-O", "-").Output()
148+
return string(out) == test.uniqueStr, nil
149+
})
150+
failNowIfError(t, err)
151+
})
152+
}
153+
}
154+
84155
func TestDevSyncAPITrigger(t *testing.T) {
85156
if testing.Short() || RunOnGCP() {
86157
t.Skip("skipping kind integration test")
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
target
2+
bin
3+
build
4+
.gradle
5+
.classpath
6+
.project
7+
.settings
8+
.vscode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
import java.net.*;
21+
import java.io.*;
22+
import java.nio.channels.*;
23+
import java.util.Properties;
24+
25+
public class MavenWrapperDownloader {
26+
27+
/**
28+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
29+
*/
30+
private static final String DEFAULT_DOWNLOAD_URL =
31+
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
32+
33+
/**
34+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
35+
* use instead of the default one.
36+
*/
37+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
38+
".mvn/wrapper/maven-wrapper.properties";
39+
40+
/**
41+
* Path where the maven-wrapper.jar will be saved to.
42+
*/
43+
private static final String MAVEN_WRAPPER_JAR_PATH =
44+
".mvn/wrapper/maven-wrapper.jar";
45+
46+
/**
47+
* Name of the property which should be used to override the default download url for the wrapper.
48+
*/
49+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
50+
51+
public static void main(String args[]) {
52+
System.out.println("- Downloader started");
53+
File baseDirectory = new File(args[0]);
54+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
55+
56+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
57+
// wrapperUrl parameter.
58+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
59+
String url = DEFAULT_DOWNLOAD_URL;
60+
if(mavenWrapperPropertyFile.exists()) {
61+
FileInputStream mavenWrapperPropertyFileInputStream = null;
62+
try {
63+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
64+
Properties mavenWrapperProperties = new Properties();
65+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
66+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
67+
} catch (IOException e) {
68+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
69+
} finally {
70+
try {
71+
if(mavenWrapperPropertyFileInputStream != null) {
72+
mavenWrapperPropertyFileInputStream.close();
73+
}
74+
} catch (IOException e) {
75+
// Ignore ...
76+
}
77+
}
78+
}
79+
System.out.println("- Downloading from: : " + url);
80+
81+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
82+
if(!outputFile.getParentFile().exists()) {
83+
if(!outputFile.getParentFile().mkdirs()) {
84+
System.out.println(
85+
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
86+
}
87+
}
88+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
89+
try {
90+
downloadFileFromURL(url, outputFile);
91+
System.out.println("Done");
92+
System.exit(0);
93+
} catch (Throwable e) {
94+
System.out.println("- Error downloading");
95+
e.printStackTrace();
96+
System.exit(1);
97+
}
98+
}
99+
100+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
101+
URL website = new URL(urlString);
102+
ReadableByteChannel rbc;
103+
rbc = Channels.newChannel(website.openStream());
104+
FileOutputStream fos = new FileOutputStream(destination);
105+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
106+
fos.close();
107+
rbc.close();
108+
}
109+
110+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.1/apache-maven-3.6.1-bin.zip
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '2.0.5.RELEASE'
4+
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
5+
id 'com.google.cloud.tools.jib' version '2.1.0'
6+
}
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
sourceCompatibility = 1.8
13+
targetCompatibility = 1.8
14+
15+
dependencies {
16+
compile "org.springframework.boot:spring-boot-starter-web"
17+
compile "org.springframework.boot:spring-boot-starter-actuator"
18+
compile "org.springframework.boot:spring-boot-devtools"
19+
20+
testCompile("org.springframework.boot:spring-boot-starter-test")
21+
}
22+
23+
jib {
24+
from {
25+
image = "gcr.io/distroless/java:debug"
26+
}
27+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)