diff --git a/sdks/java/container/boot.go b/sdks/java/container/boot.go index f37857305512..d067db2b304d 100644 --- a/sdks/java/container/boot.go +++ b/sdks/java/container/boot.go @@ -343,7 +343,8 @@ func BuildOptions(metaOptions []*MetaOption) *Options { continue } - options.JavaArguments = append(options.JavaArguments, meta.Options.JavaArguments...) + // Rightmost takes precedence + options.JavaArguments = append(meta.Options.JavaArguments, options.JavaArguments...) for key, value := range meta.Options.Properties { _, exists := options.Properties[key] diff --git a/sdks/java/container/boot_test.go b/sdks/java/container/boot_test.go new file mode 100644 index 000000000000..53942f683430 --- /dev/null +++ b/sdks/java/container/boot_test.go @@ -0,0 +1,74 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// boot is the boot code for the Java SDK harness container. It is responsible +// for retrieving staged files and invoking the JVM correctly. +package main + +import ( + "reflect" + "testing" +) + +func TestBuildOptionsEmpty(t *testing.T) { + dir := "test/empty" + metaOptions, err := LoadMetaOptions(dir) + if err != nil { + t.Fatalf("Got error %v running LoadMetaOptions", err) + } + if metaOptions != nil { + t.Fatalf("LoadMetaOptions(%v) = %v, want nil", dir, metaOptions) + } + + javaOptions := BuildOptions(metaOptions) + if len(javaOptions.JavaArguments) != 0 || len(javaOptions.Classpath) != 0 || len(javaOptions.Properties) != 0 { + t.Errorf("BuildOptions(%v) = %v, want nil", metaOptions, javaOptions) + } +} + +func TestBuildOptionsDisabled(t *testing.T) { + metaOptions, err := LoadMetaOptions("test/disabled") + if err != nil { + t.Fatalf("Got error %v running LoadMetaOptions", err) + } + + javaOptions := BuildOptions(metaOptions) + if len(javaOptions.JavaArguments) != 0 || len(javaOptions.Classpath) != 0 || len(javaOptions.Properties) != 0 { + t.Errorf("BuildOptions(%v) = %v, want nil", metaOptions, javaOptions) + } +} + +func TestBuildOptions(t *testing.T) { + metaOptions, err := LoadMetaOptions("test/priority") + if err != nil { + t.Fatalf("Got error %v running LoadMetaOptions", err) + } + + javaOptions := BuildOptions(metaOptions) + wantJavaArguments := []string{"java_args=low", "java_args=high"} + wantClasspath := []string{"classpath_high", "classpath_low"} + wantProperties := map[string]string{ + "priority":"high", + } + if !reflect.DeepEqual(javaOptions.JavaArguments, wantJavaArguments) { + t.Errorf("BuildOptions(%v).JavaArguments = %v, want %v", metaOptions, javaOptions.JavaArguments, wantJavaArguments) + } + if !reflect.DeepEqual(javaOptions.Classpath, wantClasspath) { + t.Errorf("BuildOptions(%v).Classpath = %v, want %v", metaOptions, javaOptions.Classpath, wantClasspath) + } + if !reflect.DeepEqual(javaOptions.Properties, wantProperties) { + t.Errorf("BuildOptions(%v).JavaProperties = %v, want %v", metaOptions, javaOptions.Properties, wantProperties) + } +} diff --git a/sdks/java/container/common.gradle b/sdks/java/container/common.gradle index e58f19e99f4d..88d67ef039a7 100644 --- a/sdks/java/container/common.gradle +++ b/sdks/java/container/common.gradle @@ -75,7 +75,7 @@ task copyGolangLicenses(type: Copy) { } task copyJdkOptions(type: Copy) { - if (imageJavaVersion == "17") { + if (imageJavaVersion == "17" || imageJavaVersion == "11") { from "option-jamm.json" into "build/target/options" } @@ -115,4 +115,4 @@ if (project.rootProject.hasProperty(["docker-pull-licenses"]) || dockerPrepare.dependsOn copySdkHarnessLauncher dockerPrepare.dependsOn copyDockerfileDependencies dockerPrepare.dependsOn ":sdks:java:container:downloadCloudProfilerAgent" -dockerPrepare.dependsOn copyJdkOptions \ No newline at end of file +dockerPrepare.dependsOn copyJdkOptions diff --git a/sdks/java/container/java11/option-jamm.json b/sdks/java/container/java11/option-jamm.json new file mode 100644 index 000000000000..e994648a8e2d --- /dev/null +++ b/sdks/java/container/java11/option-jamm.json @@ -0,0 +1,13 @@ +{ + "name": "jamm", + "enabled": true, + "options": { + "java_arguments": [ + "--add-modules=jamm", + "--module-path=/opt/apache/beam/jars/jamm.jar", + "--add-opens=java.base/java.lang=jamm", + "--add-opens=java.base/java.lang.ref=jamm", + "--add-opens=java.base/java.util=jamm" + ] + } +} \ No newline at end of file diff --git a/sdks/java/container/test/disabled/option-disabled.json b/sdks/java/container/test/disabled/option-disabled.json new file mode 100644 index 000000000000..8ba7e7ff00c3 --- /dev/null +++ b/sdks/java/container/test/disabled/option-disabled.json @@ -0,0 +1,6 @@ +{ + "name": "test-disabled", + "enabled": false, + "options": { + } +} \ No newline at end of file diff --git a/sdks/java/container/test/empty/README b/sdks/java/container/test/empty/README new file mode 100644 index 000000000000..16786ad6f5eb --- /dev/null +++ b/sdks/java/container/test/empty/README @@ -0,0 +1 @@ +Empty directory to test boot options \ No newline at end of file diff --git a/sdks/java/container/test/priority/option-high.json b/sdks/java/container/test/priority/option-high.json new file mode 100644 index 000000000000..f7830cc41082 --- /dev/null +++ b/sdks/java/container/test/priority/option-high.json @@ -0,0 +1,16 @@ +{ + "name": "high", + "enabled": true, + "priority": 100, + "options": { + "java_arguments": [ + "java_args=high" + ], + "classpath": [ + "classpath_high" + ], + "properties": { + "priority": "high" + } + } +} \ No newline at end of file diff --git a/sdks/java/container/test/priority/option-low.json b/sdks/java/container/test/priority/option-low.json new file mode 100644 index 000000000000..b67a8ccdfdf7 --- /dev/null +++ b/sdks/java/container/test/priority/option-low.json @@ -0,0 +1,16 @@ +{ + "name": "low", + "enabled": true, + "priority": 0, + "options": { + "java_arguments": [ + "java_args=low" + ], + "classpath": [ + "classpath_low" + ], + "properties": { + "priority": "low" + } + } +} \ No newline at end of file