Skip to content

Commit

Permalink
adjust script param, adding test with and without script execution
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrrt committed Nov 11, 2024
1 parent d79d6cf commit 9e7a1d9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Once nomad agent has been started, the nomad UI can be accessed via http://local
make dev
```


### Start Unit

```bash
Expand All @@ -52,7 +51,7 @@ make ci

## Notes

- busybox has been used instead of nginx, as it's simpler to configure, can receive dynamic port through command line, doesn't require a custom configuration file.
- only one package has been defined for the sake of simplicity, in a larger codebase, I'd split across multiple package to foster better isolation
- remote code execution is dangerous, I've skipped the sanity checks, but that's something required in a secured context.
- I've created a custom action to test my services with a Nomad cluster in GitHub actions
- busybox has been used instead of nginx, as it's simpler to configure, can receive dynamic port through command line, doesn't require a custom configuration file, then I've been able to focus on code quality over nginx configuration.
- Remote code execution is dangerous, I've skipped the sanity checks, but that's something required in a secured context.
- Created a custom action to test my services with a Nomad cluster in GitHub actions
- Replaced script query parameter type from boolean to string for parsing conveniency by routing validation
23 changes: 17 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,21 @@ func TestServiceProvisionner(t *testing.T) {
return resp, response
}

t.Run("Valid Request", func(t *testing.T) {
t.Run("Valid Request - script false", func(t *testing.T) {
resp, response := sendPutRequest("/v1/services/testName", map[string]interface{}{
"url": "http://valid-url.com",
"script": true,
"url": "https://pastebin.com/raw/hEFbnx33",
"script": "false",
})

assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.NotNil(t, response.Url)
assert.Nil(t, response.Error)
})

t.Run("Valid Request - script true", func(t *testing.T) {
resp, response := sendPutRequest("/v1/services/remoteExec", map[string]interface{}{
"url": "https://pastebin.com/raw/D0Fn7kAr",
"script": "true",
})

assert.Equal(t, http.StatusOK, resp.StatusCode)
Expand All @@ -77,7 +88,7 @@ func TestServiceProvisionner(t *testing.T) {
t.Run("Wrong path", func(t *testing.T) {
resp, response := sendPutRequest("/v1/services/", map[string]interface{}{
"url": "http://valid-url.com",
"script": true,
"script": "true",
})

assert.Equal(t, http.StatusNotFound, resp.StatusCode)
Expand All @@ -87,7 +98,7 @@ func TestServiceProvisionner(t *testing.T) {
t.Run("Invalid URI", func(t *testing.T) {
resp, response := sendPutRequest("/v1/services/testName", map[string]interface{}{
"url": "not-a-valid-url",
"script": true,
"script": "true",
})

assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
Expand All @@ -96,7 +107,7 @@ func TestServiceProvisionner(t *testing.T) {
assert.Contains(t, *response.Error, "url")
})

t.Run("Missing Script Parameter", func(t *testing.T) {
t.Run("Missing script parameter should raise bad request", func(t *testing.T) {
resp, response := sendPutRequest("/v1/services/testName", map[string]interface{}{
"url": "http://valid-url.com",
})
Expand Down
12 changes: 10 additions & 2 deletions routing/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package routing
import (
"dbrrt/noaas/nomad"
"fmt"
"log"
"net/http"
"net/url"
"strconv"

"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
Expand All @@ -14,7 +16,7 @@ type ServiceController struct{}

type NewServiceRequest struct {
Url string `json:"url" binding:"required,url"`
Script bool `json:"script"`
Script string `json:"script" binding:"required"`
}

type NewServiceResponseStruct struct {
Expand Down Expand Up @@ -44,7 +46,13 @@ func (h ServiceController) ServiceProvisionner(c *gin.Context) {

} else {
nameParam := c.Params.ByName("name")
uriDeployed, err := nomad.CreateAJobAndGetUri(nameParam, payload.Url, payload.Script)

queryBodyScriptParam, err := strconv.ParseBool(payload.Script)
if err != nil {
log.Fatal(err)
}

uriDeployed, err := nomad.CreateAJobAndGetUri(nameParam, payload.Url, queryBodyScriptParam)

if uriDeployed != "" && err == nil {
c.JSON(http.StatusOK, gin.H{
Expand Down

0 comments on commit 9e7a1d9

Please sign in to comment.