Skip to content

Commit

Permalink
Merge branch 'main' into update-dotfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
matifali authored Sep 26, 2023
2 parents 2b40b57 + cb72a16 commit 4f91c50
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 55 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: ci

on:
push:
branches:
- main

pull_request:
workflow_dispatch:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- run: bun test
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: oven-sh/setup-bun@v1
with:
bun-version: latest
- run: bun fmt:ci
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.terraform*
.terraform*
node_modules
*.tfstate
*.tfstate.lock.info
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.exclude": {
"**/terraform.tfstate": true,
"**/.terraform": true
}
}
34 changes: 5 additions & 29 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,13 @@ To create a new module, clone this repository and run:
./new.sh MOUDLE_NAME
```

Test a module by running an instance of Coder on your local machine:

```shell
coder server --in-memory
```

This will create a new module in the modules directory with the given name and scaffolding.
Edit the files, adding your module's implementation, documentation and screenshots.

## Testing a Module

Create a template and edit it to include your development module:
A suite of test-helpers exists to run `terraform apply` on modules with variables, and test script output against containers.

> [!NOTE]
> The Docker starter template is recommended for quick-iteration!
Reference existing `*.test.ts` files for implementation.

```hcl
module "MOUDLE_NAME" {
source = "/home/user/coder/modules/MOUDLE_NAME"
}
```sh
# Run tests for a specific module!
$ bun test -t '<module>'
```

You can also test your module by specifying the source as a git repository:

```hcl
module "MOUDLE_NAME" {
source = "git::https://github.com/<USERNAME>/<REPO>.git//<FOLDER>?ref=<BRANCH>"
}
```

Build a workspace and your module will be consumed! 🥳

Open a pull-request with your module, a member of the Coder team will
manually test it, and after-merge it will appear on the Registry.
25 changes: 25 additions & 0 deletions aws-region/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from "bun:test";
import {
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
} from "../test";

describe("aws-region", async () => {
await runTerraformInit(import.meta.dir);

testRequiredVariables(import.meta.dir, {});

it("default output", async () => {
const state = await runTerraformApply(import.meta.dir, {});
expect(state.outputs.value.value).toBe("us-east-1");
});

it("customized default", async () => {
const state = await runTerraformApply(import.meta.dir, {
default: "us-west-2",
});
expect(state.outputs.value.value).toBe("us-west-2");
});
});
25 changes: 25 additions & 0 deletions azure-region/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, expect, it } from "bun:test";
import {
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
} from "../test";

describe("azure-region", async () => {
await runTerraformInit(import.meta.dir);

testRequiredVariables(import.meta.dir, {});

it("default output", async () => {
const state = await runTerraformApply(import.meta.dir, {});
expect(state.outputs.value.value).toBe("eastus");
});

it("customized default", async () => {
const state = await runTerraformApply(import.meta.dir, {
default: "westus",
});
expect(state.outputs.value.value).toBe("westus");
});
});
Binary file added bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = ["./setup.ts"]
2 changes: 1 addition & 1 deletion fly-region/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ A parameter with all fly.io regions. This allows developers to select the region

## Examples

TODO
TODO
47 changes: 24 additions & 23 deletions gcp-region/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ maintainer_github: coder
verified: true
tags: [gcp, regions, parameter, helper]
---

# Google Cloud Platform Regions

This module adds Google Cloud Platform regions to your Coder template.
Expand All @@ -16,32 +17,32 @@ This module adds Google Cloud Platform regions to your Coder template.

1. Add only GPU zones in the US West 1 region:

```hcl
module "gcp_region" {
source = "https://registry.coder.com/modules/gcp-region"
default = ["us-west1-a"]
regions = ["us-west1"]
gpu_only = false
}
```
```hcl
module "gcp_region" {
source = "https://registry.coder.com/modules/gcp-region"
default = ["us-west1-a"]
regions = ["us-west1"]
gpu_only = false
}
```

2. Add all zones in the Europe West region:

```hcl
module "gcp_region" {
source = "https://registry.coder.com/modules/gcp-region"
regions = ["europe-west"]
single_zone_per_region = false
}
```
```hcl
module "gcp_region" {
source = "https://registry.coder.com/modules/gcp-region"
regions = ["europe-west"]
single_zone_per_region = false
}
```

3. Add a single zone from each region in US and Europe that laos has GPUs

```hcl
module "gcp_region" {
source = "https://registry.coder.com/modules/gcp-region"
regions = ["us", "europe"]
gpu_only = true
single_zone_per_region = true
}
```
```hcl
module "gcp_region" {
source = "https://registry.coder.com/modules/gcp-region"
regions = ["us", "europe"]
gpu_only = true
single_zone_per_region = true
}
```
1 change: 1 addition & 0 deletions git-clone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ maintainer_github: coder
verified: true
tags: [git, helper]
---

# Git Clone

This module allows you to automatically clone a repository by URL and skip if it exists in the path provided.
Expand Down
39 changes: 39 additions & 0 deletions git-clone/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "bun:test";
import {
executeScriptInContainer,
runTerraformApply,
runTerraformInit,
testRequiredVariables,
} from "../test";

describe("git-clone", async () => {
await runTerraformInit(import.meta.dir);

testRequiredVariables(import.meta.dir, {
agent_id: "foo",
url: "foo",
});

it("fails without git", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
url: "some-url",
});
const output = await executeScriptInContainer(state, "alpine");
expect(output.exitCode).toBe(1);
expect(output.stdout).toEqual(["Git is not installed!"]);
});

it("runs with git", async () => {
const state = await runTerraformApply(import.meta.dir, {
agent_id: "foo",
url: "fake-url",
});
const output = await executeScriptInContainer(state, "alpine/git");
expect(output.exitCode).toBe(128);
expect(output.stdout).toEqual([
"Creating directory ~/fake-url...",
"Cloning fake-url to ~/fake-url...",
]);
});
});
1 change: 1 addition & 0 deletions jetbrains-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ maintainer_github: coder
verified: true
tags: [ide, jetbrains, helper, parameter]
---

# JetBrains Gateway

This module adds a JetBrains Gateway Button to open any workspace with a single click.
Expand Down
2 changes: 1 addition & 1 deletion jfrog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ tags: [integration]

# JFrog

TODO
TODO
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "modules",
"scripts": {
"test": "bun test",
"fmt": "bun x prettier -w **/*.ts **/*.md *.md && terraform fmt **/*.tf",
"fmt:ci": "bun x prettier --check **/*.ts **/*.md *.md && terraform fmt -check **/*.tf"
},
"devDependencies": {
"bun-types": "^1.0.3"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
47 changes: 47 additions & 0 deletions setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { readableStreamToText, spawn } from "bun";
import { afterAll, beforeAll } from "bun:test";

const removeStatefiles = async () => {
const proc = spawn([
"find",
".",
"-type",
"f",
"-name",
"*.tfstate",
"-name",
"*.tfstate.lock.info",
"-delete",
]);
await proc.exited;
};

const removeOldContainers = async () => {
let proc = spawn([
"docker",
"ps",
"-a",
"-q",
"--filter",
`label=modules-test`,
]);
let containerIDsRaw = await readableStreamToText(proc.stdout);
let exitCode = await proc.exited;
if (exitCode !== 0) {
throw new Error(containerIDsRaw);
}
containerIDsRaw = containerIDsRaw.trim();
if (containerIDsRaw === "") {
return;
}
proc = spawn(["docker", "rm", "-f", ...containerIDsRaw.split("\n")]);
const stdout = await readableStreamToText(proc.stdout);
exitCode = await proc.exited;
if (exitCode !== 0) {
throw new Error(stdout);
}
};

afterAll(async () => {
await Promise.all([removeStatefiles(), removeOldContainers()]);
});
Loading

0 comments on commit 4f91c50

Please sign in to comment.