Skip to content

Commit

Permalink
Get input from file
Browse files Browse the repository at this point in the history
  • Loading branch information
sfodagain committed Jun 3, 2024
1 parent c12a37e commit 62c4505
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci_run_greengrass_discovery_cfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
"name": "--region",
"data": "us-east-1"
},
{
"name": "--message",
"data": "hello"
},
{
"name": "--topic",
"data": "clients/CI_Greengrass_Discovery_Thing/hello/world/$INPUT_UUID"
Expand All @@ -34,5 +30,6 @@
"name": "--mode",
"data": "publish"
}
]
],
"stdin_file": "messages.txt"
}
14 changes: 7 additions & 7 deletions samples/greengrass/basic_discovery/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ int main(int argc, char *argv[])
connection->Subscribe(cmdData.input_topic.c_str(), AWS_MQTT_QOS_AT_MOST_ONCE, onMessage, onSubAck);
}

int cnt = 0;
while (++cnt < 3)
bool first_input = true;
while (true)
{
String input;
if (cmdData.input_mode == "both" || cmdData.input_mode == "publish")
Expand All @@ -315,10 +315,13 @@ int main(int argc, char *argv[])
std::getline(std::cin, input);
cmdData.input_message = input;
}
else
else if (!first_input)
{
input = cmdData.input_message;
fprintf(stdout, "Enter a new message or enter 'exit' or 'quit' to exit the program.\n");
std::getline(std::cin, input);
cmdData.input_message = input;
}
first_input = false;
}
else
{
Expand Down Expand Up @@ -349,12 +352,9 @@ int main(int argc, char *argv[])
fprintf(stdout, "Operation failed with error %s\n", aws_error_debug_str(errorCode));
}
};
fprintf(stdout, "Publishing to topic %s\n", cmdData.input_topic.c_str());
connection->Publish(
cmdData.input_topic.c_str(), AWS_MQTT_QOS_AT_LEAST_ONCE, false, payload, onPublishComplete);
}

std::this_thread::sleep_for(std::chrono::seconds(1));
}

connection->Disconnect();
Expand Down
2 changes: 2 additions & 0 deletions tests/greengrass/basic_discovery/messages.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello
exit
1 change: 1 addition & 0 deletions tests/greengrass/basic_discovery/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Manifests:
- URI: "file:hello_world_subscriber.py"
- URI: "file:run_in_ci.py"
- URI: "file:ci_run_greengrass_discovery_cfg.json"
- URI: "file:messages.txt"
- URI: "file:basic-discovery"
Permission:
Read: ALL
Expand Down
56 changes: 46 additions & 10 deletions utils/run_in_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,18 @@ def make_windows_pfx_file(certificate_file_path, private_key_path, pfx_file_path

# Import the PFX into the Windows Certificate Store
# (Passing '$mypwd' is required even though it is empty and our certificate has no password. It fails CI otherwise)
import_pfx_arguments = ["powershell.exe", "Import-PfxCertificate", "-FilePath", pfx_file_path, "-CertStoreLocation", "Cert:\\" + pfx_certificate_store_location, "-Password", "$mypwd"]
import_pfx_arguments = [
"powershell.exe",
# Powershell 7.3 introduced an issue where launching powershell from cmd would not set PSModulePath correctly.
# As a workaround, we set `PSModulePath` to empty so powershell would automatically reset the PSModulePath to default.
# More details: https://github.com/PowerShell/PowerShell/issues/18530
"$env:PSModulePath = '';",
"Import-PfxCertificate",
"-FilePath", pfx_file_path,
"-CertStoreLocation",
"Cert:\\" + pfx_certificate_store_location,
"-Password",
"$mypwd"]
import_pfx_run = subprocess.run(args=import_pfx_arguments, shell=True, stdout=subprocess.PIPE)
if (import_pfx_run.returncode != 0):
print ("ERROR: Could not import PFX certificate into Windows store!")
Expand Down Expand Up @@ -250,14 +261,19 @@ def launch_runnable(runnable_dir):
print("No configuration JSON file data found!")
return -1

# Prepare data for runnable's STDIN
subprocess_stdin = None
if "stdin_file" in config_json:
stdin_file = os.path.join(runnable_dir, config_json['stdin_file'])
with open(stdin_file, "rb") as file:
subprocess_stdin = file.read()

exit_code = 0

print("Launching runnable...")

runnable_file = os.path.join(runnable_dir, config_json['runnable_file'])
# Java
if (config_json['language'] == "Java"):

# Flatten arguments down into a single string
arguments_as_string = ""
for i in range(0, len(config_json_arguments_list)):
Expand All @@ -267,32 +283,52 @@ def launch_runnable(runnable_dir):

arguments = ["mvn", "compile", "exec:java"]
arguments.append("-pl")
arguments.append(runnable_file)
arguments.append(config_json['runnable_file'])
arguments.append("-Dexec.mainClass=" + config_json['runnable_main_class'])
arguments.append("-Daws.crt.ci=True")

# We have to do this as a string, unfortunately, due to how -Dexec.args= works...
argument_string = subprocess.list2cmdline(arguments) + " -Dexec.args=\"" + arguments_as_string + "\""
print(f"Running cmd: {argument_string}")
runnable_return = subprocess.run(argument_string, shell=True)
runnable_return = subprocess.run(argument_string, input=subprocess_stdin, shell=True)
exit_code = runnable_return.returncode

elif (config_json['language'] == "Java JAR"):
# Flatten arguments down into a single string
arguments_as_string = ""
for i in range(0, len(config_json_arguments_list)):
arguments_as_string += str(config_json_arguments_list[i])
if (i+1 < len(config_json_arguments_list)):
arguments_as_string += " "

runnable_file = os.path.join(runnable_dir, config_json['runnable_file'])

arguments = ["java"]
arguments.append("-Daws.crt.ci=True")
arguments.append("-jar")
arguments.append(runnable_file)

argument_string = subprocess.list2cmdline(arguments) + " " + arguments_as_string
print(f"Running cmd: {argument_string}")
runnable_return = subprocess.run(argument_string, input=subprocess_stdin, shell=True)
exit_code = runnable_return.returncode

# C++
elif (config_json['language'] == "CPP"):
runnable_return = subprocess.run(
args=config_json_arguments_list, executable=runnable_file)
runnable_file = os.path.join(runnable_dir, config_json['runnable_file'])
runnable_return = subprocess.run(args=config_json_arguments_list, input=subprocess_stdin, executable=runnable_file)
exit_code = runnable_return.returncode

elif (config_json['language'] == "Python"):
config_json_arguments_list.append("--is_ci")
config_json_arguments_list.append("True")

runnable_return = subprocess.run(
args=[sys.executable, runnable_file] + config_json_arguments_list)
args=[sys.executable, config_json['runnable_file']] + config_json_arguments_list, input=subprocess_stdin)
exit_code = runnable_return.returncode

elif (config_json['language'] == "Javascript"):
os.chdir(runnable_file)
os.chdir(config_json['runnable_file'])

config_json_arguments_list.append("--is_ci")
config_json_arguments_list.append("true")
Expand All @@ -318,7 +354,7 @@ def launch_runnable(runnable_dir):
args=arguments + config_json_arguments_list, shell=True)
else:
runnable_return_two = subprocess.run(
args=arguments + config_json_arguments_list)
args=arguments + config_json_arguments_list, input=subprocess_stdin)

if (runnable_return_two != None):
exit_code = runnable_return_two.returncode
Expand Down

0 comments on commit 62c4505

Please sign in to comment.