Skip to content

Commit

Permalink
Update to changed DynamicListForm import from latest cockpit
Browse files Browse the repository at this point in the history
Adjust code and tests for the `undefined` "holes" that the data model
now gets on deletion.
  • Loading branch information
martinpitt authored and jelly committed Jan 10, 2024
1 parent 307ee09 commit 2c163bb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ COCKPIT_REPO_FILES = \
$(NULL)

COCKPIT_REPO_URL = https://github.com/cockpit-project/cockpit.git
COCKPIT_REPO_COMMIT = cf2485d79da7fb9be262eaa4ca5ef91e57a03a75 # 308 + 5 commits
COCKPIT_REPO_COMMIT = e4dcf6b1272b65bfdec28d2027906beac9724b87 # 308 + 19 commits

$(COCKPIT_REPO_FILES): $(COCKPIT_REPO_STAMP)
COCKPIT_REPO_TREE = '$(strip $(COCKPIT_REPO_COMMIT))^{tree}'
Expand Down
32 changes: 22 additions & 10 deletions src/ImageRunModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import rest from './rest.js';
import cockpit from 'cockpit';
import { onDownloadContainer, onDownloadContainerFinished } from './Containers.jsx';
import { PublishPort, validatePublishPort } from './PublishPort.jsx';
import { DynamicListForm } from 'DynamicListForm.jsx';
import { DynamicListForm } from 'cockpit-components-dynamic-list.jsx';
import { validateVolume, Volume } from './Volume.jsx';
import { EnvVar, validateEnvVar } from './Env.jsx';

Expand Down Expand Up @@ -166,9 +166,9 @@ export class ImageRunModal extends React.Component {
createConfig.resource_limits = resourceLimit;
}
createConfig.terminal = this.state.hasTTY;
if (this.state.publish.length > 0)
if (this.state.publish.some(port => port !== undefined))
createConfig.portmappings = this.state.publish
.filter(port => port.containerPort)
.filter(port => port?.containerPort)
.map(port => {
const pm = { container_port: parseInt(port.containerPort), protocol: port.protocol };
if (port.hostPort !== null)
Expand All @@ -177,14 +177,17 @@ export class ImageRunModal extends React.Component {
pm.host_ip = port.IP;
return pm;
});
if (this.state.env.length > 0) {
const ports = {};
this.state.env.forEach(item => { ports[item.envKey] = item.envValue });
createConfig.env = ports;
if (this.state.env.some(item => item !== undefined)) {
const envs = {};
this.state.env.forEach(item => {
if (item !== undefined)
envs[item.envKey] = item.envValue;
});
createConfig.env = envs;
}
if (this.state.volumes.length > 0) {
if (this.state.volumes.some(volume => volume !== undefined)) {
createConfig.mounts = this.state.volumes
.filter(volume => volume.hostPath && volume.containerPath)
.filter(volume => volume?.hostPath && volume?.containerPath)
.map(volume => {
const record = { source: volume.hostPath, destination: volume.containerPath, type: "bind" };
record.options = [];
Expand Down Expand Up @@ -589,7 +592,7 @@ export class ImageRunModal extends React.Component {
};

isFormInvalid = validationFailed => {
const groupHasError = row => Object.values(row)
const groupHasError = row => row && Object.values(row)
.filter(val => val) // Filter out empty/undefined properties
.length > 0; // If one field has error, the whole group (dynamicList) is invalid

Expand All @@ -614,6 +617,9 @@ export class ImageRunModal extends React.Component {
const validationFailed = { };

const publishValidation = publish.map(a => {
if (a === undefined)
return undefined;

return {
IP: validatePublishPort(a.IP, "IP"),
hostPort: validatePublishPort(a.hostPort, "hostPort"),
Expand All @@ -624,6 +630,9 @@ export class ImageRunModal extends React.Component {
validationFailed.publish = publishValidation;

const volumesValidation = volumes.map(a => {
if (a === undefined)
return undefined;

return {
hostPath: validateVolume(a.hostPath, "hostPath"),
containerPath: validateVolume(a.containerPath, "containerPath"),
Expand All @@ -633,6 +642,9 @@ export class ImageRunModal extends React.Component {
validationFailed.volumes = volumesValidation;

const envValidation = env.map(a => {
if (a === undefined)
return undefined;

return {
envKey: validateEnvVar(a.envKey, "envKey"),
envValue: validateEnvVar(a.envValue, "envValue"),
Expand Down
15 changes: 9 additions & 6 deletions src/PodCreateModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput"
import * as dockerNames from 'docker-names';

import { FormHelper } from 'cockpit-components-form-helper.jsx';
import { DynamicListForm } from 'DynamicListForm.jsx';
import { DynamicListForm } from 'cockpit-components-dynamic-list.jsx';
import { ErrorNotification } from './Notification.jsx';
import { PublishPort, validatePublishPort } from './PublishPort.jsx';
import { Volume } from './Volume.jsx';
Expand Down Expand Up @@ -39,7 +39,7 @@ export const PodCreateModal = ({ user, systemServiceAvailable, userServiceAvaila

if (publish.length > 0)
createConfig.portmappings = publish
.filter(port => port.containerPort)
.filter(port => port?.containerPort)
.map(port => {
const pm = { container_port: parseInt(port.containerPort), protocol: port.protocol };
if (port.hostPort !== null)
Expand All @@ -51,7 +51,7 @@ export const PodCreateModal = ({ user, systemServiceAvailable, userServiceAvaila

if (volumes.length > 0) {
createConfig.mounts = volumes
.filter(volume => volume.hostPath && volume.containerPath)
.filter(volume => volume?.hostPath && volume?.containerPath)
.map(volume => {
const record = { source: volume.hostPath, destination: volume.containerPath, type: "bind" };
record.options = [];
Expand Down Expand Up @@ -99,7 +99,7 @@ export const PodCreateModal = ({ user, systemServiceAvailable, userServiceAvaila
};

const isFormInvalid = validationFailed => {
const groupHasError = row => Object.values(row)
const groupHasError = row => row && Object.values(row)
.filter(val => val) // Filter out empty/undefined properties
.length > 0; // If one field has error, the whole group (dynamicList) is invalid

Expand All @@ -117,14 +117,17 @@ export const PodCreateModal = ({ user, systemServiceAvailable, userServiceAvaila
const newValidationFailed = { };

const publishValidation = publish.map(a => {
if (a === undefined)
return undefined;

return {
IP: validatePublishPort(a.IP, "IP"),
hostPort: validatePublishPort(a.hostPort, "hostPort"),
containerPort: validatePublishPort(a.containerPort, "containerPort"),
};
});
if (publishValidation.some(entry => Object.keys(entry).length > 0))
newValidationFailed.publish = publishValidation;
if (publishValidation.some(entry => entry && Object.keys(entry).length > 0))
newValidationFailed.publish = publishValidation.filter(entry => entry !== undefined);

const podNameValidation = validatePodName(podName);

Expand Down
42 changes: 21 additions & 21 deletions test/check-application
Original file line number Diff line number Diff line change
Expand Up @@ -1796,10 +1796,10 @@ class TestApplication(testlib.MachineCase):
b.set_input_text('#run-image-dialog-publish-2-host-port', '7001')
b.click('#run-image-dialog-publish-2-btn-close')
b.click('.publish-port-form .btn-add')
b.set_input_text('#run-image-dialog-publish-2-container-port', '8001')
b.set_input_text('#run-image-dialog-publish-3-container-port', '8001')
b.click('.publish-port-form .btn-add')
b.set_input_text('#run-image-dialog-publish-3-ip-address', '127.0.0.2')
b.set_input_text('#run-image-dialog-publish-3-container-port', '9001')
b.set_input_text('#run-image-dialog-publish-4-ip-address', '127.0.0.2')
b.set_input_text('#run-image-dialog-publish-4-container-port', '9001')

# Configure env
b.click('.env-form .btn-add')
Expand All @@ -1814,19 +1814,19 @@ class TestApplication(testlib.MachineCase):
b.click('#run-image-dialog-env-2-btn-close')
b.click('.env-form .btn-add')
# Test inputting an key=var entry
b.set_val('#run-image-dialog-env-2-value',
b.set_val('#run-image-dialog-env-3-value',
"RHUBARB=STRAWBERRY DURIAN=LEMON TEST_URL=wss://cockpit/?start=1&stop=0")
# set_val does not trigger onChange so append a space.
b.set_input_text('#run-image-dialog-env-2-value', ' ', append=True, value_check=False)
b.set_input_text('#run-image-dialog-env-3-value', ' ', append=True, value_check=False)

b.click('.env-form .btn-add')
b.set_input_text('#run-image-dialog-env-5-key', 'HOSTNAME')
b.set_input_text('#run-image-dialog-env-5-value', 'busybox')
b.set_input_text('#run-image-dialog-env-6-key', 'HOSTNAME')
b.set_input_text('#run-image-dialog-env-6-value', 'busybox')

# Test inputting a var with = in it doesn't reset key
b.click('.env-form .btn-add')
b.set_input_text('#run-image-dialog-env-6-key', 'TEST')
b.set_input_text('#run-image-dialog-env-6-value', 'REBASE=1')
b.set_input_text('#run-image-dialog-env-7-key', 'TEST')
b.set_input_text('#run-image-dialog-env-7-value', 'REBASE=1')

# Configure volumes
b.click('.volume-form .btn-add')
Expand Down Expand Up @@ -1857,13 +1857,13 @@ class TestApplication(testlib.MachineCase):
skip_layouts=["rtl"])

if self.has_selinux:
b.set_val('#run-image-dialog-volume-1-selinux', "Z")
b.set_val('#run-image-dialog-volume-2-selinux', "Z")
else:
b.wait_not_present('#run-image-dialog-volume-1-selinux')
b.wait_not_present('#run-image-dialog-volume-2-selinux')

b.set_file_autocomplete_val("#run-image-dialog-volume-1 .pf-v5-c-select", rwdir)
b.set_file_autocomplete_val("#run-image-dialog-volume-2 .pf-v5-c-select", rwdir)
b.key_press(["\r"])
b.set_input_text('#run-image-dialog-volume-1-container-path', '/tmp/rw')
b.set_input_text('#run-image-dialog-volume-2-container-path', '/tmp/rw')
rw_label = m.execute(f"ls -dZ {rwdir}").split(" ")[0]

b.click('.pf-v5-c-modal-box__footer #create-image-create-run-btn')
Expand Down Expand Up @@ -2759,16 +2759,16 @@ class TestApplication(testlib.MachineCase):

# Ports
b.click('.publish-port-form .btn-add')
b.set_input_text('#create-pod-dialog-publish-0-host-port', '6000')
b.set_input_text('#create-pod-dialog-publish-0-container-port', '5000')
b.set_input_text('#create-pod-dialog-publish-1-host-port', '6000')
b.set_input_text('#create-pod-dialog-publish-1-container-port', '5000')
b.click('.publish-port-form .btn-add')
b.set_input_text('#create-pod-dialog-publish-1-ip-address', '127.0.0.1')
b.set_input_text('#create-pod-dialog-publish-1-host-port', '6001')
b.set_input_text('#create-pod-dialog-publish-1-container-port', '5001')
b.set_val('#create-pod-dialog-publish-1-protocol', "udp")
b.set_input_text('#create-pod-dialog-publish-2-ip-address', '127.0.0.1')
b.set_input_text('#create-pod-dialog-publish-2-host-port', '6001')
b.set_input_text('#create-pod-dialog-publish-2-container-port', '5001')
b.set_val('#create-pod-dialog-publish-2-protocol', "udp")
b.click('.publish-port-form .btn-add')
b.set_input_text('#create-pod-dialog-publish-2-ip-address', '127.0.0.2')
b.set_input_text('#create-pod-dialog-publish-2-container-port', '9001')
b.set_input_text('#create-pod-dialog-publish-3-ip-address', '127.0.0.2')
b.set_input_text('#create-pod-dialog-publish-3-container-port', '9001')

# Volumes
if self.machine.image not in ["ubuntu-2204"]:
Expand Down

0 comments on commit 2c163bb

Please sign in to comment.