|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import io |
| 17 | +import gzip |
| 18 | +from typing import List, Optional |
| 19 | +from minifi_test_framework.containers.file import File |
| 20 | +from minifi_test_framework.containers.container import Container |
| 21 | +from minifi_test_framework.core.helpers import wait_for_condition |
| 22 | +from minifi_test_framework.core.minifi_test_context import MinifiTestContext |
| 23 | +from minifi_test_framework.minifi.nifi_flow_definition import NifiFlowDefinition |
| 24 | + |
| 25 | + |
| 26 | +class NifiContainer(Container): |
| 27 | + NIFI_VERSION = '2.2.0' |
| 28 | + |
| 29 | + def __init__(self, test_context: MinifiTestContext, command: Optional[List[str]] = None, use_ssl: bool = False): |
| 30 | + self.flow_definition = NifiFlowDefinition() |
| 31 | + name = f"nifi-{test_context.scenario_id}" |
| 32 | + if use_ssl: |
| 33 | + entry_command = (r"sed -i -e 's/^\(nifi.remote.input.host\)=.*/\1={name}/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 34 | + r"sed -i -e 's/^\(nifi.remote.input.secure\)=.*/\1=true/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 35 | + r"sed -i -e 's/^\(nifi.sensitive.props.key\)=.*/\1=secret_key_12345/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 36 | + r"sed -i -e 's/^\(nifi.web.https.port\)=.*/\1=8443/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 37 | + r"sed -i -e 's/^\(nifi.web.https.host\)=.*/\1={name}/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 38 | + r"sed -i -e 's/^\(nifi.security.keystore\)=.*/\1=\/tmp\/resources\/keystore.jks/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 39 | + r"sed -i -e 's/^\(nifi.security.keystoreType\)=.*/\1=jks/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 40 | + r"sed -i -e 's/^\(nifi.security.keystorePasswd\)=.*/\1=passw0rd1!/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 41 | + r"sed -i -e 's/^\(nifi.security.keyPasswd\)=.*/#\1=passw0rd1!/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 42 | + r"sed -i -e 's/^\(nifi.security.truststore\)=.*/\1=\/tmp\/resources\/truststore.jks/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 43 | + r"sed -i -e 's/^\(nifi.security.truststoreType\)=.*/\1=jks/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 44 | + r"sed -i -e 's/^\(nifi.security.truststorePasswd\)=.*/\1=passw0rd1!/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 45 | + r"sed -i -e 's/^\(nifi.remote.input.socket.port\)=.*/\1=10443/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 46 | + r"cp /tmp/nifi_config/flow.json.gz /opt/nifi/nifi-current/conf && /opt/nifi/nifi-current/bin/nifi.sh run & " |
| 47 | + r"nifi_pid=$! &&" |
| 48 | + r"tail -F --pid=${{nifi_pid}} /opt/nifi/nifi-current/logs/nifi-app.log").format(name=name) |
| 49 | + else: |
| 50 | + entry_command = (r"sed -i -e 's/^\(nifi.remote.input.host\)=.*/\1={name}/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 51 | + r"sed -i -e 's/^\(nifi.sensitive.props.key\)=.*/\1=secret_key_12345/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 52 | + r"sed -i -e 's/^\(nifi.remote.input.secure\)=.*/\1=false/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 53 | + r"sed -i -e 's/^\(nifi.web.http.port\)=.*/\1=8080/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 54 | + r"sed -i -e 's/^\(nifi.web.https.port\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 55 | + r"sed -i -e 's/^\(nifi.web.https.host\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 56 | + r"sed -i -e 's/^\(nifi.web.http.host\)=.*/\1={name}/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 57 | + r"sed -i -e 's/^\(nifi.security.keystore\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 58 | + r"sed -i -e 's/^\(nifi.security.keystoreType\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 59 | + r"sed -i -e 's/^\(nifi.security.keystorePasswd\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 60 | + r"sed -i -e 's/^\(nifi.security.keyPasswd\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 61 | + r"sed -i -e 's/^\(nifi.security.truststore\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 62 | + r"sed -i -e 's/^\(nifi.security.truststoreType\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 63 | + r"sed -i -e 's/^\(nifi.security.truststorePasswd\)=.*/\1=/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 64 | + r"sed -i -e 's/^\(nifi.remote.input.socket.port\)=.*/\1=10000/' /opt/nifi/nifi-current/conf/nifi.properties && " |
| 65 | + r"cp /tmp/nifi_config/flow.json.gz /opt/nifi/nifi-current/conf && /opt/nifi/nifi-current/bin/nifi.sh run & " |
| 66 | + r"nifi_pid=$! &&" |
| 67 | + r"tail -F --pid=${{nifi_pid}} /opt/nifi/nifi-current/logs/nifi-app.log").format(name=name) |
| 68 | + if not command: |
| 69 | + command = ["/bin/sh", "-c", entry_command] |
| 70 | + |
| 71 | + super().__init__("apache/nifi:" + self.NIFI_VERSION, name, test_context.network, entrypoint=command) |
| 72 | + |
| 73 | + def deploy(self): |
| 74 | + flow_config = self.flow_definition.to_json() |
| 75 | + buffer = io.BytesIO() |
| 76 | + |
| 77 | + with gzip.GzipFile(fileobj=buffer, mode='wb') as gz_file: |
| 78 | + gz_file.write(flow_config.encode()) |
| 79 | + |
| 80 | + gzipped_bytes = buffer.getvalue() |
| 81 | + self.files.append(File("/tmp/nifi_config/flow.json.gz", gzipped_bytes)) |
| 82 | + |
| 83 | + super().deploy() |
| 84 | + finished_str = "Started Application in" |
| 85 | + return wait_for_condition( |
| 86 | + condition=lambda: finished_str in self.get_logs(), |
| 87 | + timeout_seconds=300, |
| 88 | + bail_condition=lambda: self.exited, |
| 89 | + context=None) |
0 commit comments