github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-jvm/lib/jvm_test.go (about) 1 package mpjvm 2 3 import ( 4 "reflect" 5 "runtime" 6 "testing" 7 ) 8 9 func TestGenerateVmid(t *testing.T) { 10 var expected string 11 remote := "remotehost.local" 12 lvmid := "12345" 13 14 expected = "12345@remotehost.local" 15 if id := generateVmid(remote, lvmid); id != expected { 16 t.Errorf("vmid should be %s, but %v", expected, id) 17 } 18 19 expected = "remotehost.local" 20 if id := generateVmid(remote, ""); id != expected { 21 t.Errorf("vmid should be %s, but %v", expected, id) 22 } 23 24 expected = "12345" 25 if id := generateVmid("", lvmid); id != expected { 26 t.Errorf("vmid should be %s, but %v", expected, id) 27 } 28 29 expected = "" 30 if id := generateVmid("", ""); id != expected { 31 t.Errorf("vmid should be %s, but %v", expected, id) 32 } 33 } 34 35 func TestGenerateRemote(t *testing.T) { 36 var expected string 37 remote := "remote.local:1099" 38 host := "host.local" 39 port := 12345 40 41 expected = "remote.local:1099" 42 if r := generateRemote(remote, "", 0); r != expected { 43 t.Errorf("remote should be %s, but %s", expected, r) 44 } 45 46 expected = "remote.local:1099" 47 if r := generateRemote(remote, host, port); r != expected { 48 t.Errorf("remote should be %s, but %s", expected, r) 49 } 50 51 expected = "host.local:12345" 52 if r := generateRemote("", host, port); r != expected { 53 t.Errorf("remote should be %s, but %s", expected, r) 54 } 55 56 expected = "host.local" 57 if r := generateRemote("", host, 0); r != expected { 58 t.Errorf("remote should be %s, but %s", expected, r) 59 } 60 61 expected = "localhost:12345" 62 if r := generateRemote("", "", port); r != expected { 63 t.Errorf("remote should be %s, but %s", expected, r) 64 } 65 66 expected = "" 67 if r := generateRemote("", "", 0); r != expected { 68 t.Errorf("remote should be %s, but %s", expected, r) 69 } 70 } 71 72 func TestFetchMetrics(t *testing.T) { 73 switch runtime.GOOS { 74 case "windows", "plan9": 75 t.Skip() 76 } 77 m := JVMPlugin{ 78 Lvmid: "1", 79 JstatPath: "testdata/jstat_serialgc.sh", 80 } 81 actual, err := m.fetchJstatMetrics("-gc") 82 if err != nil { 83 t.Fatal(err) 84 } 85 expected := map[string]float64{ 86 "S0C": 45184.0, 87 "S1C": 45184.0, 88 "S0U": 45184.0, 89 "S1U": 0.0, 90 "EC": 361728.0, 91 "EU": 132414.7, 92 "OC": 904068.0, 93 "OU": 679249.5, 94 "MC": 21248.0, 95 "MU": 20787.3, 96 "CCSC": 2304.0, 97 "CCSU": 2105.8, 98 "YGC": 22, 99 "YGCT": 8.584, 100 "FGC": 6, 101 "FGCT": 2.343, 102 //"CGC": no data 103 //"CGCT": no data 104 "GCT": 10.927, 105 } 106 if !reflect.DeepEqual(actual, expected) { 107 t.Errorf("fetchMetrics('-gc') = %v; want %v", actual, expected) 108 } 109 } 110 111 func TestGraphDefinition(t *testing.T) { 112 tests := []struct { 113 plugin JVMPlugin 114 expectedKey string 115 expectedLabel string 116 }{ 117 { 118 plugin: JVMPlugin{ 119 JavaName: "Tomcat", 120 }, 121 expectedKey: "jvm.tomcat.gc_events", 122 expectedLabel: "JVM Tomcat GC events", 123 }, 124 { 125 plugin: JVMPlugin{ 126 JavaName: "Tomcat", 127 MetricKey: "foo.bar", 128 MetricLabel: "Hoge Fuga", 129 }, 130 expectedKey: "jvm.foo.bar.gc_events", 131 expectedLabel: "JVM Hoge Fuga GC events", 132 }, 133 { 134 plugin: JVMPlugin{ 135 JavaName: "Tomcat", 136 MetricLabel: "Hoge Fuga", 137 }, 138 expectedKey: "jvm.tomcat.gc_events", 139 expectedLabel: "JVM Hoge Fuga GC events", 140 }, 141 { 142 plugin: JVMPlugin{ 143 JavaName: "Tomcat", 144 MetricKey: "foo.bar", 145 }, 146 expectedKey: "jvm.foo.bar.gc_events", 147 expectedLabel: "JVM Tomcat GC events", 148 }, 149 } 150 151 for _, tt := range tests { 152 graphDefs := tt.plugin.GraphDefinition() 153 154 metric, ok := graphDefs[tt.expectedKey] 155 if !ok { 156 t.Errorf("expected to have key %s but not found", tt.expectedKey) 157 } 158 if metric.Label != tt.expectedLabel { 159 t.Errorf("expected to have label %s but got: %s", tt.expectedLabel, metric.Label) 160 } 161 } 162 }