Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sdks/java/container/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
74 changes: 74 additions & 0 deletions sdks/java/container/boot_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 (related) things here:

  1. Use t.Fatalf for errors that should stop test execution. In this case, both the err check and metaOptions check keep the rest of the test from being useful so they should be fatal.
  2. Prefer ordering the err check above the metaOptions check since the metaOptions check isn't informative if we return an error.

The Fatalf comment applies to the rest of the tests as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thanks!


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)
}
}
4 changes: 2 additions & 2 deletions sdks/java/container/common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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
dockerPrepare.dependsOn copyJdkOptions
13 changes: 13 additions & 0 deletions sdks/java/container/java11/option-jamm.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
6 changes: 6 additions & 0 deletions sdks/java/container/test/disabled/option-disabled.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-disabled",
"enabled": false,
"options": {
}
}
1 change: 1 addition & 0 deletions sdks/java/container/test/empty/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Empty directory to test boot options
16 changes: 16 additions & 0 deletions sdks/java/container/test/priority/option-high.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "high",
"enabled": true,
"priority": 100,
"options": {
"java_arguments": [
"java_args=high"
],
"classpath": [
"classpath_high"
],
"properties": {
"priority": "high"
}
}
}
16 changes: 16 additions & 0 deletions sdks/java/container/test/priority/option-low.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "low",
"enabled": true,
"priority": 0,
"options": {
"java_arguments": [
"java_args=low"
],
"classpath": [
"classpath_low"
],
"properties": {
"priority": "low"
}
}
}