package tomcat import ( "fmt" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf/testutil" ) var tomcatStatus8 = ` ` func TestHTTPTomcat8(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { if _, err := fmt.Fprintln(w, tomcatStatus8); err != nil { w.WriteHeader(http.StatusInternalServerError) t.Error(err) return } w.WriteHeader(http.StatusOK) })) defer ts.Close() tc := Tomcat{ URL: ts.URL, Username: "tomcat", Password: "s3cret", } var acc testutil.Accumulator require.NoError(t, tc.Gather(&acc)) // tomcat_jvm_memory jvmMemoryFields := map[string]interface{}{ "free": int64(17909336), "total": int64(58195968), "max": int64(620756992), } jvmMemoryTags := map[string]string{ "source": ts.URL, } for _, metric := range acc.Metrics { fmt.Println(metric.Measurement) for k, v := range metric.Tags { fmt.Printf("%s: %s\n", k, v) } } acc.AssertContainsTaggedFields(t, "tomcat_jvm_memory", jvmMemoryFields, jvmMemoryTags) // tomcat_jvm_memorypool jvmMemoryPoolFields := map[string]interface{}{ "init": int64(22020096), "committed": int64(22020096), "max": int64(174063616), "used": int64(17533952), } jvmMemoryPoolTags := map[string]string{ "name": "PS Perm Gen", "type": "Non-heap memory", "source": ts.URL, } acc.AssertContainsTaggedFields(t, "tomcat_jvm_memorypool", jvmMemoryPoolFields, jvmMemoryPoolTags) // tomcat_connector connectorFields := map[string]interface{}{ "max_threads": int64(200), "current_thread_count": int64(5), "current_threads_busy": int64(1), "max_time": int(68), "processing_time": int(88), "request_count": int(2), "error_count": int(1), "bytes_received": int64(0), "bytes_sent": int64(9286), } connectorTags := map[string]string{ "name": "http-apr-8080", "source": ts.URL, } acc.AssertContainsTaggedFields(t, "tomcat_connector", connectorFields, connectorTags) } var tomcatStatus6 = ` ` func TestHTTPTomcat6(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { if _, err := fmt.Fprintln(w, tomcatStatus6); err != nil { w.WriteHeader(http.StatusInternalServerError) t.Error(err) return } w.WriteHeader(http.StatusOK) })) defer ts.Close() tc := Tomcat{ URL: ts.URL, Username: "tomcat", Password: "s3cret", } var acc testutil.Accumulator require.NoError(t, tc.Gather(&acc)) // tomcat_jvm_memory jvmMemoryFields := map[string]interface{}{ "free": int64(1942681600), "total": int64(2040070144), "max": int64(2040070144), } acc.AssertContainsFields(t, "tomcat_jvm_memory", jvmMemoryFields) // tomcat_connector connectorFields := map[string]interface{}{ "bytes_received": int64(0), "bytes_sent": int64(550196), "current_thread_count": int64(2), "current_threads_busy": int64(2), "error_count": int(16), "max_threads": int64(150), "max_time": int(1005), "processing_time": int(2465), "request_count": int(436), } connectorTags := map[string]string{ "name": "http-8080", "source": ts.URL, } acc.AssertContainsTaggedFields(t, "tomcat_connector", connectorFields, connectorTags) }