Skip to content

Commit

Permalink
[hotfix] Migrate GlobalConfigurationTest to Junit5.
Browse files Browse the repository at this point in the history
  • Loading branch information
JunRuiLee authored and zhuzhurk committed Nov 30, 2023
1 parent acb4217 commit 1a11197
Showing 1 changed file with 60 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,28 @@

package org.apache.flink.configuration;

import org.apache.flink.util.TestLogger;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.UUID;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/**
* This class contains tests for the global configuration (parsing configuration directory
* information).
*/
public class GlobalConfigurationTest extends TestLogger {
class GlobalConfigurationTest {

@Rule public TemporaryFolder tempFolder = new TemporaryFolder();
@TempDir private File tmpDir;

@Test
public void testConfigurationYAML() {
File tmpDir = tempFolder.getRoot();
void testConfigurationYAML() {
File confFile = new File(tmpDir, GlobalConfiguration.FLINK_CONF_FILENAME);

try {
Expand Down Expand Up @@ -77,74 +71,83 @@ public void testConfigurationYAML() {
Configuration conf = GlobalConfiguration.loadConfiguration(tmpDir.getAbsolutePath());

// all distinct keys from confFile1 + confFile2 key
assertEquals(6, conf.keySet().size());
assertThat(conf.keySet()).hasSize(6);

// keys 1, 2, 4, 5, 6, 7, 8 should be OK and match the expected values
assertEquals("myvalue1", conf.getString("mykey1", null));
assertEquals("myvalue2", conf.getString("mykey2", null));
assertEquals("null", conf.getString("mykey3", "null"));
assertEquals("myvalue4", conf.getString("mykey4", null));
assertEquals("myvalue5", conf.getString("mykey5", null));
assertEquals("my: value6", conf.getString("mykey6", null));
assertEquals("null", conf.getString("mykey7", "null"));
assertEquals("null", conf.getString("mykey8", "null"));
assertEquals("myvalue10", conf.getString("mykey9", null));
assertThat(conf.getString("mykey1", null)).isEqualTo("myvalue1");
assertThat(conf.getString("mykey1", null)).isEqualTo("myvalue1");
assertThat(conf.getString("mykey2", null)).isEqualTo("myvalue2");
assertThat(conf.getString("mykey3", "null")).isEqualTo("null");
assertThat(conf.getString("mykey4", null)).isEqualTo("myvalue4");
assertThat(conf.getString("mykey5", null)).isEqualTo("myvalue5");
assertThat(conf.getString("mykey6", null)).isEqualTo("my: value6");
assertThat(conf.getString("mykey7", "null")).isEqualTo("null");
assertThat(conf.getString("mykey8", "null")).isEqualTo("null");
assertThat(conf.getString("mykey9", null)).isEqualTo("myvalue10");
} finally {
confFile.delete();
tmpDir.delete();
}
}

@Test(expected = IllegalArgumentException.class)
public void testFailIfNull() {
GlobalConfiguration.loadConfiguration((String) null);
@Test
void testFailIfNull() {
assertThatThrownBy(() -> GlobalConfiguration.loadConfiguration((String) null))
.isInstanceOf(IllegalArgumentException.class);
}

@Test(expected = IllegalConfigurationException.class)
public void testFailIfNotLoaded() {
GlobalConfiguration.loadConfiguration("/some/path/" + UUID.randomUUID());
@Test
void testFailIfNotLoaded() {
assertThatThrownBy(
() ->
GlobalConfiguration.loadConfiguration(
"/some/path/" + UUID.randomUUID()))
.isInstanceOf(IllegalConfigurationException.class);
}

@Test(expected = IllegalConfigurationException.class)
public void testInvalidConfiguration() throws IOException {
GlobalConfiguration.loadConfiguration(tempFolder.getRoot().getAbsolutePath());
@Test
void testInvalidConfiguration() {
assertThatThrownBy(() -> GlobalConfiguration.loadConfiguration(tmpDir.getAbsolutePath()))
.isInstanceOf(IllegalConfigurationException.class);
}

@Test
// We allow malformed YAML files
public void testInvalidYamlFile() throws IOException {
final File confFile = tempFolder.newFile(GlobalConfiguration.FLINK_CONF_FILENAME);
void testInvalidYamlFile() throws IOException {
final File confFile = new File(tmpDir.getPath(), GlobalConfiguration.FLINK_CONF_FILENAME);

try (PrintWriter pw = new PrintWriter(confFile)) {
pw.append("invalid");
}

assertNotNull(
GlobalConfiguration.loadConfiguration(tempFolder.getRoot().getAbsolutePath()));
assertThat(GlobalConfiguration.loadConfiguration(tmpDir.getAbsolutePath())).isNotNull();
}

@Test
public void testHiddenKey() {
assertTrue(GlobalConfiguration.isSensitive("password123"));
assertTrue(GlobalConfiguration.isSensitive("123pasSword"));
assertTrue(GlobalConfiguration.isSensitive("PasSword"));
assertTrue(GlobalConfiguration.isSensitive("Secret"));
assertTrue(GlobalConfiguration.isSensitive("polaris.client-secret"));
assertTrue(GlobalConfiguration.isSensitive("client-secret"));
assertTrue(GlobalConfiguration.isSensitive("service-key-json"));
assertTrue(GlobalConfiguration.isSensitive("auth.basic.password"));
assertTrue(GlobalConfiguration.isSensitive("auth.basic.token"));
assertTrue(GlobalConfiguration.isSensitive("avro-confluent.basic-auth.user-info"));
assertTrue(GlobalConfiguration.isSensitive("key.avro-confluent.basic-auth.user-info"));
assertTrue(GlobalConfiguration.isSensitive("value.avro-confluent.basic-auth.user-info"));
assertTrue(GlobalConfiguration.isSensitive("kafka.jaas.config"));
assertTrue(GlobalConfiguration.isSensitive("properties.ssl.truststore.password"));
assertTrue(GlobalConfiguration.isSensitive("properties.ssl.keystore.password"));

assertTrue(
GlobalConfiguration.isSensitive(
"fs.azure.account.key.storageaccount123456.core.windows.net"));
assertFalse(GlobalConfiguration.isSensitive("Hello"));
assertTrue(GlobalConfiguration.isSensitive("metrics.reporter.dghttp.apikey"));
assertThat(GlobalConfiguration.isSensitive("password123")).isTrue();
assertThat(GlobalConfiguration.isSensitive("123pasSword")).isTrue();
assertThat(GlobalConfiguration.isSensitive("PasSword")).isTrue();
assertThat(GlobalConfiguration.isSensitive("Secret")).isTrue();
assertThat(GlobalConfiguration.isSensitive("polaris.client-secret")).isTrue();
assertThat(GlobalConfiguration.isSensitive("client-secret")).isTrue();
assertThat(GlobalConfiguration.isSensitive("service-key-json")).isTrue();
assertThat(GlobalConfiguration.isSensitive("auth.basic.password")).isTrue();
assertThat(GlobalConfiguration.isSensitive("auth.basic.token")).isTrue();
assertThat(GlobalConfiguration.isSensitive("avro-confluent.basic-auth.user-info")).isTrue();
assertThat(GlobalConfiguration.isSensitive("key.avro-confluent.basic-auth.user-info"))
.isTrue();
assertThat(GlobalConfiguration.isSensitive("value.avro-confluent.basic-auth.user-info"))
.isTrue();
assertThat(GlobalConfiguration.isSensitive("kafka.jaas.config")).isTrue();
assertThat(GlobalConfiguration.isSensitive("properties.ssl.truststore.password")).isTrue();
assertThat(GlobalConfiguration.isSensitive("properties.ssl.keystore.password")).isTrue();

assertThat(
GlobalConfiguration.isSensitive(
"fs.azure.account.key.storageaccount123456.core.windows.net"))
.isTrue();
assertThat(GlobalConfiguration.isSensitive("Hello")).isFalse();
assertThat(GlobalConfiguration.isSensitive("metrics.reporter.dghttp.apikey")).isTrue();
}
}

0 comments on commit 1a11197

Please sign in to comment.