Skip to content

Commit

Permalink
Fix the file writing client readiness
Browse files Browse the repository at this point in the history
Readiness depends on 1) having an existing target folder, and 2) having a machine id file.
  • Loading branch information
jponge committed Mar 28, 2023
1 parent ff0dd02 commit 50f842a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void sendInsightsReport(String filename, InsightsReport report) {

@Override
public boolean isReadyToSend() {
return (new File(config.getCertFilePath()).exists()
&& new File(config.getKeyFilePath()).exists())
&& new File(config.getMachineIdFilePath()).exists();
return (new File(config.getArchiveUploadDir()).exists()
&& new File(config.getMachineIdFilePath()).exists());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.redhat.insights.doubles.NoopInsightsLogger;
import com.redhat.insights.logging.InsightsLogger;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -27,6 +28,7 @@ public String getIdentificationName() {
return "TEST";
}

@Override
public String getArchiveUploadDir() {
return tmpdir.toString();
}
Expand All @@ -48,7 +50,9 @@ public String getArchiveUploadDir() {
}

@Test
public void testIsReadyToSend() {
public void testIsReadyToSend() throws IOException {
Path tmpdir = Files.createTempDirectory("tmpDirPrefix");

InsightsConfiguration goodConfig =
new InsightsConfiguration() {
@Override
Expand All @@ -57,73 +61,45 @@ public String getIdentificationName() {
}

@Override
public String getKeyFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.key").toString();
}

@Override
public String getCertFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.cert").toString();
public String getArchiveUploadDir() {
return tmpdir.toString();
}

@Override
public String getMachineIdFilePath() {
return getPathFromResource("com/redhat/insights/machine-id").toString();
}
};
InsightsConfiguration wrongKeyConfig =
new InsightsConfiguration() {
@Override
public String getIdentificationName() {
return "BAD";
}

@Override
public String getKeyFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.key")
.resolveSibling("wrong.key")
.toString();
}

@Override
public String getCertFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.cert").toString();
}
};
InsightsConfiguration wrongCertConfig =
InsightsConfiguration wrongMachineIdConfig =
new InsightsConfiguration() {
@Override
public String getIdentificationName() {
return "BAD";
return "GOOD";
}

@Override
public String getKeyFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.key").toString();
public String getArchiveUploadDir() {
return tmpdir.toString();
}

@Override
public String getCertFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.cert")
.resolveSibling("wrong.cert")
.toString();
public String getMachineIdFilePath() {
return "BAD";
}
};
InsightsConfiguration wrongMachineIdConfig =

InsightsConfiguration wrongUploadPathConfig =
new InsightsConfiguration() {
@Override
public String getIdentificationName() {
return "GOOD";
}

@Override
public String getKeyFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.key").toString();
}

@Override
public String getCertFilePath() {
return getPathFromResource("com/redhat/insights/tls/dummy.cert").toString();
public String getArchiveUploadDir() {
// This is unlikely to exist
return "/a/b/c/d/FC02B2FE-B18B-48FB-B0B5-9CC8B98E9CD9";
}

@Override
Expand All @@ -133,19 +109,19 @@ public String getMachineIdFilePath() {
};

InsightsLogger logger = new NoopInsightsLogger();

InsightsHttpClient client = new InsightsFileWritingClient(logger, goodConfig);
assertTrue(client.isReadyToSend(), "Client should be ready to send");
client = new InsightsFileWritingClient(logger, wrongCertConfig);
assertFalse(
client.isReadyToSend(),
"Client shouldn't be ready to send because of wrong certificate path");
client = new InsightsFileWritingClient(logger, wrongKeyConfig);
assertFalse(
client.isReadyToSend(), "Client shouldn't be ready to send because of wrong key path");

client = new InsightsFileWritingClient(logger, wrongMachineIdConfig);
assertFalse(
client.isReadyToSend(),
"Client shouldn't be ready to send because of wrong machine-id path");

client = new InsightsFileWritingClient(logger, wrongUploadPathConfig);
assertFalse(
client.isReadyToSend(),
"Client shouldn't be ready to send because of non-existing upload directory");
}

private Path getPathFromResource(String path) {
Expand Down

0 comments on commit 50f842a

Please sign in to comment.