Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Cleaning of examples/ folder
Browse files Browse the repository at this point in the history
Removed the reference to examples/tasks/ in docs/TASKS.md

Removed the reference to examples/tasks/ in core/task_medium_test.go

Removed the reference to examples/tasks/ in readme.md

Removed the reference to examples/tasks/ in docs/SNAPCTL.md

Removed the reference to examples/tasks/ in docs/REST_API.md
  • Loading branch information
katarzyna-z committed Oct 13, 2016
1 parent 63d33ca commit 29a0b15
Show file tree
Hide file tree
Showing 31 changed files with 352 additions and 1,978 deletions.
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,44 @@ file 3 publisher false loaded
You now have one of each plugin type loaded into the framework. To begin collecting data, you need to create a task.

### Running Tasks
Tasks are most often shared as a Task Manifest and is written in JSON or YAML format. Make a copy of [this example task](./examples/tasks/mock-file.yaml) from the `examples/tasks/` directory on your local system and then start the task:
[Tasks](docs/TASKS.md) are most often shared as a Task Manifest and is written in JSON or YAML format.

Create a task manifest file, for example `mock-file.yaml` with following content:
```yaml
---
version: 1
schedule:
type: "simple"
interval: "1s"
max-failures: 10
workflow:
collect:
metrics:
/intel/mock/foo: {}
/intel/mock/bar: {}
/intel/mock/*/baz: {}
config:
/intel/mock:
name: "root"
password: "secret"
process:
-
plugin_name: "passthru"
config:
debug: true
process: null
publish:
-
plugin_name: "mock-file"
config:
file: "/tmp/snap_published_mock_file.log"
debug: true
```

and then start the task:

```
$ cd ~/snap
$ curl https://raw.githubusercontent.com/intelsdi-x/snap/master/examples/tasks/mock-file.yaml > mock-file.yaml
$ snapctl task create -t mock-file.yaml
Using task manifest to create task
Task created
Expand Down
140 changes: 137 additions & 3 deletions core/task_medium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,84 @@ func (t *taskErrors) Errors() []serror.SnapError {

const (
DUMMY_FILE = "dummy.txt"
YAML_FILE = "../examples/tasks/mock-file.yaml"
JSON_FILE = "../examples/tasks/mock-file.json"
YAML_FILE = "./mock-file.yaml"
JSON_FILE = "./mock-file.json"
DUMMY_TYPE = "dummy"
)

var (
YAML_FILE_CONTENT = []byte(`
---
version: 1
schedule:
type: "simple"
interval: "1s"
max-failures: 10
workflow:
collect:
metrics:
/intel/mock/foo: {}
/intel/mock/bar: {}
/intel/mock/*/baz: {}
config:
/intel/mock:
name: "root"
password: "secret"
process:
-
plugin_name: "passthru"
config:
debug: true
process: null
publish:
-
plugin_name: "file"
config:
file: "/tmp/snap_published_mock_file.log"
debug: true
`)

JSON_FILE_CONTENT = []byte(`
{
"version": 1,
"schedule": {
"type": "simple",
"interval": "1s"
},
"max-failures": 10,
"workflow": {
"collect": {
"metrics": {
"/intel/mock/foo": {},
"/intel/mock/bar": {},
"/intel/mock/*/baz": {}
},
"config": {
"/intel/mock": {
"name": "root",
"password": "secret"
}
},
"process": [
{
"plugin_name": "passthru",
"process": null,
"publish": [
{
"plugin_name": "file",
"config": {
"file": "/tmp/snap_published_mock_file.log"
}
}
]
}
]
}
}
}
`)
)

func koRoutine(sch schedule.Schedule,
wfMap *wmap.WorkflowMap,
startOnCreate bool,
Expand All @@ -72,8 +145,21 @@ func okRoutine(sch schedule.Schedule,
return nil, nil
}

func TestUnmarshalBodyTask(t *testing.T) {
func createTaskFile(name string, content []byte) error {
f, err := os.Create(name)
if err != nil {
return err
}
f.Write(content)
return nil
}

func deleteTaskFile(name string) error {
err := os.Remove(name)
return err
}

func TestUnmarshalBodyTask(t *testing.T) {
Convey("Non existing file", t, func() {
file, err := os.Open(DUMMY_FILE)
So(file, ShouldBeNil)
Expand All @@ -85,6 +171,9 @@ func TestUnmarshalBodyTask(t *testing.T) {
})

Convey("Bad JSON file", t, func() {
err := createTaskFile(YAML_FILE, YAML_FILE_CONTENT)
So(err, ShouldBeNil)

var tr TaskReq1
file, err := os.Open(YAML_FILE)
So(file, ShouldNotBeNil)
Expand All @@ -93,16 +182,25 @@ func TestUnmarshalBodyTask(t *testing.T) {
So(code, ShouldEqual, 400)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "invalid character '-' in numeric literal")

err = deleteTaskFile(YAML_FILE)
So(err, ShouldBeNil)
})

Convey("Proper JSON file", t, func() {
err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT)
So(err, ShouldBeNil)

var tr TaskReq1
file, err := os.Open(JSON_FILE)
So(file, ShouldNotBeNil)
So(err, ShouldBeNil)
code, err := UnmarshalBody(&tr, file)
So(code, ShouldEqual, 0)
So(err, ShouldBeNil)

err = deleteTaskFile(JSON_FILE)
So(err, ShouldBeNil)
})
}

Expand All @@ -119,16 +217,25 @@ func TestCreateTaskRequest(t *testing.T) {
})

Convey("Bad JSON file", t, func() {
err := createTaskFile(YAML_FILE, YAML_FILE_CONTENT)
So(err, ShouldBeNil)

file, err := os.Open(YAML_FILE)
So(file, ShouldNotBeNil)
So(err, ShouldBeNil)
task, err := createTaskRequest(file)
So(task, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "invalid character '-' in numeric literal")

err = deleteTaskFile(YAML_FILE)
So(err, ShouldBeNil)
})

Convey("Proper JSON file", t, func() {
err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT)
So(err, ShouldBeNil)

file, err := os.Open(JSON_FILE)
So(file, ShouldNotBeNil)
So(err, ShouldBeNil)
Expand All @@ -142,6 +249,9 @@ func TestCreateTaskRequest(t *testing.T) {
So(task.Schedule.StartTimestamp, ShouldBeNil)
So(task.Schedule.StopTimestamp, ShouldBeNil)
So(task.Start, ShouldEqual, false)

err = deleteTaskFile(JSON_FILE)
So(err, ShouldBeNil)
})
}

Expand All @@ -159,6 +269,9 @@ func TestCreateTaskFromContent(t *testing.T) {
})

Convey("Bad JSON file", t, func() {
err := createTaskFile(YAML_FILE, YAML_FILE_CONTENT)
So(err, ShouldBeNil)

file, err := os.Open(YAML_FILE)
So(file, ShouldNotBeNil)
So(err, ShouldBeNil)
Expand All @@ -167,9 +280,15 @@ func TestCreateTaskFromContent(t *testing.T) {
So(task, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "invalid character '-' in numeric literal")

err = deleteTaskFile(YAML_FILE)
So(err, ShouldBeNil)
})

Convey("Proper JSON file no workflow routine", t, func() {
err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT)
So(err, ShouldBeNil)

file, err := os.Open(JSON_FILE)
So(file, ShouldNotBeNil)
So(err, ShouldBeNil)
Expand All @@ -178,9 +297,15 @@ func TestCreateTaskFromContent(t *testing.T) {
So(task, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Missing workflow creation routine")

err = deleteTaskFile(JSON_FILE)
So(err, ShouldBeNil)
})

Convey("Proper JSON file erroring routine", t, func() {
err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT)
So(err, ShouldBeNil)

file, err := os.Open(JSON_FILE)
So(file, ShouldNotBeNil)
So(err, ShouldBeNil)
Expand All @@ -189,15 +314,24 @@ func TestCreateTaskFromContent(t *testing.T) {
So(task, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "Dummy error")

err = deleteTaskFile(JSON_FILE)
So(err, ShouldBeNil)
})

Convey("Proper JSON file proper routine", t, func() {
err := createTaskFile(JSON_FILE, JSON_FILE_CONTENT)
So(err, ShouldBeNil)

file, err := os.Open(JSON_FILE)
So(file, ShouldNotBeNil)
So(err, ShouldBeNil)
autoStart := true
task, err := CreateTaskFromContent(file, &autoStart, okRoutine)
So(task, ShouldBeNil)
So(err, ShouldBeNil)

err = deleteTaskFile(JSON_FILE)
So(err, ShouldBeNil)
})
}
49 changes: 44 additions & 5 deletions docs/REST_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,50 @@ _**Example Response**_
{"type":"metric-event","message":"","event":[{"namespace":"/intel/mock/host0/baz","data":87,"source":"egu-mac01.lan","timestamp":"2015-11-19T23:45:42.075605924-08:00"},{"namespace":"/intel/mock/host1/baz","data":89,"source":"egu-mac01.lan","timestamp":"2015-11-19T23:45:42.075609242-08:00"},{"namespace":"/intel/mock/host2/baz","data":84,"source":"egu-mac01.lan","timestamp":"2015-11-19T23:45:42.075611747-08:00"},{"namespace":"/intel/mock/host3/baz","data":82,"source":"egu-mac01.lan","timestamp":"2015-11-19T23:45:42.075613786-08:00"}...
```
**POST /v1/tasks**:
Create a task with the JSON input
Create a task with the JSON input, using for example mock-file.json with following content:
```json
{
"version": 1,
"schedule": {
"type": "simple",
"interval": "1s"
},
"max-failures": 10,
"workflow": {
"collect": {
"metrics": {
"/intel/mock/foo": {},
"/intel/mock/bar": {},
"/intel/mock/*/baz": {}
},
"config": {
"/intel/mock": {
"name": "root",
"password": "secret"
}
},
"process": [
{
"plugin_name": "passthru",
"process": null,
"publish": [
{
"plugin_name": "mock-file",
"config": {
"file": "/tmp/published"
}
}
]
}
]
}
}
}
```

_**Example Request**_
```
curl -vXPOST http://localhost:8181/v1/tasks -d @../examples/tasks/mock-file.json --header "Content-Type: application/json"
curl -vXPOST http://localhost:8181/v1/tasks -d @mock-file.json --header "Content-Type: application/json"
```
_**Example Response**_
```json
Expand Down Expand Up @@ -496,8 +535,8 @@ _**Example Response**_
},
"config": {
"/intel/mock": {
"password": "secret",
"user": "root"
"user": "root",
"password": "secret"
}
},
"process": [
Expand All @@ -506,7 +545,7 @@ _**Example Response**_
"plugin_version": 0,
"publish": [
{
"plugin_name": "file",
"plugin_name": "mock-file",
"plugin_version": 0,
"config": {
"file": "/tmp/published"
Expand Down
Loading

0 comments on commit 29a0b15

Please sign in to comment.