Skip to content

chore: enable terraform provisioners in e2e by default #13134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
switch to query params
  • Loading branch information
Emyrk committed May 5, 2024
commit 3173a5e56087579669b0db65a400bc4b9eff0c72
12 changes: 3 additions & 9 deletions site/e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,15 @@ export const createTemplate = async (
let path = "/templates/new";
if (isStarterTemplate(responses)) {
path += `?exampleId=${responses}`;
} else {
// The form page will read this value and use it as the default type.
path += "?provisioner_type=echo";
}

await page.goto(path, { waitUntil: "domcontentloaded" });
await expectUrl(page).toHavePathName("/templates/new");

if (!isStarterTemplate(responses)) {
await page
.locator(`xpath=//input[@data-testid="provisioner-type-input"]`)
.evaluate((el: HTMLElement) => {
// This is a little jank, but the "setAttribute" updates the HTML, but not the formik values.
el.setAttribute("value", "echo");
// This '.click()' activates the onClick handler that tells the input to update it's formik value.
el.click();
});

await page.getByTestId("file-upload").setInputFiles({
buffer: await createTemplateVersionTar(responses),
mimeType: "application/x-tar",
Expand Down
30 changes: 11 additions & 19 deletions site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TextField from "@mui/material/TextField";
import { Field, FormikProvider, useFormik } from "formik";

Check failure on line 2 in site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx

View workflow job for this annotation

GitHub Actions / lint

'Field' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 2 in site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx

View workflow job for this annotation

GitHub Actions / lint

'Field' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 2 in site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx

View workflow job for this annotation

GitHub Actions / fmt

'Field' is defined but never used. Allowed unused vars must match /^_/u
import camelCase from "lodash/camelCase";
import capitalize from "lodash/capitalize";
import type { FC } from "react";
Expand Down Expand Up @@ -33,6 +33,7 @@
} from "utils/schedule";
import { TemplateUpload, type TemplateUploadProps } from "./TemplateUpload";
import { VariableInput } from "./VariableInput";
import { useSearchParams } from "react-router-dom";

Check failure on line 36 in site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx

View workflow job for this annotation

GitHub Actions / lint

`react-router-dom` import should occur before import of `yup`

Check failure on line 36 in site/src/pages/CreateTemplatePage/CreateTemplateForm.tsx

View workflow job for this annotation

GitHub Actions / lint

`react-router-dom` import should occur before import of `yup`

const MAX_DESCRIPTION_CHAR_LIMIT = 128;

Expand Down Expand Up @@ -91,16 +92,23 @@
fromCopy?: Template;
variables?: TemplateVersionVariable[];
allowAdvancedScheduling: boolean;
searchParams: URLSearchParams;
};

const getInitialValues = ({
fromExample,
fromCopy,
allowAdvancedScheduling,
variables,
searchParams,
}: GetInitialValuesParams) => {
let initialValues = defaultInitialValues;

// Will assume the query param has a valid ProvisionerType, as this query param is only used
// in testing.
defaultInitialValues.provisioner_type =
(searchParams.get("provisioner_type") as ProvisionerType) || "terraform";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd kind of rather leave this as a string, because we're not doing any validation on it, so it could be anything

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean make provisioner_type a string?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the .get here returns string | undefined, and the || narrows it to string, but I think the cast here narrow it more than it should, basically. we don't actually know that the returned value is a valid ProvisionerType here because we didn't check!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is correct. The type of the defaultInitialValues.provisioner_type is ProvisionerType which I would like to keep. Essentially I think adding validation is probably the best approach, but since this is a hidden query param only used in testing, I just skipped it for now. The BE will return a validation error if someone discovers this and tries to do something unsupported.


if (!allowAdvancedScheduling) {
initialValues = {
...initialValues,
Expand Down Expand Up @@ -167,6 +175,7 @@
};

export const CreateTemplateForm: FC<CreateTemplateFormProps> = (props) => {
const [searchParams, _] = useSearchParams();
const {
onCancel,
onSubmit,
Expand All @@ -179,13 +188,15 @@
allowAdvancedScheduling,
variablesSectionRef,
} = props;

const form = useFormik<CreateTemplateData>({
initialValues: getInitialValues({
allowAdvancedScheduling,
fromExample:
"starterTemplate" in props ? props.starterTemplate : undefined,
fromCopy: "copiedTemplate" in props ? props.copiedTemplate : undefined,
variables,
searchParams,
}),
validationSchema,
onSubmit,
Expand Down Expand Up @@ -217,25 +228,6 @@
/>
)}

{/*
This value is always "terraform" in production.
For testing purposes, we expose this as a hidden form element
that can be changed. For example, to "echo"
*/}
<Field
type="hidden"
{...getFieldHelpers("provisioner_type")}
data-testid="provisioner-type-input"
label="Provisioner type"
// This is a bit jank, but when you call 'setAttribute('value', 'echo') from playwright, the formik form
// is not updated. So calling 'click' will also update formik. This is super weird, but I cannot find another
// way
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Not sure what the actual type is here.
onClick={async (e: any) => {
await form.setFieldValue("provisioner_type", e.target.value);
}}
/>

<TextField
{...getFieldHelpers("name")}
disabled={isSubmitting}
Expand Down
Loading