package passenger import ( "fmt" "io/ioutil" "os" "testing" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func fakePassengerStatus(stat string) { content := fmt.Sprintf("#!/bin/sh\ncat << EOF\n%s\nEOF", stat) ioutil.WriteFile("/tmp/passenger-status", []byte(content), 0700) } func teardown() { os.Remove("/tmp/passenger-status") } func Test_Invalid_Passenger_Status_Cli(t *testing.T) { r := &passenger{ Command: "an-invalid-command passenger-status", } var acc testutil.Accumulator err := r.Gather(&acc) require.Error(t, err) assert.Equal(t, err.Error(), `exec: "an-invalid-command": executable file not found in $PATH`) } func Test_Invalid_Xml(t *testing.T) { fakePassengerStatus("invalid xml") defer teardown() r := &passenger{ Command: "/tmp/passenger-status", } var acc testutil.Accumulator err := r.Gather(&acc) require.Error(t, err) assert.Equal(t, err.Error(), "Cannot parse input with error: EOF\n") } // We test this by ensure that the error message match the path of default cli func Test_Default_Config_Load_Default_Command(t *testing.T) { fakePassengerStatus("invalid xml") defer teardown() r := &passenger{} var acc testutil.Accumulator err := r.Gather(&acc) require.Error(t, err) assert.Equal(t, err.Error(), "exec: \"passenger-status\": executable file not found in $PATH") } func TestPassengerGenerateMetric(t *testing.T) { fakePassengerStatus(sampleStat) defer teardown() //Now we tested again above server, with our authentication data r := &passenger{ Command: "/tmp/passenger-status", } var acc testutil.Accumulator err := r.Gather(&acc) require.NoError(t, err) tags := map[string]string{ "passenger_version": "5.0.17", } fields := map[string]interface{}{ "process_count": 23, "max": 23, "capacity_used": 23, "get_wait_list_size": 3, } acc.AssertContainsTaggedFields(t, "passenger", fields, tags) tags = map[string]string{ "name": "/var/app/current/public", "app_root": "/var/app/current", "app_type": "rack", } fields = map[string]interface{}{ "processes_being_spawned": 2, "capacity_used": 23, "get_wait_list_size": 3, } acc.AssertContainsTaggedFields(t, "passenger_group", fields, tags) tags = map[string]string{ "name": "/var/app/current/public", } fields = map[string]interface{}{ "capacity_used": 23, "get_wait_list_size": 3, } acc.AssertContainsTaggedFields(t, "passenger_supergroup", fields, tags) tags = map[string]string{ "app_root": "/var/app/current", "group_name": "/var/app/current/public", "supergroup_name": "/var/app/current/public", "pid": "11553", "code_revision": "899ac7f", "life_status": "ALIVE", "process_group_id": "13608", } fields = map[string]interface{}{ "concurrency": 1, "sessions": 0, "busyness": 0, "processed": 951, "spawner_creation_time": int64(1452746835922747), "spawn_start_time": int64(1452746844946982), "spawn_end_time": int64(1452746845013365), "last_used": int64(1452747071764940), "uptime": int64(226), // in seconds of 3m 46s "cpu": int64(58), "rss": int64(418548), "pss": int64(319391), "private_dirty": int64(314900), "swap": int64(0), "real_memory": int64(314900), "vmsize": int64(1563580), } acc.AssertContainsTaggedFields(t, "passenger_process", fields, tags) } var sampleStat = ` 5.0.17 1 23 23 23 3 /var/app/current/public READY 3 23 foo /var/app/current/public /var/app/current/public /var/app/current rack production QQUrbCVYxbJYpfgyDOwJ 23 0 0 23 3 0 2 foo foo ALIVE axcoto 1001 axcoto 1001 /var/app/current /var/app/current/public rack /var/app/.rvm/gems/ruby-2.2.0-p645/gems/passenger-5.0.17/helper-scripts/rack-loader.rb config.ru Passenger RubyApp 3 90000 production / smart nobody nogroup /var/app/.rvm/gems/ruby-2.2.0-p645/wrappers/ruby python node unix:/tmp/passenger.eKFdvdC/agents.s/ust_router logging foo false false foo 22 0 300 1 11553 378579907 17173df-PoNT3J9HCf 1 0 0 951 1452746835922747 1452746844946982 1452746845013365 1452747071764940 0s ago 3m 46s 899ac7f ALIVE ENABLED true 58 418548 319391 314900 0 314900 1563580 13608 Passenger RubyApp: /var/app/current/public main
unix:/tmp/passenger.eKFdvdC/apps.s/ruby.UWF6zkRJ71aoMXPxpknpWVfC1POFqgWZzbEsdz5v0G46cSSMxJ3GHLFhJaUrK2I
session 1 0
http
tcp://127.0.0.1:49888
http 1 0
11563 1549681201 17173df-pX5iJOipd8 1 1 2147483647 756 1452746835922747 1452746845136882 1452746845172460 1452747071709179 0s ago 3m 46s 899ac7f ALIVE ENABLED true 47 418296 314036 309240 0 309240 1563608 13608 Passenger RubyApp: /var/app/current/public main
unix:/tmp/passenger.eKFdvdC/apps.s/ruby.PVCh7TmvCi9knqhba2vG5qXrlHGEIwhGrxnUvRbIAD6SPz9m0G7YlJ8HEsREHY3
session 1 1
http
tcp://127.0.0.1:52783
http 1 0
`